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 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 sig_handler(signal, frame): OM.unload("PWM0") GPIO.cleanup() PWM.cleanup() SPWM.cleanup() UT.disable_1v8_pin() sys.exit(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 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 setup(): #PWM.start(channel, duty, freq=2000, polarity=0) #duty values are valid 0 (off) to 100 (on) GPIO.setup(pin_forward, GPIO.OUT) GPIO.setup(pin_back, GPIO.OUT) PWM.start(pin_pwm, 0, 800, 0) motorStop()
def test_start_pwm(self): PWM.start("XIO-P7", 50, 10) base = GPIO.get_gpio_base() + 7 gfile = '/sys/class/gpio/gpio%d' % base assert os.path.exists(gfile) direction = open(gfile + '/direction').read() assert(direction == 'out\n') PWM.cleanup()
def __init__(self, pin, freq=500, duty_min=49.0, duty_max=90.0, stop_on_zero=False): self.pin = pin self.freq = freq self.duty_min = duty_min self.duty_max = duty_max self.stop_on_zero = stop_on_zero sleep(0.1) PWM.start(pin, 0, 500)
def RestApp(host="0.0.0.0", port=1883, debug=False): try: app.run(host=host, port=port, debug=debug) except KeyboardInterrupt: GPIO.cleanup() PWM.cleanup() SPWM.cleanup() UT.disable_1v8_pin() sys.exit(0)
def pwm(): rospy.init_node('pwm') rospy.Subscriber('speeds', Speeds, callback) while not rospy.is_shutdown(): rospy.spin() SPWM.cleanup() UT.unexport_all()
def __init__(self, pin, freq=50, duty_min=5, duty_max=10, stop_on_zero=False): self.pin = pin self.freq = freq self.duty_min = duty_min self.duty_max = duty_max self.stop_on_zero = stop_on_zero sleep(0.1) PWM.start(pin, 0, freq)
def setup(self): if not self.initialized: # reset gpio GPIO.cleanup(self.led) # init gpio pwm.start(self.led, 0, self.freq) self.currenta_angle = 0 self.delta = self.max_value - self.min_value self.angle_increment = (self.update_delay * 180) / (self.sweep_time * 60) self.initialized = True
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 setup(self): if not self.initialized: # reset gpio GPIO.cleanup(self.red) GPIO.cleanup(self.yellow) # init gpio pwm.start(self.red, 0, self.freq) pwm.start(self.yellow, 0, self.freq) self.initialized = True # init variables self.red_sweeping = False self.red_sweep_increment = 0 self.red_duty = 0 self.red_target = 0 self.yellow_ud_percent = 0 self.yellow_cooldown = 0
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_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_non_setup_key(self): with pytest.raises(RuntimeError): PWM.set_duty_cycle("XIO-P7", 100) 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_start_invalid_polarity_type(self): with pytest.raises(TypeError): PWM.start("XIO-P7", 0, 100, "1")
def teardown_module(module): 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()
def test_pwm_start_invalid_pwm_key(self): with pytest.raises(ValueError): PWM.start("P8_25", -1)
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()
import datetime if __name__ == "__main__": # SETUP VARIABLES 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:
def test_pwm_start_invalid_frequency_string(self): with pytest.raises(TypeError): PWM.start("XIO-P7", 0, "1")
def test_pwm_start_invalid_duty_cycle_string(self): with pytest.raises(TypeError): PWM.start("XIO-P7", "1")
def test_pwm_start_valid_duty_cycle_max(self): # testing an exception isn't thrown PWM.start("XIO-P7", 100) PWM.cleanup()
def test_start_pwm(self): PWM.start("XIO-P7", 50, 10) assert os.path.exists('/sys/class/gpio/gpio415') direction = open('/sys/class/gpio/gpio415/direction').read() assert direction == 'out\n' PWM.cleanup()
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_start_invalid_positive_polarity(self): with pytest.raises(ValueError): PWM.start("XIO-P7", 0, 100, 2)
def setup_method(self, test_method): PWM.cleanup()
def test_pwm_start_invalid_duty_cycle_negative(self): with pytest.raises(ValueError): PWM.start("XIO-P7", -1)
def test_pwm_start_invalid_duty_cycle_high(self): with pytest.raises(ValueError): PWM.start("XIO-P7", 101)
def test_pwm_start_invalid_frequency_negative(self): with pytest.raises(ValueError): PWM.start("XIO-P7", 0, -1)
def test_pwm_start_negative_polarity(self): with pytest.raises(ValueError): PWM.start("XIO-P7", 0, 100, -1)