Skip to content

Commit 2aadb8d

Browse files
committed
Create a branch with the V1 boards changes
1 parent 193010d commit 2aadb8d

11 files changed

Lines changed: 74 additions & 36 deletions

File tree

.DS_Store

8 KB
Binary file not shown.

XRPLib/board.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from machine import Pin, ADC, Timer
2+
from neopixel import NeoPixel
23
import time
34

45
class Board:
@@ -11,7 +12,7 @@ def get_default_board(cls):
1112
Get the default board instance. This is a singleton, so only one instance of the board will ever exist.
1213
"""
1314
if cls._DEFAULT_BOARD_INSTANCE is None:
14-
cls._DEFAULT_BOARD_INSTANCE = cls(28,22)
15+
cls._DEFAULT_BOARD_INSTANCE = cls("VIN_MEAS","USER_BUTTON")
1516
return cls._DEFAULT_BOARD_INSTANCE
1617

1718
def __init__(self, vin_pin:int, button_pin:int):
@@ -29,6 +30,7 @@ def __init__(self, vin_pin:int, button_pin:int):
2930
self.button = Pin(button_pin, Pin.IN, Pin.PULL_UP)
3031

3132
self.led = Pin("LED", Pin.OUT)
33+
self.rgb_led = NeoPixel(Pin("RGB_LED", Pin.OUT), 1)
3234
# A timer ID of -1 is a virtual timer.
3335
# Leaves the hardware timers for more important uses
3436
self._virt_timer = Timer(-1)
@@ -102,3 +104,7 @@ def led_blink(self, frequency: int=0):
102104
else:
103105
self._virt_timer.deinit()
104106
self.is_led_blinking = False
107+
108+
def set_rgb_led(self, r:int, g:int, b:int):
109+
self.rgb_led[0] = (r, g, b)
110+
self.rgb_led.write()

XRPLib/defaults.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
Run "from XRPLib.defaults import *" to use
1515
"""
1616

17-
left_motor = EncodedMotor.get_default_encoded_motor(index=1)
18-
right_motor = EncodedMotor.get_default_encoded_motor(index=2)
17+
left_motor = EncodedMotor.get_default_encoded_motor(index=3)
18+
right_motor = EncodedMotor.get_default_encoded_motor(index=4)
1919
imu = IMU.get_default_imu()
2020
drivetrain = DifferentialDrive.get_default_differential_drive()
2121
rangefinder = Rangefinder.get_default_rangefinder()

XRPLib/differential_drive.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def get_default_differential_drive(cls):
1919

2020
if cls._DEFAULT_DIFFERENTIAL_DRIVE_INSTANCE is None:
2121
cls._DEFAULT_DIFFERENTIAL_DRIVE_INSTANCE = cls(
22-
EncodedMotor.get_default_encoded_motor(index=1),
23-
EncodedMotor.get_default_encoded_motor(index=2),
22+
EncodedMotor.get_default_encoded_motor(index=3),
23+
EncodedMotor.get_default_encoded_motor(index=4),
2424
IMU.get_default_imu()
2525
)
2626

XRPLib/encoded_motor.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from machine import Timer
44
from .controller import Controller
55
from .pid import PID
6+
import sys
67

78
class EncodedMotor:
89

