This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
forked from sooty1892/lego_storm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot.py
462 lines (389 loc) · 17.3 KB
/
robot.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
import brickpi
from localisation import localisation
import time
import math
import numpy as np
class robot:
# WARNING: IT APPEARS SENSOR PORTS 4 & 5 ARE BROKEN
# ALSO SEE MAPPINGS BELOW AS THEY ARE WRONG
# S1 => port 4 => BROKEN
# S2 => port 1
# S3 => port 2
# S4 => port 3
# S5 => port 5 => BROKEN
wheel_radius = 2.8
wheel_motors = [0, 1]
wheel_separation = 15.75
sonar_motor = 2
all_verbose = False
right_touch = 3
left_touch = 2
sonar = 1
sonar_offset = 0
# sonar rotation offset, 0 is facing forward, positive is left/anticlockwise
kp = 1.1
#############################################################################
######## MAGIC METHODS ###############################################
#############################################################################
def __init__(self, x, y, theta, draw=False, record=False):
self.interface = brickpi.Interface()
self.initialize()
self.motorEnable(robot.wheel_motors[0])
self.motorEnable(robot.wheel_motors[1])
self.motorEnable(robot.sonar_motor)
motorParams = self.interface.MotorAngleControllerParameters()
motorParams.maxRotationAcceleration = 7.0
motorParams.maxRotationSpeed = 12.0
motorParams.feedForwardGain = 255 / 20.0
motorParams.minPWM = 25
motorParams.pidParameters.minOutput = -255
motorParams.pidParameters.maxOutput = 255
# proportional gain, reduces error
motorParams.pidParameters.k_p = 270.0
# integral gain, removes steady_state error
motorParams.pidParameters.k_i = 400
# differential gain, reduce settling time
motorParams.pidParameters.k_d = 160
self.setMotorAngleControllerParameters(robot.wheel_motors[0], motorParams)
self.setMotorAngleControllerParameters(robot.wheel_motors[1], motorParams)
motorParams = self.interface.MotorAngleControllerParameters()
motorParams.maxRotationAcceleration = 2.0
motorParams.maxRotationSpeed = 3
motorParams.feedForwardGain = 255 / 20.0
motorParams.minPWM = 27
motorParams.pidParameters.minOutput = -255
motorParams.pidParameters.maxOutput = 255
# proportional gain, reduces error
motorParams.pidParameters.k_p = 270.0
# integral gain, removes steady_state error
motorParams.pidParameters.k_i = 200
# differential gain, reduce settling time
motorParams.pidParameters.k_d = 160
self.setMotorAngleControllerParameters(robot.sonar_motor, motorParams)
# initialise localisation
#self.loc = localisation(x,y,theta,draw, record)
# sonar rotation offset, 0 is facing forward, positive is left/anticlockwise
self.sonar_rotation_offset = 0 #initialise to 0
#############################################################################
######## PUBLIC BRICKPI INTERFACE METHODS ############################
#############################################################################
# this starts a separate thread that continuously polls the activated sensors
# as well as controls the motors that were started
def initialize(self):
self.interface.initialize()
# softly stop all motors and sensors, stop the polling and control thread
# def terminate(self):
# self.interface.terminate()
# start individual motors
def motorEnable(self, port):
self.interface.motorEnable(port)
# activate individual sensors
def sensorEnable(self, port, sensor_type):
self.interface.sensorEnable(port, sensor_type)
# thread safe access to sensor values
def getSensorValue(self, port):
return self.interface.getSensorValue(port)
# low-level motor interface -- overrides controller
# useful for instant stop of motor
def setMotorPwm(self, port, pwm):
self.interface.setMotorPwm(port, pwm)
# set the controller parameters to non-default values
def setMotorAngleControllerParameters(self, motor_port, motor_params):
self.interface.setMotorAngleControllerParameters(motor_port, motor_params)
# set controller speed references -- overrides low-level setMotorPwm.
# this version guarantees synchronous operation
def setMotorRotationSpeedReferences(self, motors, speeds):
self.interface.setMotorRotationSpeedReferences(motors, speeds)
# figure out, if the set speed reference has been reached
def motorRotationSpeedReferenceReached(self, motor):
return self.interface.motorRotationSpeedReferenceReached(motor)
# increase a controller angle reference
def increaseMotorAngleReference(self, motor, angle):
self.interface.increaseMotorAngleReference(motor, angle)
# increase controller speed references
# this version guarantees synchronous
def increaseMotorAngleReferences(self, motors, angles):
self.interface.increaseMotorAngleReferences(motors, angles)
# query the current (actual) motor speeds
def getMotorAngles(self, motors):
return self.interface.getMotorAngles(motors)
# query the current (actual) motor speed
def getMotorAngle(self, motor):
return self.interface.getMotorAngle(motor)
# figure out, if the set angle reference has been reached
def motorAngleReferencesReached(self, motors):
return self.interface.motorAngleReferencesReached(motors)
#
# # figure out, if the set angle reference has been reached
def motorAngleReferenceReached(self, motor):
return self.interface.motorAngleReferenceReached(motor)
def startLogging(self, file_name):
self.interface.startLogging(file_name)
def stopLogging(self):
self.interface.stopLogging()
def sensorEnableUltrasonic(self, port):
self.sensorEnable(port, brickpi.SensorType.SENSOR_ULTRASONIC)
def sensorEnableTouch(self, port):
self.sensorEnable(port, brickpi.SensorType.SENSOR_TOUCH)
#############################################################################
######## PUBLIC MOVEMENT METHODS #####################################
#############################################################################
# distance in cm
def forward(self, distance, verbose=False):
self.linearMove(distance)
#self.loc.loc_distance(distance)
if verbose or robot.all_verbose: print "Completed forward " + str(distance)
#self.loc.loc_distance(-distance)
if verbose or robot.all_verbose: print "Completed backward " + str(distance)
def turnRightRad(self, radius, verbose=False):
length = radius * robot.wheel_separation / 2
angle = length / robot.wheel_radius
self.turn([angle, -angle])
#self.loc.loc_rotation(math.degrees(-radius))
if verbose or robot.all_verbose: print "Completed right turn " + str(radius)
def turnLeftRad(self, radius, verbose=False):
length = radius * robot.wheel_separation / 2
angle = length / robot.wheel_radius
self.turn([-angle, angle])
#self.loc.loc_rotation(math.degrees(radius))
if verbose or robot.all_verbose: print "Completed left turn " + str(radius)
def turnRightDeg(self, degrees):
self.turnRightRad(math.radians(degrees))
def turnLeftDeg(self, degrees):
self.turnLeftRad(math.radians(degrees))
def turnDeg(self, degrees):
if degrees < 180:
self.turnLeftDeg(degrees)
else:
self.turnRightDeg(360 - degrees)
def instantStop(self, verbose=False):
self.setMotorPwm(0, 0)
self.setMotorPwm(1, 0)
if verbose or robot.all_verbose: print "Instant stop!!!"
def navigateToWaypoint(self, x, y):
currentX, currentY, theta = self.loc.get_average()
dx = x - currentX
dy = y - currentY
alpha = math.degrees(math.atan2(dy, dx))
beta = robot.normalise_angle(alpha - theta)
self.turnDeg(beta)
self.getSonarAndUpdate()
currentX, currentY, theta = self.loc.get_average()
dx = x - currentX
dy = y - currentY
distance = math.hypot(dx, dy)
if distance > 20:
self.forward(20)
self.getSonarAndUpdate()
self.navigateToWaypoint(x,y)
else:
self.forward(distance)
self.getSonarAndUpdate()
print "-------------->IM AT THE WAYPOINT : " + str(x) + ", " + str(y)
def rotateAndUpdate(self):
currentX, currentY, theta = self.loc.get_average()
# Turn to have theta 0
print "^~~~~~~^ I WANT TO ROTATEeeeee :3"
self.turnDeg(90 - (theta - 0 ))
self.getSonarAndUpdate()
# Now the theta should be 90
currentX, currentY, theta = self.loc.get_average()
self.turnDeg(90 - (theta - 90))
self.getSonarAndUpdate()
# Now the theta should be 180
currentX, currentY, theta = self.loc.get_average()
self.turnDeg(90 - (theta - 180))
self.getSonarAndUpdate()
# Now the theta should be 270
currentX, currentY, theta = self.loc.get_average()
self.turnDeg(90 - (theta - 270))
self.getSonarAndUpdate()
def getSonarAndUpdate(self):
# Get sonar measurements
sonarMeasurements = self.getSonarMeasurements(200)
# Update particles
if len(sonarMeasurements) > 0:
self.loc.update(sonarMeasurements)
self.loc.drawAllParticles()
#############################################################################
######## PUBLIC SENSOR METHODS #######################################
#############################################################################
def enableSonar(self, verbose=False):
self.sensorEnableUltrasonic(robot.sonar)
if verbose or robot.all_verbose: print "Sonar Enabled"
def getSonarMeasurements(self, n):
readings = []
for i in range(n):
reading = self.getSonarSingle()
readings.append(reading + robot.sonar_offset)
return readings
def getSonarSingle(self):
m = self.getSensorValue(robot.sonar)
while len(m) != 2:
m = self.getSensorValue(robot.sonar)
return m[0]
##############SONAR TURNING#################
#sonar spin left/anti-clockwise
def sonarSpin(self, degrees):
self.increaseMotorAngleReference(robot.sonar_motor, math.radians(-degrees))
while not self.motorAngleReferenceReached(robot.sonar_motor):
time.sleep(0.1)
self.sonar_rotation_offset += degrees
print "SONAR rotation offset : " + str(self.sonar_rotation_offset)
#sonar return back to origin location
def sonarReset(self):
self.sonarSpin(-self.sonar_rotation_offset)
def turnSonarTakingMeasurements(self):
# sonar = self.getSonarMeasurements(1)[0]
measurements = []
initialMotorAngle = self.getMotorAngle(robot.sonar_motor)[0]
step = 2*math.pi / 360.0
next_step = initialMotorAngle
# left turning
step_measurements = []
self.increaseMotorAngleReference(robot.sonar_motor, -math.radians(360))
while not self.motorAngleReferenceReached(robot.sonar_motor):
motorAngle = self.getMotorAngle(robot.sonar_motor)[0]
step_measurements.append(self.getSonarMeasurements(1)[0])
if motorAngle < next_step and len(measurements)<360:
measurements.append(int(np.median(step_measurements)))
step_measurements = []
next_step -= step
self.sonar_rotation_offset += 360
return measurements
@staticmethod
def getMeanAngle(sonarMeasurements):
top = 100
low = 55
ranges = []
top_i = 0
low_i = 0
j = 0
started = False
for m in sonarMeasurements:
if m in range(low,top):
if started:
top_i += 1
else:
started = True
low_i = j
top_i = j+1
else:
if started:
ranges.append((low_i,top_i))
started = False
j += 1
if started:
ranges.append((low_i,top_i))
# assert(len(ranges)<=2)
# if (len(ranges) == 2) and (not ranges[1][1] == 360):
# ranges = [(ranges[0][0], ranges[1][1])]
print ranges
mid_angle = 0
#if len(ranges)==1:
if not ranges[len(ranges)-1][1] == len[sonarMeasurements]: # if the last one is not at the end
#mid_angle = (ranges[0][0] + ranges[0][1] ) / 2.0
mid_angle = (ranges[0][0] + ranges[len(ranges)-1][1] ) / 2.0 # compute the interval of all
pass
else:
#wrap_diff = ranges[1][0] - len(sonarMeasurements)
# need to find the biggers interval
end = 0 # end of the from part in measurements
start = 0 # start of the tail part in measurements
current_diff = 0
assert(len(ranges)-2 >= 0)
for int i in range(0,len(ranges)-2):
end = ranges[i][1]
start = ranges[i+1][0]
if (start - end) > current_diff:
current_diff = start - end
wrap_diff = start - len(sonarMeasurements)
print wrap_diff
mid_angle = (ranges[0][1] + wrap_diff) / 2.0
print mid_angle
return mid_angle
def turnSonarTillDistance(self, distance):
length = 2*math.pi * robot.wheel_separation / 2
angle = length / robot.wheel_radius
sonar = self.getSonarMeasurements(1)[0]
sonars = [sonar]
self.increaseMotorAngleReferences(robot.wheel_motors, [angle,-angle])
while not (sonar in range(distance-1,distance+1)):
sonar = self.getSonarMeasurements(1)[0]
sonars.append(sonar)
self.instantStop(0)
self.instantStop(1)
print sonars
return -1
#############################################################################
######## LOCALISATION METHODS ########################################
#############################################################################
def get_loc(self):
return self.loc
def followWallLeft(self, distance, wallDistance):
vc = 14 # TODO
Kp = robot.kp # TODO
maxV = 19
minV = 9
initial0,initial1 = self.getMotorAngles(self.wheel_motors)
angle_toreach0 = (distance / robot.wheel_radius) + initial0[0]
angle_toreach1 = (distance / robot.wheel_radius) + initial1[0]
while self.getMotorAngle(0)[0]<angle_toreach0 and self.getMotorAngle(1)[0]<angle_toreach1:
sonar = self.getSonarMeasurements(1)[0] - self.sonar_offset
diff = sonar - wallDistance
if 45 > sonar > 10:
#print "diff = " + str(diff)
#print "sonar = " + str(sonar)
vl = min(vc - 0.5*Kp*diff, maxV)
vr = min(vc + 0.5*Kp*diff, maxV)
vl = max(vl, minV)
vr = max(vr, minV)
self.setMotorRotationSpeedReferences([0,1], [vl,vr])
#print "new speed left = " + str(vl)
#print "new speed right = " + str(vr)
time.sleep(0.1)
self.instantStop()
def followWallBackwards(self, distance, wallDistance):
vc = 14 # TODO
Kp = robot.kp # TODO
maxV = 17.5
minV = 9
initial0,initial1 = self.getMotorAngles(self.wheel_motors)
angle_toreach0 = initial0[0] - (distance / robot.wheel_radius)
angle_toreach1 = initial1[0] - (distance / robot.wheel_radius)
while self.getMotorAngle(0)[0]>angle_toreach0 and self.getMotorAngle(1)[0]>angle_toreach1:
sonar = self.getSonarMeasurements(1)[0] - self.sonar_offset
diff = sonar - wallDistance
if 45 > sonar > 10:
print "diff = " + str(diff)
print "sonar = " + str(sonar)
vl = min(vc - 0.5*Kp*diff, maxV)
vr = min(vc + 0.5*Kp*diff, maxV)
vl = max(vl, minV)
vr = max(vr, minV)
self.setMotorRotationSpeedReferences([0,1], [-vl,-vr])
print "new speed left = " + str(vl)
print "new speed right = " + str(vr)
time.sleep(0.1)
self.instantStop()
#############################################################################
######## PRIVATE METHODS #############################################
#############################################################################
@staticmethod
def normalise_angle(angle):
if angle < 0:
return robot.normalise_angle(angle + 360)
if angle > 360 :
return robot.normalise_angle(angle - 360)
else :
return angle
def turn(self, angles):
self.increaseMotorAngleReferences(robot.wheel_motors, angles)
while not self.motorAngleReferencesReached(robot.wheel_motors):
time.sleep(0.1)
# direction is true if forward, false if backward
def linearMove(self, distance):
angle = distance / robot.wheel_radius
self.increaseMotorAngleReferences(robot.wheel_motors, [angle, angle])
while not self.motorAngleReferencesReached(robot.wheel_motors):
time.sleep(0.1)