-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmotion_tools.py
More file actions
321 lines (265 loc) · 10.1 KB
/
Copy pathmotion_tools.py
File metadata and controls
321 lines (265 loc) · 10.1 KB
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
#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor,
InfraredSensor, UltrasonicSensor, GyroSensor)
from pybricks.parameters import (Port, Stop, Direction, Button, Color,
SoundFile, ImageFile, Align)
from pybricks.tools import print, wait, StopWatch
from pybricks.robotics import DriveBase
from threading import Thread
#Variables
scan_head_loop = True
scan_head_move = False
scan_head_move_2 = False
GyroAvailable = False
GyroPort = Port.S1
#Rotaion Angles depends on robot wheel diameter and distance between wheels
Rotation_angle_90dg_tank_turn = 595
Rotation_angle_45dg_tank_turn = Rotation_angle_90dg_tank_turn/2
Rotation_angle_90dg_curve_turn = 1800
# Initialize Gyro
def Init_Gyro():
global GyroAvailable
GyroAvailable = True
global gyro
gyro = GyroSensor(GyroPort)
gyro.reset_angle(0)
#init Head motors
def Init_MotorA():
global motorA
motorA = Motor(Port.A)
def Init_MotorD():
global motorD
motorD = Motor(Port.D)
# 90dg Left tank turn using gyro if available
def left_tank_turn(BC_turn_speed, motorB, motorC):
global GyroAvailable
global Rotation_angle_90dg_tank_turn
if GyroAvailable:
gyro.reset_angle(0)
while gyro.angle() != 0:
gyro.reset_angle(0)
wait(50)
motorB.run(BC_turn_speed/2)
motorC.run(-BC_turn_speed/2)
while gyro.angle() > -89:
wait(10)
motorB.stop(Stop.BRAKE)
motorC.stop(Stop.BRAKE)
else:
motorB.run_angle( BC_turn_speed/2, Rotation_angle_90dg_tank_turn, Stop.BRAKE, False)
motorC.run_angle( BC_turn_speed/2,-Rotation_angle_90dg_tank_turn, Stop.BRAKE, True)
# 90dg Right tank turn using gyro if available
def right_tank_turn(BC_turn_speed, motorB, motorC):
global GyroAvailable
global Rotation_angle_90dg_tank_turn
if GyroAvailable:
gyro.reset_angle(0)
while gyro.angle() != 0:
gyro.reset_angle(0)
wait(50)
motorB.run(-BC_turn_speed/2)
motorC.run(BC_turn_speed/2)
while gyro.angle() < 89:
wait(10)
motorB.stop(Stop.BRAKE)
motorC.stop(Stop.BRAKE)
else:
motorC.run_angle( BC_turn_speed/2, Rotation_angle_90dg_tank_turn, Stop.BRAKE, False)
motorB.run_angle( BC_turn_speed/2,-Rotation_angle_90dg_tank_turn, Stop.BRAKE, True)
# 45dg Left tank turn using gyro if available
def left_tank_turn_45(BC_turn_speed, motorB, motorC):
global GyroAvailable
global Rotation_angle_45dg_tank_turn
if GyroAvailable:
gyro.reset_angle(0)
while gyro.angle() != 0:
gyro.reset_angle(0)
wait(50)
motorB.run(BC_turn_speed/2)
motorC.run(-BC_turn_speed/2)
while gyro.angle() > -44:
wait(10)
motorB.stop(Stop.BRAKE)
motorC.stop(Stop.BRAKE)
else:
motorB.run_angle( BC_turn_speed/2, Rotation_angle_45dg_tank_turn, Stop.BRAKE, False)
motorC.run_angle( BC_turn_speed/2,-Rotation_angle_45dg_tank_turn, Stop.BRAKE, True)
# 45dg Right tank turn using gyro
def right_tank_turn_45(BC_turn_speed, motorB, motorC):
global GyroAvailable
global Rotation_angle_45dg_tank_turn
if GyroAvailable:
gyro.reset_angle(0)
while gyro.angle() != 0:
gyro.reset_angle(0)
wait(50)
motorB.run(-BC_turn_speed/2)
motorC.run(BC_turn_speed/2)
while gyro.angle() < 44:
wait(10)
motorB.stop(Stop.BRAKE)
motorC.stop(Stop.BRAKE)
else:
motorC.run_angle( BC_turn_speed/2, Rotation_angle_45dg_tank_turn, Stop.BRAKE, False)
motorB.run_angle( BC_turn_speed/2,-Rotation_angle_45dg_tank_turn, Stop.BRAKE, True)
# 90dg Left curve turn using gyro
def left_curve_turn(BC_turn_speed, motorB, motorC):
global GyroAvailable
global Rotation_angle_90dg_curve_turn
if GyroAvailable:
gyro.reset_angle(0)
while gyro.angle() != 0:
gyro.reset_angle(0)
wait(50)
motorB.run(BC_turn_speed)
motorC.run(BC_turn_speed/3)
while gyro.angle() > -90:
wait(10)
motorB.stop(Stop.BRAKE)
motorC.stop(Stop.BRAKE)
else:
motorC.run_angle( BC_turn_speed/3, Rotation_angle_90dg_curve_turn/3, Stop.BRAKE, False)
motorB.run_angle( BC_turn_speed , Rotation_angle_90dg_curve_turn, Stop.BRAKE, True)
# 90dg Right curve turn using gyro
def right_curve_turn(BC_turn_speed, motorB, motorC):
global GyroAvailable
global Rotation_angle_90dg_curve_turn
if GyroAvailable:
gyro.reset_angle(0)
while gyro.angle() != 0:
gyro.reset_angle(0)
wait(50)
motorB.run(BC_turn_speed/3)
motorC.run(BC_turn_speed)
while gyro.angle() < 90:
wait(10)
motorB.stop(Stop.BRAKE)
motorC.stop(Stop.BRAKE)
else:
motorB.run_angle( BC_turn_speed/3, Rotation_angle_90dg_curve_turn/3, Stop.BRAKE, False)
motorC.run_angle( BC_turn_speed , Rotation_angle_90dg_curve_turn, Stop.BRAKE, True)
#Init Scan Head
#Home at +90 or -90dg
def init_scan_head(head_speed,homedirection,homeoffset):
if(homedirection):
motorA.run_until_stalled(head_speed, Stop.BRAKE, 50)
motorA.reset_angle(0)
motorA.run_target(head_speed, homeoffset, Stop.BRAKE, True)
else:
motorA.run_until_stalled(-head_speed, Stop.BRAKE, 50)
motorA.reset_angle(0)
motorA.run_target(head_speed, homeoffset, Stop.BRAKE, True)
motorA.reset_angle(0)
#Init Scan Head 2
#Home at +90 or -90dg
def init_scan_head_2(head_speed,homedirection,homeoffset):
if(homedirection):
motorD.run_until_stalled(head_speed, Stop.BRAKE, 50)
motorD.reset_angle(0)
motorD.run_target(head_speed, homeoffset, Stop.BRAKE, True)
else:
motorD.run_until_stalled(-head_speed, Stop.BRAKE, 50)
motorD.reset_angle(0)
motorD.run_target(head_speed, homeoffset, Stop.BRAKE, True)
motorD.reset_angle(0)
# Move MotorA with Relative Angle
def move_motorA_angle(speed,rotation_angle):
motorA.run_angle(speed, rotation_angle, Stop.BRAKE, True)
# Move MotorD with Relative Angle
def move_motorD_angle(speed,rotation_angle):
motorD.run_angle(speed, rotation_angle, Stop.BRAKE, True)
# Move Scan Head with Relative Angle
def move_scan_head_angle(head_speed, rotation_angle):
motorA.run_angle(head_speed, rotation_angle, Stop.BRAKE, True)
# Move Scan Head 2with Relative Angle
def move_scan_head_angle_2(head_speed, rotation_angle):
motorD.run_angle(head_speed, rotation_angle, Stop.BRAKE, True)
# Move Scan Head with Target Angle
def move_scan_head_target(head_speed, target_angle):
motorA.run_target(head_speed, target_angle, Stop.BRAKE, True)
#Move Scan Head with Target Angle
def move_scan_head_target_2(head_speed, target_angle):
motorD.run_target(head_speed, target_angle, Stop.BRAKE, True)
# Home Scan Head to zero dg
def home_scan_head():
global scan_head_speed
move_scan_head_target(scan_head_speed, 0)
# Home Scan Head 2 to zero dg
def home_scan_head_2():
global scan_head_speed
move_scan_head_target_2(scan_head_speed, 0)
def direction_sound(direction,GoSound = True):
# This function says Direction Number
if GoSound:
brick.sound.file(SoundFile.GO)
if direction == 0:
brick.sound.file(SoundFile.ZERO)
elif direction == 1:
brick.sound.file(SoundFile.ONE)
elif direction == 2:
brick.sound.file(SoundFile.TWO)
elif direction == 3:
brick.sound.file(SoundFile.THREE)
elif direction == 4:
brick.sound.file(SoundFile.FOUR)
elif direction == 5:
brick.sound.file(SoundFile.FIVE)
elif direction == 6:
brick.sound.file(SoundFile.SIX)
elif direction == 7:
brick.sound.file(SoundFile.SEVEN)
elif direction == 8:
brick.sound.file(SoundFile.EIGHT)
elif direction == 9:
brick.sound.file(SoundFile.NINE)
elif direction == 10:
brick.sound.file(SoundFile.TEN)
#Set up scan head thread
def scan_head_thread():
global scan_head_loop
global scan_head_move
global scan_head_move_2
global scan_head_speed
while scan_head_loop:
#Double check scan_head_loop since it could change while we are in the scan loop
if scan_head_move and scan_head_loop:
move_scan_head_target(scan_head_speed,-50)
if scan_head_move_2 and scan_head_loop:
move_scan_head_target_2(scan_head_speed,-50)
if scan_head_loop:
wait(200)
if scan_head_move and scan_head_loop:
move_scan_head_target(scan_head_speed,-25)
if scan_head_move_2 and scan_head_loop:
move_scan_head_target_2(scan_head_speed,-25)
if scan_head_loop:
wait(200)
if scan_head_move and scan_head_loop:
move_scan_head_target(scan_head_speed, 0)
if scan_head_move_2 and scan_head_loop:
move_scan_head_target_2(scan_head_speed, 0)
if scan_head_loop:
wait(200)
if scan_head_move and scan_head_loop:
move_scan_head_target(scan_head_speed, 50)
if scan_head_move_2 and scan_head_loop:
move_scan_head_target_2(scan_head_speed, 50)
if scan_head_loop:
wait(200)
if scan_head_move and scan_head_loop:
move_scan_head_target(scan_head_speed, 25)
if scan_head_move_2 and scan_head_loop:
move_scan_head_target_2(scan_head_speed, 25)
if scan_head_loop:
wait(200)
if scan_head_move and scan_head_loop:
move_scan_head_target(scan_head_speed, 0)
if scan_head_move_2 and scan_head_loop:
move_scan_head_target_2(scan_head_speed, 0)
if scan_head_loop:
wait(200)
#Start scan head thread
def start_scan_head_thread():
t_scan_head_thread = Thread(target=scan_head_thread)
t_scan_head_thread.start()