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

-43
This file was deleted.
File renamed without changes.
File renamed without changes.

XRPExamples/led_example.py

+79
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

+11-1
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

+11-3
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)

XRPLib/reflectance.py

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
class Reflectance:
44

5-
6-
75
_DEFAULT_REFLECTANCE_INSTANCE = None
86

97
@classmethod

XRPLib/servo.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from machine import Pin, PWM
2+
import sys
23

34
class Servo:
45

@@ -24,11 +25,11 @@ def get_default_servo(cls, index:int):
2425
if cls._DEFAULT_SERVO_TWO_INSTANCE is None:
2526
cls._DEFAULT_SERVO_TWO_INSTANCE = cls("SERVO_2")
2627
servo = cls._DEFAULT_SERVO_TWO_INSTANCE
27-
elif index == 3:
28+
elif index == 3 and hasattr(Pin.board, "SERVO_3"):
2829
if cls._DEFAULT_SERVO_THREE_INSTANCE is None:
2930
cls._DEFAULT_SERVO_THREE_INSTANCE = cls("SERVO_3")
3031
servo = cls._DEFAULT_SERVO_THREE_INSTANCE
31-
elif index == 4:
32+
elif index == 4 and hasattr(Pin.board, "SERVO_4"):
3233
if cls._DEFAULT_SERVO_FOUR_INSTANCE is None:
3334
cls._DEFAULT_SERVO_FOUR_INSTANCE = cls("SERVO_4")
3435
servo = cls._DEFAULT_SERVO_FOUR_INSTANCE

XRPLib/webserver.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def __init__(self):
2525
gc.threshold(50000) # garbage collection
2626
self.logged_data = {}
2727
self.buttons = {"forwardButton": lambda: logging.debug("Button not initialized"),
28-
"backButton": lambda: logging.debug("Button not initialized"),
28+
"backButton": lambda: logging.debug("Button not initialized"),
2929
"leftButton": lambda: logging.debug("Button not initialized"),
3030
"rightButton": lambda: logging.debug("Button not initialized"),
3131
"stopButton": lambda: logging.debug("Button not initialized")}

docs/api.rst

+10-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@ API Reference
1313

1414
Hardware
1515
--------
16-
.. autoclass:: XRPLib.motor.Motor
16+
17+
.. autoclass:: XRPLib.board.Board
18+
:members:
19+
:undoc-members:
20+
21+
.. autoclass:: XRPLib.motor.SinglePWMMotor
22+
:members:
23+
:undoc-members:
24+
25+
.. autoclass:: XRPLib.motor.DualPWMMotor
1726
:members:
1827
:undoc-members:
1928

@@ -55,10 +64,6 @@ Sensors
5564
Miscellaneous
5665
-------------
5766

58-
.. autoclass:: XRPLib.board.Board
59-
:members:
60-
:undoc-members:
61-
6267
.. autoclass:: XRPLib.controller.Controller
6368
:members:
6469
:undoc-members:

docs/conf.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
3+
# SPDX-FileCopyrightText: 2023 Kevin Siegall, written for Open-STEM and the XRP Consortium
44
#
55
# SPDX-License-Identifier: MIT
66

@@ -26,7 +26,6 @@
2626
"sphinx.ext.todo",
2727
]
2828

29-
# TODO: Please Read!
3029
# Uncomment the below if you use native CircuitPython modules such as
3130
# digitalio, micropython and busio. List the modules you use. Without it, the
3231
# autodoc module docs will fail to generate with a warning.

docs/examples.rst

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ Example Code
33

44
Feel free to read through, import, and use any of the below sample code:
55

6-
.. literalinclude:: ../Examples/drive_examples.py
7-
:caption: Examples/drive_examples.py
6+
.. literalinclude:: ../XRPExamples/drive_examples.py
7+
:caption: XRPExamples/drive_examples.py
88
:linenos:
99

10-
.. literalinclude:: ../Examples/sensor_examples.py
11-
:caption: Examples/sensor_examples.py
10+
.. literalinclude:: ../XRPExamples/sensor_examples.py
11+
:caption: XRPExamples/sensor_examples.py
1212
:linenos:
1313

14-
.. literalinclude:: ../Examples/misc_examples.py
15-
:caption: Examples/misc_examples.py
14+
.. literalinclude:: ../XRPExamples/led_example.py
15+
:caption: XRPExamples/led_example.py
1616
:linenos:
1717

18-
.. literalinclude:: ../Examples/webserver_example.py
19-
:caption: Examples/webserver_example.py
18+
.. literalinclude:: ../XRPExamples/webserver_example.py
19+
:caption: XRPExamples/webserver_example.py
2020
:linenos:

package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
["XRPExamples/webserver_example.py", "github:Open-STEM/XRP_Micropython/Examples/webserver_example.py"]
2727
],
2828
"deps": [
29-
["github:pimoroni/phew", "latest"],
30-
["collections-defaultdict", "latest"],
31-
["os-path", "latest"]
29+
["github:pimoroni/phew", "latest"]
3230
],
3331
"version": "2.0.0"
3432
}

0 commit comments

Comments
 (0)