-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc2.py
400 lines (352 loc) · 11 KB
/
c2.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
from ev3dev2.motor import MediumMotor, OUTPUT_C, OUTPUT_A, OUTPUT_B
from time import sleep
from ev3dev2.motor import LargeMotor, OUTPUT_A, OUTPUT_B, MoveTank, MoveDifferential
from ev3dev2.sensor.lego import ColorSensor
from ev3dev2.sensor import INPUT_1
from ev3dev2.led import Leds
from ev3dev2.sensor.lego import GyroSensor, UltrasonicSensor, ColorSensor
from threading import Thread, Event
from ev3dev2.wheel import EV3Tire, Wheel, EV3EducationSetTire
import os
print("System control: ")
# Define multithreading events for communication
stop_event = Event()
detected_event = Event()
# Initialize motors, sensors, and constants
drive = MoveTank(OUTPUT_A, OUTPUT_B)
grabber = MediumMotor(OUTPUT_C)
colorSensor = ColorSensor()
leds = Leds()
us = UltrasonicSensor()
grabberSpeed = 50
driveSpeed = 15
wheelCirc = 5.6 * 3.14
x = 0
y = 0
onBorder = False
right = True
chill = False
detected = False
calibrationrot = 100
mdiff = MoveDifferential(OUTPUT_A, OUTPUT_B, EV3EducationSetTire, calibrationrot)
mdiff.gyro = GyroSensor()
mdiff.gyro.calibrate()
# Lists for square and sector movements
sqXList = [0, 1500, 1500, 0, 0, 1200, 1200, 300, 300, 900, 900, 600, 600, 750, 750, 600]
sqYList = [0, 0, 1500, 1500, 300, 300, 1200, 1200, 600, 600, 900, 900, 600, 600, 750, 750]
secXList = [0, 1200, 750, 600, 0, 1500, 1200, 600, 0, 750, 900, 1500, 900, 300]
secYList = [0, 1200, 1500, 0, 600, 750, 0, 1500, 900, 750, 1500, 900, 0, 750]
# Function to drive for a given distance
def driveFor(meters):
rots = meters / wheelCirc
drive.on_for_rotations(driveSpeed, driveSpeed, rots, brake=False)
# Function to open the claw
def open_claw():
grabber.on_for_degrees(speed=grabberSpeed, degrees=360 * 2.7)
# Function to close the claw
def close_claw():
grabber.on_for_degrees(speed=-grabberSpeed, degrees=360 * 2.7)
# Thread function to check for obstacles and borders
def checkBorder():
global onBorder
global chill
global detected
while not stop_event.is_set():
color = colorSensor.color
dist = us.distance_centimeters
x = mdiff.x_pos_mm
y = mdiff.y_pos_mm
# Check for obstacle detection
if int(dist) < 4.3:
detected = True
print("DETECTED at position X:" + str(round(x)) + " Y:" + str(round(y)))
detected_event.set()
stop_event.wait()
# Pause if required
if chill:
sleep(13)
# Check for border detection
if color == 1:
onBorder = True
# Function to gracefully kill a thread using the system exit
def killThread(thread):
thread_id = thread.ident
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id, ctypes.py_object(SystemExit))
if res > 1:
ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id, 0)
print('Exception raise failure')
# Thread function to log the robot's position
def log():
while not stop_event.is_set():
x = mdiff.x_pos_mm
y = mdiff.y_pos_mm
print("X:" + str(round(x)) + " Y:" + str(round(y)))
sleep(1)
# Thread function to control the robot based on obstacle detection
def controlThread():
global detected
global sqXList, sqYList, secXList, secYList
while not stop_event.is_set():
detected_event.wait()
print("Killing Thread")
mdiff.off()
close_claw()
stop_event.set()
mdiff.odometry_stop()
mdiff.odometry_start()
mdiff.on_to_coordinates(driveSpeed, 0, 0)
print("Going Home")
sleep(20)
open_claw()
# Function to turn the robot to the right
def turn_around_r():
global y
global x
drive.turn_right(driveSpeed, 90 - 4)
driveFor(10)
y += 10
drive.turn_right(driveSpeed, 90 - 4)
driveFor(5)
# Function to turn the robot to the left
def turn_around_l():
global y
global x
drive.turn_left(driveSpeed, 90 - 5)
driveFor(10)
y += 10
drive.turn_left(driveSpeed, 90 - 5)
driveFor(5)
# Thread
2 / 2
User
rewrtie all the code please with comments
ChatGPT
Certainly! Below is your code with detailed comments explaining each section:
python
Copy code
from ev3dev2.motor import MediumMotor, OUTPUT_C, OUTPUT_A, OUTPUT_B
from time import sleep
from ev3dev2.motor import LargeMotor, OUTPUT_A, OUTPUT_B, MoveTank, MoveDifferential
from ev3dev2.sensor.lego import ColorSensor
from ev3dev2.sensor import INPUT_1
from ev3dev2.led import Leds
from ev3dev2.sensor.lego import GyroSensor, UltrasonicSensor, ColorSensor
from threading import Thread, Event
from ev3dev2.wheel import EV3Tire, Wheel, EV3EducationSetTire
import os
# Print system control message
print("System control: ")
# Define multithreading events for communication
stop_event = Event()
detected_event = Event()
# Initialize motors, sensors, and constants
drive = MoveTank(OUTPUT_A, OUTPUT_B)
grabber = MediumMotor(OUTPUT_C)
colorSensor = ColorSensor()
leds = Leds()
us = UltrasonicSensor()
grabberSpeed = 50
driveSpeed = 15
wheelCirc = 5.6 * 3.14
x = 0
y = 0
onBorder = False
right = True
chill = False
detected = False
calibrationrot = 100
mdiff = MoveDifferential(OUTPUT_A, OUTPUT_B, EV3EducationSetTire, calibrationrot)
mdiff.gyro = GyroSensor()
mdiff.gyro.calibrate()
# Lists for square and sector movements
sqXList = [0, 1500, 1500, 0, 0, 1200, 1200, 300, 300, 900, 900, 600, 600, 750, 750, 600]
sqYList = [0, 0, 1500, 1500, 300, 300, 1200, 1200, 600, 600, 900, 900, 600, 600, 750, 750]
secXList = [0, 1200, 750, 600, 0, 1500, 1200, 600, 0, 750, 900, 1500, 900, 300]
secYList = [0, 1200, 1500, 0, 600, 750, 0, 1500, 900, 750, 1500, 900, 0, 750]
# Function to drive for a given distance
def driveFor(meters):
rots = meters / wheelCirc
drive.on_for_rotations(driveSpeed, driveSpeed, rots, brake=False)
# Function to open the claw
def open_claw():
grabber.on_for_degrees(speed=grabberSpeed, degrees=360 * 2.7)
# Function to close the claw
def close_claw():
grabber.on_for_degrees(speed=-grabberSpeed, degrees=360 * 2.7)
# Thread function to check for obstacles and borders
def checkBorder():
global onBorder
global chill
global detected
while not stop_event.is_set():
color = colorSensor.color
dist = us.distance_centimeters
x = mdiff.x_pos_mm
y = mdiff.y_pos_mm
# Check for obstacle detection
if int(dist) < 4.3:
detected = True
print("DETECTED at position X:" + str(round(x)) + " Y:" + str(round(y)))
detected_event.set()
stop_event.wait()
# Pause if required
if chill:
sleep(13)
# Check for border detection
if color == 1:
onBorder = True
# Function to gracefully kill a thread using the system exit
def killThread(thread):
thread_id = thread.ident
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id, ctypes.py_object(SystemExit))
if res > 1:
ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id, 0)
print('Exception raise failure')
# Thread function to log the robot's position
def log():
while not stop_event.is_set():
x = mdiff.x_pos_mm
y = mdiff.y_pos_mm
print("X:" + str(round(x)) + " Y:" + str(round(y)))
sleep(1)
# Thread function to control the robot based on obstacle detection
def controlThread():
global detected
global sqXList, sqYList, secXList, secYList
while not stop_event.is_set():
detected_event.wait()
print("Killing Thread")
mdiff.off()
close_claw()
stop_event.set()
mdiff.odometry_stop()
mdiff.odometry_start()
mdiff.on_to_coordinates(driveSpeed, 0, 0)
print("Going Home")
sleep(20)
open_claw()
# Function to turn the robot to the right
def turn_around_r():
global y
global x
drive.turn_right(driveSpeed, 90 - 4)
driveFor(10)
y += 10
drive.turn_right(driveSpeed, 90 - 4)
driveFor(5)
# Function to turn the robot to the left
def turn_around_l():
global y
global x
drive.turn_left(driveSpeed, 90 - 5)
driveFor(10)
y += 10
drive.turn_left(driveSpeed, 90 - 5)
driveFor(5)
# Thread function for a snake-like movement
def snake():
global onBorder
global right
global x
while True:
drive.on(driveSpeed, driveSpeed)
print('x:' + str(x) + ' y:' + str(y))
dist = us.distance_centimeters
print('dist:' + str(dist))
os.system('clear')
if dist < 10:
drive.off()
driveFor(5)
close_claw()
if onBorder == True:
drive.off()
if right == True:
x = 150
turn_around_r()
chill = True
right = False
else:
x = 0
chill = True
right = True
onBorder = False
else:
continue
# Thread function for a square movement
def square():
global onBorder
global right
global x
global sqXList,sqYList
mdiff.gyro.calibrate()
mdiff.odometry_start(90,0,0,0.005)
while not stop_event.is_set():
for i in range(len(sqXList)):
mdiff.on_to_coordinates(driveSpeed,sqXList[i],sqYList[i])
sleep(1)
# Thread funciton for secotr movement
def sector():
global onBorder
global right
global x
global secXList,secYList
mdiff.gyro.calibrate()
mdiff.odometry_start(90, 0, 0, 0.005)
for i in range(len(secXList)):
mdiff.on_to_coordinates(driveSpeed,secXList[i], secYList[i])
sleep(1)
#Command Line interface
def menu():
global detected
escape = False
while escape==False:
direction = input("1.Right 2.Left 3.Open 4.Close 5.Square 6.MoveFor 7.Snake 8.Sector\n"
)
if direction=='1':
deg = input('Degrees:\n')
drive.turn_right(driveSpeed,float(deg))
elif direction=='2':
deg = input('Degrees\n')
drive.turn_left(driveSpeed,float(deg))
elif direction=='3':
open_claw()
elif direction=='4':
close_claw()
elif direction=='5':
p1=Thread(target=checkBorder)
p2=Thread(target=square)
p3=Thread(target=controlThread)
p4=Thread(target=log)
p4.start()
p1.start()
p2.start()
p3.start()
sleep(1)
elif direction=='8':
p1=Thread(target=checkBorder)
p2=Thread(target=sector)
p3=Thread(target=controlThread)
p4=Thread(target=log)
p1.start()
p2.start()
p3.start()
p4.start()
elif direction == '7':
p1 = Thread(target=snake)
p2 = Thread(target=checkBorder)
p1.start()
p2.start()
elif direction == '6':
try:
dist = input("\n")
driveFor(float(dist))
except KeyboardInterrupt:
menu()
else:
print("Exiting")
escape==True
if __name__ == '__main__':
try:
menu()
except KeyboardInterrupt:
print('Exiting\n')