forked from Clown0503/PyRoomba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoombaSCI.py
executable file
·593 lines (418 loc) · 12.7 KB
/
RoombaSCI.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#!/usr/bin/env python
# 2008(c) K Jacobson
# http://solidcoding.blogspot.com/2008/07/python-library-for-roomba-sci.html
# 2011(c) Modifications and cleaning by Jerome Flesch <[email protected]>
import serial
import sys
import time
from cStringIO import StringIO
class ByteBuilder(object):
def __init__(self):
self.collection = StringIO()
self.strCollection = StringIO()
def append(self, byte):
x = hex(byte)
x = x.replace("0x", "")
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/510399
result = chr(int(x, 16))
self.collection.write(result)
self.strCollection.write("\\x" + x)
def toCharString(self):
return self.collection.getvalue()
def toString(self):
return self.strCollection.getvalue()
class ConvertToBytes(object):
def __init__(self, data):
self.int = data
def high_byte(self):
s = self.to_hex()
s = str(s).replace('0x', '')
high = s[0:2]
high = "0x" + high
return int(high, 16)
def low_byte(self):
s = self.to_hex()
s = str(s).replace('0x', '')
low = s[2:]
low = "0x" + low
return int(low, 16)
def to_hex(self):
s = hex(self.int)
s = str(s).replace('0x', '')
while(len(s) < 4):
s = "0" + s
return "0x" + s
class Bumps(object):
def __init__(self, data):
self.data = data
def __right(self):
x = self.data & 1
return bool(x > 0)
right = property(__right)
def __left(self):
x = self.data & 2
return bool(x > 0)
left = property(__left)
class Buttons(object):
def __init__(self, data):
self.data = data
def __max(self):
x = self.data & 1
return bool(x > 0)
max = property(__max)
def __clean(self):
x = self.data & 2
return bool(x > 0)
clean = property(__clean)
def __spot(self):
x = self.data & 4
return bool(x > 0)
spot = property(__spot)
def __power(self):
x = self.data & 8
return bool(x > 0)
power = property(__power)
class Cliff(object):
def __init__(self, dataList):
self.dataList = dataList
def __left(self):
return bool(self.dataList[0] == 1)
left = property(__left)
def __front_left(self):
return bool(self.dataList[1] == 1)
front_left = property(__front_left)
def __front_right(self):
return bool(self.dataList[2] == 1)
front_right = property(__front_right)
def __right(self):
return bool(self.dataList[3] == 1)
right = property(__right)
class DirtDetector(object):
def __init__(self, dataList):
self.dataList = dataList
def __left(self):
return self.dataList[0]
left = property(__left)
def __right(self):
return self.dataList[1]
right = property(__right)
class MotorOvercurrents(object):
def __init__(self, data):
self.__data = data
def __sidebrush(self):
x = self.__data & 1
return bool(x > 0)
sidebrush = property(__sidebrush)
def __vacuum(self):
x = self.__data & 2
return bool(x > 0)
vacuum = property(__vacuum)
def __mainbrush(self):
x = self.__data & 4
return bool(x > 0)
mainbrush = property(__mainbrush)
def __drive_right(self):
x = self.__data & 8
return bool(x > 0)
drive_right = property(__drive_right)
def __drive_left(self):
x = self.__data & 16
return bool(x > 0)
drive_left = property(__drive_left)
class WheelDrops(object):
def __init__(self, data):
self.__data = data
def __right(self):
x = self.__data & 4
return bool(x > 0)
right = property(__right)
def __left(self):
x = self.__data & 8
return bool(x > 0)
left = property(__left)
def __castor(self):
x = self.__data & 16
return bool(x > 0)
castor = property(__castor)
class TwoBytes:
def __init__(self, data):
self.data = data
def to_int16(self):
high = hex(self.data[0])
low = hex(self.data[1])
high = str(high).replace("0x", "")
if (len(high) == 1):
high = "0"+high
low = str(low).replace("0x", "")
if(len(low) == 1):
low = "0" + low
cmb = "0x" + high + low
intX = int(cmb, 16)
if intX > 32767:
return intX - 65536
return intX
def to_uint16(self):
high = hex(self.data[0])
low = hex(self.data[1])
high = str(high).replace("0x", "")
if (len(high) == 1):
high = "0"+high
low = str(low).replace("0x", "")
if(len(low) == 1):
low = "0" + low
cmb = "0x" + high + low
intX = int(cmb, 16)
return intX
class SensorData(object):
def __init__(self, data):
self.__data = data
def __bumps(self):
return Bumps(self.__data[0])
bumps = property(__bumps)
def __wheel_drops(self):
return WheelDrops(self.__data[0])
wheel_drops = property(__wheel_drops)
def __wall(self):
return bool(self.__data[1] == 1)
wall = property(__wall)
def __cliff(self):
return Cliff(self.__data[2:6])
cliff = property(__cliff)
def __virtual_wall(self):
return bool(self.__data[6] == 1)
virtual_wall = property(__virtual_wall)
def __motor_overcurrents(self):
return MotorOvercurrents(self.__data[7])
motor_overcurrents = property(__motor_overcurrents)
def __dirt_detector(self):
return DirtDetector(self.__data[8:10])
dirt_detector = property(__dirt_detector)
def __remote_control_cmds(self):
return self.__data[10]
remote_control_cmds = property(__remote_control_cmds)
def __buttons(self):
return Buttons(self.__data[11])
buttons = property(__buttons)
def __distance(self):
return TwoBytes(self.__data[12:14]).to_int16()
distance = property(__distance)
def __angle(self):
return TwoBytes(self.__data[14:16]).to_int16()
angle = property(__angle)
def __charging_state(self):
return self.__data[16]
charging_state = property(__charging_state)
def __voltage(self):
return TwoBytes(self.__data[17:19]).to_uint16()
voltage = property(__voltage)
def __current(self):
return TwoBytes(self.__data[19:21]).to_int16()
current = property(__current)
def __temperature(self):
return self.__data[21]
temperature = property(__temperature)
def __charge(self):
return TwoBytes(self.__data[22:24]).to_uint16()
charge = property(__charge)
def __capacity(self):
return TwoBytes(self.__data[24:26]).to_uint16()
capacity = property(__capacity)
class int16(object):
def __init__(self, i):
self.__val = 0
self.__raw = i
if (i < 0):
self.__val = (65535 + i) + 1
else:
self.__val = i
def __get_value(self):
return self.__val
value = property(__get_value)
class RoombaAPI(object):
def __init__(self, port, baudrate):
self.__speed = 250
self.port = serial.Serial()
# should be connected upon initialization. run again to verify
# connection settings
self.port.port = port
self.port.baudrate = baudrate
self.port.timeout = 10
if not self.port.isOpen():
self.port.open()
def connect(self):
# XXX(Jflesch): Don't set rootooth baudrate here. Roomba don't have all
# the same default baudrate. For instance, the original author had a
# baudrate of 56.2K. Mine is 115.2K (Roomba 560). Rootooth can memorize
# this information, so we will just assume the user already set it right
self.wakeup()
#def __rootoothVersion(self):
# self.port.write("$$$")
# self.port.readline() # read 'CMD'
# self.port.write("V\n")
# s = self.port.readline()
# s = s.strip()
# # skip the copyright info. we already know about it
# self.port.readline()
# self.port.write("---\n")
# self.port.readline() # read 'END'
# return s
#rootoothVersion = property(__rootoothVersion)
def __isconnected(self):
return self.port.isopen()
isconnected = property(__isconnected)
def wakeup(self):
#self.port.read()
#self.port.write("$$$")
#self.port.readline() # read 'CMD'
#self.port.write("S@,8080\n")
#self.port.readline() # read 'AOK'
#self.port.write("S&,8000\n")
#self.port.readline() # read 'AOK'
#self.port.write("S&,8080\n")
#self.port.readline() # read 'AOK'
#self.port.write("---\n")
#self.port.readline() # read 'END'
self.start()
def close(self):
self.port.close()
def send_to_roomba(self, data):
count = 0
bytes = ByteBuilder()
while(count < len(data)):
bytes.append(data[count])
count = count + 1
self.port.write(bytes.toCharString())
self.port.flush()
def sendcmd(self, cmd):
self.send_to_roomba([cmd])
def start(self):
self.sendcmd(128)
def control(self):
self.sendcmd(130)
def safe(self):
self.sendcmd(131)
def full(self):
self.sendcmd(132)
def off(self):
self.sendcmd(133)
def spot(self):
self.sendcmd(134)
def clean(self):
self.sendcmd(135)
def max(self):
self.sendcmd(136)
def __get_speed(self):
if self.__speed < 0:
return 0
if self.__speed > 500:
return 500
return self.__speed
def __set_speed(self, speedInt):
if speedInt < 0:
self.__speed = 0
elif speedInt > 500:
self.__speed = 500
else:
self.__speed = speedInt
speed = property(__get_speed, __set_speed)
def drive(self, velocity, radius):
vel = ConvertToBytes(int16(velocity).value)
rad = ConvertToBytes(int16(radius).value)
self.send_to_roomba([
137,
vel.high_byte(),
vel.low_byte(),
rad.high_byte(),
rad.low_byte()
])
def forward(self):
self.drive(self.speed, -32768)
def backward(self):
self.drive(self.speed * -1, -32768)
def left(self):
self.drive(self.speed, 2)
def right(self):
self.drive(self.speed, -2)
def spin_left(self):
self.drive(self.speed, 1)
def spin_right(self):
self.drive(self.speed, -1)
def stop(self):
self.drive(0, 0)
def motors(self, data):
self.send_to_roomba([138, data])
def led(self, led, color, intensity):
self.send_to_roomba([
139,
led,
color,
intensity
])
def song(self):
return
def play(self, songNum):
self.send_to_roomba([
141,
songNum
])
def dock(self):
self.sendcmd(143)
def __sensors(self):
self.port.flushInput()
self.send_to_roomba([
142,
0
])
self.port.flush()
s = self.port.read(26)
#print "sensors len: " + str(len(str(s)))
#if len(s) < 26:
# return None
if len(s) > 26:
return self.sensors
data = []
output = str(s)
x = 0
end = len(output)
while(x < end):
data.append(ord(output[x]))
x = x + 1
#print "sensors: " + str(data)
# hack to bypass length issue
while (x < 26):
data.append(0)
x = x + 1
return SensorData(data)
sensors = property(__sensors)
if __name__ == "__main__":
# example
print "starting ..."
if (len(sys.argv) < 2):
port = "/dev/ttyS0"
else:
port = sys.argv[1]
if (len(sys.argv) < 3):
baudrate = 115200
else:
baudrate = int(sys.argv[2])
x = RoombaAPI(port, baudrate)
try:
print "Connect"
x.connect()
print "Control"
x.control()
print "cliff " + str(x.sensors.cliff.left)
print "cliff " + str(x.sensors.cliff.front_left)
print "cliff " + str(x.sensors.cliff.front_right)
print "cliff " + str(x.sensors.cliff.right)
print "battery " + str(x.sensors.voltage)
print "charging state " + str(x.sensors.charging_state)
print "charge " + str(x.sensors.charge)
print "capacity " + str(x.sensors.capacity)
x.spin_left()
time.sleep(5)
x.stop()
finally:
print "Off"
x.off()
x.close()