class Motor: """Klasse for å kontrollere en motor""" def __init__(self, forward, backward, pwm): // Disse er inn signalene til h-blokken self.forward = DigitalOutputDevice(forward) self.backward = DigitalOutputDevice(backward) self.pwm = PWMOutputDevice(pwm, True, 0, 1000) def speed(self, speed): """Justerer hastigheten og rettningen til motoren""" self.direction(speed) self.pwm.value = norm(speed) def direction(self, speed): """Bestemmer rettningen basert på hastigheten""" if speed > 0: self.forward.on() self.backward.off() else: self.forward.off() self.backward.on() def close(self): """Frigjør og rydder opp""" self.forward.close() self.backward.close() self.pwm.close()
class TB6612FNG: def __init__(self, pin_fig_in1, pin_fig_in2, pin_fig_pwm, frequency=None): self.in1 = DigitalOutputDevice(pin=pin_fig_in1) self.in2 = DigitalOutputDevice(pin=pin_fig_in2) if frequency is None: # Not PWM mode self.pwm = DigitalOutputDevice(pin=pin_fig_pwm) else: # PWM mode self.pwm = PWMOutputDevice(pin=pin_fig_pwm, frequency=frequency) def cw(self): self.in1.on() self.in2.off() self.pwm.on() def ccw(self): self.in1.off() self.in2.on() self.pwm.on() def stop(self): self.in1.off() self.in2.off() self.pwm.off() def stop_and_close(self): self.stop() self.in1.close() self.in2.close() self.pwm.close()
class Motor: """ The class takes three pin numbers as the input to control one of the motor connected to TB6612FNG module. """ def __init__(self, in1, in2, pwm): self.in1 = DigitalOutputDevice(in1) self.in1.off() self.in2 = DigitalOutputDevice(in2) self.in2.on() self.pwm = PWMOutputDevice(pwm, frequency=1000) def set_throttle(self, val): """Control the orientation and the speed of the motor. Arguments: val: a number between -1.0 and 1.0. The motor rotates in forward direction if val > 1, otherwise in reverse direction. Setting val to None will set the motor to stop mode. """ # Set the motor to stop mode. if val is None: self.in1.off() self.in2.off() self.pwm.value = 1.0 else: # Determine the orientation of the motor. if val > 0.0: self.in1.off() self.in2.on() else: self.in1.on() self.in2.off() # Clamp the pwm signal (throttle) to [0, 1]. pwm = max(0.0, min(abs(val), 1.0)) # Note that setting PWM to low will brake the motor no matter what # in1 and in2 input is. self.pwm.value = pwm def close(self): self.in1.close() self.in2.close() self.pwm.close()
class cBLDC(QtCore.QObject): def __init__(self, pinArah, pinPWM): super(cBLDC, self).__init__() self.zf = DigitalOutputDevice(pinArah) self.pwm = PWMOutputDevice(pinPWM) def setDirection(self, direction): self.zf.value = direction def setSpeed(self, speed): self.pwm.value = speed def accellerate(self): """ buat kecepatan meningkat sampai maksimum, tapi untuk sekarang hanya dibuat kecematan konstan """ self.pwm.value = MAX_SPEED def decellerate(self): """ buat kecepatannya menurun sampai berhenti, tapi sekarang dibuat konstan dulu """ self.pwm.value = 0.0 def hold(self): """ digunakan untuk menghentikan/menahan nilai pwm ketika motor di accelerate/decellerate """ def close(self): """ digunakan untuk closing pin gpiozero. Apakah perlu dicek kecepatan saat itu? """ # this is abrupt stop: self.pwm.value = 0 self.pwm.close() self.zf.close()
class XmasTree(): # define our xmas tree as a Python class # When we create a new tree, we set the pins that it uses. # Each pin is a 'node' that is used to control 2 LEDs. Each LED # uses 2 nodes: one is the anode, the other the cathode def __init__(self, node1, node2, node3, node4): self.anode = InputDevice(node1) self.cathode = InputDevice(node2) self.node1 = node1 self.node2 = node2 self.node3 = node3 self.node4 = node4 # A function to turn off any LED which is on. def last_off(self): self.anode.close() self.cathode.close() # A function to turn on an LED # We pass in anode and cathode values def gen_on(self, anode, cathode): self.last_off() self.anode = OutputDevice(anode) self.cathode = OutputDevice(cathode) self.anode.on() self.cathode.off() def pwm_on(self, anode, cathode, fade_in, fade_out, n): self.last_off() self.anode = PWMOutputDevice(anode) self.cathode = OutputDevice(cathode) self.anode.pulse(fade_in_time=fade_in, fade_out_time=fade_out, n=n, background=False) self.cathode.off() # A function for each of the LEDs # It just calls the gen_on() and passes in # the correct anode and cathode values def red1_on(self): self.gen_on(self.node4, self.node3) def red2_on(self): self.gen_on(self.node3, self.node4) def red3_on(self): self.gen_on(self.node4, self.node2) def red4_on(self): self.gen_on(self.node2, self.node4) def red5_on(self): self.gen_on(self.node1, self.node2) def red6_on(self): self.gen_on(self.node2, self.node1) def yellow_on(self): self.gen_on(self.node3, self.node1) def all_on(self, time): t = 0.001 for p in range(int((time / t) / 7)): self.red1_on() sleep(t) self.red2_on() sleep(t) self.red3_on() sleep(t) self.red4_on() sleep(t) self.red5_on() sleep(t) self.red6_on() sleep(t) self.yellow_on() sleep(t) self.last_off() def yellow_pulse(self, fade_in, fade_out, n): self.pwm_on(self.node3, self.node1, fade_in, fade_out, n)
pwm = PWMOutputDevice(pwmPin, initial_value = v, frequency = f) print("Tekan ctrl-c untuk berhenti!") pl = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0] try: while True: zf.value = 0 for p in pl: pwm.value = p; sleep(0.25) print("dir = {}, val = {}".format(zf.value, p)) sleep(1) zf.value = 1 for p in pl: pwm.value = p; sleep(0.25) print("dir = {}, val = {}".format(zf.value, p)) sleep(1) except KeyboardInterrupt: v = pwm.value while v > 0.0: v -= 0.1 if v < 0.0: v = 0.0 else: if v>=0: pwm.value -= v; sleep(0.25) pwm.close() zf.close() print("\n\nDone...!")