/
tether_drive.py
executable file
·55 lines (44 loc) · 1.06 KB
/
tether_drive.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
#!/usr/bin/env python
import traceback
import time
import sys
import select
import tty
import termios
from roomba.create2 import Create2
def isData():
return select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], [])
old_settings = termios.tcgetattr(sys.stdin)
roomba = Create2()
roomba.start()
roomba.safe()
print "Hold keys to move:"
print "\tw - forward"
print "\ta - left"
print "\td - right"
print "\ts - backward"
print "\tctrl+c - quit"
try:
tty.setcbreak(sys.stdin.fileno())
while True:
if isData():
char = sys.stdin.read(1)
else:
char = -1
if char == 'w':
roomba.straight(300)
elif char == 'a':
roomba.counterclockwise(100)
elif char == 'd':
roomba.clockwise(100)
elif char == 's':
roomba.straight(-200)
else:
roomba.drive(0, 0)
time.sleep(0.05)
except:
traceback.print_exc()
finally:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
roomba.drive(0,0)
sys.exit()