Skip to content

Commit 59c13ab

Browse files
committed
Update sphinx comments and remove all but 4 warnings
1 parent 34758f5 commit 59c13ab

16 files changed

+279
-199
lines changed

XRPLib/board.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ def get_default_board(cls):
1414
return cls._DEFAULT_BOARD_INSTANCE
1515

1616
def __init__(self, vin_pin:int, button_pin:int):
17+
"""
18+
Implements for extra features on the XRP v2 board. Handles the on/off switch, button, and LED.
19+
20+
:param vin_pin: The pin the on/off switch is connected to
21+
:type vin_pin: int
22+
:param button_pin: The pin the button is connected to
23+
:type button_pin: int
24+
"""
25+
1726
self.on_switch = ADC(Pin(vin_pin))
1827

1928
self.button = Pin(button_pin, Pin.IN, Pin.PULL_UP)
@@ -28,8 +37,8 @@ def __init__(self, vin_pin:int, button_pin:int):
2837

2938
def are_motors_powered(self) -> bool:
3039
"""
31-
: return: Returns true if the batteries are connected and powering the motors, false otherwise
32-
: rytpe: bool
40+
:return: Returns true if the batteries are connected and powering the motors, false otherwise
41+
:rytpe: bool
3342
"""
3443
return self.on_switch.read_u16() > 20000
3544

