diff --git a/XRPLib/board.py b/XRPLib/board.py index 8039f77..eb17e4f 100644 --- a/XRPLib/board.py +++ b/XRPLib/board.py @@ -46,7 +46,11 @@ def are_motors_powered(self) -> bool: :return: Returns true if the batteries are connected and powering the motors, false otherwise :rytpe: bool """ - return self.on_switch.read_u16() > 20000 + if "NanoXRP" in sys.implementation._machine: + return True + + threshold_voltage = 4.272 + return self.get_battery_voltage() > threshold_voltage def is_button_pressed(self) -> bool: """ @@ -125,3 +129,27 @@ def set_rgb_led(self, r:int, g:int, b:int): self.rgb_led.write() else: raise NotImplementedError("Board.set_rgb_led not implemented for the XRP Beta") + + def get_battery_voltage(self, vin_pin="BOARD_VIN_MEASURE") -> float: + """ + Returns the current battery voltage in volts. + + :param vin_pin: The pin the on/off switch is connected to + :type vin_pin: int + :return: Battery voltage in volts + :rtype: float + """ + + if "NanoXRP" in sys.implementation._machine: + # VIN pin on NanoXRP is also used for RM2. + self.on_switch = ADC(Pin(vin_pin)) + + battery_voltage = self.on_switch.read_u16() * (4.09 / 26000.0) + + Pin(vin_pin, Pin.OUT) + else: + # RP2040 ADC is 0-4095, MicroPython scales it to 0-65535. + # The voltage divider is calibrated to a 14V full-scale reading. + battery_voltage = self.on_switch.read_u16() / ((1024 * 64) / 14) + + return battery_voltage diff --git a/XRPLib/resetbot.py b/XRPLib/resetbot.py index 9a3bc37..0c6e3fb 100644 --- a/XRPLib/resetbot.py +++ b/XRPLib/resetbot.py @@ -70,14 +70,11 @@ def reset_hard(): if "XRPLib.board" in sys.modules: reset_led() -if "XRPLib.buzzer" in sys.modules: +if hasattr(Pin.board, "BOARD_BUZZER"): reset_buzzer() if "XRPLib.servo" in sys.modules: reset_servos() if "XRPLib.webserver" in sys.modules: - reset_webserver() - -if "NanoXRP" in sys.implementation._machine: - Pin("BOARD_VIN_MEASURE", Pin.OUT) + reset_webserver() \ No newline at end of file diff --git a/package.json b/package.json index fb9c107..3fb3f3f 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "urls": [ ["XRPLib/__init__.py", "github:Open-STEM/XRP_Micropython/XRPLib/__init__.py"], ["XRPLib/board.py", "github:Open-STEM/XRP_Micropython/XRPLib/board.py"], + ["XRPLib/buzzer.py", "github:Open-STEM/XRP_Micropython/XRPLib/buzzer.py"], ["XRPLib/controller.py", "github:Open-STEM/XRP_Micropython/XRPLib/controller.py"], ["XRPLib/defaults.py", "github:Open-STEM/XRP_Micropython/XRPLib/defaults.py"], ["XRPLib/differential_drive.py", "github:Open-STEM/XRP_Micropython/XRPLib/differential_drive.py"], @@ -20,6 +21,7 @@ ["XRPLib/timeout.py", "github:Open-STEM/XRP_Micropython/XRPLib/timeout.py"], ["XRPLib/webserver.py", "github:Open-STEM/XRP_Micropython/XRPLib/webserver.py"], ["XRPExamples/__init__.py", "github:Open-STEM/XRP_Micropython/XRPExamples/__init__.py"], + ["XRPExamples/buzzer_examples.py", "github:Open-STEM/XRP_Micropython/XRPExamples/buzzer_examples.py"], ["XRPExamples/drive_examples.py", "github:Open-STEM/XRP_Micropython/XRPExamples/drive_examples.py"], ["XRPExamples/gamepad_example.blocks", "github:Open-STEM/XRP_Micropython/XRPExamples/gamepad_example.blocks"], ["XRPExamples/installation_verification.py", "github:Open-STEM/XRP_Micropython/XRPExamples/installation_verification.py"], @@ -30,5 +32,5 @@ "deps": [ ["github:pimoroni/phew", "latest"] ], - "version": "2.1.3" + "version": "2.1.4" }