hakkoso/control/fixed_duty.py

47 lines
1.6 KiB
Python

from . import controllers
from scm import stringify
from time import perf_counter
class FixedDutyCycle(controllers.Controller):
def __init__(self, *args, **kwargs):
self.start = kwargs.pop('start_time', perf_counter())
self.period = kwargs.pop('period')
self.set_duty(kwargs.pop('duty_perc'))
super().__init__(*args, **kwargs)
self.last_time = None
def set_duty(self, duty=None):
# Updates the duty cycle to a point in time where the switch
# must occur
if duty is not None:
self.duty_perc = duty
self.duty = self.duty_perc/100 * self.period
def set_period(self, period):
# Shift start point so that we know where in the new period we are.
# Then, move period
# "|-----X----------------|" punto % old_period
# "|-----X----|-----------|" start = now - (punto % old_period)
# "|----|X----|-----|-----|"
self.start = self.last_time - (self.timepoint() % self.period)
print(period, self.start)
self.period = period
self.set_duty()
def timepoint(self):
return ((self.last_time - self.start) % self.period)
def one_cycle(self, time):
self.last_time = time
return (self.timepoint() < self.duty) if (
self.input < self.target) else False
def fixed_duty_wrapper(args):
name, duty, period = stringify(args.car, quote=False), args.cdr.car, args.cdr.cdr.car
print('name, duty, period', name, duty*100, period)
return (name, FixedDutyCycle(
target=None, # reach_period_s=60*60,
duty_perc=duty*100, period=period,
start_time=perf_counter()))