Skip to content

Commit 0fe4672

Browse files
committed
Add battery voltage getter; simplify resetbot
Replace the raw ADC threshold in Board.is_on with a voltage-based check (threshold 4.272 V) and add get_battery_voltage to read and convert ADC values. get_battery_voltage handles NanoXRP and RP2040 scaling and briefly toggles the VIN pin when needed. Also remove manual VIN pin reconfiguration from resetbot.py.
1 parent 959bcd0 commit 0fe4672

2 files changed

Lines changed: 30 additions & 5 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/resetbot.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,4 @@ def reset_hard():
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()

0 commit comments

Comments
 (0)