@@ -145,24 +145,26 @@ def straight(self, distance: float, max_effort: float = 0.5, timeout: float = No
145145 distance *= - 1
146146
147147 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 ()
150150
151151
152152 if main_controller is None :
153153 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 ,
160162 )
161163
162164 # Secondary controller to keep encoder values in sync
163165 if secondary_controller is None :
164166 secondary_controller = PID (
165- kp = 0.025 , kd = 0.0025 ,
167+ kp = 0.075 , kd = 0.005 ,
166168 )
167169
168170 if self .imu is not None :
@@ -174,13 +176,13 @@ def straight(self, distance: float, max_effort: float = 0.5, timeout: float = No
174176 while True :
175177
176178 # 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
180182
181183 # 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 )
184186
185187 if main_controller .is_done () or time_out .is_done ():
186188 break
@@ -190,7 +192,7 @@ def straight(self, distance: float, max_effort: float = 0.5, timeout: float = No
190192 # record current heading to maintain it
191193 current_heading = self .imu .get_yaw ()
192194 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 )
194196
195197 headingCorrection = secondary_controller .update (initial_heading - current_heading )
196198
@@ -206,12 +208,12 @@ def straight(self, distance: float, max_effort: float = 0.5, timeout: float = No
206208 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 :
207209 """
208210 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)
210212 Uses the IMU to determine the heading of the robot and P control for the motor controller.
211213
212214 :param turnDegrees: The number of angle for the robot to turn (In Degrees)
213215 :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)
215217 :type max_effort: float
216218 :param timeout: The amount of time before the robot stops trying to turn and continues to the next step (In Seconds)
217219 :type timeout: float
@@ -226,27 +228,29 @@ def turn(self, turn_degrees: float, max_effort: float = 0.5, timeout: float = No
226228 """
227229
228230 if max_effort < 0 :
229- max_effort * = - 1
230- turn_degrees * = - 1
231+ max_effort = - max_effort
232+ turn_degrees = - turn_degrees
231233
232234 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 ()
235237
236238 if main_controller is None :
237239 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
244248 )
245249
246250 # Secondary controller to keep encoder values in sync
247251 if secondary_controller is None :
248252 secondary_controller = PID (
249- kp = 0.002 ,
253+ kp = 1.0 ,
250254 )
251255
252256 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
255259 while True :
256260
257261 # 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 )
261265
262266 if use_imu and (self .imu is not None ):
263267 # 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 ()
265269 else :
266270 # 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 )
268272
269273 # 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 )
271275
272276 # exit if timeout or tolerance reached
273277 if main_controller .is_done () or time_out .is_done ():
274278 break
275279
276- self .set_effort (- turnSpeed - encoderCorrection , turnSpeed - encoderCorrection )
280+ self .set_effort (- turn_speed - encoder_correction , turn_speed - encoder_correction )
277281
278282 time .sleep (0.01 )
279283
0 commit comments