@@ -145,24 +145,26 @@ def straight(self, distance: float, max_effort: float = 0.5, timeout: float = No
145
145
distance *= - 1
146
146
147
147
time_out = Timeout (timeout )
148
- startingLeft = self .get_left_encoder_position ()
149
- startingRight = self .get_right_encoder_position ()
148
+ starting_left = self .get_left_encoder_position ()
149
+ starting_right = self .get_right_encoder_position ()
150
150
151
151
152
152
if main_controller is None :
153
153
main_controller = PID (
154
- kp = 0.075 ,
155
- kd = 0.005 ,
156
- minOutput = 0.25 ,
157
- maxOutput = max_effort ,
158
- tolerance = 0.1 ,
159
- toleranceCount = 3 ,
154
+ kp = 0.1 ,
155
+ ki = 0.04 ,
156
+ kd = 0.06 ,
157
+ min_output = 0.25 ,
158
+ max_output = 0.5 ,
159
+ max_integral = 10 ,
160
+ tolerance = 0.25 ,
161
+ tolerance_count = 3 ,
160
162
)
161
163
162
164
# Secondary controller to keep encoder values in sync
163
165
if secondary_controller is None :
164
166
secondary_controller = PID (
165
- kp = 0.025 , kd = 0.0025 ,
167
+ kp = 0.075 , kd = 0.005 ,
166
168
)
167
169
168
170
if self .imu is not None :
@@ -174,13 +176,13 @@ def straight(self, distance: float, max_effort: float = 0.5, timeout: float = No
174
176
while True :
175
177
176
178
# calculate the distance traveled
177
- leftDelta = self .get_left_encoder_position () - startingLeft
178
- rightDelta = self .get_right_encoder_position () - startingRight
179
- distTraveled = (leftDelta + rightDelta ) / 2
179
+ left_delta = self .get_left_encoder_position () - starting_left
180
+ right_delta = self .get_right_encoder_position () - starting_right
181
+ dist_traveled = (left_delta + right_delta ) / 2
180
182
181
183
# PID for distance
182
- distanceError = distance - distTraveled
183
- effort = main_controller .update (distanceError )
184
+ distance_error = distance - dist_traveled
185
+ effort = main_controller .update (distance_error )
184
186
185
187
if main_controller .is_done () or time_out .is_done ():
186
188
break
@@ -190,7 +192,7 @@ def straight(self, distance: float, max_effort: float = 0.5, timeout: float = No
190
192
# record current heading to maintain it
191
193
current_heading = self .imu .get_yaw ()
192
194
else :
193
- current_heading = ((rightDelta - leftDelta )/ 2 )* 360 / (self .track_width * math .pi )
195
+ current_heading = ((right_delta - left_delta )/ 2 )* 360 / (self .track_width * math .pi )
194
196
195
197
headingCorrection = secondary_controller .update (initial_heading - current_heading )
196
198
@@ -206,12 +208,12 @@ def straight(self, distance: float, max_effort: float = 0.5, timeout: float = No
206
208
def turn (self , turn_degrees : float , max_effort : float = 0.5 , timeout : float = None , main_controller : Controller = None , secondary_controller : Controller = None , use_imu :bool = True ) -> bool :
207
209
"""
208
210
Turn the robot some relative heading given in turnDegrees, and exit function when the robot has reached that heading.
209
- Speed is bounded from -1 (turn counterclockwise the relative heading at full speed) to 1 (turn clockwise the relative heading at full speed)
211
+ effort is bounded from -1 (turn counterclockwise the relative heading at full speed) to 1 (turn clockwise the relative heading at full speed)
210
212
Uses the IMU to determine the heading of the robot and P control for the motor controller.
211
213
212
214
:param turnDegrees: The number of angle for the robot to turn (In Degrees)
213
215
:type turnDegrees: float
214
- :param max_effort: The max effort for which the robot to travel (Bounded from -1 to 1). Default is half effort forward.
216
+ :param max_effort: The max speed for which the robot to travel (Bounded from -1 to 1)
215
217
:type max_effort: float
216
218
:param timeout: The amount of time before the robot stops trying to turn and continues to the next step (In Seconds)
217
219
:type timeout: float
@@ -226,27 +228,29 @@ def turn(self, turn_degrees: float, max_effort: float = 0.5, timeout: float = No
226
228
"""
227
229
228
230
if max_effort < 0 :
229
- max_effort * = - 1
230
- turn_degrees * = - 1
231
+ max_effort = - max_effort
232
+ turn_degrees = - turn_degrees
231
233
232
234
time_out = Timeout (timeout )
233
- startingLeft = self .get_left_encoder_position ()
234
- startingRight = self .get_right_encoder_position ()
235
+ starting_left = self .get_left_encoder_position ()
236
+ starting_right = self .get_right_encoder_position ()
235
237
236
238
if main_controller is None :
237
239
main_controller = PID (
238
- kp = .015 ,
239
- kd = 0.0012 ,
240
- minOutput = 0.25 ,
241
- maxOutput = max_effort ,
242
- tolerance = 0.5 ,
243
- toleranceCount = 3
240
+ kp = 0.02 ,
241
+ ki = 0.001 ,
242
+ kd = 0.00165 ,
243
+ min_output = 0.35 ,
244
+ max_output = 0.5 ,
245
+ max_integral = 75 ,
246
+ tolerance = 1 ,
247
+ tolerance_count = 3
244
248
)
245
249
246
250
# Secondary controller to keep encoder values in sync
247
251
if secondary_controller is None :
248
252
secondary_controller = PID (
249
- kp = 0.002 ,
253
+ kp = 1.0 ,
250
254
)
251
255
252
256
if use_imu and (self .imu is not None ):
@@ -255,25 +259,25 @@ def turn(self, turn_degrees: float, max_effort: float = 0.5, timeout: float = No
255
259
while True :
256
260
257
261
# calculate encoder correction to minimize drift
258
- leftDelta = self .get_left_encoder_position () - startingLeft
259
- rightDelta = self .get_right_encoder_position () - startingRight
260
- encoderCorrection = secondary_controller .update (leftDelta + rightDelta )
262
+ left_delta = self .get_left_encoder_position () - starting_left
263
+ right_delta = self .get_right_encoder_position () - starting_right
264
+ encoder_correction = secondary_controller .update (left_delta + right_delta )
261
265
262
266
if use_imu and (self .imu is not None ):
263
267
# calculate turn error (in degrees) from the imu
264
- turnError = turn_degrees - self .imu .get_yaw ()
268
+ turn_error = turn_degrees - self .imu .get_yaw ()
265
269
else :
266
270
# calculate turn error (in degrees) from the encoder counts
267
- turnError = turn_degrees - ((rightDelta - leftDelta )/ 2 )* 360 / (self .track_width * math .pi )
271
+ turn_error = turn_degrees - ((right_delta - left_delta )/ 2 )* 360 / (self .track_width * math .pi )
268
272
269
273
# Pass the turn error to the main controller to get a turn speed
270
- turnSpeed = main_controller .update (turnError )
274
+ turn_speed = main_controller .update (turn_error )
271
275
272
276
# exit if timeout or tolerance reached
273
277
if main_controller .is_done () or time_out .is_done ():
274
278
break
275
279
276
- self .set_effort (- turnSpeed - encoderCorrection , turnSpeed - encoderCorrection )
280
+ self .set_effort (- turn_speed - encoder_correction , turn_speed - encoder_correction )
277
281
278
282
time .sleep (0.01 )
279
283
0 commit comments