Skip to content

Commit f09510f

Browse files
committed
Fix speed control indeterminant timestep issue
1 parent 2fdf624 commit f09510f

File tree

5 files changed

+11
-12
lines changed

5 files changed

+11
-12
lines changed

.picowgo

Lines changed: 0 additions & 3 deletions
This file was deleted.

Examples/drive_examples.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def circle():
3838
# Follow the perimeter of a square with variable sidelength
3939
def square(sidelength):
4040
for sides in range(4):
41-
drivetrain.straight(sidelength, 80)
41+
drivetrain.straight(sidelength, 0.8)
4242
drivetrain.turn(90)
4343
# Alternatively:
4444
# polygon(sidelength, 4)

XRPLib/differential_drive.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,16 +203,16 @@ def straight(self, distance: float, max_effort: float = 0.5, timeout: float = No
203203
return not time_out.is_done()
204204

205205

206-
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:
206+
def turn(self, turn_degrees: float, max_speed: float = 40, timeout: float = None, main_controller: Controller = None, secondary_controller: Controller = None, use_imu:bool = True) -> bool:
207207
"""
208208
Turn the robot some relative heading given in turnDegrees, and exit function when the robot has reached that heading.
209209
Speed is bounded from -1 (turn counterclockwise the relative heading at full speed) to 1 (turn clockwise the relative heading at full speed)
210210
Uses the IMU to determine the heading of the robot and P control for the motor controller.
211211
212212
:param turnDegrees: The number of angle for the robot to turn (In Degrees)
213213
: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.
215-
:type max_effort: float
214+
:param max_speed: The max speed for each wheel of the robot to spin, in cm/s. Default is 40 cm/s.
215+
:type max_speed: float
216216
:param timeout: The amount of time before the robot stops trying to turn and continues to the next step (In Seconds)
217217
:type timeout: float
218218
:param main_controller: The main controller, for handling the angle turned
@@ -225,8 +225,8 @@ def turn(self, turn_degrees: float, max_effort: float = 0.5, timeout: float = No
225225
:rtype: bool
226226
"""
227227

228-
if max_effort < 0:
229-
max_effort *= -1
228+
if max_speed < 0:
229+
max_speed *= -1
230230
turn_degrees *= -1
231231

232232
time_out = Timeout(timeout)
@@ -238,7 +238,7 @@ def turn(self, turn_degrees: float, max_effort: float = 0.5, timeout: float = No
238238
kp = .015,
239239
kd = 0.0012,
240240
minOutput = 0.25,
241-
maxOutput = max_effort,
241+
maxOutput = max_speed,
242242
tolerance = 0.5,
243243
toleranceCount = 3
244244
)
@@ -273,7 +273,7 @@ def turn(self, turn_degrees: float, max_effort: float = 0.5, timeout: float = No
273273
if main_controller.is_done() or time_out.is_done():
274274
break
275275

276-
self.set_effort(-turnSpeed - encoderCorrection, turnSpeed - encoderCorrection)
276+
self.set_speed(-turnSpeed - encoderCorrection, turnSpeed - encoderCorrection)
277277

278278
time.sleep(0.01)
279279

XRPLib/encoded_motor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ def set_speed(self, speed_rpm: float = None):
131131
# Convert from rev per min to counts per 20ms (60 sec/min, 50 Hz)
132132
self.target_speed = speed_rpm*self._encoder.resolution/(60*50)
133133
self.speedController.clear_history()
134+
self.prev_position = self.get_position_counts()
134135

135136
def set_speed_controller(self, new_controller: Controller):
136137
"""

XRPLib/pid.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,5 @@ def clear_history(self):
117117
self.prevError = 0
118118
self.prevIntegral = 0
119119
self.prevOutput = 0
120-
self.times = 0
120+
self.times = 0
121+
self.prevTime = None

0 commit comments

Comments
 (0)