examples: Generate hub examples while building.

This way we don't have to commit the generated scripts.

While we do this, rename ExampleHub to ThisHub so that the common scripts can actually run.
This commit is contained in:
Laurens Valk
2022-07-04 14:29:33 +02:00
parent 9d706d719a
commit 1b51d71061
62 changed files with 121 additions and 903 deletions
+5
View File
@@ -4,6 +4,7 @@
# Pybricks documentation build configuration file
#
import os
import sys
# General information about the project.
project = "pybricks"
@@ -50,3 +51,7 @@ if "ide" in tags.tags: # noqa F821
extensions.remove("sphinx.ext.mathjax") # noqa F821
extensions.append("sphinx.ext.imgmath") # noqa F821
html_theme_options["prev_next_buttons_location"] = None # noqa F821
# Build hub specific example scripts.
sys.path.append(os.path.abspath("../../examples/pup/hub_common"))
import make_examples # noqa F401, E402
+6 -6
View File
@@ -44,25 +44,25 @@ Turning the light on and off
****************************
.. literalinclude::
../../../examples/pup/hub_cityhub/light_off.py
../../../examples/pup/hub_common/build/light_off_cityhub.py
Changing brightness and using custom colors
*******************************************
.. literalinclude::
../../../examples/pup/hub_cityhub/light_hsv.py
../../../examples/pup/hub_common/build/light_hsv_cityhub.py
Making the light blink
**********************
.. literalinclude::
../../../examples/pup/hub_cityhub/light_blink.py
../../../examples/pup/hub_common/build/light_blink_cityhub.py
Creating light animations
*************************
.. literalinclude::
../../../examples/pup/hub_cityhub/light_animate.py
../../../examples/pup/hub_common/build/light_animate_cityhub.py
Button and system examples
----------------------------------
@@ -71,10 +71,10 @@ Using the stop button during your program
*****************************************
.. literalinclude::
../../../examples/pup/hub_cityhub/button_single.py
../../../examples/pup/hub_common/build/button_single_cityhub.py
Turning the hub off
*****************************************
.. literalinclude::
../../../examples/pup/hub_cityhub/system_shutdown.py
../../../examples/pup/hub_common/build/system_shutdown_cityhub.py
+12 -12
View File
@@ -79,25 +79,25 @@ Turning the light on and off
****************************
.. literalinclude::
../../../examples/pup/hub_essentialhub/light_off.py
../../../examples/pup/hub_common/build/light_off_essentialhub.py
Changing brightness and using custom colors
*******************************************
.. literalinclude::
../../../examples/pup/hub_essentialhub/light_hsv.py
../../../examples/pup/hub_common/build/light_hsv_essentialhub.py
Making the light blink
**********************
.. literalinclude::
../../../examples/pup/hub_essentialhub/light_blink.py
../../../examples/pup/hub_common/build/light_blink_essentialhub.py
Creating light animations
*************************
.. literalinclude::
../../../examples/pup/hub_essentialhub/light_animate.py
../../../examples/pup/hub_common/build/light_animate_essentialhub.py
Button examples
---------------
@@ -106,7 +106,7 @@ Detecting button presses
************************
.. literalinclude::
../../../examples/pup/hub_essentialhub/button_single.py
../../../examples/pup/hub_common/build/button_single_essentialhub.py
IMU examples
---------------
@@ -115,32 +115,32 @@ Testing which way is up
********************************
.. literalinclude::
../../../examples/pup/hub_essentialhub/imu_up.py
../../../examples/pup/hub_common/build/imu_up_essentialhub.py
Reading the tilt value
********************************
.. literalinclude::
../../../examples/pup/hub_essentialhub/imu_tilt.py
../../../examples/pup/hub_common/build/imu_tilt_essentialhub.py
Using a custom hub orientation
**************************************************
.. literalinclude::
../../../examples/pup/hub_essentialhub/imu_tilt_blast.py
../../../examples/pup/hub_common/build/imu_tilt_blast_essentialhub.py
Reading acceleration and angular velocity vectors
**************************************************
.. literalinclude::
../../../examples/pup/hub_essentialhub/imu_read_vector.py
../../../examples/pup/hub_common/build/imu_read_vector_essentialhub.py
Reading acceleration and angular velocity on one axis
*****************************************************
.. literalinclude::
../../../examples/pup/hub_essentialhub/imu_read_scalar.py
../../../examples/pup/hub_common/build/imu_read_scalar_essentialhub.py
System examples
----------------------------------
@@ -149,10 +149,10 @@ Using the stop button during your program
*****************************************
.. literalinclude::
../../../examples/pup/hub_essentialhub/button_single.py
../../../examples/pup/hub_common/build/button_single_essentialhub.py
Turning the hub off
*****************************************
.. literalinclude::
../../../examples/pup/hub_essentialhub/system_shutdown.py
../../../examples/pup/hub_common/build/system_shutdown_essentialhub.py
+5 -5
View File
@@ -56,13 +56,13 @@ Turning the light on and off
******************************
.. literalinclude::
../../../examples/pup/hub_movehub/light_off.py
../../../examples/pup/hub_common/build/light_off_movehub.py
Making the light blink
******************************
.. literalinclude::
../../../examples/pup/hub_movehub/light_blink.py
../../../examples/pup/hub_common/build/light_blink_movehub.py
IMU examples
---------------
@@ -71,7 +71,7 @@ Testing which way is up
********************************
.. literalinclude::
../../../examples/pup/hub_movehub/imu_up.py
../../../examples/pup/hub_common/build/imu_up_movehub.py
Reading acceleration
**************************************************
@@ -86,10 +86,10 @@ Using the stop button during your program
*****************************************
.. literalinclude::
../../../examples/pup/hub_movehub/button_single.py
../../../examples/pup/hub_common/build/button_single_movehub.py
Turning the hub off
*****************************************
.. literalinclude::
../../../examples/pup/hub_movehub/system_shutdown.py
../../../examples/pup/hub_common/build/system_shutdown_movehub.py
+10 -10
View File
@@ -119,25 +119,25 @@ Turning the light on and off
****************************
.. literalinclude::
../../../examples/pup/hub_primehub/light_off.py
../../../examples/pup/hub_common/build/light_off_primehub.py
Changing brightness and using custom colors
*******************************************
.. literalinclude::
../../../examples/pup/hub_primehub/light_hsv.py
../../../examples/pup/hub_common/build/light_hsv_primehub.py
Making the light blink
**********************
.. literalinclude::
../../../examples/pup/hub_primehub/light_blink.py
../../../examples/pup/hub_common/build/light_blink_primehub.py
Creating light animations
*************************
.. literalinclude::
../../../examples/pup/hub_primehub/light_animate.py
../../../examples/pup/hub_common/build/light_animate_primehub.py
Matrix display examples
-----------------------
@@ -209,32 +209,32 @@ Testing which way is up
********************************
.. literalinclude::
../../../examples/pup/hub_primehub/imu_up.py
../../../examples/pup/hub_common/build/imu_up_primehub.py
Reading the tilt value
********************************
.. literalinclude::
../../../examples/pup/hub_primehub/imu_tilt.py
../../../examples/pup/hub_common/build/imu_tilt_primehub.py
Using a custom hub orientation
**************************************************
.. literalinclude::
../../../examples/pup/hub_primehub/imu_tilt_blast.py
../../../examples/pup/hub_common/build/imu_tilt_blast_primehub.py
Reading acceleration and angular velocity vectors
**************************************************
.. literalinclude::
../../../examples/pup/hub_primehub/imu_read_vector.py
../../../examples/pup/hub_common/build/imu_read_vector_primehub.py
Reading acceleration and angular velocity on one axis
*****************************************************
.. literalinclude::
../../../examples/pup/hub_primehub/imu_read_scalar.py
../../../examples/pup/hub_common/build/imu_read_scalar_primehub.py
System examples
----------------------------------
@@ -249,7 +249,7 @@ Turning the hub off
*****************************************
.. literalinclude::
../../../examples/pup/hub_primehub/system_shutdown.py
../../../examples/pup/hub_common/build/system_shutdown_primehub.py
.. _installation instructions: https://pybricks.com/install/
+11 -11
View File
@@ -58,25 +58,25 @@ Turning the light on and off
****************************
.. literalinclude::
../../../examples/pup/hub_technichub/light_off.py
../../../examples/pup/hub_common/build/light_off_technichub.py
Changing brightness and using custom colors
*******************************************
.. literalinclude::
../../../examples/pup/hub_technichub/light_hsv.py
../../../examples/pup/hub_common/build/light_hsv_technichub.py
Making the light blink
**********************
.. literalinclude::
../../../examples/pup/hub_technichub/light_blink.py
../../../examples/pup/hub_common/build/light_blink_technichub.py
Creating light animations
*************************
.. literalinclude::
../../../examples/pup/hub_technichub/light_animate.py
../../../examples/pup/hub_common/build/light_animate_technichub.py
IMU examples
---------------
@@ -85,32 +85,32 @@ Testing which way is up
********************************
.. literalinclude::
../../../examples/pup/hub_technichub/imu_up.py
../../../examples/pup/hub_common/build/imu_up_technichub.py
Reading the tilt value
********************************
.. literalinclude::
../../../examples/pup/hub_technichub/imu_tilt.py
../../../examples/pup/hub_common/build/imu_tilt_technichub.py
Using a custom hub orientation
**************************************************
.. literalinclude::
../../../examples/pup/hub_technichub/imu_tilt_blast.py
../../../examples/pup/hub_common/build/imu_tilt_blast_technichub.py
Reading acceleration and angular velocity vectors
**************************************************
.. literalinclude::
../../../examples/pup/hub_technichub/imu_read_vector.py
../../../examples/pup/hub_common/build/imu_read_vector_technichub.py
Reading acceleration and angular velocity on one axis
*****************************************************
.. literalinclude::
../../../examples/pup/hub_technichub/imu_read_scalar.py
../../../examples/pup/hub_common/build/imu_read_scalar_technichub.py
Button and system examples
----------------------------------
@@ -119,10 +119,10 @@ Using the stop button during your program
*****************************************
.. literalinclude::
../../../examples/pup/hub_technichub/button_single.py
../../../examples/pup/hub_common/build/button_single_technichub.py
Turning the hub off
*****************************************
.. literalinclude::
../../../examples/pup/hub_technichub/system_shutdown.py
../../../examples/pup/hub_common/build/system_shutdown_technichub.py
-22
View File
@@ -1,22 +0,0 @@
from pybricks.hubs import CityHub
from pybricks.parameters import Color
from pybricks.tools import wait
from umath import sin, pi
# Initialize the hub.
hub = CityHub()
# 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)
-16
View File
@@ -1,16 +0,0 @@
from pybricks.hubs import CityHub
from pybricks.parameters import Color
from pybricks.tools import wait
# Initialize the hub
hub = CityHub()
# 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)
-21
View File
@@ -1,21 +0,0 @@
from pybricks.hubs import CityHub
from pybricks.parameters import Color
from pybricks.tools import wait
# Initialize the hub.
hub = CityHub()
# 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)
@@ -1,12 +0,0 @@
from pybricks.hubs import CityHub
from pybricks.tools import wait
# Initialize the hub.
hub = CityHub()
# Say goodbye and give some time to send it.
print("Goodbye!")
wait(100)
# Shut the hub down.
hub.system.shutdown()
@@ -1,9 +1,10 @@
from pybricks.hubs import CityHub
# ThisHub = MoveHub CityHub TechnicHub EssentialHub
from pybricks.hubs import ThisHub
from pybricks.parameters import Color, Button
from pybricks.tools import wait, StopWatch
# Initialize the hub.
hub = CityHub()
hub = ThisHub()
# Disable the stop button.
hub.system.set_stop_button(None)
@@ -1,9 +1,10 @@
from pybricks.hubs import PrimeHub
# ThisHub = TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ThisHub
from pybricks.tools import wait
from pybricks.geometry import Axis
# Initialize the hub.
hub = PrimeHub()
hub = ThisHub()
# Get the acceleration or angular_velocity along a single axis.
# If you need only one value, this is more memory efficient.
@@ -1,8 +1,9 @@
from pybricks.hubs import PrimeHub
# ThisHub = TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ThisHub
from pybricks.tools import wait
# Initialize the hub.
hub = PrimeHub()
hub = ThisHub()
# Get the acceleration vector in g's.
print(hub.imu.acceleration() / 9810)
@@ -1,8 +1,9 @@
from pybricks.hubs import PrimeHub
# ThisHub = TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ThisHub
from pybricks.tools import wait
# Initialize the hub.
hub = PrimeHub()
hub = ThisHub()
while True:
# Read the tilt values.
@@ -1,11 +1,12 @@
from pybricks.hubs import TechnicHub
# ThisHub = TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ThisHub
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 = TechnicHub(top_side=Axis.X, front_side=-Axis.Y)
hub = ThisHub(top_side=Axis.X, front_side=-Axis.Y)
while True:
# Read the tilt values. Now, the values are 0 when BLAST stands upright.
@@ -1,9 +1,10 @@
from pybricks.hubs import PrimeHub
# ThisHub = TechnicHub PrimeHub MoveHub EssentialHub
from pybricks.hubs import ThisHub
from pybricks.parameters import Color, Side
from pybricks.tools import wait
# Initialize the hub.
hub = PrimeHub()
hub = ThisHub()
# Define colors for each side in a dictionary.
SIDE_COLORS = {
@@ -1,10 +1,11 @@
from pybricks.hubs import EssentialHub
# ThisHub = CityHub TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ThisHub
from pybricks.parameters import Color
from pybricks.tools import wait
from umath import sin, pi
# Initialize the hub.
hub = EssentialHub()
hub = ThisHub()
# Make an animation with multiple colors.
hub.light.animate([Color.RED, Color.GREEN, Color.NONE], interval=500)
@@ -1,9 +1,10 @@
from pybricks.hubs import MoveHub
# ThisHub = MoveHub CityHub TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ThisHub
from pybricks.parameters import Color
from pybricks.tools import wait
# Initialize the hub
hub = MoveHub()
hub = ThisHub()
# Keep blinking red on and off.
hub.light.blink(Color.RED, [500, 500])
@@ -1,9 +1,10 @@
from pybricks.hubs import PrimeHub
# ThisHub = CityHub TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ThisHub
from pybricks.parameters import Color
from pybricks.tools import wait
# Initialize the hub.
hub = PrimeHub()
hub = ThisHub()
# Show the color at 30% brightness.
hub.light.on(Color.RED * 0.3)
@@ -1,9 +1,10 @@
from pybricks.hubs import CityHub
# ThisHub = MoveHub CityHub TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ThisHub
from pybricks.parameters import Color
from pybricks.tools import wait
# Initialize the hub.
hub = CityHub()
hub = ThisHub()
# Turn the light on and off 5 times.
for i in range(5):
+39
View File
@@ -0,0 +1,39 @@
#!/usr/bin/env python3
"""Generates hub-specific examples from common template script."""
import pathlib
# Make build directory.
dir_path = pathlib.Path(__file__).parent
build_path = dir_path / "build"
build_path.mkdir(exist_ok=True)
# Get list of scripts to be parsed.
file_paths = [f for f in dir_path.glob("*.py") if f.stem != "make_examples"]
# Go through all template scripts
for file_path in file_paths:
with open(file_path) as template:
# First line contains hub info
hubs = template.readline().strip().split()[3:]
print("Converting", template.name, "to", hubs)
for hub in hubs:
# Path to hub-specific output script.
gen_path = build_path / (file_path.stem + "_" + hub.lower() + ".py")
# Reset source script and skip over header.
template.seek(0)
template.readline()
# Open destination script:
with open(gen_path, "w") as dest_file:
# Read script line by line.
for line in template.readlines():
# Replace hub name if present.
dest_file.writelines(line.replace("ThisHub", hub))
@@ -1,8 +1,9 @@
from pybricks.hubs import MoveHub
# ThisHub = MoveHub CityHub TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ThisHub
from pybricks.tools import wait
# Initialize the hub.
hub = MoveHub()
hub = ThisHub()
# Say goodbye and give some time to send it.
print("Goodbye!")
@@ -1,25 +0,0 @@
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)
@@ -1,20 +0,0 @@
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)
@@ -1,14 +0,0 @@
from pybricks.hubs import EssentialHub
from pybricks.tools import wait
# Initialize the hub.
hub = EssentialHub()
# Get the acceleration vector in g's.
print(hub.imu.acceleration() / 9810)
# Get the angular velocity vector.
print(hub.imu.angular_velocity())
# Wait so we can see what we printed
wait(5000)
-13
View File
@@ -1,13 +0,0 @@
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)
@@ -1,17 +0,0 @@
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
@@ -1,29 +0,0 @@
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)
@@ -1,16 +0,0 @@
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)
@@ -1,21 +0,0 @@
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)
@@ -1,15 +0,0 @@
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)
@@ -1,12 +0,0 @@
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()
-25
View File
@@ -1,25 +0,0 @@
from pybricks.hubs import MoveHub
from pybricks.parameters import Color, Button
from pybricks.tools import wait, StopWatch
# Initialize the hub.
hub = MoveHub()
# 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)
-29
View File
@@ -1,29 +0,0 @@
from pybricks.hubs import MoveHub
from pybricks.parameters import Color, Side
from pybricks.tools import wait
# Initialize the hub.
hub = MoveHub()
# 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)
-15
View File
@@ -1,15 +0,0 @@
from pybricks.hubs import MoveHub
from pybricks.parameters import Color
from pybricks.tools import wait
# Initialize the hub.
hub = MoveHub()
# 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)
@@ -1,17 +0,0 @@
from pybricks.hubs import PrimeHub
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 = PrimeHub(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)
@@ -1,22 +0,0 @@
from pybricks.hubs import PrimeHub
from pybricks.parameters import Color
from pybricks.tools import wait
from umath import sin, pi
# Initialize the hub.
hub = PrimeHub()
# 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)
-16
View File
@@ -1,16 +0,0 @@
from pybricks.hubs import PrimeHub
from pybricks.parameters import Color
from pybricks.tools import wait
# Initialize the hub
hub = PrimeHub()
# 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)
-15
View File
@@ -1,15 +0,0 @@
from pybricks.hubs import PrimeHub
from pybricks.parameters import Color
from pybricks.tools import wait
# Initialize the hub.
hub = PrimeHub()
# 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)
@@ -1,12 +0,0 @@
from pybricks.hubs import PrimeHub
from pybricks.tools import wait
# Initialize the hub.
hub = PrimeHub()
# Say goodbye and give some time to send it.
print("Goodbye!")
wait(100)
# Shut the hub down.
hub.system.shutdown()
-26
View File
@@ -1,26 +0,0 @@
# ExampleHub = MoveHub CityHub TechnicHub EssentialHub
from pybricks.hubs import ExampleHub
from pybricks.parameters import Color, Button
from pybricks.tools import wait, StopWatch
# Initialize the hub.
hub = ExampleHub()
# 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)
@@ -1,21 +0,0 @@
# ExampleHub = TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ExampleHub
from pybricks.tools import wait
from pybricks.geometry import Axis
# Initialize the hub.
hub = ExampleHub()
# 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)
@@ -1,15 +0,0 @@
# ExampleHub = TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ExampleHub
from pybricks.tools import wait
# Initialize the hub.
hub = ExampleHub()
# Get the acceleration vector in g's.
print(hub.imu.acceleration() / 9810)
# Get the angular velocity vector.
print(hub.imu.angular_velocity())
# Wait so we can see what we printed
wait(5000)
-14
View File
@@ -1,14 +0,0 @@
# ExampleHub = TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ExampleHub
from pybricks.tools import wait
# Initialize the hub.
hub = ExampleHub()
while True:
# Read the tilt values.
pitch, roll = hub.imu.tilt()
# Print the result.
print(pitch, roll)
wait(200)
-18
View File
@@ -1,18 +0,0 @@
# ExampleHub = TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ExampleHub
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 = ExampleHub(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)
-30
View File
@@ -1,30 +0,0 @@
# ExampleHub = TechnicHub PrimeHub MoveHub EssentialHub
from pybricks.hubs import ExampleHub
from pybricks.parameters import Color, Side
from pybricks.tools import wait
# Initialize the hub.
hub = ExampleHub()
# 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)
-23
View File
@@ -1,23 +0,0 @@
# ExampleHub = CityHub TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ExampleHub
from pybricks.parameters import Color
from pybricks.tools import wait
from umath import sin, pi
# Initialize the hub.
hub = ExampleHub()
# 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)
-17
View File
@@ -1,17 +0,0 @@
# ExampleHub = MoveHub CityHub TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ExampleHub
from pybricks.parameters import Color
from pybricks.tools import wait
# Initialize the hub
hub = ExampleHub()
# 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)
-22
View File
@@ -1,22 +0,0 @@
# ExampleHub = CityHub TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ExampleHub
from pybricks.parameters import Color
from pybricks.tools import wait
# Initialize the hub.
hub = ExampleHub()
# 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)
-16
View File
@@ -1,16 +0,0 @@
# ExampleHub = MoveHub CityHub TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ExampleHub
from pybricks.parameters import Color
from pybricks.tools import wait
# Initialize the hub.
hub = ExampleHub()
# 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)
@@ -1,31 +0,0 @@
#!/usr/bin/env python3
"""Replaces all instances of ExampleHub in template with actual hub names."""
import os
# Get list of scripts to be parsed
script_names = [f for f in os.listdir(".") if f != "make_shared_examples.py"]
# Go through all template scripts
for script in (open(f, "r") for f in script_names):
# First line contains hub info
hubs = script.readline().strip().split()[3:]
print("Converting", script.name, "for", hubs)
for hub in hubs:
# Determine path to the hub
hub_path = os.path.join("..", "hub_" + hub.lower())
# Reset source script
script.seek(0)
script.readline()
# Open destination script:
with open(os.path.join(hub_path, script.name), "w") as dest_file:
# Read script line by line
for line in script.readlines():
# Replace hub name if present
dest_file.writelines(line.replace("ExampleHub", hub))
@@ -1,13 +0,0 @@
# ExampleHub = MoveHub CityHub TechnicHub PrimeHub EssentialHub
from pybricks.hubs import ExampleHub
from pybricks.tools import wait
# Initialize the hub.
hub = ExampleHub()
# Say goodbye and give some time to send it.
print("Goodbye!")
wait(100)
# Shut the hub down.
hub.system.shutdown()
@@ -1,25 +0,0 @@
from pybricks.hubs import TechnicHub
from pybricks.parameters import Color, Button
from pybricks.tools import wait, StopWatch
# Initialize the hub.
hub = TechnicHub()
# 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)
@@ -1,20 +0,0 @@
from pybricks.hubs import TechnicHub
from pybricks.tools import wait
from pybricks.geometry import Axis
# Initialize the hub.
hub = TechnicHub()
# 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)
@@ -1,14 +0,0 @@
from pybricks.hubs import TechnicHub
from pybricks.tools import wait
# Initialize the hub.
hub = TechnicHub()
# Get the acceleration vector in g's.
print(hub.imu.acceleration() / 9810)
# Get the angular velocity vector.
print(hub.imu.angular_velocity())
# Wait so we can see what we printed
wait(5000)
-13
View File
@@ -1,13 +0,0 @@
from pybricks.hubs import TechnicHub
from pybricks.tools import wait
# Initialize the hub.
hub = TechnicHub()
while True:
# Read the tilt values.
pitch, roll = hub.imu.tilt()
# Print the result.
print(pitch, roll)
wait(200)
-29
View File
@@ -1,29 +0,0 @@
from pybricks.hubs import TechnicHub
from pybricks.parameters import Color, Side
from pybricks.tools import wait
# Initialize the hub.
hub = TechnicHub()
# 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)
@@ -1,22 +0,0 @@
from pybricks.hubs import TechnicHub
from pybricks.parameters import Color
from pybricks.tools import wait
from umath import sin, pi
# Initialize the hub.
hub = TechnicHub()
# 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)
@@ -1,16 +0,0 @@
from pybricks.hubs import TechnicHub
from pybricks.parameters import Color
from pybricks.tools import wait
# Initialize the hub
hub = TechnicHub()
# 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)
-21
View File
@@ -1,21 +0,0 @@
from pybricks.hubs import TechnicHub
from pybricks.parameters import Color
from pybricks.tools import wait
# Initialize the hub.
hub = TechnicHub()
# 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)
-15
View File
@@ -1,15 +0,0 @@
from pybricks.hubs import TechnicHub
from pybricks.parameters import Color
from pybricks.tools import wait
# Initialize the hub.
hub = TechnicHub()
# 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)
@@ -1,12 +0,0 @@
from pybricks.hubs import TechnicHub
from pybricks.tools import wait
# Initialize the hub.
hub = TechnicHub()
# Say goodbye and give some time to send it.
print("Goodbye!")
wait(100)
# Shut the hub down.
hub.system.shutdown()