From d6071427a0d7a1726f371afa91df2d2cae538ccb Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Mon, 20 Nov 2023 14:45:43 +0000 Subject: [PATCH] Added boilerplate --- examples/boilerplate.py | 98 +++++++++++++++++++ .../modules/bench_power/multiple_powers.py | 2 +- .../big_motor/all_motors_no_encoders.py | 2 +- examples/modules/big_motor/multiple_motors.py | 2 +- .../modules/big_motor/position_control.py | 2 +- .../big_motor/position_on_velocity_control.py | 2 +- examples/modules/big_motor/single_motor.py | 2 +- .../tuning/position_on_velocity_tuning.py | 2 +- .../big_motor/tuning/position_tuning.py | 2 +- .../big_motor/tuning/velocity_tuning.py | 2 +- .../modules/big_motor/velocity_control.py | 2 +- examples/modules/dual_motor/all_motors.py | 2 +- .../modules/dual_motor/multiple_motors.py | 2 +- examples/modules/dual_motor/two_motors.py | 2 +- .../modules/dual_output/multiple_outputs.py | 2 +- examples/modules/led_strip/multiple_strips.py | 2 +- examples/modules/led_strip/single_strip.py | 2 +- examples/modules/quad_servo/all_servos.py | 2 +- examples/modules/quad_servo/four_servos.py | 2 +- .../modules/quad_servo/multiple_servos.py | 2 +- examples/modules/serial_servo/all_servos.py | 2 +- examples/modules/serial_servo/drive_servo.py | 2 +- examples/modules/serial_servo/move_servo.py | 2 +- examples/showcase/rover/main.py | 2 +- examples/showcase/spidertank/main.py | 2 +- 25 files changed, 122 insertions(+), 24 deletions(-) create mode 100644 examples/boilerplate.py diff --git a/examples/boilerplate.py b/examples/boilerplate.py new file mode 100644 index 0000000..98efd49 --- /dev/null +++ b/examples/boilerplate.py @@ -0,0 +1,98 @@ +from pimoroni_yukon import Yukon + +# Perform system level imports here +# e.g. import math + +# Import any slots needed for your modules +# e.g. from pimoroni_yukon import SLOT1 as SLOT + +# Import any Yukon modules you are using +# e.g. from pimoroni_yukon.modules import DualMotorModule + +# Import the logging level to use (if you wish to change from the default) +# e.g. from pimoroni_yukon.logging import LOG_NONE, LOG_WARN, LOG_INFO, LOG_DEBUG + +from pimoroni_yukon.timing import ticks_ms, ticks_add # This import is only needed if using .monitor_until_ms() + +# Perform any other imports here +# e.g. from pimoroni_yukon.devices.stepper import OkayStepper + +""" +This is a boilerplate example for Yukon. Use it as a base for your own programs. + +Press "Boot/User" to exit the program. +""" + +# Constants +SLEEP_TIME = 0.1 + +# Place other constants here +# e.g. VOLTAGE_LIMIT = 12 + +# Variables +yukon = Yukon() # Create a new Yukon object. These optional keyword parameters are supported: + # `voltage_limit`, `current_limit`, `temperature_limit`, and `logging_level` + +# Create your module objects here +# e.g. module = DualMotorModule() + +# Place other variables here +# e.g. button_state = False + + +# Put any functions needed for your program here +# e.g. def my_function(): +# pass + + +# Wrap the code in a try block, to catch any exceptions (including KeyboardInterrupt) +try: + # Register your module with their respective slots + # e.g. yukon.register_with_slot(module, SLOT) + + # Verify that the correct modules are attached to Yukon, and initialise them + # This is not necessary if your program uses Yukon's IO directly, or via proto modules + # yukon.verify_and_initialise() + + # Set up any variables or objects that need initialised modules + # e.g. stepper = OkayStepper(module.motor1, module.motor2) + + yukon.enable_main_output() # Turn on power to the module slots + + # Enable any modules or objects + # e.g. module.enable() + + current_time = ticks_ms() # Record the start time of the program loop. Only needed if using .monitor_until_ms() + + # Loop until the BOOT/USER button is pressed + while not yukon.is_boot_pressed(): + + ####################### + # Put your program here + ####################### + + # Choose one of three options for monitoring Yukon's sensors + + # 1) Perform a single check of Yukon's internal voltage, current, and temperature sensors + # yukon.monitor_once() + + # 2) Monitor sensors for a number of seconds, recording the min, max, and average for each + # yukon.monitored_sleep(SLEEP_TIME) + + # 3) Advance the current time by a number of seconds + current_time = ticks_add(current_time, int(SLEEP_TIME * 1000)) + + # Monitor sensors until the current time is reached, recording the min, max, and average for each + # This approach accounts for the updates taking a non-zero amount of time to complete + yukon.monitor_until_ms(current_time) + + # Print out any readings from the monitoring period that may be useful + yukon.print_readings(allowed="Vi_avg") + +# Reset the yukon instance if the program completes successfully or an exception occurs +finally: + # Clean up any objects that hold on to hardware resources + # e.g. if stepper is not None: + # stepper.release() + + yukon.reset() diff --git a/examples/modules/bench_power/multiple_powers.py b/examples/modules/bench_power/multiple_powers.py index b394018..58c09c0 100644 --- a/examples/modules/bench_power/multiple_powers.py +++ b/examples/modules/bench_power/multiple_powers.py @@ -75,7 +75,7 @@ def voltage_from_index(index, offset=0.0): current_time = ticks_add(current_time, int(1000 / UPDATES)) # Monitor sensors until the current time is reached, recording the min, max, and average for each - # This approach accounts for the updates takinga non-zero amount of time to complete + # This approach accounts for the updates taking a non-zero amount of time to complete yukon.monitor_until_ms(current_time) finally: diff --git a/examples/modules/big_motor/all_motors_no_encoders.py b/examples/modules/big_motor/all_motors_no_encoders.py index c5afe5c..6fe6a35 100644 --- a/examples/modules/big_motor/all_motors_no_encoders.py +++ b/examples/modules/big_motor/all_motors_no_encoders.py @@ -84,7 +84,7 @@ def speed_from_index(index, offset=0.0): current_time = ticks_add(current_time, int(1000 / UPDATES)) # Monitor sensors until the current time is reached, recording the min, max, and average for each - # This approach accounts for the updates takinga non-zero amount of time to complete + # This approach accounts for the updates taking a non-zero amount of time to complete yukon.monitor_until_ms(current_time) finally: diff --git a/examples/modules/big_motor/multiple_motors.py b/examples/modules/big_motor/multiple_motors.py index 60fbf11..6afdcc1 100644 --- a/examples/modules/big_motor/multiple_motors.py +++ b/examples/modules/big_motor/multiple_motors.py @@ -93,7 +93,7 @@ def pio_and_sm_generator(): current_time = ticks_add(current_time, int(1000 / UPDATES)) # Monitor sensors until the current time is reached, recording the min, max, and average for each - # This approach accounts for the updates takinga non-zero amount of time to complete + # This approach accounts for the updates taking a non-zero amount of time to complete yukon.monitor_until_ms(current_time) finally: diff --git a/examples/modules/big_motor/position_control.py b/examples/modules/big_motor/position_control.py index 915df3a..ac1ef35 100644 --- a/examples/modules/big_motor/position_control.py +++ b/examples/modules/big_motor/position_control.py @@ -114,7 +114,7 @@ current_time = ticks_add(current_time, int(1000 * UPDATE_RATE)) # Monitor sensors until the current time is reached, recording the min, max, and average for each - # This approach accounts for the updates takinga non-zero amount of time to complete + # This approach accounts for the updates taking a non-zero amount of time to complete yukon.monitor_until_ms(current_time) module.motor.disable() # Disable the motor diff --git a/examples/modules/big_motor/position_on_velocity_control.py b/examples/modules/big_motor/position_on_velocity_control.py index db02915..d948dca 100644 --- a/examples/modules/big_motor/position_on_velocity_control.py +++ b/examples/modules/big_motor/position_on_velocity_control.py @@ -131,7 +131,7 @@ current_time = ticks_add(current_time, int(1000 * UPDATE_RATE)) # Monitor sensors until the current time is reached, recording the min, max, and average for each - # This approach accounts for the updates takinga non-zero amount of time to complete + # This approach accounts for the updates taking a non-zero amount of time to complete yukon.monitor_until_ms(current_time) module.motor.disable() # Disable the motor diff --git a/examples/modules/big_motor/single_motor.py b/examples/modules/big_motor/single_motor.py index 0b07404..3b0e9b4 100644 --- a/examples/modules/big_motor/single_motor.py +++ b/examples/modules/big_motor/single_motor.py @@ -59,7 +59,7 @@ current_time = ticks_add(current_time, int(1000 / UPDATES)) # Monitor sensors until the current time is reached, recording the min, max, and average for each - # This approach accounts for the updates takinga non-zero amount of time to complete + # This approach accounts for the updates taking a non-zero amount of time to complete yukon.monitor_until_ms(current_time) finally: diff --git a/examples/modules/big_motor/tuning/position_on_velocity_tuning.py b/examples/modules/big_motor/tuning/position_on_velocity_tuning.py index 1f72c60..a47392a 100644 --- a/examples/modules/big_motor/tuning/position_on_velocity_tuning.py +++ b/examples/modules/big_motor/tuning/position_on_velocity_tuning.py @@ -114,7 +114,7 @@ current_time = ticks_add(current_time, int(1000 * UPDATE_RATE)) # Monitor sensors until the current time is reached, recording the min, max, and average for each - # This approach accounts for the updates takinga non-zero amount of time to complete + # This approach accounts for the updates taking a non-zero amount of time to complete yukon.monitor_until_ms(current_time) module.motor.disable() # Disable the motor diff --git a/examples/modules/big_motor/tuning/position_tuning.py b/examples/modules/big_motor/tuning/position_tuning.py index d68d2d2..a2252b2 100644 --- a/examples/modules/big_motor/tuning/position_tuning.py +++ b/examples/modules/big_motor/tuning/position_tuning.py @@ -97,7 +97,7 @@ current_time = ticks_add(current_time, int(1000 * UPDATE_RATE)) # Monitor sensors until the current time is reached, recording the min, max, and average for each - # This approach accounts for the updates takinga non-zero amount of time to complete + # This approach accounts for the updates taking a non-zero amount of time to complete yukon.monitor_until_ms(current_time) module.motor.disable() # Disable the motor diff --git a/examples/modules/big_motor/tuning/velocity_tuning.py b/examples/modules/big_motor/tuning/velocity_tuning.py index 5911b7f..a7a37dc 100644 --- a/examples/modules/big_motor/tuning/velocity_tuning.py +++ b/examples/modules/big_motor/tuning/velocity_tuning.py @@ -99,7 +99,7 @@ current_time = ticks_add(current_time, int(1000 * UPDATE_RATE)) # Monitor sensors until the current time is reached, recording the min, max, and average for each - # This approach accounts for the updates takinga non-zero amount of time to complete + # This approach accounts for the updates taking a non-zero amount of time to complete yukon.monitor_until_ms(current_time) module.motor.disable() # Disable the motor diff --git a/examples/modules/big_motor/velocity_control.py b/examples/modules/big_motor/velocity_control.py index 2184a7a..496482f 100644 --- a/examples/modules/big_motor/velocity_control.py +++ b/examples/modules/big_motor/velocity_control.py @@ -116,7 +116,7 @@ current_time = ticks_add(current_time, int(1000 * UPDATE_RATE)) # Monitor sensors until the current time is reached, recording the min, max, and average for each - # This approach accounts for the updates takinga non-zero amount of time to complete + # This approach accounts for the updates taking a non-zero amount of time to complete yukon.monitor_until_ms(current_time) module.motor.disable() # Disable the motor diff --git a/examples/modules/dual_motor/all_motors.py b/examples/modules/dual_motor/all_motors.py index 907c065..3d61aa9 100644 --- a/examples/modules/dual_motor/all_motors.py +++ b/examples/modules/dual_motor/all_motors.py @@ -81,7 +81,7 @@ def speed_from_index(index, offset=0.0): current_time = ticks_add(current_time, int(1000 / UPDATES)) # Monitor sensors until the current time is reached, recording the min, max, and average for each - # This approach accounts for the updates takinga non-zero amount of time to complete + # This approach accounts for the updates taking a non-zero amount of time to complete yukon.monitor_until_ms(current_time) finally: diff --git a/examples/modules/dual_motor/multiple_motors.py b/examples/modules/dual_motor/multiple_motors.py index fecd460..b154b1c 100644 --- a/examples/modules/dual_motor/multiple_motors.py +++ b/examples/modules/dual_motor/multiple_motors.py @@ -75,7 +75,7 @@ def speed_from_index(index, offset=0.0): current_time = ticks_add(current_time, int(1000 / UPDATES)) # Monitor sensors until the current time is reached, recording the min, max, and average for each - # This approach accounts for the updates takinga non-zero amount of time to complete + # This approach accounts for the updates taking a non-zero amount of time to complete yukon.monitor_until_ms(current_time) finally: diff --git a/examples/modules/dual_motor/two_motors.py b/examples/modules/dual_motor/two_motors.py index 7c1236b..4bb45ed 100644 --- a/examples/modules/dual_motor/two_motors.py +++ b/examples/modules/dual_motor/two_motors.py @@ -62,7 +62,7 @@ def speed_from_index(index, offset=0.0): current_time = ticks_add(current_time, int(1000 / UPDATES)) # Monitor sensors until the current time is reached, recording the min, max, and average for each - # This approach accounts for the updates takinga non-zero amount of time to complete + # This approach accounts for the updates taking a non-zero amount of time to complete yukon.monitor_until_ms(current_time) finally: diff --git a/examples/modules/dual_output/multiple_outputs.py b/examples/modules/dual_output/multiple_outputs.py index 7bf7a31..0fb0246 100644 --- a/examples/modules/dual_output/multiple_outputs.py +++ b/examples/modules/dual_output/multiple_outputs.py @@ -73,7 +73,7 @@ def state_from_index(index, offset=0.0): current_time = ticks_add(current_time, int(1000 / UPDATES)) # Monitor sensors until the current time is reached, recording the min, max, and average for each - # This approach accounts for the updates takinga non-zero amount of time to complete + # This approach accounts for the updates taking a non-zero amount of time to complete yukon.monitor_until_ms(current_time) finally: diff --git a/examples/modules/led_strip/multiple_strips.py b/examples/modules/led_strip/multiple_strips.py index 8d184a2..7d1312f 100644 --- a/examples/modules/led_strip/multiple_strips.py +++ b/examples/modules/led_strip/multiple_strips.py @@ -99,7 +99,7 @@ def pio_and_sm_generator(): current_time = ticks_add(current_time, int(SLEEP * 1000)) # Monitor sensors until the current time is reached, recording the min, max, and average for each - # This approach accounts for the updates takinga non-zero amount of time to complete + # This approach accounts for the updates taking a non-zero amount of time to complete yukon.monitor_until_ms(current_time) finally: diff --git a/examples/modules/led_strip/single_strip.py b/examples/modules/led_strip/single_strip.py index f98a924..de0c615 100644 --- a/examples/modules/led_strip/single_strip.py +++ b/examples/modules/led_strip/single_strip.py @@ -72,7 +72,7 @@ def update_rainbow(strip, offset): current_time = ticks_add(current_time, int(SLEEP * 1000)) # Monitor sensors until the current time is reached, recording the min, max, and average for each - # This approach accounts for the updates takinga non-zero amount of time to complete + # This approach accounts for the updates taking a non-zero amount of time to complete yukon.monitor_until_ms(current_time) finally: diff --git a/examples/modules/quad_servo/all_servos.py b/examples/modules/quad_servo/all_servos.py index 8313fac..2d91b57 100644 --- a/examples/modules/quad_servo/all_servos.py +++ b/examples/modules/quad_servo/all_servos.py @@ -93,7 +93,7 @@ def angle_from_index(index, offset=0.0): current_time = ticks_add(current_time, int(1000 / UPDATES)) # Monitor sensors until the current time is reached, recording the min, max, and average for each - # This approach accounts for the updates takinga non-zero amount of time to complete + # This approach accounts for the updates taking a non-zero amount of time to complete yukon.monitor_until_ms(current_time) finally: diff --git a/examples/modules/quad_servo/four_servos.py b/examples/modules/quad_servo/four_servos.py index 4d9166b..1b28fdb 100644 --- a/examples/modules/quad_servo/four_servos.py +++ b/examples/modules/quad_servo/four_servos.py @@ -74,7 +74,7 @@ def angle_from_index(index, offset=0.0): current_time = ticks_add(current_time, int(1000 / UPDATES)) # Monitor sensors until the current time is reached, recording the min, max, and average for each - # This approach accounts for the updates takinga non-zero amount of time to complete + # This approach accounts for the updates taking a non-zero amount of time to complete yukon.monitor_until_ms(current_time) finally: diff --git a/examples/modules/quad_servo/multiple_servos.py b/examples/modules/quad_servo/multiple_servos.py index 63bdf8a..4d61c58 100644 --- a/examples/modules/quad_servo/multiple_servos.py +++ b/examples/modules/quad_servo/multiple_servos.py @@ -89,7 +89,7 @@ def angle_from_index(index, offset=0.0): current_time = ticks_add(current_time, int(1000 / UPDATES)) # Monitor sensors until the current time is reached, recording the min, max, and average for each - # This approach accounts for the updates takinga non-zero amount of time to complete + # This approach accounts for the updates taking a non-zero amount of time to complete yukon.monitor_until_ms(current_time) finally: diff --git a/examples/modules/serial_servo/all_servos.py b/examples/modules/serial_servo/all_servos.py index 8787e46..aacde5e 100644 --- a/examples/modules/serial_servo/all_servos.py +++ b/examples/modules/serial_servo/all_servos.py @@ -90,7 +90,7 @@ def angle_from_index(index, offset=0.0): current_time = ticks_add(current_time, int(1000 / UPDATES)) # Monitor sensors until the current time is reached, recording the min, max, and average for each - # This approach accounts for the updates takinga non-zero amount of time to complete + # This approach accounts for the updates taking a non-zero amount of time to complete yukon.monitor_until_ms(current_time) finally: diff --git a/examples/modules/serial_servo/drive_servo.py b/examples/modules/serial_servo/drive_servo.py index 48e3bd4..5eb83e8 100644 --- a/examples/modules/serial_servo/drive_servo.py +++ b/examples/modules/serial_servo/drive_servo.py @@ -80,7 +80,7 @@ def button_newly_pressed(btn): current_time = ticks_add(current_time, int(SLEEP * 1000)) # Monitor sensors until the current time is reached, recording the min, max, and average for each - # This approach accounts for the updates takinga non-zero amount of time to complete + # This approach accounts for the updates taking a non-zero amount of time to complete yukon.monitor_until_ms(current_time) finally: diff --git a/examples/modules/serial_servo/move_servo.py b/examples/modules/serial_servo/move_servo.py index fd895b7..40abe46 100644 --- a/examples/modules/serial_servo/move_servo.py +++ b/examples/modules/serial_servo/move_servo.py @@ -82,7 +82,7 @@ def button_newly_pressed(btn): current_time = ticks_add(current_time, int(SLEEP * 1000)) # Monitor sensors until the current time is reached, recording the min, max, and average for each - # This approach accounts for the updates takinga non-zero amount of time to complete + # This approach accounts for the updates taking a non-zero amount of time to complete yukon.monitor_until_ms(current_time) finally: diff --git a/examples/showcase/rover/main.py b/examples/showcase/rover/main.py index 5552ff7..18e5ecf 100644 --- a/examples/showcase/rover/main.py +++ b/examples/showcase/rover/main.py @@ -145,7 +145,7 @@ def joystick_callback(x, y): current_time = ticks_add(current_time, TIMESTEP_MS) # Monitor sensors until the current time is reached, recording the min, max, and average for each - # This approach accounts for the updates takinga non-zero amount of time to complete + # This approach accounts for the updates taking a non-zero amount of time to complete yukon.monitor_until_ms(current_time) except RuntimeError as e: left_driver.disable() diff --git a/examples/showcase/spidertank/main.py b/examples/showcase/spidertank/main.py index b62b941..a345524 100644 --- a/examples/showcase/spidertank/main.py +++ b/examples/showcase/spidertank/main.py @@ -173,7 +173,7 @@ def extent_to_displacements(x_extent, z_extent, percent): current_time = ticks_add(current_time, TIMESTEP_MS) # Monitor sensors until the current time is reached, recording the min, max, and average for each - # This approach accounts for the updates takinga non-zero amount of time to complete + # This approach accounts for the updates taking a non-zero amount of time to complete yukon.monitor_until_ms(current_time) # Get the average voltage recorded from monitoring, and print it out