-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwith_PID.py
361 lines (272 loc) · 9.86 KB
/
with_PID.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
from controller import Robot
import numpy as np
robot = Robot() # Robot object
timestep = int(robot.getBasicTimeStep())
delta_t = robot.getBasicTimeStep()/1000.0 # [s]
# Robot pose
# Adjust the initial values to match the initial robot pose in your simulation
x = -0.06 # position in x [m]
y = 0.436 # position in y [m]
phi = 0.0531 # orientation [rad]
# Robot velocity and acceleration
dx = 0.0 # speed in x [m/s]
dy = 0.0 # speed in y [m/s]
ddx = 0.0 # acceleration in x [m/s^2]
ddy = 0.0 # acceleration in y [m/s^2]
# Robot wheel speeds
wl = 0.0 # angular speed of the left wheel [rad/s]
wr = 0.0 # angular speed of the right wheel [rad/s]
# Robot linear and angular speeds
u = 0.0 # linear speed [m/s]
w = 0.0 # angular speed [rad/s]
# e-puck Physical parameters for the kinematics model (constants)
R = 0.0205 # radius of the wheels: 20.5mm [m]
D = 0.0565 # distance between the wheels: 52mm [m]
# distance from the center of the wheels to the point of interest [m]
A = 0.05
# defining constants
MAX_VELOCITY = 6.28
PI = np.pi
# defining Distnace sensors
FDS = robot.getDevice("FrontDistance") # Front distance sensor
FRDS = robot.getDevice("FrontRightDistance") # Front Right distance sensor
FLDS = robot.getDevice("FrontLeftDistance") # Front Left distance sensor
RDS = robot.getDevice("RightDistance") # Right distance sensor
LDS = robot.getDevice("LeftDistance") # Left distnace sensor
distanceSensors = [FDS, FRDS, FLDS, RDS, LDS]
# defining Location sensor
location = robot.getDevice("GpsSensor") # Location for GPS sensor'
# defining Color sensor
colorSensor = robot.getDevice("Color") # Color Sensor
# defining Camera sensors
FCamera = robot.getDevice("FrontCamera")
RCamera = robot.getDevice("RightCamera")
LCamera = robot.getDevice("LeftCamera")
# defining Motor wheels
leftWheel = robot.getDevice("LeftWheel motor")
rightWheel = robot.getDevice("RightWheel motor")
wheels = [leftWheel, rightWheel] # Grouping the wheels for loops
# defining motor Encoders
rightEncoder = robot.getDevice("RightWheel sensor")
leftEncoder = robot.getDevice("LeftWheel sensor")
encoderSensors = [leftEncoder, rightEncoder]
oldEncoderValues = list()
# defining robot emitter
emitter = robot.getDevice("emitter")
# defining robot Gyro
gyroScope = robot.getDevice("GyroSensor")
# enabling Distance sensors
FDS.enable(timestep)
FRDS.enable(timestep)
FLDS.enable(timestep)
RDS.enable(timestep)
LDS.enable(timestep)
# enabling GPS sensor for location
location.enable(timestep)
# enabling Color Sensor
colorSensor.enable(timestep)
# enabling Camera sensors
FCamera.enable(timestep)
RCamera.enable(timestep)
LCamera.enable(timestep)
# enabling Gyro Sensor
gyroScope.enable(timestep)
# enabling encoder sensors
rightEncoder.enable(timestep)
leftEncoder.enable(timestep)
# Setting the position and velocity for the wheels
leftWheel.setPosition(float("inf"))
rightWheel.setPosition(float("inf"))
leftWheel.setVelocity(0.0)
rightWheel.setVelocity(0.0)
# starting time of the loop for getting the duration if needed
startPlayingTime = robot.getTime()
#####################################
##### PID Class STARTS HERE #####
#####################################
class PID:
def __init__(self, Kp, Ki, Kd, setpoint):
self.Kp = Kp
self.Ki = Ki
self.Kd = Kd
self.setpoint = setpoint
self.integral = 0
self.previous_error = 0
def update(self, current_value, delta_time):
error = self.setpoint - current_value
self.integral += error * delta_time
derivative = (error - self.previous_error) / delta_time
output = self.Kp * error + self.Ki * self.integral + self.Kd * derivative
self.previous_error = error
print(f"Error: {error}\toutput: {output}")
print(f"integral{self.Ki * self.integral}")
return output
#################################################
##### MAIN PROGRAM FUNCTIONS STARTS HERE #####
#################################################
def get_wheels_speed(encoderValues, oldEncoderValues, delta_t):
# Encoder values indicate the angular position of the wheel in radians
wl = (encoderValues[0] - oldEncoderValues[0])/delta_t
wr = (encoderValues[1] - oldEncoderValues[1])/delta_t
return wl, wr
def get_robot_speeds(wl, wr, r, d):
u = r/2.0 * (wr + wl)
w = r/d * (wr - wl)
return u, w
def linear_angular_2_wl_wr(u, w):
wl = (2*u + w*D)/(2*R)
wr = (2*u - w*D)/(2*R)
return wl, wr
def get_robot_pose(u, w, x_old, y_old, phi_old, delta_t):
delta_phi = w * delta_t
phi = phi_old + delta_phi
phi_avg = (phi_old + phi)/2
if phi >= np.pi:
phi = phi - 2*np.pi
elif phi < -np.pi:
phi = phi + 2*np.pi
delta_x = u * np.cos(phi_avg) * delta_t
delta_y = u * np.sin(phi_avg) * delta_t
x = x_old + delta_x
y = y_old + delta_y
return x, y, phi
def getColor():
img = colorSensor.getImage() # Grab color sensor camera's image view
# Return grayness of the only pixel (0-255)
return colorSensor.imageGetGray(img, colorSensor.getWidth(), 0, 0)
def delay(ms):
initTime = robot.getTime() # Store starting time (in seconds)
while robot.step(timestep) != -1:
# If time elapsed (converted into ms) is greater than value passed in
if (robot.getTime() - initTime) * 1000.0 > ms:
break
else:
avoidTiles()
def turning_right():
turn_N_degree(90)
def turning_left():
turn_N_degree(-90)
def turning_around():
turn_N_degree(180)
def turning_smooth_right():
turn_N_degree(30)
def turning_smooth_left():
turn_N_degree(-30)
def moveForward(color):
if color < 80:
leftWheel.setVelocity(0.6 * MAX_VELOCITY)
rightWheel.setVelocity(0.6 * MAX_VELOCITY)
delay(180)
leftWheel.setVelocity(0 * MAX_VELOCITY)
rightWheel.setVelocity(0 * MAX_VELOCITY)
delay(200)
elif 170 < color < 180:
print("yellow")
leftWheel.setVelocity(0.6 * MAX_VELOCITY)
rightWheel.setVelocity(0.6 * MAX_VELOCITY)
delay(1600)
leftWheel.setVelocity(0 * MAX_VELOCITY)
rightWheel.setVelocity(0 * MAX_VELOCITY)
delay(200)
else:
leftWheel.setVelocity(0.6 * MAX_VELOCITY)
rightWheel.setVelocity(0.6 * MAX_VELOCITY)
delay(533)
leftWheel.setVelocity(0 * MAX_VELOCITY)
rightWheel.setVelocity(0 * MAX_VELOCITY)
delay(200)
def avoidTiles():
tileColor = getColor()
if tileColor < 80:
move_backwards()
def move_backwards():
# set left wheel speed
rightWheel.setVelocity(-0.8 * MAX_VELOCITY)
# set right wheel speed
leftWheel.setVelocity(-0.3 * MAX_VELOCITY)
delay(1500)
def get_robot_speeds(wl, wr, r, d):
u = r/2.0 * (wr + wl)
w = r/d * (wr - wl)
return u, w
def updating_robot():
global wl, wr, u, w, x, y, phi, delta_t, R, D, distanceSensors, encoderSensors, oldEncoderValues
# Update sensor readings
dsValues = list()
for sensor in distanceSensors:
dsValues.append(sensor.getValue())
encoderValues = list()
for encoder in encoderSensors:
encoderValues.append(encoder.getValue()) # [rad]
# Update old encoder values if not done before
if len(oldEncoderValues) < 2:
for encoder in encoderSensors:
oldEncoderValues.append(encoder.getValue())
[wl, wr] = get_wheels_speed(encoderValues, oldEncoderValues, delta_t)
# Compute robot linear and angular speeds
[u, w] = get_robot_speeds(wl, wr, R, D)
# Compute new robot pose
[x, y, phi] = get_robot_pose(u, w, x, y, phi, delta_t)
# phi = phi % (2*PI)
def turn_N_degree(degree):
global phi, delta_t, wl, wr
radian = ((degree * PI) / 180) + phi
if radian >= np.pi:
radian = radian - 2*np.pi
elif radian < -np.pi:
radian = radian + 2*np.pi
KP = 0.1
KI = 0.001
KD = 0.0
pid = PID(KP, KI, KD, radian)
# last_angle = phi + 1
while robot.step(timestep) != -1:
updating_robot()
# Update the PID controller and get the output
current_angle = phi # get the current angle from the robot pose
pid_output = pid.update(current_angle, delta_t)
# if(abs(current_angle - last_angle) < 0.001):
# leftWheel.setVelocity(0.0)
# rightWheel.setVelocity(0.0)
# print("breaking")
# break
# last_angle = phi
# Apply the PID output to the robot
wl = pid_output
wr = -1 * pid_output
leftWheel.setVelocity(wl)
rightWheel.setVelocity(wr)
NEAR = 0.068
############################################
##### MAIN PROGRAM LOOP STARTS HERE #####
############################################
while robot.step(timestep) != -1:
# Updating the values from sensors
updating_robot()
# Avoid tiles
avoidTiles()
# Check sensor values and perform appropriate action
if (FDS.getValue() < NEAR and LDS.getValue() < NEAR and RDS.getValue() < NEAR):
print("turning around")
turning_around(getColor())
elif (FDS.getValue() < NEAR and RDS.getValue() > NEAR):
print("turning right")
turning_right()
elif ((FDS.getValue() < NEAR and RDS.getValue() < NEAR and FRDS.getValue() > NEAR)):
print("turning smooth right")
turning_smooth_right()
elif ((FDS.getValue() < NEAR and RDS.getValue() < NEAR and LDS.getValue() > NEAR)):
print("turning left")
turning_left()
elif ((FDS.getValue() < NEAR and LDS.getValue() < NEAR and FLDS.getValue() > NEAR)):
print("turning smooth left")
turning_smooth_left()
else:
print("going forward")
moveForward(getColor())
# Print sensor values for debugging
print(f"LDS sensor: {LDS.getValue()}")
print(f"RDS sensor: {RDS.getValue()}")
print(f"FLDS sensor: {FLDS.getValue()}")
print(f"FRDS sensor: {FRDS.getValue()}")
print(f"FDS sensor: {FDS.getValue()}")