forked from robotstreamer/robotstreamer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
open_roomba_interface.py
192 lines (145 loc) · 4.37 KB
/
open_roomba_interface.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import subprocess
import serial
import time
import _thread
import sys
import robot_util
try:
ser = serial.Serial(port='/dev/ttyUSB0', baudrate=115200, timeout=0.03)
except Exception as e:
print("serial error 1", e)
#ser = serial.Serial(port='/dev/ttyAMA0', baudrate=115200)
#ser = serial.Serial(port='/dev/ttyUSB0', baudrate=57600)
#ser = serial.Serial(port='/dev/ttyUSB0', baudrate=19200)
RESET = b'\x07'
PASSIVE = b'\x80'
SAFE = b'\x83'
CLEAN = b'\x87'
BEEP = b'\x8c\x03\x01\x40\x10\x8d\x03'
VOLTAGE = b'\x8e\x16'
POWER = b'\x85'
LVCO = 11000 #roomba will power down at this battery voltage in mV
movementSystemActive = False
commandArgs = None
def handleCommand(command, keyPosition, price=0):
global lastCommand
print("handling command")
print("keyposition: ", keyPosition)
print("command: ", command)
speed = int(commandArgs.straight_speed / 2)
if command == 'L':
move(100, -100, 0, .20, 0)
elif command == 'R':
move(-100, 100, 0, .20, 0)
elif command == 'F':
move(speed, speed, .09, .40, .18)
elif command == 'B':
move(-speed, -speed, .09, .40, .18)
ser.reset_input_buffer()
ser.write(VOLTAGE)
ser.flush()
packet=b''
packet=ser.read(2)
if len(packet) == 2:
voltage=int.from_bytes(packet, 'big')
print('voltage:', voltage)
if voltage < LVCO:
robot_util.aplayFile('/home/pi/sound/recharge.wav')
ser.write(POWER)
ser.flush()
time.sleep(3)
subprocess.run(['/usr/bin/sudo', '/sbin/shutdown', '-h', 'now' ] )
robot_util.handleSoundCommand(command, keyPosition, price)
def init(cArgs):
global commandArgs
commandArgs = cArgs
try:
print("init roomba")
ser.write(RESET)
time.sleep(0.1)
#ser.write(PASSIVE)
#ser.write(SAFE)
#print("sending 3 beep to roomba")
#ser.write(BEEP)
#time.sleep(0.7)
#ser.write(BEEP)
#time.sleep(0.7)
#ser.write(BEEP)
except Exception as e:
print("serial error 2", e)
#def serialWrite(s):
# ser.write(s.encode())
def negative(x):
return 256 - x
def readAll():
print("read all")
while True:
sys.stdout.write(str(ser.read()))
sys.stdout.flush()
def twosComp(x):
if x < 0:
return negative(-x)
return x
def move(motorA, motorB, rampUpTime, fullSpeedTime, rampDownTime):
global movementSystemActive
motorA1 = twosComp(motorA)
motorA2 = twosComp(int(motorA / 3))
motorB1 = twosComp(motorB)
motorB2 = twosComp(int(motorB / 3))
# set high bytes
if motorA < 0:
highA = 0xFF
else:
highA = 0
if motorB < 0:
highB = 0xFF
else:
highB = 0
if not movementSystemActive:
movementSystemActive = True
# ramp up
rawMove(highA, motorA2, highB, motorB2)
time.sleep(rampUpTime)
# move full speed
rawMove(highA, motorA1, highB, motorB1)
time.sleep(fullSpeedTime)
# ramp down
rawMove(highA, motorA2, highB, motorB2)
time.sleep(rampDownTime)
# stop
rawMove(0, 0, 0, 0)
movementSystemActive = False
def rawMove(motorAHigh, motorALow, motorBHigh, motorBLow):
print("raw move", motorAHigh, motorALow, motorBHigh, motorBLow)
#ser.write(bytes([128,128])
#ser.write(bytes([131])
ser.write(bytes([128]))
ser.write(bytes([131]))
ser.write(bytes([146, motorAHigh, motorALow, motorBHigh, motorBLow]))
def inputFromKeyboard():
while True:
data = input("PROMPT>")
if data == "beep":
ser.write(BEEP)
elif data == 'l':
move(100, -100, .05, .2, .25)
elif data == 'r':
move(-100, 100, .05, .2, .25)
elif data == 'f':
move(100, 100, .05, .2, .25)
elif data == 'b':
move(-100, -100, .05, .2, .25)
elif data == 's': #stop
move(0, 0, 0, 0, .05, .2, .25)
else:
print("data:", data)
num = int(data, 10)
print("num", num, "end")
c = chr(num)
print("char", c, "end")
#ser.write(c)
ser.write(bytes([num]))
ser.flush()
if __name__ == "__main__":
_thread.start_new_thread(readAll, ())
inputFromKeyboard()