-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontroller.py
More file actions
71 lines (56 loc) · 1.68 KB
/
controller.py
File metadata and controls
71 lines (56 loc) · 1.68 KB
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
65
66
67
68
69
import serial
import time
import json
import threading
import Queue
import cmd
import sys
commsQueue = Queue.Queue()
class ComThread(threading.Thread):
def __init__(self, threadID, name, device, baud):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.device = device
self.baud = baud
self.ser = serial.Serial(device, baud)
self.lastHeartbeat = int(round(time.time() * 1000))
def doHeartbeat(self):
now = int(round(time.time() * 1000))
if (now - self.lastHeartbeat) > 500:
self.lastHeartbeat = now
command = json.dumps({'cmd': 'heartbeat'})
self.ser.write(command)
def run(self):
while True:
self.doHeartbeat()
try:
item = commsQueue.get(False)
command = json.dumps(item)
self.ser.write(command)
commsQueue.task_done()
except Queue.Empty:
pass
time.sleep(0.01)
class QuadCmd(cmd.Cmd):
def __init__(self):
cmd.Cmd.__init__(self)
self.prompt = '> '
def do_f(self, forceStr):
force = int(forceStr)
command = {'cmd': 'force', 'val': force}
commsQueue.put(command)
def do_q(self, s):
sys.exit(1)
def do_h(self, s):
command = {'cmd': 'hover'}
commsQueue.put(command)
if __name__ == "__main__":
comThread = ComThread(1, 'ComThread', '/dev/tty.usbmodem1411', 9600)
comThread.daemon = True
# Start the com thread
comThread.start()
cli = QuadCmd()
cli.cmdloop()
while threading.active_count() > 0:
time.sleep(0.1)