@@ -20,32 +21,36 @@ def get_default_encoded_motor(cls, index:int = 1):
2021
:param index: The index of the motor to get; 1 for left, 2 for right, 3 for motor 3, 4 for motor 4
2122
:type index: int
2223
"""
24+
if sys.implementation._machine == "SparkFun XRP V2 with RP2350":
25+
usePwm = True
26+
else:
27+
usePwm = False
2328
if index == 1:
2429
if cls._DEFAULT_LEFT_MOTOR_INSTANCE is None:
2530
cls._DEFAULT_LEFT_MOTOR_INSTANCE = cls(
26-
Motor(6, 7, flip_dir=True),
27-
Encoder(0, 4, 5)
31+
Motor("MOTOR_L_IN1", "MOTOR_L_IN2", flip_dir=True, pwmMode=usePwm),
32+
Encoder(0, "ENCODER_LA", "ENCODER_LB")
2833
)
2934
motor = cls._DEFAULT_LEFT_MOTOR_INSTANCE
3035
elif index == 2:
3136
if cls._DEFAULT_RIGHT_MOTOR_INSTANCE is None:
3237
cls._DEFAULT_RIGHT_MOTOR_INSTANCE = cls(
33-
Motor(14, 15),
34-
Encoder(1, 12, 13)
38+
Motor("MOTOR_R_IN1", "MOTOR_R_IN2", pwmMode=usePwm),
39+
Encoder(1, "ENCODER_RA", "ENCODER_RB")
3540
)
3641
motor = cls._DEFAULT_RIGHT_MOTOR_INSTANCE
3742
elif index == 3:
3843
if cls._DEFAULT_MOTOR_THREE_INSTANCE is None:
3944
cls._DEFAULT_MOTOR_THREE_INSTANCE = cls(
40-
Motor(2, 3),
41-
Encoder(2, 0, 1)
45+
Motor("MOTOR_3_IN1", "MOTOR_3_IN2", flip_dir=True, pwmMode=usePwm),
46+
Encoder(2, "ENCODER_3A", "ENCODER_3B")
4247
)
4348
motor = cls._DEFAULT_MOTOR_THREE_INSTANCE
4449
elif index == 4:
4550
if cls._DEFAULT_MOTOR_FOUR_INSTANCE is None:
4651
cls._DEFAULT_MOTOR_FOUR_INSTANCE = cls(
47-
Motor(10, 11, flip_dir=True),
48-
Encoder(3, 8, 9)
52+
Motor("MOTOR_4_IN1", "MOTOR_4_IN2", pwmMode=usePwm),
53+
Encoder(3, "ENCODER_4A", "ENCODER_4B")
4954
)
5055
motor = cls._DEFAULT_MOTOR_FOUR_INSTANCE
5156
else:

XRPLib/encoder.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ def __init__(self, index, encAPin, encBPin):
2121
:param encBPin: The pin the right reflectance sensor is connected to
2222
:type encBPin: int
2323
"""
24-
if(abs(encAPin - encBPin) != 1):
25-
raise Exception("Encoder pins must be successive!")
26-
basePin = machine.Pin(min(encAPin, encBPin))
24+
# if(abs(encAPin - encBPin) != 1):
25+
# raise Exception("Encoder pins must be successive!")
26+
basePin = machine.Pin(min(encAPin, encBPin), machine.Pin.IN)
27+
nextPin = machine.Pin(max(encAPin, encBPin), machine.Pin.IN)
2728
self.sm = rp2.StateMachine(index, self._encoder, in_base=basePin)
2829
self.reset_encoder_position()
2930
self.sm.active(1)

XRPLib/imu.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ def get_default_imu(cls):
2424

2525
if cls._DEFAULT_IMU_INSTANCE is None:
2626
cls._DEFAULT_IMU_INSTANCE = cls(
27-
scl_pin=19,
28-
sda_pin=18,
27+
scl_pin="SCL1",
28+
sda_pin="SDA1",
2929
addr=LSM_ADDR_PRIMARY
3030
)
3131
cls._DEFAULT_IMU_INSTANCE.calibrate()

