Skip to content

Commit

Permalink
Added single power example, and added more multisampling options
Browse files Browse the repository at this point in the history
  • Loading branch information
ZodiusInfuser committed Oct 23, 2023
1 parent 50586cd commit ec50ff3
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 25 deletions.
69 changes: 69 additions & 0 deletions examples/modules/bench_power/single_power.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from pimoroni_yukon import Yukon
from pimoroni_yukon import SLOT1 as SLOT
from pimoroni_yukon.modules import BenchPowerModule

"""
How to control the variable output of a Bench Power Module connected to Slot1.
"""

# Constants
INITIAL_VOLTAGE = 5.0 # The voltage to start the BenchPowerModule with
VOLTAGE_STEP = 0.1 # How much to increase/decrease the voltage by each update loop
DELAY = 0.2 # The time to sleep after setting a new voltage before it can be read back
SAMPLES = 100 # How many voltage readings to take to produce an average

# Variables
yukon = Yukon() # Create a new Yukon object
module = BenchPowerModule() # Create a BenchPowerModule object
voltage = INITIAL_VOLTAGE # The voltage to have the BenchPowerModule output


# Function to print out the target and measured voltages
def print_voltages():
global voltage
global module
yukon.monitored_sleep(DELAY)
measured = module.read_voltage(SAMPLES) # Measure the voltage that is actually being output
print(f"Target = {voltage} V, Measured = {measured} V") # Print out intended and measured voltages


# Wrap the code in a try block, to catch any exceptions (including KeyboardInterrupt)
try:
yukon.register_with_slot(module, SLOT) # Register the BenchPowerModule object with the slot
yukon.verify_and_initialise() # Verify that a BenchPowerModule is attached to Yukon, and initialise it

yukon.enable_main_output() # Turn on power to the module slots
module.set_voltage(voltage) # Set the initial voltage to output
module.enable() # Enable the BenchPowerModule's onboard regulator
print_voltages() # Print out the new voltages

# Loop until the BOOT/USER button is pressed
while not yukon.is_boot_pressed():

# Read the state of the onboard buttons
state_a = yukon.is_pressed('A')
state_b = yukon.is_pressed('B')

# Have the LEDs mirror the button states to show the program is working
yukon.set_led('A', state_a)
yukon.set_led('B', state_b)

if state_a != state_b:
# Has the A button been newly pressed?
if state_a is True:
voltage -= 0.1 # Decrease the voltage
module.set_voltage(voltage) # Set the new voltage to output
print_voltages() # Print out the new voltages

# Has the B button been newly pressed?
if state_b is True:
voltage += 0.1 # Increase the voltage
module.set_voltage(voltage) # Set the new voltage to output
print_voltages() # Print out the new voltages

# Perform a single check of Yukon's internal voltage, current, and temperature sensors
yukon.monitor_once()

finally:
# Put the board back into a safe state, regardless of how the program may have ended
yukon.reset()
4 changes: 2 additions & 2 deletions examples/modules/dual_output/pwm_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
# Loop until the BOOT/USER button is pressed
while not yukon.is_boot_pressed():

# Monitor sensors for a number of seconds, recording the min, max, and average for each
yukon.monitored_sleep(SLEEP)
# Perform a single check of Yukon's internal voltage, current, and temperature sensors
yukon.monitor_once()

finally:
# Put the board back into a safe state, regardless of how the program may have ended
Expand Down
4 changes: 2 additions & 2 deletions lib/pimoroni_yukon/modules/audio_amp.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ def set_volume(self, volume):

self.write_i2c_reg(DVC, int((1.0 - volume) * 0xC8))

def read_temperature(self):
return self.__read_adc2_as_temp()
def read_temperature(self, samples=1):
return self.__read_adc2_as_temp(samples)

def monitor(self):
temperature = self.read_temperature()
Expand Down
10 changes: 5 additions & 5 deletions lib/pimoroni_yukon/modules/bench_power.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ def set_percent(self, percent):

