-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcleaner_bot_control.py
427 lines (343 loc) · 12.1 KB
/
cleaner_bot_control.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
'''
Name: cleaner_bot_controller.py
Author: Chandravaran Kunjeti
'''
import time
import RPi.GPIO as GPIO
import sys, select, termios, tty
'''
Class that contains all the function required to control the robot.
'''
class CleanerBotControl:
def __init__(self):
'''
Looking at the robot from the back the naming convention is given.
'''
self.motor_left_in1 = 24 # Pin for the left motor positive terminal
self.motor_left_in2 = 23 # Pin for the left motor negative terminal
self.motor_left_en = 25 # Pin to enable the left motor
self.motor_right_in1 = 4 # Pin for the right motor positive terminal
self.motor_right_in2 = 27 # Pin for the right motor negative terminal
self.motor_right_en = 22 # Pin to enable the right motor
self.servo1_left_pin = 19 # PWM pin of the left front motor-servo
self.servo2_right_pin = 26 # PWM pin of the right front motor-servo
self.servo3_flap_pin = 21 # PWM pin of the flap motor-servo
self.fan_motor_pin = 12 # Trigger pin for the PC Fan.
self.count = 1 # Varriable to reprint the options.
# Setting Mode
GPIO.setmode(GPIO.BCM)
# Setup
GPIO.setup(self.motor_right_in1,GPIO.OUT)
GPIO.setup(self.motor_right_in2,GPIO.OUT)
GPIO.setup(self.motor_right_en,GPIO.OUT)
GPIO.setup(self.motor_left_in1,GPIO.OUT)
GPIO.setup(self.motor_left_in2,GPIO.OUT)
GPIO.setup(self.motor_left_en,GPIO.OUT)
GPIO.setup(self.servo1_left_pin,GPIO.OUT)
GPIO.setup(self.servo2_right_pin,GPIO.OUT)
GPIO.setup(self.servo3_flap_pin,GPIO.OUT)
GPIO.setup(self.fan_motor_pin,GPIO.OUT)
# Initialization
GPIO.output(self.motor_right_in1,GPIO.LOW)
GPIO.output(self.motor_right_in2,GPIO.LOW)
GPIO.output(self.motor_left_in1,GPIO.LOW)
GPIO.output(self.motor_left_in2,GPIO.LOW)
GPIO.output(self.fan_motor_pin,GPIO.LOW)
# Setting up pdm on enable pin
self.motor_right=GPIO.PWM(self.motor_right_en,1000)
self.motor_left=GPIO.PWM(self.motor_left_en,1000)
self.servo1 = GPIO.PWM(self.servo1_left_pin,50)
self.servo2 = GPIO.PWM(self.servo2_right_pin,50)
self.servo3 = GPIO.PWM(self.servo3_flap_pin,50)
self.duty_cycle_right = 40
self.duty_cycle_left = 40
# Keeping low speed
self.motor_right.start(self.duty_cycle_right)
self.motor_left.start(self.duty_cycle_left)
self.servo1.start(0)
self.servo2.start(0)
self.servo3.start(0)
self.msg = """
This code allows us teleoperation control of cleaner bot.
The input is a keyboard press, with the following options
---------------------------
Moving around:
u i o
j k l
m , .
anything else : stop
1 : Low
2 : Medium
3 : High
c: Closing the front arms
v: Opening the front arms
d: Closing the front flap
f: opening the front flap
r: Fan on
t: Fan on
e: to quit
---------------------------
"""
print(self.msg)
self.main()
'''
Brief: The Function to move forward.
'''
def moveForward(self):
# Right Motor
GPIO.output(self.motor_right_in1,GPIO.HIGH)
GPIO.output(self.motor_right_in2,GPIO.LOW)
# Left Motor
GPIO.output(self.motor_left_in1,GPIO.HIGH)
GPIO.output(self.motor_left_in2,GPIO.LOW)
print("Forward")
'''
Brief: The Function to stop the robot.
'''
def stop(self):
# Right Motor
GPIO.output(self.motor_right_in1,GPIO.LOW)
GPIO.output(self.motor_right_in2,GPIO.LOW)
# Left Motor
GPIO.output(self.motor_left_in1,GPIO.LOW)
GPIO.output(self.motor_left_in2,GPIO.LOW)
print("Stop")
'''
Brief: The Function to move backward.
'''
def moveBackward(self):
# Right Motor
GPIO.output(self.motor_right_in1,GPIO.LOW)
GPIO.output(self.motor_right_in2,GPIO.HIGH)
# Left Motor
GPIO.output(self.motor_left_in1,GPIO.LOW)
GPIO.output(self.motor_left_in2,GPIO.HIGH)
print("Backward")
'''
Brief: The Function to turn left in place.
'''
def turnLeftInPlace(self):
# Right Motor
GPIO.output(self.motor_right_in1,GPIO.HIGH)
GPIO.output(self.motor_right_in2,GPIO.LOW)
# Left Motor
GPIO.output(self.motor_left_in1,GPIO.LOW)
GPIO.output(self.motor_left_in2,GPIO.HIGH)
print("Turn left in place")
'''
Brief: The Function to turn right in place.
'''
def turnRightInPlace(self):
# Right Motor
GPIO.output(self.motor_right_in1,GPIO.LOW)
GPIO.output(self.motor_right_in2,GPIO.HIGH)
# Left Motor
GPIO.output(self.motor_left_in1,GPIO.HIGH)
GPIO.output(self.motor_left_in2,GPIO.LOW)
print("Turn right in place")
'''
Brief: The Function to turn left about left wheel while moving forward.
'''
def turnLeftAboutLeftWheelFront(self):
# Right Motor
GPIO.output(self.motor_right_in1,GPIO.HIGH)
GPIO.output(self.motor_right_in2,GPIO.LOW)
# Left Motor
GPIO.output(self.motor_left_in1,GPIO.LOW)
GPIO.output(self.motor_left_in2,GPIO.LOW)
print("Turn left about left wheel front")
'''
Brief: The Function to turn right about right wheel while moving forward.
'''
def turnRightAboutRightWheelFront(self):
# Right Motor
GPIO.output(self.motor_right_in1,GPIO.LOW)
GPIO.output(self.motor_right_in2,GPIO.LOW)
# Left Motor
GPIO.output(self.motor_left_in1,GPIO.HIGH)
GPIO.output(self.motor_left_in2,GPIO.LOW)
print("Turn right about right wheel front")
'''
Brief: The Function to turn left about left wheel while moving backward.
'''
def turnLeftAboutLeftWheelBack(self):
# Right Motor
GPIO.output(self.motor_right_in1,GPIO.LOW)
GPIO.output(self.motor_right_in2,GPIO.HIGH)
# Left Motor
GPIO.output(self.motor_left_in1,GPIO.LOW)
GPIO.output(self.motor_left_in2,GPIO.LOW)
print("Turn left about left wheel back")
'''
Brief: The Function to turn right about right wheel while moving backward.
'''
def turnRightAboutRightWheelBack(self):
# Right Motor
GPIO.output(self.motor_right_in1,GPIO.LOW)
GPIO.output(self.motor_right_in2,GPIO.LOW)
# Left Motor
GPIO.output(self.motor_left_in1,GPIO.LOW)
GPIO.output(self.motor_left_in2,GPIO.HIGH)
print("Turn right about right wheel back")
'''
Brief: The Function to close the front arms, as the servo in use is a 360* servo
so it uses velocities to move. As there is no proper relation for between the speed
of revolution and direction. I am commanding one servo to move for a longer period of
time.
'''
def closeFront(self):
self.servo1.ChangeDutyCycle(11)
self.servo2.ChangeDutyCycle(6)
time.sleep(0.25)
self.servo1.ChangeDutyCycle(1)
time.sleep(0.2)
self.servo2.ChangeDutyCycle(1)
'''
Brief: The Function to open the front arms, as the servo in use is a 360* servo
so it uses velocities to move. As there is no proper relation for between the speed
of revolution and direction. I am commanding one servo to move for a longer period of
time.
'''
def openFront(self):
self.servo1.ChangeDutyCycle(6)
self.servo2.ChangeDutyCycle(11)
time.sleep(0.25)
self.servo2.ChangeDutyCycle(1)
time.sleep(0.2)
self.servo1.ChangeDutyCycle(1)
'''
Brief: The Function to close the front flap.
'''
def flapUp(self):
self.servo3.ChangeDutyCycle(0.1)
'''
Brief: The Function to open the front flap.
'''
def flapDown(self):
self.servo3.ChangeDutyCycle(11.9)
'''
Brief: The Function to switch on the vaccum fan.
'''
def fanOn(self):
GPIO.output(self.fan_motor_pin,GPIO.HIGH)
'''
Brief: The Function to switch off the vaccum fan.
'''
def fanOff(self):
GPIO.output(self.fan_motor_pin,GPIO.LOW)
'''
Brief: The Function to set the duty cycles for the back wheels, to control the speed.
'''
def setDuty(self,right,left):
self.motor_right.ChangeDutyCycle(right)
self.motor_left.ChangeDutyCycle(left)
'''
Brief: The function that takes the keyboard interupt
'''
def getKey(self):
tty.setraw(sys.stdin.fileno())
rlist, _, _ = select.select([sys.stdin], [], [], 0.1)
if rlist:
key = sys.stdin.read(1)
else:
key = ''
termios.tcsetattr(sys.stdin, termios.TCSADRAIN,termios.tcgetattr(sys.stdin))
return key
'''
Brief: The main function that takes in keyboard input.
'''
def main(self):
while(1):
key =self.getKey() #raw_input()
try:
if key=='k':
self.stop()
self.count += 1
elif key=='i':
self.moveForward()
key='z'
self.count += 1
elif key==',':
self.moveBackward()
key='z'
self.count += 1
elif key=='j':
self.turnLeftInPlace()
key='z'
elif key=='l':
self.turnRightInPlace()
key='z'
self.count += 1
elif key=='u':
self.turnLeftAboutLeftWheelFront()
key='z'
self.count += 1
elif key=='o':
self.turnRightAboutRightWheelFront()
key='z'
self.count += 1
elif key=='m':
self.turnLeftAboutLeftWheelBack()
key='z'
self.count += 1
elif key=='.':
self.turnRightAboutRightWheelBack()
key='z'
self.count += 1
elif key=='1':
self.setDuty(40,40)
print("Low")
key='z'
self.count += 1
elif key=='2':
self.setDuty(70,70)
print("Medium")
key='z'
self.count += 1
elif key=='3':
self.setDuty(100,100)
print("High")
key='z'
self.count += 1
elif key=='c':
self.closeFront()
self.count += 1
print("close")
key = 'z'
elif key=='v':
self.openFront()
self.count += 1
print("open")
key = 'z'
elif key=='f':
self.flapUp()
self.count += 1
print("Close Flap")
key = 'z'
elif key=='d':
self.flapDown()
self.count += 1
print("Open Flap")
key = 'z'
elif key=='r':
self.fanOn()
self.count += 1
print("Fan On")
key = 'z'
elif key=='t':
self.fanOff()
self.count += 1
print("Fan off")
key = 'z'
elif key=='e':
GPIO.cleanup()
print("GPIO Clean up")
break
if self.count%10 == 0:
print(self.msg)
except Exception as e:
print(e)
finally:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN,termios.tcgetattr(sys.stdin))
if __name__ == "__main__":
control = CleanerBotControl()