-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti.py
396 lines (289 loc) · 15.3 KB
/
multi.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
import threading
import time
import math
import noise
from limits import angleMod, mapToLimits
from actions import *
from debug import DEBUG_MOTION, DEBUG_Y, DEBUG_S, DEBUG_M
from robot_1 import INITIAL_MPU_YAW_OFFSET, WHEEL_BASE
SAMPLE_SIZE = 40
SAMPLE_TIMEPERIOD = 0.1 #seconds
MAX_YAW_SAMPLE_RANGE = 0.05 #degrees
# INITIAL_MPU_YAW_OFFSET = 9 #degrees
INITIAL_ROBOT_ACTION = STILL
INITIAL_ROBOT_ACTION_VALUE_1 = 0
INITIAL_ROBOT_ACTION_VALUE_2 = 0
DEFAULT_ROBOT_LOCATION = None
# WHEEL_BASE = 0.33 #meters
class MotorHandlerPlaceholder():
def setDesiredSpeedAndSteering(self, desired_speed, desired_steering):
return False
def debug(self):
print "MotorHandler not passed to MotionThread yet"
def robotDisplacementByArcApproximation(length, theta_1, theta_2):
theta_1 = math.radians(theta_1) #convert angles into radians
theta_2 = math.radians(theta_2)
dtheta = theta_2 - theta_1
radius = (length / dtheta) + (WHEEL_BASE / 2)
dx = radius * (math.sin(theta_2) - math.sin(theta_1))
dy = radius * (math.cos(theta_1) - math.cos(theta_2))
return {'x': dx, 'y': dy}
def wheelDisplacementByLineApproximation(length, theta_1, theta_2):
theta_1 = math.radians(theta_1) #convert angles into radians
theta_2 = math.radians(theta_2)
average_theta = (theta_1 + theta_2) / 2
dx = length * math.cos(average_theta)
dy = length * math.sin(average_theta)
return {'x': dx, 'y': dy}
class MotionThread(threading.Thread):
def __init__(self, power, MpuHandler, YawPid, DistancePid, EncoderHandler, time_period = 0.02): #50hz default
threading.Thread.__init__(self)
self.name = "MotionThread"
self.steering = 0
self.speed = 0
self.yaw = 0
self.last_yaw = 0
self.desired_yaw = 0
self.distance = 0
self.last_distance = 0
self.desired_distance = 0
self.location_update_lock = threading.Lock()
self.robot_location = DEFAULT_ROBOT_LOCATION
self.power = power
self.D = MpuHandler
self.Y = YawPid
self.S = DistancePid
self.E = EncoderHandler
self.M = MotorHandlerPlaceholder()
self.time_period = time_period
self.time_to_sleep = time_period
self.mpu_yaw_offset = INITIAL_MPU_YAW_OFFSET
self.action_bundle_stack = []
self.action_lock = threading.Lock()
self.addAction(INITIAL_ROBOT_ACTION, INITIAL_ROBOT_ACTION_VALUE_1, INITIAL_ROBOT_ACTION_VALUE_2)
def addAction(self, action, action_value_1 = 0, action_value_2 = 0):
with self.action_lock:
action_bundle = [action, action_value_1, action_value_2]
self.action_bundle_stack.append(action_bundle)
def clearActionBundleStack(self):
with self.action_lock:
self.action_bundle_stack = []
def setAction(self, action, action_value_1 = 0, action_value_2 = 0):
self.clearActionBundleStack()
self.addAction(action, action_value_1, action_value_2)
def processActionBundleStack(self):
with self.action_lock:
while (len(self.action_bundle_stack) != 0):
action_bundle = self.action_bundle_stack.pop(0)
action = action_bundle[0]
action_value_1 = action_bundle[1]
action_value_2 = action_bundle[2]
if (action == STILL):
self.Y.stop()
self.S.stop()
print "New Action: STILL"
noise.signalAction(self.power)
elif (action == TURN):
self.Y.restart()
self.desired_yaw = self.yaw + action_value_1
self.S.stop()
print "New Action: TURN, with value = " + str(action_value_1)
noise.signalAction(self.power)
elif (action == TURN_CHANGE):
self.desired_yaw = self.yaw + action_value_1
#print "New Action: TURN_CHANGE, with value = " + str(action_value_1)
elif (action == TURN_TO):
self.Y.restart()
self.desired_yaw = action_value_1
self.S.stop()
print "New Action: TURN_TO, with value = " + str(action_value_1)
noise.signalAction(self.power)
elif (action == TURN_TO_CHANGE):
self.desired_yaw = action_value_1
#print "New Action: TURN_TO_CHANGE, with value = " + str(action_value_1)
elif (action == MOVE):
self.Y.stop()
self.S.restart()
self.desired_distance = self.distance + action_value_1
self.S.setSetpoint(self.desired_distance)
print "New Action: MOVE, with value = " + str(action_value_1)
noise.signalAction(self.power)
elif (action == MOVE_CHANGE):
self.desired_distance = self.distance + action_value_1
self.S.setSetpoint(self.desired_distance)
#print "New Action: MOVE_CHANGE, with value = " + str(action_value_1)
elif (action == MOVE_TO):
self.Y.stop()
self.S.restart()
self.desired_distance = action_value_1
self.S.setSetpoint(self.desired_distance)
print "New Action: MOVE_TO, with value = " + str(action_value_1)
noise.signalAction(self.power)
elif (action == MOVE_TO_CHANGE):
self.desired_distance = action_value_1
self.S.setSetpoint(self.desired_distance)
#print "New Action: MOVE_TO_CHANGE, with value = " + str(action_value_1)
elif (action == MOVE_HOLD):
self.Y.restart()
self.desired_yaw = self.yaw
self.S.restart()
self.desired_distance = self.distance + action_value_1
self.S.setSetpoint(self.desired_distance)
print "New Action: MOVE_HOLD, with value = " + str(action_value_1)
noise.signalAction(self.power)
elif (action == MOVE_TO_HOLD):
self.Y.restart()
self.desired_yaw = self.yaw
self.S.restart()
self.desired_distance = action_value_1
self.S.setSetpoint(self.desired_distance)
print "New Action: MOVE_TO_HOLD, with value = " + str(action_value_1)
noise.signalAction(self.power)
elif (action == MOVE_AND_TURN_TO):
self.Y.restart()
self.desired_yaw = action_value_2
self.S.restart()
self.desired_distance = self.distance + action_value_1
self.S.setSetpoint(self.desired_distance)
print "New Action: MOVE_AND_TURN_TO, with value_1 = " + str(action_value_1) + ", value_2 = " + str(action_value_2)
noise.signalAction(self.power)
elif (action == MOVE_AND_TURN_TO_CHANGE):
self.desired_yaw = action_value_2
self.desired_distance = self.distance + action_value_1
self.S.setSetpoint(self.desired_distance)
#print "New Action: MOVE_AND_TURN_TO_CHANGE, with value_1 = " + str(action_value_1) + ", value_2 = " + str(action_value_2)
else:
print "ERROR: unknown action processed in motionThread.processAction"
noise.signalBad(self.power)
def setRobotLocation(self, robot_location):
with self.location_update_lock:
self.robot_location = robot_location
self.last_distance = self.distance
self.mpu_yaw_offset += self.yaw - self.robot_location['yaw']
self.yaw = self.robot_location['yaw']
self.last_yaw = self.yaw
def updateRobotLocation(self):
with self.location_update_lock:
theta_1 = self.last_yaw
theta_2 = self.yaw
length = self.distance - self.last_distance
#displacement = wheelDisplacementByLineApproximation(length, theta_1, theta_2)
if (theta_2 == theta_1): #error case for arc length /0 err
displacement = wheelDisplacementByLineApproximation(length, theta_1, theta_2)
else:
displacement = robotDisplacementByArcApproximation(length, theta_1, theta_2)
self.robot_location['x'] += displacement['x']
self.robot_location['y'] += displacement['y']
self.robot_location['yaw'] = self.yaw
#self.robot_location['pitch'] = self.pitch
#self.robot_location['roll'] = self.roll
self.last_yaw = self.yaw
self.last_distance = self.distance
def updateYaw(self):
if (self.D.updateAll() == True):
with self.location_update_lock:
self.yaw = self.D.yaw_without_drift - self.mpu_yaw_offset
else: #self.D.update() == False
print "ERROR: D returned false in Motion Thread"
noise.signalBad(self.power)
def updateDistance(self):
if (self.E.update() == True):
with self.location_update_lock:
self.distance = self.E.distance
else: #self.E.update() == False
print "ERROR: E returned false in Motion Thread"
noise.signalBad(self.power)
def updateSensors(self):
self.updateDistance()
self.updateYaw()
def runYawPid(self):
new_steering = False
yaw_pid_input = angleMod(self.desired_yaw - self.yaw)
if (self.Y.run(yaw_pid_input) == True):
self.steering = self.Y.output
new_steering = True
else:
print "ERROR: Y returned false in Motion Thread"
noise.signalBad(self.power)
return new_steering
def runDistancePid(self):
new_speed = False
distance_pid_input = self.distance
if (self.S.run(distance_pid_input) == True):
self.speed = self.S.output
new_speed = True
else:
print "ERROR: S returned false in Motion Thread"
noise.signalBad(self.power)
return new_speed
def updateMotors(self, new_steering, new_speed):
if (new_steering == True or new_speed == True):
if (self.M.setDesiredSpeedAndSteering(self.speed, self.steering) == False):
print "ERROR: speed and steering not set in MotorHandler"
noise.signalBad(self.power)
else:
print "ERROR: speed and steering not set in Motion Thread"
noise.signalBad(self.power)
def calibrationCheck(self):
def check():
yaws = ()
distances = ()
for x in range(0, SAMPLE_SIZE):
self.updateYaw()
self.updateDistance()
yaws += (self.yaw,)
distances += (self.distance,)
time.sleep(SAMPLE_TIMEPERIOD)
yaw_range = abs(angleMod(max(yaws) - min(yaws)))
distance_range = max(distances) - min(distances)
passed = True
if (yaw_range < MAX_YAW_SAMPLE_RANGE):
print "MPU calibration test passed with yaw_range = " + str(yaw_range)
else: #range >= MAX_YAW_SAMPLE_RANGE
print "MPU calibration test failed with yaw_range = " + str(yaw_range) + ", yaws = " + str(yaws)
passed = False
if (distance_range == 0):
print "encoder calibration test passed"
else: #distance_range != 0
print "encoder calibration test failed with distance_range = " + str(distance_range) + ", distances = " + str(distances)
passed = False
return passed
print "Running MPU and encoder calibration check..."
attempts = 1
while (check() == False):
print "Test failed, trying again attempts = " +str(attempts)
noise.signalBad(self.power)
attempts += 1
print "Test passed with attempts = " +str(attempts)
noise.signalGood(self.power)
def prepareForStart(self, MotorHandler): #def prepareForStart(self, MotorHandler, start_robot_location):
self.M = MotorHandler
self.updateSensors() # freshen up sensor readings so that offsets can be set correctly
def run(self):
print "Starting " + self.name
wake_up_time = time.time() + self.time_period
while (True):
self.time_to_sleep = mapToLimits(wake_up_time - time.time(), self.time_period, 0)
wake_up_time += self.time_period
time.sleep(self.time_to_sleep)
self.updateSensors()
self.updateRobotLocation()
self.processActionBundleStack()
new_steering = self.runYawPid()
new_speed = self.runDistancePid()
self.updateMotors(new_steering, new_speed)
print "Exiting " + self.name
def debug(self):
if (DEBUG_MOTION == True):
print self.name + ", time_to_sleep = " + str(self.time_to_sleep)
print "desired_yaw = " + str(self.desired_yaw) + ", yaw = " + str(self.yaw) + ", D.yaw = " + str(self.D.yaw) + ", D.error = " + str(self.D.error)
if (DEBUG_Y == True):
self.Y.debug()
print "steering " + str(self.steering)
print "desired_distance = " + str(self.desired_distance) + ", distance = " + str(self.distance)
if (DEBUG_S == True):
self.S.debug()
print "speed = " + str(self.speed)
if (DEBUG_M == True):
self.M.debug()
print "robot_location = " + str(self.robot_location)