XRPLib/motor.py

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@ class Motor:
55
A wrapper class handling direction and power sets for DC motors on the XRP robots
66
"""
77

8-
def __init__(self, direction_pin: int, speed_pin: int, flip_dir:bool=False):
9-
self._dirPin = Pin(direction_pin, Pin.OUT)
10-
self._speedPin = PWM(Pin(speed_pin, Pin.OUT))
11-
self._speedPin.freq(50)
8+
def __init__(self, in1_direction_pin: int, in2_speed_pin: int, flip_dir:bool=False, pwmMode:bool=False):
9+
self._pwmMode = pwmMode
1210
self.flip_dir = flip_dir
1311
self._MAX_PWM = 65534 # Motor holds when actually at full power
1412

13+
if self._pwmMode:
14+
self._in1DirPin = PWM(Pin(in1_direction_pin, Pin.OUT))
15+
self._in2SpeedPin = PWM(Pin(in2_speed_pin, Pin.OUT))
16+
self._in1DirPin.freq(50)
17+
self._in2SpeedPin.freq(50)
18+
else:
19+
self._in1DirPin = Pin(in1_direction_pin, Pin.OUT)
20+
self._in2SpeedPin = PWM(Pin(in2_speed_pin, Pin.OUT))
21+
self._in2SpeedPin.freq(50)
22+
1523
def set_effort(self, effort: float):
1624
"""
1725
Sets the effort value of the motor (corresponds to power)
@@ -20,18 +28,28 @@ def set_effort(self, effort: float):
2028
:type effort: float
2129
"""
2230

23-
if effort < 0:
24-
# Change direction if negative power
25-
effort *= -1
26-
self._set_direction(1)
31+
if self._pwmMode:
32+
in1Pwm = (effort < 0) ^ (self.flip_dir)
33+
if in1Pwm:
34+
self._in1DirPin.duty_u16(int(abs(effort)*self._MAX_PWM))
35+
self._in2SpeedPin.duty_u16(int(0))
36+
else:
37+
self._in1DirPin.duty_u16(int(0))
38+
self._in2SpeedPin.duty_u16(int(abs(effort)*self._MAX_PWM))
2739
else:
28-
self._set_direction(0)
29-
# Cap power to [0,1]
30-
effort = max(0,min(effort,1))
31-
self._speedPin.duty_u16(int(effort*self._MAX_PWM))
40+
if effort < 0:
41+
# Change direction if negative power
42+
effort *= -1
43+
self._set_direction(1)
44+
else:
45+
self._set_direction(0)
46+
# Cap power to [0,1]
47+
effort = max(0,min(effort,1))
48+
self._in2SpeedPin.duty_u16(int(effort*self._MAX_PWM))
49+
3250

3351
def _set_direction(self, direction: int):
3452
if self.flip_dir:
35-
self._dirPin.value(not direction)
53+
self._in1DirPin.value(not direction)
3654
else:
37-
self._dirPin.value(direction)
55+
self._in1DirPin.value(direction)

XRPLib/rangefinder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def get_default_rangefinder(cls):
1111
Get the default XRP v2 rangefinder instance. This is a singleton, so only one instance of the rangefinder will ever exist.
1212
"""
1313
if cls._DEFAULT_RANGEFINDER_INSTANCE is None:
14-
cls._DEFAULT_RANGEFINDER_INSTANCE = cls(20, 21)
14+
cls._DEFAULT_RANGEFINDER_INSTANCE = cls("DIST_TRIG", "DIST_ECHO")
1515
return cls._DEFAULT_RANGEFINDER_INSTANCE
1616

1717
def __init__(self, trigger_pin:int, echo_pin:int, timeout_us:int=500*2*30):

XRPLib/reflectance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def get_default_reflectance(cls):
1212
Get the default XRP v2 reflectance sensor instance. This is a singleton, so only one instance of the reflectance sensor will ever exist.
1313
"""
1414
if cls._DEFAULT_REFLECTANCE_INSTANCE is None:
15-
cls._DEFAULT_REFLECTANCE_INSTANCE = cls(26, 27)
15+
cls._DEFAULT_REFLECTANCE_INSTANCE = cls("LINE_L", "LINE_R")
1616
return cls._DEFAULT_REFLECTANCE_INSTANCE
1717

1818
def __init__(self, leftPin:int, rightPin:int):

0 commit comments

Comments
 (0)