@@ -39,10 +48,10 @@ def set_button_callback(self, trigger, callback):
3948
Follow the link for more information on how to write an Interrupt Service Routine (ISR).
4049
https://docs.micropython.org/en/latest/reference/isr_rules.html
4150
42-
: param trigger: The type of trigger to be used for the interrupt
43-
: type trigger: Pin.IRQ_RISING | Pin.IRQ_FALLING
44-
: param callback: The function to be called when the interrupt is triggered
45-
: type callback: function | None
51+
:param trigger: The type of trigger to be used for the interrupt
52+
:type trigger: Pin.IRQ_RISING | Pin.IRQ_FALLING
53+
:param callback: The function to be called when the interrupt is triggered
54+
:type callback: function | None
4655
"""
4756
self.button_callback = callback
4857
self.button.irq(trigger=trigger, handler=self.button_callback)
@@ -51,8 +60,8 @@ def is_button_pressed(self) -> bool:
5160
"""
5261
Returns the state of the button
5362
54-
: return: True if the button is pressed, False otherwise
55-
: rtype: bool
63+
:return: True if the button is pressed, False otherwise
64+
:rtype: bool
5665
"""
5766
return not self.button.value()
5867

@@ -78,8 +87,8 @@ def led_blink(self, frequency: int):
7887
"""
7988
Blinks the LED at a given frequency
8089
81-
: param frequency: The frequency to blink the LED at (in Hz)
82-
: type frequency: int
90+
:param frequency: The frequency to blink the LED at (in Hz)
91+
:type frequency: int
8392
"""
8493
if self.is_led_blinking:
8594
# disable the old timer so we can reinitialize it

XRPLib/differential_drive.py

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ def get_default_differential_drive(cls):
2727
return cls._DEFAULT_DIFFERENTIAL_DRIVE_INSTANCE
2828

2929
def __init__(self, left_motor: EncodedMotor, right_motor: EncodedMotor, imu: IMU | None = None, wheel_diam:float = 6.0, wheel_track:float = 13.5):
30+
"""
31+
A Differential Drive class designed for the XRP two-wheeled drive robot.
32+
33+
:param leftMotor: The left motor of the drivetrain
34+
:type leftMotor: EncodedMotor
35+
:param rightMotor: The right motor of the drivetrain
36+
:type rightMotor: EncodedMotor
37+
:param imu: The IMU of the robot. If None, the robot will not use the IMU for turning or maintaining heading.
38+
:type imu: IMU
39+
:param wheelDiam: The diameter of the wheels in inches. Defaults to 6 inches.
40+
:type wheelDiam: float
41+
:param wheelTrack: The distance between the wheels in inches. Defaults to 13.5 inches.
42+
:type wheelTrack: float
43+
"""
44+
3045
self.left_motor = left_motor
3146
self.right_motor = right_motor
3247
self.imu = imu
@@ -38,10 +53,10 @@ def set_effort(self, left_effort: float, right_effort: float) -> None:
3853
"""
3954
Set the raw effort of both motors individually
4055
41-
: param leftEffort: The power (Bounded from -1 to 1) to set the left motor to.
42-
: type leftEffort: float
43-
: param rightEffort: The power (Bounded from -1 to 1) to set the right motor to.
44-
: type rightEffort: float
56+
:param leftEffort: The power (Bounded from -1 to 1) to set the left motor to.
57+
:type leftEffort: float
58+
:param rightEffort: The power (Bounded from -1 to 1) to set the right motor to.
59+
:type rightEffort: float
4560
"""
4661

4762
self.left_motor.set_effort(left_effort)
@@ -51,10 +66,10 @@ def set_speed(self, left_speed: float, right_speed: float) -> None:
5166
"""
5267
Set the speed of both motors individually
5368
54-
: param leftSpeed: The speed (In Centimeters per Second) to set the left motor to.
55-
: type leftSpeed: float
56-
: param rightSpeed: The speed (In Centimeters per Second) to set the right motor to.
57-
: type rightSpeed: float
69+
:param leftSpeed: The speed (In Centimeters per Second) to set the left motor to.
70+
:type leftSpeed: float
71+
:param rightSpeed: The speed (In Centimeters per Second) to set the right motor to.
72+
:type rightSpeed: float
5873
"""
5974
# Convert from cm/s to RPM
6075
cmpsToRPM = 60 / (math.pi * self.wheel_diam)
@@ -80,13 +95,15 @@ def reset_encoder_position(self) -> None:
8095

8196
def get_left_encoder_position(self) -> float:
8297
"""
83-
Return the current position of the left motor's encoder in revolutions.
98+
:return: the current position of the left motor's encoder in revolutions.
99+
:rtype: float
84100
"""
85101
return self.left_motor.get_position()
86102

87103
def get_right_encoder_position(self) -> float:
88104
"""
89-
Return the current position of the right motor's encoder in revolutions.
105+
:return: the current position of the right motor's encoder in revolutions.
106+
:rtype: float
90107
"""
91108
return self.right_motor.get_position()
92109

@@ -96,18 +113,18 @@ def straight(self, distance: float, max_effort: float = 0.5, timeout: float = No
96113
Go forward the specified distance in centimeters, and exit function when distance has been reached.
97114
Speed is bounded from -1 (reverse at full speed) to 1 (forward at full speed)
98115
99-
: param distance: The distance for the robot to travel (In Centimeters)
100-
: type distance: float
101-
: param speed: The speed for which the robot to travel (Bounded from -1 to 1). Default is half speed forward
102-
: type speed: float
103-
: param timeout: The amount of time before the robot stops trying to move forward and continues to the next step (In Seconds)
104-
: type timeout: float
105-
: param main_controller: The main controller, for handling the distance driven forwards
106-
: type main_controller: Controller
107-
: param secondary_controller: The secondary controller, for correcting heading error that may result during the drive.
108-
: type secondary_controller: Controller
109-
: return: if the distance was reached before the timeout
110-
: rtype: bool
116+
:param distance: The distance for the robot to travel (In Centimeters)
117+
:type distance: float
118+
:param speed: The speed for which the robot to travel (Bounded from -1 to 1). Default is half speed forward
119+
:type speed: float
120+
:param timeout: The amount of time before the robot stops trying to move forward and continues to the next step (In Seconds)
121+
:type timeout: float
122+
:param main_controller: The main controller, for handling the distance driven forwards
123+
:type main_controller: Controller
124+
:param secondary_controller: The secondary controller, for correcting heading error that may result during the drive.
125+
:type secondary_controller: Controller
126+
:return: if the distance was reached before the timeout
127+
:rtype: bool
111128
"""
112129
# ensure distance is always positive while speed could be either positive or negative
113130
if distance < 0:
@@ -181,19 +198,19 @@ def turn(self, turn_degrees: float, max_effort: float = 0.5, timeout: float = No
181198
Speed is bounded from -1 (turn counterclockwise the relative heading at full speed) to 1 (turn clockwise the relative heading at full speed)
182199
Uses the IMU to determine the heading of the robot and P control for the motor controller.
183200
184-
: param turnDegrees: The number of angle for the robot to turn (In Degrees)
185-
: type turnDegrees: float
186-
: param speed: The speed for which the robot to travel (Bounded from -1 to 1). Default is half speed forward.
187-
: type speed: float
188-
: param timeout: The amount of time before the robot stops trying to turn and continues to the next step (In Seconds)
189-
: type timeout: float
190-
: param main_controller: The main controller, for handling the angle turned
191-
: type main_controller: Controller
192-
: param secondary_controller: The secondary controller, for maintaining position during the turn by controlling the encoder count difference
193-
: type secondary_controller: Controller
194-
: param use_imu: A boolean flag that changes if the main controller bases it's movement off of
195-
: return: if the distance was reached before the timeout
196-
: rtype: bool
201+
:param turnDegrees: The number of angle for the robot to turn (In Degrees)
202+
:type turnDegrees: float
203+
:param speed: The speed for which the robot to travel (Bounded from -1 to 1). Default is half speed forward.
204+
:type speed: float
205+
:param timeout: The amount of time before the robot stops trying to turn and continues to the next step (In Seconds)
206+
:type timeout: float
207+
:param main_controller: The main controller, for handling the angle turned
208+
:type main_controller: Controller
209+
:param secondary_controller: The secondary controller, for maintaining position during the turn by controlling the encoder count difference
210+
:type secondary_controller: Controller
211+
:param use_imu: A boolean flag that changes if the main controller bases it's movement off of
212+
:return: if the distance was reached before the timeout
213+
:rtype: bool
197214
"""
198215

199216
if max_effort < 0:

XRPLib/encoded_motor.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,16 @@ class EncodedMotor:
1212
_DEFAULT_MOTOR_FOUR_INSTANCE = None
1313

1414
@classmethod
15-
def get_default_encoded_motor(cls, index:int = 0):
15+
def get_default_encoded_motor(cls, index:int = 1):
1616
"""
1717
Get one of the default XRP v2 motor instances. These are singletons, so only one instance of each of these will ever exist.
18-
Motor indexes:
19-
0 - Left Motor
20-
1 - Right Motor
21-
2 - Motor 3
22-
3 - Motor 4
23-
Left Motor is the default, so if no index is specified, the left motor will be returned.
18+
Left Motor is the default, so if no index (or an invalid index) is specified, the left motor will be returned.
19+
20+
:param index: The index of the motor to get; 1 for left, 2 for right, 3 for motor 3, 4 for motor 4
21+
:type index: int
2422
"""
2523

26-
if index == 1:
24+
if index == 2:
2725
if cls._DEFAULT_RIGHT_MOTOR_INSTANCE is None:
2826
cls._DEFAULT_RIGHT_MOTOR_INSTANCE = cls(
2927
Motor(14, 15),
@@ -37,7 +35,7 @@ def get_default_encoded_motor(cls, index:int = 0):
3735
Encoder(2, 0, 1)
3836
)
3937
motor = cls._DEFAULT_MOTOR_THREE_INSTANCE
40-
elif index == 3:
38+
elif index == 4:
4139
if cls._DEFAULT_MOTOR_FOUR_INSTANCE is None:
4240
cls._DEFAULT_MOTOR_FOUR_INSTANCE = cls(
4341
Motor(10, 11, flip_dir=True),
@@ -73,15 +71,15 @@ def __init__(self, motor: Motor, encoder: Encoder):
7371

7472
def set_effort(self, effort: float):
7573
"""
76-
: param effort: The effort to set this motor to, from -1 to 1
77-
: type effort: float
74+
:param effort: The effort to set this motor to, from -1 to 1
75+
:type effort: float
7876
"""
7977
self._motor.set_effort(effort)
8078

8179
def get_position(self) -> float:
8280
"""
83-
: return: The position of the encoded motor, in revolutions, relative to the last time reset was called.
84-
: rtype: float
81+
:return: The position of the encoded motor, in revolutions, relative to the last time reset was called.
82+
:rtype: float
8583
"""
8684
if self._motor.flip_dir:
8785
invert = -1
@@ -91,8 +89,8 @@ def get_position(self) -> float:
9189

9290
def get_position_ticks(self) -> int:
9391
"""
94-
: return: The position of the encoded motor, in encoder ticks, relative to the last time reset was called.
95-
: rtype: int
92+
:return: The position of the encoded motor, in encoder ticks, relative to the last time reset was called.
93+
:rtype: int
9694
"""
9795
if self._motor.flip_dir:
9896
invert = -1
@@ -108,10 +106,8 @@ def reset_encoder_position(self):
108106

109107
def get_speed(self) -> float:
110108
"""
111-
Gets the speed of the motor, in rpm
112-
113-
: return: The speed of the motor, in rpm
114-
: rtype: float
109+
:return: The speed of the motor, in rpm
110+
:rtype: float
115111
"""
116112
# Convert from ticks per 20ms to rpm (60 sec/min, 50 Hz)
117113
return self.speed*(60*50)/self._encoder.ticks_per_rev
@@ -121,8 +117,8 @@ def set_speed(self, speed_rpm: float | None = None):
121117
Sets target speed (in rpm) to be maintained passively
122118
Call with no parameters to turn off speed control
123119
124-
: param target_speed_rpm: The target speed for the motor in rpm, or None
125-
: type target_speed_rpm: float, or None
120+
:param target_speed_rpm: The target speed for the motor in rpm, or None
121+
:type target_speed_rpm: float, or None
126122
"""
127123
if speed_rpm is None:
128124
self.target_speed = None
@@ -137,8 +133,8 @@ def set_speed_controller(self, new_controller: Controller):
137133
"""
138134
Sets a new controller for speed control
139135
140-
: param new_controller: The new Controller for speed control
141-
: type new_controller: Controller
136+
:param new_controller: The new Controller for speed control
137+
:type new_controller: Controller
142138
"""
143139
self.speedController = new_controller
144140

XRPLib/encoder.py

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

77
class Encoder:
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
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):
@@ -18,19 +18,30 @@ def __init__(self, index, encAPin, encBPin):
1818
self.sm.active(1)
1919

2020
def reset_encoder_position(self):
21+
"""
22+
Resets the encoder position to 0
23+
"""
2124
# It's possible for this to cause issues if in the middle of inrementing
2225
# or decrementing, but the result should only be off by 1. If that's a
2326
# problem, an alternative solution is to stop the state machine, then
2427
# reset both x and the program counter. But that's excessive.
2528
self.sm.exec("set(x, 0)")
2629

2730
def get_position_ticks(self):
31+
"""
32+
:return: The position of the encoded motor, in ticks, relative to the last time reset was called.
33+
:rtype: int
34+
"""
2835
ticks = self.sm.get()
2936
if(ticks > 2**31):
3037
ticks -= 2**32
3138
return ticks
3239

3340
def get_position(self):
41+
"""
42+
:return: The position of the encoded motor, in revolutions, relative to the last time reset was called.
43+
:rtype: float
44+
"""
3445
return self.get_position_ticks() / self.ticks_per_rev
3546

3647
@rp2.asm_pio(in_shiftdir=rp2.PIO.SHIFT_LEFT, out_shiftdir=rp2.PIO.SHIFT_RIGHT)

0 commit comments

Comments
 (0)