Skip to content

Commit 4dfb76e

Browse files
committed
Variable and Function name change
1 parent 037c37f commit 4dfb76e

File tree

4 files changed

+30
-29
lines changed

4 files changed

+30
-29
lines changed

XRPLib/encoded_motor.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,16 @@ def get_position(self) -> float:
8787
invert = 1
8888
return self._encoder.get_position()*invert
8989

90-
def get_position_ticks(self) -> int:
90+
def get_position_counts(self) -> int:
9191
"""
92-
:return: The position of the encoded motor, in encoder ticks, relative to the last time reset was called.
92+
:return: The position of the encoded motor, in encoder counts, relative to the last time reset was called.
9393
:rtype: int
9494
"""
9595
if self._motor.flip_dir:
9696
invert = -1
9797
else:
9898
invert = 1
99-
return self._encoder.get_position_ticks()*invert
99+
return self._encoder.get_position_counts()*invert
100100

101101
def reset_encoder_position(self):
102102
"""
@@ -109,8 +109,8 @@ def get_speed(self) -> float:
109109
:return: The speed of the motor, in rpm
110110
:rtype: float
111111
"""
112-
# Convert from ticks per 20ms to rpm (60 sec/min, 50 Hz)
113-
return self.speed*(60*50)/self._encoder.ticks_per_rev
112+
# Convert from counts per 20ms to rpm (60 sec/min, 50 Hz)
113+
return self.speed*(60*50)/self._encoder.resolution
114114

115115
def set_speed(self, speed_rpm: float | None = None):
116116
"""
@@ -127,8 +127,8 @@ def set_speed(self, speed_rpm: float | None = None):
127127
return
128128
# If the update timer is not running, start it at 50 Hz (20ms updates)
129129
self.updateTimer.init(period=20, callback=lambda t:self._update())
130-
# Convert from rev per min to ticks per 20ms (60 sec/min, 50 Hz)
131-
self.target_speed = speed_rpm*self._encoder.ticks_per_rev/(60*50)
130+
# Convert from rev per min to counts per 20ms (60 sec/min, 50 Hz)
131+
self.target_speed = speed_rpm*self._encoder.resolution/(60*50)
132132
self.speedController.clear_history()
133133

134134
def set_speed_controller(self, new_controller: Controller):
@@ -145,11 +145,11 @@ def _update(self):
145145
"""
146146
Non-api method; used for updating motor efforts for speed control
147147
"""
148-
current_position = self.get_position_ticks()
148+
current_position = self.get_position_counts()
149149
self.speed = current_position - self.prev_position
150150
if self.target_speed is not None:
151151
error = self.target_speed - self.speed
152-
effort = self.speedController.tick(error)
152+
effort = self.speedController.update(error)
153153
self._motor.set_effort(effort)
154154
else:
155155
self.updateTimer.deinit()

XRPLib/encoder.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
class Encoder:
88
_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
9+
_counts_per_motor_shaft_revolution = 12
10+
resolution = _counts_per_motor_shaft_revolution * _gear_ratio # 585
1111

1212
def __init__(self, index, encAPin, encBPin):
1313
"""
@@ -38,26 +38,27 @@ def reset_encoder_position(self):
3838
# reset both x and the program counter. But that's excessive.
3939
self.sm.exec("set(x, 0)")
4040

41-
def get_position_ticks(self):
41+
def get_position_counts(self):
4242
"""
43-
:return: The position of the encoded motor, in ticks, relative to the last time reset was called.
43+
:return: The position of the encoded motor, in counts, relative to the last time reset was called.
4444
:rtype: int
4545
"""
46-
ticks = self.sm.get()
47-
ticks = self.sm.get()
48-
ticks = self.sm.get()
49-
ticks = self.sm.get()
50-
ticks = self.sm.get()
51-
if(ticks > 2**31):
52-
ticks -= 2**32
53-
return ticks
46+
# Read 5 times for it to get past the buffer
47+
counts = self.sm.get()
48+
counts = self.sm.get()
49+
counts = self.sm.get()
50+
counts = self.sm.get()
51+
counts = self.sm.get()
52+
if(counts > 2**31):
53+
counts -= 2**32
54+
return counts
5455

5556
def get_position(self):
5657
"""
5758
:return: The position of the encoded motor, in revolutions, relative to the last time reset was called.
5859
:rtype: float
5960
"""
60-
return self.get_position_ticks() / self.ticks_per_rev
61+
return self.get_position_counts() / self.resolution
6162

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

XRPLib/pid.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ def _handle_exit_condition(self, error: float):
5555
# otherwise, reset times in tolerance, because we need to be in tolerance for numTimesInTolerance consecutive times
5656
self.times = 0
5757

58-
def tick(self, error: float) -> float:
58+
def update(self, error: float) -> float:
5959
"""
60-
Handle a new tick of this PID loop given an error.
60+
Handle a new update of this PID loop given an error.
6161
6262
:param error: The error of the system being controlled by this PID controller
6363
:type error: float
@@ -67,13 +67,13 @@ def tick(self, error: float) -> float:
6767
"""
6868
currentTime = time.ticks_ms()
6969
if self.prevTime is None:
70-
# First tick after instantiation
70+
# First update after instantiation
7171
self.startTime = currentTime
7272
timestep = 0.01
7373
else:
7474
# get time delta in seconds
7575
timestep = time.ticks_diff(currentTime, self.prevTime) / 1000
76-
self.prevTime = currentTime # cache time for next tick
76+
self.prevTime = currentTime # cache time for next update
7777

7878
self._handle_exit_condition(error)
7979

@@ -100,7 +100,7 @@ def tick(self, error: float) -> float:
100100
upperBound = self.prevOutput + self.maxDerivative * timestep
101101
output = max(lowerBound, min(upperBound, output))
102102

103-
# cache output for next tick
103+
# cache output for next update
104104
self.prevOutput = output
105105

106106
return output

XRPLib/servo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ def __init__(self, signal_pin:int):
2727
self.MICROSEC_PER_DEGREE: int = 10000
2828
self.LOW_ANGLE_OFFSET: int = 1000000
2929

30-
def set_angle(self, degrees: float):
30+
def set_position(self, degrees: float):
3131
"""
3232
Sets the angle of the servo
33-
:param degrees: The angle to set the servo to [0,135]
33+
:param degrees: The angle to set the servo to [0,180]
3434
:ptype degrees: float
3535
"""
3636
self._servo.duty_ns(int(degrees * self.MICROSEC_PER_DEGREE + self.LOW_ANGLE_OFFSET))

0 commit comments

Comments
 (0)