Skip to content

Commit 223085f

Browse files
authored
Merge pull request #96 from Open-STEM/jacob-nanoxrp
NanoXRP: Battery voltage function, resetbot improvements, fixed package.json, fixed wheel_diam/wheel_track
2 parents 36be75c + 7d8cb98 commit 223085f

5 files changed

Lines changed: 48 additions & 17 deletions

File tree

XRPLib/board.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ def are_motors_powered(self) -> bool:
4646
:return: Returns true if the batteries are connected and powering the motors, false otherwise
4747
:rytpe: bool
4848
"""
49-
return self.on_switch.read_u16() > 20000
49+
if "NanoXRP" in sys.implementation._machine:
50+
return True
51+
52+
threshold_voltage = 4.272
53+
return self.get_battery_voltage() > threshold_voltage
5054

5155
def is_button_pressed(self) -> bool:
5256
"""
@@ -125,3 +129,27 @@ def set_rgb_led(self, r:int, g:int, b:int):
125129
self.rgb_led.write()
126130
else:
127131
raise NotImplementedError("Board.set_rgb_led not implemented for the XRP Beta")
132+
133+
def get_battery_voltage(self, vin_pin="BOARD_VIN_MEASURE") -> float:
134+
"""
135+
Returns the current battery voltage in volts.
136+
137+
:param vin_pin: The pin the on/off switch is connected to
138+
:type vin_pin: int
139+
:return: Battery voltage in volts
140+
:rtype: float
141+
"""
142+
143+
if "NanoXRP" in sys.implementation._machine:
144+
# VIN pin on NanoXRP is also used for RM2.
145+
self.on_switch = ADC(Pin(vin_pin))
146+
147+
battery_voltage = self.on_switch.read_u16() * (4.09 / 26000.0)
148+
149+
Pin(vin_pin, Pin.OUT)
150+
else:
151+
# RP2040 ADC is 0-4095, MicroPython scales it to 0-65535.
152+
# The voltage divider is calibrated to a 14V full-scale reading.
153+
battery_voltage = self.on_switch.read_u16() / ((1024 * 64) / 14)
154+
155+
return battery_voltage

XRPLib/defaults.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from .webserver import Webserver
1111
from .buzzer import Buzzer
1212
from machine import Pin
13-
from sys import implementation
1413

1514
"""
1615
A simple file that constructs all of the default objects for the XRP robot
@@ -24,12 +23,7 @@
2423
motor_four = EncodedMotor.get_default_encoded_motor(index=4)
2524

2625
imu = IMU.get_default_imu()
27-
28-
if "NanoXRP" in implementation._machine:
29-
drivetrain = DifferentialDrive(left_motor, right_motor, imu, wheel_diam=3.46, wheel_track=7.8)
30-
else:
31-
drivetrain = DifferentialDrive.get_default_differential_drive()
32-
26+
drivetrain = DifferentialDrive.get_default_differential_drive()
3327
rangefinder = Rangefinder.get_default_rangefinder()
3428
reflectance = Reflectance.get_default_reflectance()
3529
servo_one = Servo.get_default_servo(index=1)

XRPLib/differential_drive.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def get_default_differential_drive(cls):
2727

2828
return cls._DEFAULT_DIFFERENTIAL_DRIVE_INSTANCE
2929

30-
def __init__(self, left_motor: EncodedMotor, right_motor: EncodedMotor, imu: IMU = None, wheel_diam:float = 6.0, wheel_track:float = 15.5):
30+
def __init__(self, left_motor: EncodedMotor, right_motor: EncodedMotor, imu: IMU = None, wheel_diam:float = 0.0, wheel_track:float = 0.0):
3131
"""
3232
A Differential Drive class designed for the XRP two-wheeled drive robot.
3333
@@ -48,8 +48,18 @@ def __init__(self, left_motor: EncodedMotor, right_motor: EncodedMotor, imu: IMU
4848
self.imu = imu
4949

5050
self.brake_at_zero_power = False
51-
self.wheel_diam = wheel_diam
52-
self.track_width = wheel_track
51+
52+
if (wheel_diam == 0.0):
53+
if "NanoXRP" in implementation._machine:
54+
self.wheel_diam = 3.46
55+
else:
56+
self.wheel_diam = 6.0
57+
58+
if (wheel_track == 0.0):
59+
if "NanoXRP" in implementation._machine:
60+
self.wheel_track = 7.8
61+
else:
62+
self.wheel_track = 15.5
5363

5464
self.heading_pid = None
5565
self.current_heading = None

XRPLib/resetbot.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,11 @@ def reset_hard():
7070
if "XRPLib.board" in sys.modules:
7171
reset_led()
7272

73-
if "XRPLib.buzzer" in sys.modules:
73+
if hasattr(Pin.board, "BOARD_BUZZER"):
7474
reset_buzzer()
7575

7676
if "XRPLib.servo" in sys.modules:
7777
reset_servos()
7878

7979
if "XRPLib.webserver" in sys.modules:
80-
reset_webserver()
81-
82-
if "NanoXRP" in sys.implementation._machine:
83-
Pin("BOARD_VIN_MEASURE", Pin.OUT)
80+
reset_webserver()

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"urls": [
33
["XRPLib/__init__.py", "github:Open-STEM/XRP_Micropython/XRPLib/__init__.py"],
44
["XRPLib/board.py", "github:Open-STEM/XRP_Micropython/XRPLib/board.py"],
5+
["XRPLib/buzzer.py", "github:Open-STEM/XRP_Micropython/XRPLib/buzzer.py"],
56
["XRPLib/controller.py", "github:Open-STEM/XRP_Micropython/XRPLib/controller.py"],
67
["XRPLib/defaults.py", "github:Open-STEM/XRP_Micropython/XRPLib/defaults.py"],
78
["XRPLib/differential_drive.py", "github:Open-STEM/XRP_Micropython/XRPLib/differential_drive.py"],
@@ -20,6 +21,7 @@
2021
["XRPLib/timeout.py", "github:Open-STEM/XRP_Micropython/XRPLib/timeout.py"],
2122
["XRPLib/webserver.py", "github:Open-STEM/XRP_Micropython/XRPLib/webserver.py"],
2223
["XRPExamples/__init__.py", "github:Open-STEM/XRP_Micropython/XRPExamples/__init__.py"],
24+
["XRPExamples/buzzer_examples.py", "github:Open-STEM/XRP_Micropython/XRPExamples/buzzer_examples.py"],
2325
["XRPExamples/drive_examples.py", "github:Open-STEM/XRP_Micropython/XRPExamples/drive_examples.py"],
2426
["XRPExamples/gamepad_example.blocks", "github:Open-STEM/XRP_Micropython/XRPExamples/gamepad_example.blocks"],
2527
["XRPExamples/installation_verification.py", "github:Open-STEM/XRP_Micropython/XRPExamples/installation_verification.py"],
@@ -30,5 +32,5 @@
3032
"deps": [
3133
["github:pimoroni/phew", "latest"]
3234
],
33-
"version": "2.1.3"
35+
"version": "2.1.4"
3436
}

0 commit comments

Comments
 (0)