def test_start_pwm(self): PWM.start("P9_14", 0) files = os.listdir('/sys/devices') ocp = '/sys/devices/'+[s for s in files if s.startswith('ocp')][0] files = os.listdir(ocp) pwm_test = ocp+'/'+[s for s in files if s.startswith('pwm_test_P9_14')][0] assert os.path.exists(pwm_test) duty = open(pwm_test + '/duty').read() period = open(pwm_test + '/period').read() assert int(duty) == 0 assert int(period) == 500000 PWM.cleanup()
def convert(s): global last values = map(ord, s) output = '' #send servo commands to Arduino based on command string for x in range(1, 6): if values[8] == 250: output = 'FAILSAFE MODE!!!!!!!!!!!!!!!!!!!!!!!' elif values[8] == 125: arm_motors() else: if values[x] != last[x]: mapped = map_val(values[x], 0, 250, 5.0, 10.0) PWM.set_duty_cycle(pins[x], float(mapped)) if x < 4: output += ' | ' + str(mapped) + ',' else: output += ' | ' + str(mapped) output += str(values[x]) + ' | ' print output last = s
def test_pwm_start_invalid_frequency_string(self): with pytest.raises(TypeError): PWM.start("P9_14", 0, "1")
def test_pwm_start_invalid_frequency_negative(self): with pytest.raises(ValueError): PWM.start("P9_14", 0, -1)
def teardown_module(module): PWM.cleanup()
def test_pwm_start_invalid_duty_cycle_string(self): with pytest.raises(TypeError): PWM.start("P9_14", "1")
def test_pwm_start_invalid_duty_cycle_high(self): with pytest.raises(ValueError): PWM.start("P9_14", 101)
def init_pwm(): # due to a small issue with the library, # the first pin initialized is not usable # until it is stopped and started again # after this, all pins work normally PWM.start(pins[1], 7.5) time.sleep(0.5) PWM.stop(pins[1]) time.sleep(0.5) # start PWM output at 50Hz, all middle except throttle # 1.5/20 = .075 = 7.5% PWM.start(pins[1], 7.5, 50) PWM.start(pins[2], 7.5, 50) PWM.start(pins[3], 7.5, 50) PWM.start(pins[4], 10, 50) PWM.start(pins[5], 7.64, 50) PWM.on(pins[1]) PWM.on(pins[2]) PWM.on(pins[3]) PWM.on(pins[4]) PWM.on(pins[5])
def test_pwm_start_invalid_pwm_key(self): with pytest.raises(ValueError): PWM.start("P8_25", -1)
def test_pwm_freq_non_setup_key(self): with pytest.raises(ValueError): PWM.set_frequency("P9_15", 100) PWM.cleanup()
def test_pwm_frequency_invalid_value_string(self): PWM.start("P9_14", 0) with pytest.raises(TypeError): PWM.set_frequency("P9_14", "11") PWM.cleanup()
def test_pwm_frequency_invalid_value_negative(self): PWM.start("P9_14", 0) with pytest.raises(ValueError): PWM.set_frequency("P9_14", -1) PWM.cleanup()
def test_pwm_duty_cycle_invalid_value_string(self): PWM.start("P9_14", 0) with pytest.raises(TypeError): PWM.set_duty_cycle("P9_14", "a") PWM.cleanup()
def arm_motors(): PWM.set_duty_cycle(pins[1], 5) PWM.set_duty_cycle(pins[2], 10) PWM.set_duty_cycle(pins[3], 5) PWM.set_duty_cycle(pins[4], 10)
def test_pwm_duty_cycle_non_setup_key(self): with pytest.raises(RuntimeError): PWM.set_duty_cycle("P9_14", 100) PWM.cleanup()
def test_pwm_start_invalid_duty_cycle_negative(self): with pytest.raises(ValueError): PWM.start("P9_14", -1)
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_valid_duty_cycle_max(self): #testing an exception isn't thrown PWM.start("P9_14", 100) PWM.cleanup()
def test_pwm_duty_cycle_invalid_value_negative(self): PWM.start("P9_14", 0) with pytest.raises(ValueError): PWM.set_duty_cycle("P9_14", -1) PWM.cleanup()
#this will eventually be used to send sensor data back to command s.sendall(last) except KeyboardInterrupt: raise except: s.close() print 'CONNECTION LOST: ATTEMPTING RECONNECT -- '# + msg[1] #loop to beginning except socket.error, msg: print 'Send failed: ' except KeyboardInterrupt: raise except socket.error, msg: print 'Failed to create socket. Error code: ' + str(msg[0]) + ' Error message: ' + msg[1] except KeyboardInterrupt: print 'Closing socket...' s.close() print 'Shutting down PWM outputs...' PWM.cleanup() time.sleep(.5) sys.exit() # Allow use as a module or standalone script if __name__ == "__main__": main()