self.__set_pwm((percent * (self.PWM_MAX - self.PWM_MIN)) + self.PWM_MIN)

def read_voltage(self):
# return (self.__shared_adc_voltage() * (100 + 22)) / 22
voltage = self.__read_adc1()
def read_voltage(self, samples=1):
# return (self.__read_adc1(samples) * (100 + 22)) / 22
voltage = self.__read_adc1(samples)
if voltage >= self.VOLTAGE_MID_MEASURE:
return ((voltage - self.VOLTAGE_MID_MEASURE) * (self.VOLTAGE_MAX - self.VOLTAGE_MID)) / (self.VOLTAGE_MAX_MEASURE - self.VOLTAGE_MID_MEASURE) + self.VOLTAGE_MID
else:
Expand All @@ -96,8 +96,8 @@ def read_voltage(self):
def read_power_good(self):
return self.__power_good.value() == 1

def read_temperature(self):
return self.__read_adc2_as_temp()
def read_temperature(self, samples=1):
return self.__read_adc2_as_temp(samples)

def monitor(self):
pgood = self.read_power_good()
Expand Down
8 changes: 4 additions & 4 deletions lib/pimoroni_yukon/modules/big_motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ def is_enabled(self):
def read_fault(self):
return self.__motor_nfault.value() != 1

def read_current(self):
def read_current(self, samples=1):
# This needs more validation
return (abs(self.__read_adc1() - (3.3 / 2))) / (self.SHUNT_RESISTOR * self.GAIN)
return (abs(self.__read_adc1(samples) - (3.3 / 2))) / (self.SHUNT_RESISTOR * self.GAIN)

def read_temperature(self):
return self.__read_adc2_as_temp()
def read_temperature(self, samples=1):
return self.__read_adc2_as_temp(samples)

def monitor(self):
fault = self.read_fault()
Expand Down
12 changes: 6 additions & 6 deletions lib/pimoroni_yukon/modules/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ def reset(self):
# Override this to reset the module back into a default state post-initialisation
pass

def __read_adc1(self):
return self.__adc1_func(self.slot)
def __read_adc1(self, samples=1):
return self.__adc1_func(self.slot, samples)

def __read_adc2(self):
return self.__adc2_func(self.slot)
def __read_adc2(self, samples=1):
return self.__adc2_func(self.slot, samples)

def __read_adc2_as_temp(self):
return analog_to_temp(self.__adc2_func(self.slot))
def __read_adc2_as_temp(self, samples=1):
return analog_to_temp(self.__adc2_func(self.slot, samples))

def assign_monitor_action(self, callback_function):
if not None and not callable(callback_function):
Expand Down
4 changes: 2 additions & 2 deletions lib/pimoroni_yukon/modules/dual_motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ def motor2(self):
def read_fault(self):
return self.__read_adc1() <= self.FAULT_THRESHOLD

def read_temperature(self):
return self.__read_adc2_as_temp()
def read_temperature(self, samples=1):
return self.__read_adc2_as_temp(samples)

def monitor(self):
fault = self.read_fault()
Expand Down
4 changes: 2 additions & 2 deletions lib/pimoroni_yukon/modules/dual_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ def read_power_good1(self):
def read_power_good2(self):
return self.__power_good[1].value() == 1

def read_temperature(self):
return self.__read_adc2_as_temp()
def read_temperature(self, samples=1):
return self.__read_adc2_as_temp(samples)

def monitor(self):
pgood1 = self.read_power_good1()
Expand Down
4 changes: 2 additions & 2 deletions lib/pimoroni_yukon/modules/led_strip.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ def strip2(self):
def read_power_good(self):
return self.__power_good.value() == 1

def read_temperature(self):
return self.__read_adc2_as_temp()
def read_temperature(self, samples=1):
return self.__read_adc2_as_temp(samples)

def monitor(self):
pgood = self.read_power_good()
Expand Down

0 comments on commit ec50ff3

Please sign in to comment.