Skip to content

Commit df514c0

Browse files
committed
Merge branch 'main' of github.com:Open-STEM/XRP_MicroPython
2 parents e4e6908 + 6d8df23 commit df514c0

17 files changed

+125
-70
lines changed

Examples/misc_examples.py

Lines changed: 0 additions & 43 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.

XRPExamples/led_example.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
from XRPLib.board import Board
2+
from machine import Timer, Pin
3+
import time
4+
5+
# Get a reference to the board
6+
board = Board.get_default_board()
7+
8+
# Create a timer for the RGB LED, assuming it's present on this board
9+
rgb_led_timer = Timer(-1)
10+
11+
# Conversion from hue to RGB
12+
def hue_to_rgb(hue):
13+
# Initialize RGB values
14+
r = 0
15+
g = 0
16+
b = 0
17+
18+
# Ensure hue is in range of [0,360)
19+
hue %= 360
20+
21+
if(hue < 120):
22+
# Red to green region
23+
r = (120 - hue) / 120 * 255
24+
g = (hue - 0) / 120 * 255
25+
b = 0
26+
elif(hue < 240):
27+
# Green to blue region
28+
hue -= 120
29+
r = 0
30+
g = (120 - hue) / 120 * 255
31+
b = (hue - 0) / 120 * 255
32+
else:
33+
# Blue to red region
34+
hue -= 240
35+
r = (hue - 0) / 120 * 255
36+
g = 0
37+
b = (120 - hue) / 120 * 255
38+
39+
# Return RGB as tuple of integers in range of [0,255]
40+
return (int(r), int(g), int(b))
41+
42+
def update_rgb_led_rainbow(timer):
43+
# Set hue based on current time. Hue is an angle up to 360 degrees,
44+
# so using the milliseconds divided by 10 creates a rainbow that
45+
# repeats every 3.6 seconds
46+
hue = time.ticks_ms() / 10
47+
48+
# Compute RGB values
49+
rgb = hue_to_rgb(hue)
50+
51+
# Max brightness is blinding, so recompute RGB values with 10% brightness
52+
brightness = 0.1
53+
r = int(rgb[0] * brightness)
54+
g = int(rgb[1] * brightness)
55+
b = int(rgb[2] * brightness)
56+
57+
# Set the RGB LED color
58+
board.set_rgb_led(r, g, b)
59+
60+
def start_led_demo():
61+
# Make the monochrome LED start blinking
62+
board.led_blink(1)
63+
64+
# If this board has an RGB LED, make it start changing colors
65+
if hasattr(Pin.board, "BOARD_NEOPIXEL"):
66+
# Set up timer to update the RGB LED at 100Hz for smooth color changes
67+
rgb_led_timer.init(freq = 100, callback = update_rgb_led_rainbow)
68+
69+
def stop_led_demo():
70+
# Make the monochrome LED stop blinking and turn off the LED
71+
board.led_blink(0)
72+
board.led_off()
73+
74+
# If this board has an RGB LED, stop the timer and turn off the LED
75+
if hasattr(Pin.board, "BOARD_NEOPIXEL"):
76+
rgb_led_timer.deinit()
77+
board.set_rgb_led(0, 0, 0)
78+
79+
start_led_demo()
File renamed without changes.
File renamed without changes.
File renamed without changes.

XRPLib/board.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def __init__(self, vin_pin="BOARD_VIN_MEASURE", button_pin="BOARD_USER_BUTTON",
3333

3434
self.led = Pin(led_pin, Pin.OUT)
3535

36-
if "RP2350" in sys.implementation._machine:
36+
if hasattr(Pin.board, rgb_led_pin):
3737
self.rgb_led = NeoPixel(Pin(rgb_led_pin, Pin.OUT), 1)
3838
# A timer ID of -1 is a virtual timer.
3939
# Leaves the hardware timers for more important uses
@@ -110,6 +110,16 @@ def led_blink(self, frequency: int=0):
110110
self.is_led_blinking = False
111111

112112
def set_rgb_led(self, r:int, g:int, b:int):
113+
"""
114+
Sets the Neopixel RGB LED to a specified color. Throws a NotImplementedError on the XRP Beta
115+
116+
:param r: The amount of red in the desired color
117+
:type r: int
118+
:param g: The amount of green in the desired color
119+
:type g: int
120+
:param b: The amount of blue in the desired color
121+
:type b: int
122+
"""
113123
if "rgb_led" in self.__dict__:
114124
self.rgb_led[0] = (r, g, b)
115125
self.rgb_led.write()

XRPLib/defaults.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,27 @@
88
from .reflectance import Reflectance
99
from .servo import Servo
1010
from .webserver import Webserver
11+
from machine import Pin
1112

1213
"""
1314
A simple file that constructs all of the default objects for the XRP robot
1415
Run "from XRPLib.defaults import *" to use
1516
"""
1617

17-
left_motor = EncodedMotor.get_default_encoded_motor(index=3)
18-
right_motor = EncodedMotor.get_default_encoded_motor(index=4)
18+
left_motor = EncodedMotor.get_default_encoded_motor(index=1)
19+
right_motor = EncodedMotor.get_default_encoded_motor(index=2)
20+
motor_three = EncodedMotor.get_default_encoded_motor(index=3)
21+
motor_four = EncodedMotor.get_default_encoded_motor(index=4)
1922
imu = IMU.get_default_imu()
2023
drivetrain = DifferentialDrive.get_default_differential_drive()
2124
rangefinder = Rangefinder.get_default_rangefinder()
2225
reflectance = Reflectance.get_default_reflectance()
2326
servo_one = Servo.get_default_servo(index=1)
2427
servo_two = Servo.get_default_servo(index=2)
2528
webserver = Webserver.get_default_webserver()
26-
board = Board.get_default_board()
29+
board = Board.get_default_board()
30+
31+
if hasattr(Pin.board, "SERVO_3"):
32+
servo_three = Servo.get_default_servo(index=3)
33+
if hasattr(Pin.board, "SERVO_4"):
34+
servo_four = Servo.get_default_servo(index=4)

0 commit comments

Comments
 (0)