def cycle(self): self.setup() dc = self.min_value + abs(math.sin(self.currenta_angle) * self.delta) self.currenta_angle = (self.currenta_angle + self.angle_increment) % 360 pwm.set_duty_cycle(self.led, dc)
def speed(self, val): self._speed = val if val == 0 and not self.stop_on_zero: duty = 0 else: duty = int(map(val, -1.0, 1.0, self.duty_min, self.duty_max)) PWM.set_duty_cycle(self.pin, duty)
def motoroff(): GPIO.output(drive_0, GPIO.LOW) GPIO.output(drive_1, GPIO.LOW) GPIO.output(drive_2, GPIO.LOW) GPIO.output(drive_3, GPIO.LOW) SPWM.set_duty_cycle(PWM_0, 0) SPWM.set_duty_cycle(PWM_1, 0)
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 on_message(client, userdata, msg_raw): msg = msg_raw.payload.rstrip('\t\r\n\0').split( '||') #Split rate data and speech data #print(msg_raw.topic+" "+msg[0]+" "+msg[1]) #Debug #Converting data into integers from -100 to 100 if msg[0] == "disconnect": # Debug client.disconnect() elif msg_raw.topic == "romibo/emotion": file = open("emotion.txt", "w") file.write(msg[0]) file.close() print("emotion received") elif msg_raw.topic == "romibo/action": x = int(float(msg[0]) * 100) y = int(float(msg[1]) * 100) L_mag = int(L_mag_calculation(x, y)) R_mag = int(R_mag_calculation(x, y)) print("L: " + str(L_mag) + "||R:" + str(R_mag)) #left motor if L_mag > 0: #set to forward GPIO.output(en_l_fw, 1) GPIO.output(en_l_bw, 0) elif L_mag <= 0: #set to backward GPIO.output(en_l_fw, 0) GPIO.output(en_l_bw, 1) #set velocity via duty cycle SPWM.set_duty_cycle(left, abs(L_mag)) #right motor if R_mag > 0: #set to forward GPIO.output(en_r_fw, 1) GPIO.output(en_r_bw, 0) elif R_mag <= 0: #set to backward GPIO.output(en_r_fw, 0) GPIO.output(en_r_bw, 1) #set velocity via duty cycle SPWM.set_duty_cycle(right, abs(R_mag))
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 cycle(self): self.setup() ## handle red if self.red_sweeping: if self.red_duty < self.red_target: self.red_duty += self.red_sweep_increment if self.red_duty >= self.red_target: self.red_sweeping = False else: self.red_duty -= self.red_sweep_increment if self.red_duty <= self.red_target: self.red_sweeping = False self.red_duty = max(min(self.red_duty, 100), 0) pwm.set_duty_cycle(self.red, self.red_duty) elif numpy.random.randint(0, 100) < self.red_ud_percent: self.red_sweeping = True self.red_target = max( min(numpy.random.normal(self.red_mean, self.red_sigma), 100), 0) self.red_sweep_increment = abs(self.red_duty - self.red_target) / max( self.red_sweep_sigma, numpy.random.normal( self.red_sweep_mean, self.red_sweep_sigma)) ## handle yellow if self.yellow_cooldown <= 0: # yellow off if numpy.random.randint(0, 100) < self.yellow_ud_percent: self.yellow_cooldown = numpy.random.normal( self.yellow_on_mean, self.yellow_on_sigma) self.yellow_ud_percent = 0 else: self.yellow_ud_percent += self.yellow_percent_increment else: self.yellow_cooldown -= self.fire_update_delay if self.yellow_cooldown <= 0: pwm.set_duty_cycle(self.yellow, 0) else: pwm.set_duty_cycle( self.yellow, max( min((abs(self.yellow_mean - numpy.random.normal( self.yellow_mean, self.yellow_sigma)) * self.yellow_mult), 100), 0))
def test_pwm_duty_cycle_invalid_value_string(self): PWM.start("XIO-P7", 0) with pytest.raises(TypeError): PWM.set_duty_cycle("XIO-P7", "a") PWM.cleanup()
def test_pwm_duty_cycle_invalid_value_negative(self): PWM.start("XIO-P7", 0) with pytest.raises(ValueError): PWM.set_duty_cycle("XIO-P7", -1) PWM.cleanup()
def test_pwm_duty_cycle_invalid_key(self): with pytest.raises(ValueError): PWM.set_duty_cycle("P9_15", 100) PWM.cleanup()
def test_pwm_duty_cycle_non_setup_key(self): with pytest.raises(RuntimeError): PWM.set_duty_cycle("XIO-P7", 100) PWM.cleanup()
# Run the test try: 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")
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() #OM.unload("PWM0") #GPIO.cleanup()
def clear(self): pwm.set_duty_cycle(self.red, 0) pwm.set_duty_cycle(self.yellow, 0)
def MachAnders(x): #print(x) PWM.set_duty_cycle("XIO-P4", x / 4)
def callback(cmd): if cmd.s1 > 0: SPWM.set_duty_cycle("CSID0", cmd.s1) SPWM.set_duty_cycle("CSID1", 0) print "pin 0 set to ", cmd.s1 print "pin 1 set to ", 0 else: SPWM.set_duty_cycle("CSID0", 0) SPWM.set_duty_cycle("CSID1", -cmd.s1) print "pin 0 set to ", 0 print "pin 1 set to ", -cmd.s1 if cmd.s2 > 0: SPWM.set_duty_cycle("CSID2", cmd.s2) SPWM.set_duty_cycle("CSID3", 0) print "pin 2 set to ", cmd.s2 print "pin 3 set to ", 0 else: SPWM.set_duty_cycle("CSID2", 0) SPWM.set_duty_cycle("CSID3", -cmd.s2) print "pin 2 set to ", 0 print "pin 3 set to ", -cmd.s2 if cmd.s3 > 0: SPWM.set_duty_cycle("CSID4", cmd.s3) SPWM.set_duty_cycle("CSID5", 0) print "pin 4 set to ", cmd.s3 print "pin 5 set to ", 0 else: SPWM.set_duty_cycle("CSID4", 0) SPWM.set_duty_cycle("CSID5", -cmd.s3) print "pin 4 set to ", 0 print "pin 5 set to ", -cmd.s3
def clear(self): pwm.set_duty_cycle(self.led, 0)
print 'Press ctrl+c to quit' # Loop indefinitely while True: leftmotor, rightmotor = pygamehandler(pygame.event.get()) if rightmotor > 0: drive0 = rightdrive off0 = rightback if rightmotor < 0: drive0 = rightback off0 = rightdrive if leftmotor > 0: drive1 = leftdrive off1 = leftback if leftmotor < 0: drive1 = leftback off1 = leftdrive GPIO.output(drive0, GPIO.HIGH) GPIO.output(drive1, GPIO.HIGH) GPIO.output(off0, GPIO.LOW) GPIO.output(off1, GPIO.LOW) SPWM.set_duty_cycle(PWM_0, abs(leftmotor * 100)) SPWM.set_duty_cycle(PWM_1, abs(rightmotor * 100)) time.sleep(interval) motoroff() except KeyboardInterrupt: # CTRL+C exit, disable all drives motoroff() SPWM.cleanup() GPIO.cleanup()
def setPin(self, pin, value): # we assume RGB values from 0 to 255, but SPWM expects 0 to 100, # so we have to normalize duty = value / 2.55 print('setting value ' + str(duty) + ' for ' + pin) SPWM.set_duty_cycle(pin, duty)
def motorSpeed(p): PWM.set_duty_cycle(pin_pwm, p)