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 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)
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 fanOFF(): pigpio.set_PWM_dutycycle(gpiochan, 0) # switch fan off return () def signalHandler(_signo, _stack_frame): fanOFF() sys.exit(0) try: signal.signal(signal.SIGTERM, signalHandler) pigpio = pigpio.pi() pigpio.set_PWM_frequency(gpiochan, 23000) print(pigpio.get_PWM_frequency(gpiochan)) pigpio.set_PWM_range(gpiochan, 100) fanOFF() while True: handleFan() sleep(3) # Read the temperature every 3 sec except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt fanOFF() # GPIO.cleanup() # resets all GPIO ports used by this program
def pigpio_init_pwm(self): pigpio.set_PWM_frequency(self.pin, 50) # 50Hz pulses pigpio.set_PWM_range(self.pin, 20000) # 1,000,000 / 50 = 20,000us for 100% duty cycle self.logger.info("PWM cycle frequency: %dHz" % pigpio.get_PWM_frequency(self.pin))
def init_channel(pin): pigpio.set_PWM_frequency(pin, 50) println("PWM cycle frequency: %dHz" % pigpio.get_PWM_frequency(pin)) pigpio.set_PWM_range(pin, RANGE)