-
Notifications
You must be signed in to change notification settings - Fork 1
/
rcController.py
64 lines (57 loc) · 2.41 KB
/
rcController.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from mqttClient import MqttClient
import controllerMessages as messages
import Tkinter as tk
import sys
import json
sys.path.append('..')
# Connect to the client and subscribe to the necessary topics
drive_topic = "sbrick/01/sp/drive"
stop_topic = "sbrick/01/sp/stop"
adc_topic = "sbrick/01/rr/get_adc"
my_client = MqttClient()
my_client.setup_client()
# Handles the behavior expected from each key press
def key_input(event):
stri = event.keysym.lower()
print("Press w,s,a,d to drive, b to break and q to quit")
if stri == 'd':
print("Drive right")
my_client.publish(drive_topic, json.dumps(messages.drive_msgd,
sort_keys=True))
elif stri == 'b':
my_client.publish(drive_topic, json.dumps(messages.stop_msg_l,
sort_keys=True))
my_client.publish(drive_topic, json.dumps(messages.stop_msg_a,
sort_keys=True))
print("Stop")
elif stri == 'a':
my_client.publish(drive_topic, json.dumps(messages.drive_msga,
sort_keys=True))
print("Drive left")
elif stri == 'w':
my_client.publish(drive_topic, json.dumps(messages.drive_msgw,
sort_keys=True))
print("Drive forward")
elif stri == 's':
my_client.publish(drive_topic, json.dumps(messages.drive_msgs,
sort_keys=True))
print("Drive back")
elif stri == 'q':
my_client.publish(drive_topic, json.dumps(messages.stop_msg_l,
sort_keys=True))
my_client.publish(drive_topic, json.dumps(messages.stop_msg_a,
sort_keys=True))
print("Stop and quit")
sys.exit()
elif stri == 'p':
my_client.publish(adc_topic, json.dumps(messages.get_adc,
sort_keys=True))
elif stri == 'o':
my_client.publish(stop_topic, json.dumps(messages.stop_msg,
sort_keys=True))
print("Stop angular")
# Simple Tkinter program to monitor key presses and act on them
if __name__ == "__main__":
command = tk.Tk()
command.bind_all('<KeyPress>', key_input)
command.mainloop()