Skip to content

Commit 0c159d4

Browse files
committed
Fixed pending variable and method name changes; added set_speed to differential drive
1 parent 39bfd1f commit 0c159d4

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

XRPLib/differential_drive.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def get_right_encoder_position(self) -> float:
7575
return self.right_motor.get_position()
7676

7777

78-
def straight(self, distance: float, speed: float = 0.5, timeout: float = None, main_controller: Controller = None, secondary_controller: Controller = None) -> bool:
78+
def straight(self, distance: float, max_effort: float = 0.5, timeout: float = None, main_controller: Controller = None, secondary_controller: Controller = None) -> bool:
7979
"""
8080
Go forward the specified distance in centimeters, and exit function when distance has been reached.
8181
Speed is bounded from -1 (reverse at full speed) to 1 (forward at full speed)
@@ -95,7 +95,7 @@ def straight(self, distance: float, speed: float = 0.5, timeout: float = None, m
9595
"""
9696
# ensure distance is always positive while speed could be either positive or negative
9797
if distance < 0:
98-
speed *= -1
98+
max_effort *= -1
9999
distance *= -1
100100

101101
time_out = Timeout(timeout)
@@ -107,7 +107,7 @@ def straight(self, distance: float, speed: float = 0.5, timeout: float = None, m
107107
main_controller = PID(
108108
kp = 0.5,
109109
minOutput = 0.12,
110-
maxOutput = speed,
110+
maxOutput = max_effort,
111111
tolerance = 0.1,
112112
toleranceCount = 3,
113113
)
@@ -159,7 +159,7 @@ def straight(self, distance: float, speed: float = 0.5, timeout: float = None, m
159159
return not time_out.is_done()
160160

161161

162-
def turn(self, turn_degrees: float, speed: float = 0.5, timeout: float = None, main_controller: Controller = None, secondary_controller: Controller = None, use_imu:bool = True) -> bool:
162+
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:
163163
"""
164164
Turn the robot some relative heading given in turnDegrees, and exit function when the robot has reached that heading.
165165
Speed is bounded from -1 (turn counterclockwise the relative heading at full speed) to 1 (turn clockwise the relative heading at full speed)
@@ -180,8 +180,8 @@ def turn(self, turn_degrees: float, speed: float = 0.5, timeout: float = None, m
180180
: rtype: bool
181181
"""
182182

183-
if speed < 0:
184-
speed *= -1
183+
if max_effort < 0:
184+
max_effort *= -1
185185
turn_degrees *= -1
186186

187187
time_out = Timeout(timeout)
@@ -193,7 +193,7 @@ def turn(self, turn_degrees: float, speed: float = 0.5, timeout: float = None, m
193193
kp = .015,
194194
kd = 0.0012,
195195
minOutput = 0.25,
196-
maxOutput = speed,
196+
maxOutput = max_effort,
197197
tolerance = 0.5,
198198
toleranceCount = 3
199199
)

XRPLib/encoded_motor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,22 +116,22 @@ def get_speed(self) -> float:
116116
# Convert from ticks per 20ms to rpm (60 sec/min, 50 Hz)
117117
return self.speed*(60*50)/self._encoder.ticks_per_rev
118118

119-
def set_target_speed(self, target_speed_rpm: float | None = None):
119+
def set_speed(self, speed_rpm: float | None = None):
120120
"""
121121
Sets target speed (in rpm) to be maintained passively
122122
Call with no parameters to turn off speed control
123123
124124
: param target_speed_rpm: The target speed for the motor in rpm, or None
125125
: type target_speed_rpm: float, or None
126126
"""
127-
if target_speed_rpm is None:
127+
if speed_rpm is None:
128128
self.target_speed = None
129129
self.set_effort(0)
130130
return
131131
# If the update timer is not running, start it at 50 Hz (20ms updates)
132132
self.updateTimer.init(period=20, callback=lambda t:self._update())
133133
# Convert from rev per min to ticks per 20ms (60 sec/min, 50 Hz)
134-
self.target_speed = target_speed_rpm*self._encoder.ticks_per_rev/(60*50)
134+
self.target_speed = speed_rpm*self._encoder.ticks_per_rev/(60*50)
135135

136136
def set_speed_controller(self, new_controller):
137137
"""

XRPLib/encoder.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import time
66

77
class Encoder:
8-
gearRatio = (30/14) * (28/16) * (36/9) * (26/8) # 48.75
9-
ticksPerMotorRotation = 12
10-
ticksPerShaftRotation = ticksPerMotorRotation * gearRatio # 585
8+
gear_ratio = (30/14) * (28/16) * (36/9) * (26/8) # 48.75
9+
ticks_per_motor_shaft_revolution = 12
10+
ticks_per_rev = ticks_per_motor_shaft_revolution * gear_ratio # 585
1111

1212
def __init__(self, index, encAPin, encBPin):
1313
if(abs(encAPin - encBPin) != 1):
@@ -31,7 +31,7 @@ def get_position_ticks(self):
3131
return ticks
3232

3333
def get_position(self):
34-
return self.get_position_ticks() / self.ticksPerShaftRotation
34+
return self.get_position_ticks() / self.ticks_per_rev
3535

3636
@rp2.asm_pio(in_shiftdir=rp2.PIO.SHIFT_LEFT, out_shiftdir=rp2.PIO.SHIFT_RIGHT)
3737
def _encoder():

0 commit comments

Comments
 (0)