Skip to content

Commit 788f23d

Browse files
authored
Merge pull request #253 from Open-STEM/F-staging-release
Updated XRPLib for Nano
2 parents a8b259d + 5bc0326 commit 788f23d

15 files changed

Lines changed: 758 additions & 147 deletions

public/XRPLib.zip

4.74 KB
Binary file not shown.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
from XRPLib.defaults import *
2+
import time
3+
4+
"""
5+
By the end of this file students will learn how to use the Buzzer class
6+
to play individual notes, custom songs, and how to make the robot
7+
play music while it is moving.
8+
"""
9+
10+
def simple_scale():
11+
"""Plays a basic C major scale."""
12+
print("Playing a simple scale...")
13+
notes = ["C4", "D4", "E4", "F4", "G4", "A4", "B4", "C5"]
14+
for note in notes:
15+
buzzer.play_note(note, "quarter")
16+
17+
def custom_song_example():
18+
"""Demonstrates how to create and play a custom list of notes."""
19+
print("Playing a custom melody...")
20+
# A song is a list of (note, duration) tuples
21+
# Durations can be "whole", "half", "quarter", "eighth", "sixteenth"
22+
my_song = [
23+
("E4", "eighth"), ("G4", "eighth"), ("E5", "quarter"),
24+
("rest", "eighth"),
25+
("C4", "quarter"), ("G3", "half")
26+
]
27+
28+
# Set tempo to 140 Beats Per Minute
29+
buzzer.set_tempo(140)
30+
buzzer.play_song(my_song)
31+
32+
def siren():
33+
"""Simple two-tone siren effect using individual notes."""
34+
print("Siren started!")
35+
for _ in range(3):
36+
buzzer.play_note("A4", "quarter")
37+
buzzer.play_note("E4", "quarter")
38+
39+
def musical_robot():
40+
"""
41+
Playing music while driving.
42+
We use blocking=False so the code continues to the next line
43+
while the music plays in the background.
44+
"""
45+
print("Dancing with music!")
46+
47+
# Start 'Move It' in the background
48+
buzzer.play_move_it(blocking=False)
49+
50+
# While the song is playing, the robot can perform actions
51+
for i in range(4):
52+
drivetrain.set_effort(0.6, -0.6) # Spin right
53+
time.sleep(0.5)
54+
drivetrain.set_effort(-0.6, 0.6) # Spin left
55+
time.sleep(0.5)
56+
57+
drivetrain.stop()
58+
print("Dance finished.")
59+
60+
def drive_and_beep():
61+
"""Driving forward and playing a note at the same time."""
62+
# Start driving forward (this is non-blocking)
63+
drivetrain.set_effort(0.5, 0.5)
64+
65+
# Play a long note (blocking=True) so the robot drives for the duration of the note
66+
print("Driving for a whole note...")
67+
buzzer.play_note("C5", "whole", blocking=True)
68+
69+
drivetrain.stop()
70+
71+
def main():
72+
# Run the examples
73+
simple_scale()
74+
time.sleep(1)
75+
76+
siren()
77+
time.sleep(1)
78+
79+
musical_robot()
80+
81+
main()

public/lib/XRPExamples/dashboard sample.py

Lines changed: 0 additions & 65 deletions
This file was deleted.

public/lib/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

0 commit comments

Comments
 (0)