-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisinfect_main_api.py
executable file
·408 lines (342 loc) · 13.9 KB
/
disinfect_main_api.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
import qwiic_vl53l1x
import qwiic_tca9548a
import serial
import time
from pycreate2 import Create2
DRIVE_SPEED = 100
ROTATE_SPEED = 50
"""
drive roomba rotate 90 degrees
in clockwise direction or counterclockwise direction
"""
def rotate90(clockwise): # rotate 90 deg
sensors = roomba.get_sensors()
angleRotated = 0 # variable to calculated amount of rotation
if clockwise == 0: # ccw
print("Detected, rotate ccw")
roomba.drive_direct(DRIVE_SPEED, -DRIVE_SPEED)
while angleRotated < 90:
sensors = roomba.get_sensors()
angleRotated += abs(sensors.angle)
print("Angle Rotated: ", angleRotated)
else: # cw
print("Detected, rotate cw")
roomba.drive_direct(-DRIVE_SPEED, DRIVE_SPEED)
while angleRotated < 90:
sensors = roomba.get_sensors()
angleRotated += abs(sensors.angle)
print("Angle Rotated: ", angleRotated)
roomba.drive_stop()
"""
# Another way to rotate 90 degrees
def rotate90(clockwise):
if clockwise == 0: # ccw
print("Rotate ccw")
roomba.drive_direct(ROTATE_SPEED, -ROTATE_SPEED)
time.sleep(3.1)
else: # cw
print("Rotate cw")
roomba.drive_direct(-ROTATE_SPEED, ROTATE_SPEED)
time.sleep(3.1)
roomba.drive_stop()
"""
"""
drive roomba forward for a short distance
will quit disinfection mode when hit obstacle
"""
def forwardShort():
global stopDisinfection
startTime = time.time()
roomba.drive_direct(DRIVE_SPEED, DRIVE_SPEED)
print("Move forward a short distance")
continueDrive = True
while continueDrive:
sensors = roomba.get_sensors()
# time.sleep(0.5)
# print(time.time(), "\t", sensors.bumps_wheeldrops[0], sensors.bumps_wheeldrops[1])
if sensors.bumps_wheeldrops[0] or sensors.bumps_wheeldrops[1]:
print("Obstacle detected, stop disinfection")
roomba.drive_stop()
continueDrive = False
stopDisinfection = True
break
if time.time() - startTime > 4:
roomba.drive_stop()
continueDrive = False
"""
drive roomba backword for a short distance
"""
def backwardShort():
print("Move backward a short distance")
startTime = time.time()
roomba.drive_direct(-DRIVE_SPEED, -DRIVE_SPEED)
continueDrive = True
while continueDrive:
if time.time() - startTime > 2:
roomba.drive_stop()
continueDrive = 0
"""
drive roomba forward while under the table
until outside table based on ToF measured distance
"""
def forwardUntilOutsideTable(): # move forward until outside table based on ToF
global stopDisinfection
print("Move forward until outside table based on ToF")
roomba.drive_direct(DRIVE_SPEED, DRIVE_SPEED)
continueDrive = True # Variable on whether we should keep moving forward
underTable = True # Variable on whether robot is under a table or not
for _ in range(2): # throw first 20 measurements
getFrontTof()
while continueDrive:
sensors = roomba.get_sensors()
# time.sleep(0.5)
# print(time.time(), "\t", sensors.bumps_wheeldrops[0], sensors.bumps_wheeldrops[1])
if sensors.bumps_wheeldrops[0] or sensors.bumps_wheeldrops[1]: # If detect a table leg, we should stop disinfecting
print("Obstacle detected, stop disinfection")
roomba.drive_stop()
continueDrive = False
stopDisinfection = True
break
if underTable == True: # Table detected
if getFrontTof() == 0: # If table no longer detected
underTable = False
startTime = time.time() # Store time since table first not detected
else: # No table detected
if time.time() - startTime > 2: # Has been out of table for 2sec
roomba.drive_stop()
continueDrive = False
"""
drive roomba forward while looking for leg
stop driving when leg is detected
"""
def lookForLeg(): # Keep moving forward, and stop when leg is detected
print("Looking for leg")
roomba.drive_direct(DRIVE_SPEED, DRIVE_SPEED)
continueDrive = True # Variable on whether we should keep moving forward
while continueDrive:
sensors = roomba.get_sensors()
# time.sleep(0.5)
# print(time.time(), "\t", sensors.bumps_wheeldrops[0], sensors.bumps_wheeldrops[1])
if sensors.bumps_wheeldrops[0] or sensors.bumps_wheeldrops[1]: # If leg is detected, stop
print("Table leg detected")
roomba.drive_stop()
continueDrive = False
break
"""
align to table using ToF sensors
"""
def alignToTable(): # Align robot to table when facing to it using ToF sensor
print("Align to table ...")
global stopDisinfection
for _ in range(20):
getRightTof()
getLeftTof()
continueDrive = 1 # Variable on whether we should keep moving forward
while continueDrive:
sensors = roomba.get_sensors()
# time.sleep(0.5)
# print(time.time(), "\t", sensors.bumps_wheeldrops[0], sensors.bumps_wheeldrops[1])
if sensors.bumps_wheeldrops[0] or sensors.bumps_wheeldrops[1]:
print("Obstacle detected, stop disinfection")
roomba.drive_stop()
print("Roomba stopped")
continueDrive = False
stopDisinfection = True
break
if getRightTof() == 1 and getLeftTof() == 1: # If both sensor detects table, stop as table is aligned
roomba.drive_stop()
continueDrive = False
elif getRightTof() == 1 and getLeftTof() == 0: # If right ToF detects table, move only left wheel
roomba.drive_direct(0, DRIVE_SPEED)
elif (getRightTof() == 0 and getLeftTof() == 1): # If left ToF detects table, move only right wheel
roomba.drive_direct(DRIVE_SPEED, 0)
else: # no ToF sensor detects table yet
roomba.drive_direct(DRIVE_SPEED, DRIVE_SPEED)
def getFrontTof(): # Measure using forward ToF sensor, return 1 if Roomba is under table
mux.disable_all()
mux.enable_channels(1)
ToF.start_ranging() # Write configuration bytes to initiate measurement
time.sleep(0.05)
distance = ToF.get_distance() # Get the result of the measurement from the sensor
# print(distance)
time.sleep(0.05)
ToF.stop_ranging()
# print("Front ToF: ", distance)
if (distance < 1300): # may need to be adjusted
return 1
else:
return 0
def getRightTof(): # Measure using right ToF sensor, return 1 if Roomba is under table
mux.disable_all()
mux.enable_channels(7)
ToF.start_ranging() # Write configuration bytes to initiate measurement
time.sleep(.005)
distance = ToF.get_distance() # Get the result of the measurement from the sensor
time.sleep(.005)
ToF.stop_ranging()
#print(distance)
if (distance < 1300): # may need to be adjusted
return 1
else:
return 0
def getLeftTof(): # Measure using left ToF sensor, return 1 if Roomba is under table
mux.disable_all()
mux.enable_channels(6)
ToF.start_ranging() # Write configuration bytes to initiate measurement
time.sleep(.005)
distance = ToF.get_distance() # Get the result of the measurement from the sensor
time.sleep(.005)
ToF.stop_ranging()
#print(distance)
if (distance < 1300): # may need to be adjusted
return 1
else:
return 0
"""
Set arm positions
"""
def armUp():
print("Move robot arm to up position")
arm.write(b'\x55\x55\x02\x07')
time.sleep(1)
arm.write(b'\x55\x55\x05\x06\x02\x01\x00')
def armDown():
print("Move robot arm to down position")
arm.write(b'\x55\x55\x02\x07')
time.sleep(1)
arm.write(b'\x55\x55\x05\x06\x01\x01\x00')
def armStorage():
print("Move robot arm to storage position")
arm.write(b'\x55\x55\x02\x07')
time.sleep(1)
arm.write(b'\x55\x55\x05\x06\x00\x01\x00')
def tableDisinfection(roomba):
global stopDisinfection
time.sleep(1)
roomba.start()
print("Roomba starting ...")
time.sleep(1)
roomba.safe()
print("Set Roomba to SAFE mode")
time.sleep(1)
# robotMode: 1 == wander and look for table, 2 == initial setup when detecting a table, 3 == disinfect table
robotMode = 1 # If True, robot will wander in cleanmode to look for table. if false will disinfect table
stopDisinfection = False # set to True if entire table has been disinfected, verified by bump and prox sensors, or if error is predicted
lastTableDisinfectTime = -100 # Set to -10s ago
try:
while True:
if robotMode == 1:
# Set to clean mode
armDown()
time.sleep(5)
roomba.clean()
print("Set Roomba to CLEAN mode")
time.sleep(0.2) # NEED TO MAKE THIOS LONGER TO ALLOW EXIT TABLE
for _ in range(2):
getFrontTof() # Throw away early measurements
while (robotMode == 1): # Continue to look for table
if (getFrontTof() == 1) and (time.time() - lastTableDisinfectTime > 30): # If table detected, go to robotMode 2
robotMode = 2
print("Detected Roomba under table")
elif robotMode == 2:
# Set to safe mode
roomba.safe()
time.sleep(0.2)
armUp()
# STEP 1: Align robot to table using ToF sensor
backwardShort()
alignToTable()
# STEP 2: Rotate ccw
rotate90(0) # Rotate ccw after aligning with table
# STEP 3: Move along table while alinging using ToF sensor, will stop when table leg deteced
lookForLeg() # Move along the table until table leg is detected
backwardShort()
# STEP 4: Rotate clockwise to face table again
rotate90(1) # rotate 90deg cw
# STEP 5: Move back a bit to allow alignment in the next step
backwardShort()
# Next mode is to disninfect the table
robotMode = 3
elif robotMode == 3:
while (robotMode == 3):
# STEP 1: alignment to table first before continue
alignToTable()
if stopDisinfection == True:
robotMode = 1
break
# STEP 2: Move forward until leaving table, table not detected by front ToF sensor
forwardUntilOutsideTable() # Should be moving forward until leaving table, but ToF not available yet
if stopDisinfection == True:
robotMode = 1
break
# STEP 3: Rotate to the next lane clockwise direction
rotate90(1)
forwardShort()
if stopDisinfection == True:
robotMode = 1
break
rotate90(1)
# STEP 4: alignment to table first before continue (repeat of step 1)
backwardShort()
alignToTable()
if stopDisinfection == True:
robotMode = 1
break
# Step 5: Move forward until leaving table, table not detected by front ToF sensor (repeat of step 2)
forwardUntilOutsideTable()
if stopDisinfection == True:
robotMode = 1
break
# Step 6: Rotate to the next lane counter-clockwise direction (reverse direction of step 3)
rotate90(0)
forwardShort()
if stopDisinfection == True:
robotMode = 1
break
rotate90(0)
# Loop around to align to table again and continue to disinfect table.
# Disinfection will stop if entire table has been disinfected, verified by bump and prox sensors
robotMode = 1 # Next state will be robotMode 1 to look for another table
roomba.safe()
time.sleep(2)
lastTableDisinfectTime = time.time()
stopDisinfection = False # Reset stopDisinfection value
except KeyboardInterrupt:
print("Exit - from keyboard")
roomba.drive_stop()
roomba.safe()
armStorage()
time.sleep(5)
if __name__ == "__main__":
#### Arm setup start ###
arm = serial.Serial(port='/dev/ttyS0',
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1)
#### Arm setup end ###
### TOF distance sensor setup start ###
mux = qwiic_tca9548a.QwiicTCA9548A()
mux.disable_all()
mux.enable_channels(1)
ToF = qwiic_vl53l1x.QwiicVL53L1X()
ToF.sensor_init()
if ToF.sensor_init() == None:
print("Sensor 1 front online!")
ToF.set_distance_mode(2)
mux.disable_all()
mux.enable_channels(6)
ToF.sensor_init()
if ToF.sensor_init() == None:
print("Sensor 6 right online!")
ToF.set_distance_mode(2)
mux.disable_all()
mux.enable_channels(7)
ToF.sensor_init()
if ToF.sensor_init() == None:
print("Sensor 7 left online!")
ToF.set_distance_mode(2)
### TOF distance sensor setup end ###
roomba = Create2("/dev/ttyUSB0", 115200) # set up roomba
tableDisinfection(roomba) # start disinfection