Skip to content

Commit

Permalink
Added quad servo adc example
Browse files Browse the repository at this point in the history
  • Loading branch information
ZodiusInfuser committed Oct 17, 2023
1 parent 97e58eb commit 21c6ca6
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 9 deletions.
6 changes: 5 additions & 1 deletion examples/board/monitor_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
2 changes: 1 addition & 1 deletion examples/modules/quad_servo/all_servos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion examples/modules/quad_servo/four_servos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion examples/modules/quad_servo/multiple_servos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
47 changes: 47 additions & 0 deletions examples/modules/quad_servo/servo_feedback.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion lib/pimoroni_yukon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions lib/pimoroni_yukon/modules/quad_servo_direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 21c6ca6

Please sign in to comment.