class FIT_Motor: def __init__(self, in1, in2, in3): self.direction = OutputDevice(in1, initial_value=True) # direction Pin self.speed = PWMOutputDevice(in2, initial_value=1, frequency=980) # speed Pin self.encoder = DigitalInputDevice(in3) #self.encoder = SmoothedInputDevice(in3, threshold=0.01, partial=True) self.total_time = 1.0 / (self.speed.frequency / 2.0) def changeDirection(self): self.direction.toggle() def setStop(self): self.speed.on() def setSpeed(self, speed): speed_val = abs(speed) / 20.8 if speed_val < 0.01: self.setStop() else: self.speed.blink(on_time=self.total_time - speed_val * self.total_time, off_time=self.total_time * speed_val) def readEncoder(self, val): # print("Nothing") reading = self.encoder.value return reading
from PWMChannel import PWMInputDevice from gpiozero import PWMOutputDevice PWM_FREQ = 500 PWM_MIN_PERIOD = 0.001 # 1ms PWM_MAX_PERIOD = 0.002 # 1ms ########## # Inputs # ########## input_channel = PWMInputDevice(24) ########### # Outputs # ########### emulated_pwm = PWMOutputDevice(23, frequency=PWM_FREQ) emulated_pwm.blink(on_time=PWM_MIN_PERIOD, off_time=(1 / PWM_FREQ - PWM_MIN_PERIOD)) while True: print(input_channel.pulse_width)