def api_softpwm(self, pin, command, option, req_method, req_args): resp = copy.deepcopy(self.CHIP_INFO) resp["connected"] = True # Figure out our command if command == "start" and req_method == 'GET': # Get the arguments duty_cycle = req_args.get('duty_cycle', 25.0) frequency = req_args.get('frequency', 35.0) polarity = req_args.get('polarity', 0) # Start the SoftPWM SPWM.start(pin, duty_cycle, frequency, polarity) resp[ "message"] = "Setting {0} to duty cycle: {1}, frequency: {2}, and polarity {3}".format( pin, duty_cycle, frequency, polarity) elif command == "stop" and req_method == 'GET': SPWM.stop(pin) resp["message"] = "Stopping {0}".format(pin) elif command == "cleanup" and req_method == 'GET': SPWM.cleanup(pin) resp["message"] = "Cleaning up {0}".format(pin) elif command == "duty_cycle" and req_method in ['GET', 'PUT', 'POST']: SPWM.set_duty_cycle(pin, float(option)) resp["message"] = "Changing duty cycle on {0} to {1}".format( pin, option) elif command == "frequency" and req_method in ['GET', 'PUT', 'POST']: SPWM.set_frequency(pin, float(option)) resp["message"] = "Changing duty cycle on {0} to {1}".format( pin, option) return jsonify(resp)
def steer_left(): SPWM.start("CSID0", 50) SPWM.set_duty_cycle("CSID0", 0) SPWM.set_frequency("CSID0", freq) SPWM.start("CSID1", 50) SPWM.set_duty_cycle("CSID1", duty * 0.75) SPWM.set_frequency("CSID1", freq) SPWM.start("CSID2", 50) SPWM.set_duty_cycle("CSID2", 0) SPWM.set_frequency("CSID2", freq) SPWM.start("CSID3", 50) SPWM.set_duty_cycle("CSID3", duty) SPWM.set_frequency("CSID3", freq)
def reverse(): SPWM.start("CSID0", 50) SPWM.set_duty_cycle("CSID0", 0) SPWM.set_frequency("CSID0", freq) SPWM.start("CSID1", 50) SPWM.set_duty_cycle("CSID1", duty) SPWM.set_frequency("CSID1", freq) SPWM.start("CSID2", 50) SPWM.set_duty_cycle("CSID2", 0) SPWM.set_frequency("CSID2", freq) SPWM.start("CSID3", 50) SPWM.set_duty_cycle("CSID3", duty) SPWM.set_frequency("CSID3", freq)
def __init__(self): green = 'XIO-P0' blue = 'XIO-P1' red = 'XIO-P2' frequency = 100 self.green = green self.red = red self.blue = blue GPIO.cleanup(red) GPIO.cleanup(green) GPIO.cleanup(blue) SPWM.start(red, 0) SPWM.set_frequency(red, frequency) SPWM.start(green, 0) SPWM.set_frequency(green, frequency) SPWM.start(blue, 0) SPWM.set_frequency(blue, frequency)
def test_pwm_frequency_invalid_value_negative(self): PWM.start("XIO-P7", 0) with pytest.raises(ValueError): PWM.set_frequency("XIO-P7", -1) PWM.cleanup()
def test_pwm_frequency_invalid_value_string(self): PWM.start("XIO-P7", 0) with pytest.raises(TypeError): PWM.set_frequency("XIO-P7", "11") PWM.cleanup()
GPIO.output("XIO-P0", GPIO.HIGH) GPIO.output("XIO-P1", GPIO.LOW) print("Testing duty cycle...") # Test duty cycle # for x in range(0,100): # SPWM.set_duty_cycle("XIO-P7", x) # print(x) # time.sleep(.1) # Test frequency SPWM.set_duty_cycle("XIO-P7", 50) print("Testing frequency at 50% duty") for f in range(100, 5000, 100): SPWM.set_frequency("XIO-P7", f) print(f) time.sleep(.1) # Hold at high 50% print("Holding high at 50% duty") SPWM.set_duty_cycle("XIO-P7", 50) SPWM.set_frequency("XIO-P7", 5000) time.sleep(3) # Hold at high 100% print("Holding high at 100% duty") SPWM.set_duty_cycle("XIO-P7", 100) SPWM.set_frequency("XIO-P7", 5000)
SPWM.cleanup() # ISSUE #16 VERIFICATION try: print( "VERIFYING FIX FOR ISSUE #16, GPIO CONFIGURED THAT SPWM WANTS TO USE" ) GPIO.setup(SPWMGPIO, GPIO.OUT) SPWM.start(SPWMGPIO, 50, 1) except Exception as e: print("EXCEPTION: {}".format(e)) print("GPIO CLEANUP") GPIO.cleanup() # SETUP SOFTPWM print("STARTING SOFTPWM TEST") SPWM.start(SPWMGPIO, 50, 1) SPWM.set_frequency(SPWMGPIO, 2) # SETUP SOFTPWM RECEIVER rcvr = SPWMReceiver(GPIO, RECEIVERGPIO, COUNT, SLEEPTIME) rcvr.start() time.sleep(COUNT * SLEEPTIME + 1) # CLEANUP print("CLEANUP") SPWM.stop(SPWMGPIO) SPWM.cleanup() GPIO.cleanup()
PWMGPIO = "XIO-P7" #PWMGPIO = "LCD-D4" COUNT = 150 SLEEPTIME = 0.01 time.sleep(1) # SETUP PWM try: print("PWM START") #PWM.toggle_debug() PWM.start(PWMGPIO, 50, 45, 1) # UNCOMMENT FOR CRASH print("PWM SET FREQUENCY") PWM.set_frequency(PWMGPIO, 10) # UNCOMMENT FOR CRASH print("PWM SET DUTY CYCLE") PWM.set_duty_cycle(PWMGPIO, 25) #time.sleep(COUNT*SLEEPTIME + 1) raw_input("PRESS ENTER WHEN DONE") except: raise finally: # CLEANUP print("CLEANUP") PWM.stop(PWMGPIO) PWM.cleanup()