From 21c6ca67b64295e71dc5e713f84f047c63480564 Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Tue, 17 Oct 2023 13:24:51 +0100 Subject: [PATCH] Added quad servo adc example --- examples/board/monitor_internals.py | 6 ++- 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/quad_servo/servo_feedback.py | 47 +++++++++++++++++++ lib/pimoroni_yukon/__init__.py | 2 +- .../modules/quad_servo_direct.py | 8 ++-- 7 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 examples/modules/quad_servo/servo_feedback.py diff --git a/examples/board/monitor_internals.py b/examples/board/monitor_internals.py index 4e38c00..71f5b27 100644 --- a/examples/board/monitor_internals.py +++ b/examples/board/monitor_internals.py @@ -2,6 +2,8 @@ """ Use Yukon's monitoring function to read the internal sensors. +Power needs to be provided to the XT30 connector, otherwise +the monitoring will raise an UnderVoltageError. """ # Constants @@ -16,9 +18,11 @@ while not yukon.is_boot_pressed(): # Monitor sensors for a number of seconds, recording the min, max, and average for each - # With the default logging level, these values will be also be printed out yukon.monitored_sleep(SLEEP) + # Print out the readings taken during monitoring + yukon.print_readings() + finally: # Put the board back into a safe state, regardless of how the program may have ended yukon.reset() diff --git a/examples/modules/quad_servo/all_servos.py b/examples/modules/quad_servo/all_servos.py index 6822df1..1c4f799 100644 --- a/examples/modules/quad_servo/all_servos.py +++ b/examples/modules/quad_servo/all_servos.py @@ -21,7 +21,7 @@ CLUSTER_SM = 0 # The State Machines (SM) to use to drive the servo cluster # Variables -yukon = Yukon() # Create a new Yukon object, with its logging level lowered +yukon = Yukon() # Create a new Yukon object modules = [] # A list to store QuadServo module objects created later phase_offset = 0 # The offset used to animate the servos diff --git a/examples/modules/quad_servo/four_servos.py b/examples/modules/quad_servo/four_servos.py index 1600b06..a0e7e3d 100644 --- a/examples/modules/quad_servo/four_servos.py +++ b/examples/modules/quad_servo/four_servos.py @@ -20,7 +20,7 @@ START_DELAY = 0.5 # The time to sleep between activating and animating the servos # Variables -yukon = Yukon() # Create a new Yukon object, with its logging level lowered +yukon = Yukon() # Create a new Yukon object module = QuadServoModule() # Create a QuadServoModule object phase_offset = 0 # The offset used to animate the servos diff --git a/examples/modules/quad_servo/multiple_servos.py b/examples/modules/quad_servo/multiple_servos.py index bfa47d3..fd731b7 100644 --- a/examples/modules/quad_servo/multiple_servos.py +++ b/examples/modules/quad_servo/multiple_servos.py @@ -17,7 +17,7 @@ START_DELAY = 0.5 # The time to sleep between activating and animating the servos # Variables -yukon = Yukon() # Create a new Yukon object, with its logging level lowered +yukon = Yukon() # Create a new Yukon object modules = [] # A list to store QuadServo module objects created later phase_offset = 0 # The offset used to animate the servos diff --git a/examples/modules/quad_servo/servo_feedback.py b/examples/modules/quad_servo/servo_feedback.py new file mode 100644 index 0000000..4d68bb0 --- /dev/null +++ b/examples/modules/quad_servo/servo_feedback.py @@ -0,0 +1,47 @@ +import math +from pimoroni_yukon import Yukon +from pimoroni_yukon.modules import QuadServoDirectModule +from pimoroni_yukon.timing import ticks_ms, ticks_add + +""" +How to read the analog inputs on Quad Servo Direct modules connected to Slots. +""" + +# Constants +SLEEP = 0.5 # The time to sleep between each reading +SAMPLES = 1 # How many times each input should be sampled. Increasing this will reduce noise but take longer to run + +# Variables +yukon = Yukon() # Create a new Yukon object +modules = [] # A list to store QuadServo module objects created later + + +# Wrap the code in a try block, to catch any exceptions (including KeyboardInterrupt) +try: + # Find out which slots of Yukon have QuadServoDirectModules attached + for slot in yukon.find_slots_with(QuadServoDirectModule): + module = QuadServoDirectModule() # Create a QuadServoDirectModule object + yukon.register_with_slot(module, slot) # Register the QuadServoDirectModule object with the slot + modules.append(module) # Add the object to the module list + + # Record the number of servos that will be driven + NUM_SERVOS = len(modules) * QuadServoDirectModule.NUM_SERVOS + print(f"Up to {NUM_SERVOS} servos available") + + yukon.verify_and_initialise() # Verify that QuadServo modules are attached to Yukon, and initialise them + yukon.enable_main_output() # Turn on power to the module slots + + # Loop until the BOOT/USER button is pressed + while not yukon.is_boot_pressed(): + + # Read all the sensors and print out their values + for index, module in enumerate(modules): + print(f"[M{index}] A1 = {module.read_adc1()}, A2 = {module.read_adc2()}", end=", ") + print() + + # Monitor sensors for a number of seconds, recording the min, max, and average for each + yukon.monitored_sleep(SLEEP) + +finally: + # Put the board back into a safe state, regardless of how the program may have ended + yukon.reset() diff --git a/lib/pimoroni_yukon/__init__.py b/lib/pimoroni_yukon/__init__.py index 44df440..1fd2a58 100644 --- a/lib/pimoroni_yukon/__init__.py +++ b/lib/pimoroni_yukon/__init__.py @@ -625,7 +625,7 @@ def monitor(self): self.__undervoltage_count += 1 if self.__undervoltage_count > self.UNDERVOLTAGE_COUNT_LIMIT: self.disable_main_output() - raise UnderVoltageError(f"[Yukon] Input voltage of {voltage_in}V below minimum operating level. Turning off output") + raise UnderVoltageError(f"[Yukon] Input voltage of {voltage_in}V below minimum operating level of {self.VOLTAGE_LOWER_LIMIT}V. Turning off output") else: self.__undervoltage_count = 0 diff --git a/lib/pimoroni_yukon/modules/quad_servo_direct.py b/lib/pimoroni_yukon/modules/quad_servo_direct.py index d72692e..c740b50 100644 --- a/lib/pimoroni_yukon/modules/quad_servo_direct.py +++ b/lib/pimoroni_yukon/modules/quad_servo_direct.py @@ -71,8 +71,8 @@ def servo4(self): return self.servos[3] raise RuntimeError("servo4 is only accessible if init_servos was True during initialisation") - def read_adc1(self): - return self.__read_adc1() + def read_adc1(self, samples=1): + return self.__read_adc1(samples) - def read_adc2(self): - return self.__read_adc2() + def read_adc2(self, samples=1): + return self.__read_adc2(samples)