pybricks.hubs.EssentialHub: Added missing Hub

This commit is contained in:
Pavel Lobodinský
2022-06-29 12:08:33 -05:00
committed by David Lechner
parent e467bb57c9
commit 025147680c
28 changed files with 407 additions and 14 deletions
+5
View File
@@ -2,6 +2,11 @@
<!-- refer to https://keepachangelog.com/en/1.0.0/ for guidance -->
## Unreleased
### Added
- Code auto-completion for `EssentialHub`.
## 3.2.0b1-r3 - 2022-06-26
### Fixed
+147
View File
@@ -0,0 +1,147 @@
.. pybricks-requirements:: essentialhub
Essential Hub
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. figure:: ../../main/images/primehub.png
:height: 15 em
.. autoclass:: pybricks.hubs.EssentialHub
:no-members:
.. rubric:: Using the hub status light
.. figure:: ../../main/images/primehub_light_label.png
:width: 22 em
.. automethod:: pybricks.hubs::EssentialHub.light.on
.. automethod:: pybricks.hubs::EssentialHub.light.off
.. automethod:: pybricks.hubs::EssentialHub.light.blink
.. automethod:: pybricks.hubs::EssentialHub.light.animate
.. rubric:: Using the button
.. figure:: ../../main/images/primehub_buttons_label.png
:width: 22 em
.. automethod:: pybricks.hubs::EssentialHub.button.pressed
.. rubric:: Using the IMU
.. automethod:: pybricks.hubs::EssentialHub.imu.up
.. automethod:: pybricks.hubs::EssentialHub.imu.tilt
.. automethod:: pybricks.hubs::EssentialHub.imu.acceleration
.. automethod:: pybricks.hubs::EssentialHub.imu.angular_velocity
.. automethod:: pybricks.hubs::EssentialHub.imu.heading
.. automethod:: pybricks.hubs::EssentialHub.imu.reset_heading
.. rubric:: Using the battery
.. automethod:: pybricks.hubs::EssentialHub.battery.voltage
.. automethod:: pybricks.hubs::EssentialHub.battery.current
.. rubric:: Getting the charger status
.. automethod:: pybricks.hubs::EssentialHub.charger.connected
.. automethod:: pybricks.hubs::EssentialHub.charger.current
.. automethod:: pybricks.hubs::EssentialHub.charger.status
.. rubric:: System control
.. automethod:: pybricks.hubs::EssentialHub.system.set_stop_button
.. automethod:: pybricks.hubs::EssentialHub.system.name
.. automethod:: pybricks.hubs::EssentialHub.system.shutdown
.. automethod:: pybricks.hubs::EssentialHub.system.reset_reason
Status light examples
---------------------
Turning the light on and off
****************************
.. literalinclude::
../../../examples/pup/hub_essentialhub/light_off.py
Changing brightness and using custom colors
*******************************************
.. literalinclude::
../../../examples/pup/hub_essentialhub/light_hsv.py
Making the light blink
**********************
.. literalinclude::
../../../examples/pup/hub_essentialhub/light_blink.py
Creating light animations
*************************
.. literalinclude::
../../../examples/pup/hub_essentialhub/light_animate.py
Button examples
---------------
Detecting button presses
************************
.. literalinclude::
../../../examples/pup/hub_essentialhub/button_single.py
IMU examples
---------------
Testing which way is up
********************************
.. literalinclude::
../../../examples/pup/hub_essentialhub/imu_up.py
Reading the tilt value
********************************
.. literalinclude::
../../../examples/pup/hub_essentialhub/imu_tilt.py
Using a custom hub orientation
**************************************************
.. literalinclude::
../../../examples/pup/hub_essentialhub/imu_tilt_blast.py
Reading acceleration and angular velocity vectors
**************************************************
.. literalinclude::
../../../examples/pup/hub_essentialhub/imu_read_vector.py
Reading acceleration and angular velocity on one axis
*****************************************************
.. literalinclude::
../../../examples/pup/hub_essentialhub/imu_read_scalar.py
System examples
----------------------------------
Turning the hub off
*****************************************
.. literalinclude::
../../../examples/pup/hub_essentialhub/system_shutdown.py
+7
View File
@@ -12,6 +12,7 @@
movehub
cityhub
technichub
essentialhub
primehub
.. pybricks-classlink:: MoveHub
@@ -32,6 +33,12 @@
:height: 10 em
:target: technichub.html
.. pybricks-classlink:: EssentialHub
.. figure:: ../../main/images/primehub.png
:height: 10 em
:target: essentialhub.html
.. pybricks-classlink:: PrimeHub
.. figure:: ../../main/images/primehub.png
+2 -2
View File
@@ -28,8 +28,8 @@ Pybricks Documentation
.. list-table::
* - **Technic Hub, City Hub, BOOST Move Hub**
- **SPIKE Prime Hub / MINDSTORMS Inventor Hub**
* - **Technic, City, and BOOST Move Hubs**
- **SPIKE Essential/Prime, and MINDSTORMS Inventor Hubs**
* - .. figure:: ../main/images/powereduphubs.png
:target: https://pybricks.com/install/technic-boost-city/
@@ -0,0 +1,25 @@
from pybricks.hubs import EssentialHub
from pybricks.parameters import Color, Button
from pybricks.tools import wait, StopWatch
# Initialize the hub.
hub = EssentialHub()
# Disable the stop button.
hub.system.set_stop_button(None)
# Check the button for 5 seconds.
watch = StopWatch()
while watch.time() < 5000:
# Set light to green if pressed, else red.
if hub.button.pressed():
hub.light.on(Color.GREEN)
else:
hub.light.on(Color.RED)
# Enable the stop button again.
hub.system.set_stop_button(Button.CENTER)
# Now you can press the stop button as usual.
wait(5000)
@@ -0,0 +1,20 @@
from pybricks.hubs import EssentialHub
from pybricks.tools import wait
from pybricks.geometry import Axis
# Initialize the hub.
hub = EssentialHub()
# Get the acceleration or angular_velocity along a single axis.
# If you need only one value, this is more memory efficient.
while True:
# Read the forward acceleration.
forward_acceleration = hub.imu.acceleration(Axis.X)
# Read the yaw rate.
yaw_rate = hub.imu.angular_velocity(Axis.Z)
# Print the yaw rate.
print(yaw_rate)
wait(100)
@@ -0,0 +1,14 @@
from pybricks.hubs import EssentialHub
from pybricks.tools import wait
# Initialize the hub.
hub = EssentialHub()
# Get the acceleration vector.
print(hub.imu.acceleration())
# Get the angular velocity vector.
print(hub.imu.angular_velocity())
# Wait so we can see what we printed
wait(5000)
+13
View File
@@ -0,0 +1,13 @@
from pybricks.hubs import EssentialHub
from pybricks.tools import wait
# Initialize the hub.
hub = EssentialHub()
while True:
# Read the tilt values.
pitch, roll = hub.imu.tilt()
# Print the result.
print(pitch, roll)
wait(200)
@@ -0,0 +1,17 @@
from pybricks.hubs import EssentialHub
from pybricks.tools import wait
from pybricks.geometry import Axis
# Initialize the hub. In this case, specify that the hub is mounted with the
# top side facing forward and the front side facing to the right.
# For example, this is how the hub is mounted in BLAST in the 51515 set.
hub = EssentialHub(top_side=Axis.X, front_side=-Axis.Y)
while True:
# Read the tilt values. Now, the values are 0 when BLAST stands upright.
# Leaning forward gives positive pitch. Leaning right gives positive roll.
pitch, roll = hub.imu.tilt()
# Print the result.
print(pitch, roll)
wait(200)
+29
View File
@@ -0,0 +1,29 @@
from pybricks.hubs import EssentialHub
from pybricks.parameters import Color, Side
from pybricks.tools import wait
# Initialize the hub.
hub = EssentialHub()
# Define colors for each side in a dictionary.
SIDE_COLORS = {
Side.TOP: Color.RED,
Side.BOTTOM: Color.BLUE,
Side.LEFT: Color.GREEN,
Side.RIGHT: Color.YELLOW,
Side.FRONT: Color.MAGENTA,
Side.BACK: Color.BLACK,
}
# Keep updating the color based on detected up side.
while True:
# Check which side of the hub is up.
up_side = hub.imu.up()
# Change the color based on the side.
hub.light.on(SIDE_COLORS[up_side])
# Also print the result.
print(up_side)
wait(50)
@@ -0,0 +1,23 @@
from pybricks.hubs import EssentialHub
from pybricks.parameters import Color
from pybricks.tools import wait
from umath import sin, pi
# Initialize the hub.
hub = EssentialHub()
# Make an animation with multiple colors.
hub.light.animate([Color.RED, Color.GREEN, Color.NONE], interval=500)
wait(10000)
# Make the color RED grow faint and bright using a sine pattern.
hub.light.animate(
[Color.RED * (0.5 * sin(i / 15 * pi) + 0.5) for i in range(30)], 40)
wait(10000)
# Cycle through a rainbow of colors.
hub.light.animate([Color(h=i*8) for i in range(45)], interval=40)
wait(10000)
@@ -0,0 +1,16 @@
from pybricks.hubs import EssentialHub
from pybricks.parameters import Color
from pybricks.tools import wait
# Initialize the hub
hub = EssentialHub()
# Keep blinking red on and off.
hub.light.blink(Color.RED, [500, 500])
wait(10000)
# Keep blinking green slowly and then quickly.
hub.light.blink(Color.GREEN, [500, 500, 50, 900])
wait(10000)
@@ -0,0 +1,21 @@
from pybricks.hubs import EssentialHub
from pybricks.parameters import Color
from pybricks.tools import wait
# Initialize the hub.
hub = EssentialHub()
# Show the color at 30% brightness.
hub.light.on(Color.RED * 0.3)
wait(2000)
# Use your own custom color.
hub.light.on(Color(h=30, s=100, v=50))
wait(2000)
# Go through all the colors.
for hue in range(360):
hub.light.on(Color(hue))
wait(10)
@@ -0,0 +1,15 @@
from pybricks.hubs import EssentialHub
from pybricks.parameters import Color
from pybricks.tools import wait
# Initialize the hub.
hub = EssentialHub()
# Turn the light on and off 5 times.
for i in range(5):
hub.light.on(Color.RED)
wait(1000)
hub.light.off()
wait(500)
@@ -0,0 +1,12 @@
from pybricks.hubs import EssentialHub
from pybricks.tools import wait
# Initialize the hub.
hub = EssentialHub()
# Say goodbye and give some time to send it.
print("Goodbye!")
wait(100)
# Shut the hub down.
hub.system.shutdown()
+1 -1
View File
@@ -1,4 +1,4 @@
# ExampleHub = MoveHub CityHub TechnicHub
# ExampleHub = MoveHub CityHub TechnicHub EssentialHub
from pybricks.hubs import ExampleHub
from pybricks.parameters import Color, Button
from pybricks.tools import wait, StopWatch
+1 -1
View File
@@ -1,4 +1,4 @@
# ExampleHub = TechnicHub PrimeHub
# ExampleHub = TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ExampleHub
from pybricks.tools import wait
from pybricks.geometry import Axis
+1 -1
View File
@@ -1,4 +1,4 @@
# ExampleHub = TechnicHub PrimeHub
# ExampleHub = TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ExampleHub
from pybricks.tools import wait
+1 -1
View File
@@ -1,4 +1,4 @@
# ExampleHub = TechnicHub PrimeHub
# ExampleHub = TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ExampleHub
from pybricks.tools import wait
+1 -1
View File
@@ -1,4 +1,4 @@
# ExampleHub = TechnicHub PrimeHub
# ExampleHub = TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ExampleHub
from pybricks.tools import wait
from pybricks.geometry import Axis
+1 -1
View File
@@ -1,4 +1,4 @@
# ExampleHub = TechnicHub PrimeHub MoveHub
# ExampleHub = TechnicHub PrimeHub MoveHub EssentialHub
from pybricks.hubs import ExampleHub
from pybricks.parameters import Color, Side
from pybricks.tools import wait
+1 -1
View File
@@ -1,4 +1,4 @@
# ExampleHub = CityHub TechnicHub PrimeHub
# ExampleHub = CityHub TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ExampleHub
from pybricks.parameters import Color
from pybricks.tools import wait
+1 -1
View File
@@ -1,4 +1,4 @@
# ExampleHub = MoveHub CityHub TechnicHub PrimeHub
# ExampleHub = MoveHub CityHub TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ExampleHub
from pybricks.parameters import Color
from pybricks.tools import wait
+1 -1
View File
@@ -1,4 +1,4 @@
# ExampleHub = CityHub TechnicHub PrimeHub
# ExampleHub = CityHub TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ExampleHub
from pybricks.parameters import Color
from pybricks.tools import wait
+1 -1
View File
@@ -1,4 +1,4 @@
# ExampleHub = MoveHub CityHub TechnicHub PrimeHub
# ExampleHub = MoveHub CityHub TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ExampleHub
from pybricks.parameters import Color
from pybricks.tools import wait
View File
+1 -1
View File
@@ -1,4 +1,4 @@
# ExampleHub = MoveHub CityHub TechnicHub PrimeHub
# ExampleHub = MoveHub CityHub TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ExampleHub
from pybricks.tools import wait
+30 -1
View File
@@ -79,8 +79,37 @@ class TechnicHub:
"""
class EssentialHub:
"""LEGO® SPIKE Essential Hub."""
# These class attributes are here for auto-documentation only.
# In reality, they are instance attributes created by __init__.
battery = _Battery()
button = _Keypad((_Button.CENTER,))
charger = _Charger()
light = _ColorLight()
imu = _IMU()
system = _System()
def __init__(self, top_side=_Axis.Z, front_side=_Axis.X):
"""__init__(top_side=Axis.Z, front_side=Axis.X)
Initializes the hub. Optionally, specify how the hub is
:ref:`placed in your design <robotframe>` by saying in which
direction the top side (with the button) and the front side (with the USB
port, and I/O ports A and B) are pointing.
Arguments:
top_side (Axis): The axis that passes through the *top side* of
the hub.
front_side (Axis): The axis that passes through the *front side* of
the hub.
"""
pass
class PrimeHub:
"""LEGO® SPIKE Prime Hub or LEGO® MINDSTORMS Inventor Hub."""
"""LEGO® SPIKE Prime Hub."""
# These class attributes are here for auto-documentation only.
# In reality, they are instance attributes created by __init__.