def set_duty_cycle(self, pulse_width_us): self.set_motor_direction_pins() if (math.fabs(pulse_width_us) == self.min): # If we have reached the bottom of the power range, turn the power off entirely so we don't waste any self.logger.info("Shutting down motor") pigpio.set_PWM_dutycycle(self.pin, 0) else: pigpio.set_PWM_dutycycle(self.pin, math.fabs(pulse_width_us)) self.logger.info("Motor Speed = %s" % pulse_width_us)
def set_duty_cycle(self, pulse_width_us): self.set_motor_direction_pins() if ( math.fabs(pulse_width_us) == self.min ): # If we have reached the bottom of the power range, turn the power off entirely so we don't waste any self.logger.info("Shutting down motor") pigpio.set_PWM_dutycycle(self.pin, 0) else: pigpio.set_PWM_dutycycle(self.pin, math.fabs(pulse_width_us)) self.logger.info("Motor Speed = %s" % pulse_width_us)
def t3(): global t3_reset, t3_count, t3_on, t3_off pw=[500.0, 1500.0, 2500.0] dc=[0.2, 0.4, 0.6, 0.8] print("PWM/Servo pulse accuracy tests.") t3cb = pigpio.callback(GPIO, pigpio.EITHER_EDGE, t3cbf) t = 0 for x in pw: t += 1 pigpio.set_servo_pulsewidth(GPIO, x) time.sleep(1) t3_reset = True time.sleep(4) c = t3_count on = t3_on off = t3_off CHECK(3, t, int((1E3*(on+off))/on), int(2E7/x), 1, "set servo pulsewidth") pigpio.set_servo_pulsewidth(GPIO, 0) pigpio.set_PWM_frequency(GPIO, 1000) f = pigpio.get_PWM_frequency(GPIO) CHECK(3, 4, f, 1000, 0, "set/get PWM frequency") rr = pigpio.set_PWM_range(GPIO, 100) CHECK(3, 5, rr, 200, 0, "set PWM range") t = 5 for x in dc: t += 1 pigpio.set_PWM_dutycycle(GPIO, x*100) time.sleep(1) t3_reset = True time.sleep(2) c = t3_count on = t3_on off = t3_off CHECK(3, t, int((1E3*on)/(on+off)), int(1E3*x), 1, "set PWM dutycycle") pigpio.set_PWM_dutycycle(GPIO, 0)
def handleFan(): global fanSpeed, sum actualTemp = float(getCPUtemperature()) diff = actualTemp - desiredTemp sum = sum + diff pDiff = diff * pTemp iDiff = sum * iTemp fanSpeed = pDiff + iDiff if fanSpeed > 100: fanSpeed = 100 if fanSpeed < 0: fanSpeed = 0 if sum > 100: sum = 100 if sum < -50: sum = -50 #print("actualTemp %4.2f TempDiff %4.2f pDiff %4.2f iDiff %4.2f fanSpeed %5d" % (actualTemp,diff,pDiff,iDiff,fanSpeed)) pigpio.set_PWM_dutycycle(gpiochan, fanSpeed) return ()
def t2(): global t2_count print("PWM dutycycle/range/frequency tests.") pigpio.set_PWM_range(GPIO, 255) pigpio.set_PWM_frequency(GPIO, 0) f = pigpio.get_PWM_frequency(GPIO) CHECK(2, 1, f, 10, 0, "set PWM range, set/get PWM frequency") t2cb = pigpio.callback(GPIO, pigpio.EITHER_EDGE, t2cbf) pigpio.set_PWM_dutycycle(GPIO, 0) time.sleep(0.5) # allow old notifications to flush oc = t2_count time.sleep(2) f = t2_count - oc CHECK(2, 2, f, 0, 0, "set PWM dutycycle, callback") pigpio.set_PWM_dutycycle(GPIO, 128) time.sleep(1) oc = t2_count time.sleep(2) f = t2_count - oc CHECK(2, 3, f, 40, 5, "set PWM dutycycle, callback") pigpio.set_PWM_frequency(GPIO, 100) f = pigpio.get_PWM_frequency(GPIO) CHECK(2, 4, f, 100, 0, "set/get PWM frequency") time.sleep(1) oc = t2_count time.sleep(2) f = t2_count - oc CHECK(2, 5, f, 400, 1, "callback") pigpio.set_PWM_frequency(GPIO, 1000) f = pigpio.get_PWM_frequency(GPIO) CHECK(2, 6, f, 1000, 0, "set/get PWM frequency") time.sleep(1) oc = t2_count time.sleep(2) f = t2_count - oc CHECK(2, 7, f, 4000, 1, "callback") r = pigpio.get_PWM_range(GPIO) CHECK(2, 8, r, 255, 0, "get PWM range") rr = pigpio.get_PWM_real_range(GPIO) CHECK(2, 9, rr, 200, 0, "get PWM real range") pigpio.set_PWM_range(GPIO, 2000) r = pigpio.get_PWM_range(GPIO) CHECK(2, 10, r, 2000, 0, "set/get PWM range") rr = pigpio.get_PWM_real_range(GPIO) CHECK(2, 11, rr, 200, 0, "get PWM real range") pigpio.set_PWM_dutycycle(GPIO, 0)
def t2(): global t2_count print("PWM dutycycle/range/frequency tests.") pigpio.set_PWM_range(GPIO, 255) pigpio.set_PWM_frequency(GPIO,0) f = pigpio.get_PWM_frequency(GPIO) CHECK(2, 1, f, 10, 0, "set PWM range, set/get PWM frequency") t2cb = pigpio.callback(GPIO, pigpio.EITHER_EDGE, t2cbf) pigpio.set_PWM_dutycycle(GPIO, 0) time.sleep(0.5) # allow old notifications to flush oc = t2_count time.sleep(2) f = t2_count - oc CHECK(2, 2, f, 0, 0, "set PWM dutycycle, callback") pigpio.set_PWM_dutycycle(GPIO, 128) time.sleep(1) oc = t2_count time.sleep(2) f = t2_count - oc CHECK(2, 3, f, 40, 5, "set PWM dutycycle, callback") pigpio.set_PWM_frequency(GPIO,100) f = pigpio.get_PWM_frequency(GPIO) CHECK(2, 4, f, 100, 0, "set/get PWM frequency") time.sleep(1) oc = t2_count time.sleep(2) f = t2_count - oc CHECK(2, 5, f, 400, 1, "callback") pigpio.set_PWM_frequency(GPIO,1000) f = pigpio.get_PWM_frequency(GPIO) CHECK(2, 6, f, 1000, 0, "set/get PWM frequency") time.sleep(1) oc = t2_count time.sleep(2) f = t2_count - oc CHECK(2, 7, f, 4000, 1, "callback") r = pigpio.get_PWM_range(GPIO) CHECK(2, 8, r, 255, 0, "get PWM range") rr = pigpio.get_PWM_real_range(GPIO) CHECK(2, 9, rr, 200, 0, "get PWM real range") pigpio.set_PWM_range(GPIO, 2000) r = pigpio.get_PWM_range(GPIO) CHECK(2, 10, r, 2000, 0, "set/get PWM range") rr = pigpio.get_PWM_real_range(GPIO) CHECK(2, 11, rr, 200, 0, "get PWM real range") pigpio.set_PWM_dutycycle(GPIO, 0)
def t4(): print("Pipe notification tests.") pigpio.set_PWM_frequency(GPIO, 0) pigpio.set_PWM_dutycycle(GPIO, 0) pigpio.set_PWM_range(GPIO, 100) h = pigpio.notify_open() e = pigpio.notify_begin(h, (1 << 4)) CHECK(4, 1, e, 0, 0, "notify open/begin") time.sleep(1) with open("/dev/pigpio" + str(h), "rb") as f: pigpio.set_PWM_dutycycle(GPIO, 50) time.sleep(4) pigpio.set_PWM_dutycycle(GPIO, 0) e = pigpio.notify_pause(h) CHECK(4, 2, e, 0, 0, "notify pause") e = pigpio.notify_close(h) CHECK(4, 3, e, 0, 0, "notify close") n = 0 s = 0 seq_ok = 1 toggle_ok = 1 while True: chunk = f.read(12) if len(chunk) == 12: S, fl, t, v = struct.unpack("HHII", chunk) if s != S: seq_ok = 0 L = v & (1 << 4) if n: if l != L: toggle_ok = 0 if L: l = 0 else: l = 1 << 4 s += 1 n += 1 # print(S, fl, t, hex(v)) else: break f.close() CHECK(4, 4, seq_ok, 1, 0, "sequence numbers ok") CHECK(4, 5, toggle_ok, 1, 0, "gpio toggled ok") CHECK(4, 6, n, 80, 10, "number of notifications")
def fanOFF(): pigpio.set_PWM_dutycycle(gpiochan, 0) # switch fan off return ()
def set_duty_cycle(self, pulse_width_us): pigpio.set_PWM_dutycycle(self.pin, math.fabs(pulse_width_us)) self.logger.info("Position = %s" % pulse_width_us) self.set_direction_pin()
def set_channel(pin, duty_cycle_us): pigpio.set_PWM_dutycycle(pin, duty_cycle_us) print_at(5, 0, "Channel Duty Cycle: %d/%d " % (PERIOD, RANGE))
def stop_channel(pin): pigpio.set_PWM_dutycycle(pin, 0)
def t4(): print("Pipe notification tests.") pigpio.set_PWM_frequency(GPIO, 0) pigpio.set_PWM_dutycycle(GPIO, 0) pigpio.set_PWM_range(GPIO, 100) h = pigpio.notify_open() e = pigpio.notify_begin(h, (1<<4)) CHECK(4, 1, e, 0, 0, "notify open/begin") time.sleep(1) with open("/dev/pigpio"+ str(h), "rb") as f: pigpio.set_PWM_dutycycle(GPIO, 50) time.sleep(4) pigpio.set_PWM_dutycycle(GPIO, 0) e = pigpio.notify_pause(h) CHECK(4, 2, e, 0, 0, "notify pause") e = pigpio.notify_close(h) CHECK(4, 3, e, 0, 0, "notify close") n = 0 s = 0 seq_ok = 1 toggle_ok = 1 while True: chunk = f.read(12) if len(chunk) == 12: S, fl, t, v = struct.unpack('HHII', chunk) if s != S: seq_ok = 0 L = v & (1<<4) if n: if l != L: toggle_ok = 0 if L: l = 0 else: l = (1<<4) s += 1 n += 1 # print(S, fl, t, hex(v)) else: break f.close() CHECK(4, 4, seq_ok, 1, 0, "sequence numbers ok") CHECK(4, 5, toggle_ok, 1, 0, "gpio toggled ok") CHECK(4, 6, n, 80, 10, "number of notifications")