def test_servo_config_min_max_pulse(self): self.board.servo_config(2, 600, 2000) data = chain([0xF0, 0x70, 2], to_two_bytes(600), to_two_bytes(2000), [0xF7, 0xE0 + 2, 0, 0]) self.assert_serial(*data)
def test_to_two_bytes(self): for i in range(32768): val = to_two_bytes(i) self.assertEqual(len(val), 2) self.assertEqual(to_two_bytes(32767), bytearray(b'\x7f\xff')) self.assertRaises(ValueError, to_two_bytes, 32768)
def test_servo_config(self): self.board.servo_config(2) data = chain([0xF0, 0x70, 2], to_two_bytes(544), to_two_bytes(2400), [0xF7, 0xE0 + 2, 0, 0]) self.assert_serial(*list(data))
def test_set_mode_servo(self): p = self.board.digital[2] p.mode = pyfirmata.SERVO data = chain([chr(0xF0), chr(0x70), chr(2)], to_two_bytes(544), to_two_bytes(2400), chr(0xF7), chr(0xE0 + 2), chr(0), chr(0)) self.assert_serial(*data)
def test_servo_config_min_max_pulse_angle(self): self.board.servo_config(2, 600, 2000, angle=90) data = chain([0xF0, 0x70, 2], to_two_bytes(600), to_two_bytes(2000), [0xF7]) angle_set = [0xE0 + 2, 90 % 128, 90 >> 7] # Angle set happens through analog message data = list(data) + angle_set self.assert_serial(*data)
def test_set_mode_servo(self): p = self.board.digital[2] p.mode = pyfirmata.SERVO data = chain([0xF0, 0x70, 2], to_two_bytes(544), to_two_bytes(2400), [0xF7, 0xE0 + 2, 0, 0]) self.assert_serial(*data)
def test_servo_config_min_max_pulse_angle(self): self.board.servo_config(2, 600, 2000, angle=90) data = chain([chr(0xF0), chr(0x70), chr(2)], to_two_bytes(600), to_two_bytes(2000), chr(0xF7)) angle_set = [chr(0xE0 + 2), chr(90 % 128), chr(90 >> 7)] # Angle set happens through analog message data = list(data) + angle_set self.assert_serial(*data)
def test_from_two_bytes(self): for i in range(32766, 32768): val = to_two_bytes(i) ret = from_two_bytes(val) self.assertEqual(ret, i) self.assertEqual(from_two_bytes(('\xff', '\xff')), 32767) self.assertEqual(from_two_bytes(('\x7f', '\xff')), 32767)
def exec_ping(self, trigger_mode=1, trigger_duration=10, echo_timeout=65000): """ Trigger the pin and wait for a pulseIn echo. Used with HC-SR04 ultrasonic ranging sensors :arg trigger_mode: Uses value as a boolean, 0 to trigger LOW, 1 to trigger HIGH (default, for HC-SR04 modules). :arg trigger_duration: Duration (us) for the trigger signal. :arg echo_timeout: Time (us) to wait for the echo (pulseIn timeout). """ if trigger_mode not in (0, 1): raise IOError("trigger_mode should be 0 or 1") # This is the protocol to ask for a pulseIn: # START_SYSEX(0xF0) // send_sysex(...) # puseIn/pulseOut(0x74) // send_sysex(PULSE_IN, ...) # pin(0-127) # value(1 or 0, HIGH or LOW) # pulseOutDuration 0 (LSB) # pulseOutDuration 0 (MSB) # pulseOutDuration 1 (LSB) # pulseOutDuration 1 (MSB) # pulseOutDuration 2 (LSB) # pulseOutDuration 2 (MSB) # pulseOutDuration 3 (LSB) # pulseOutDuration 3 (MSB) # pulseInTimeout 0 (LSB) # pulseInTimeout 0 (MSB) # pulseInTimeout 1 (LSB) # pulseInTimeout 1 (MSB) # pulseInTimeout 2 (LSB) # pulseInTimeout 2 (MSB) # pulseInTimeout 3 (LSB) # pulseInTimeout 3 (MSB) # END_SYSEX(0xF7) // send_sysex(...) data = bytearray() data.append(self.index) # Pin number data.append(trigger_mode) # Trigger mode (1 or 0, HIGH or LOW) trigger_duration_arr = util.to_two_bytes((trigger_duration >> 24) & 0xFF) \ + util.to_two_bytes((trigger_duration >> 16) & 0xFF) \ + util.to_two_bytes((trigger_duration >> 8) & 0xFF) \ + util.to_two_bytes(trigger_duration & 0xFF) data.extend(trigger_duration_arr) # pulseOutDuration echo_timeout_arr = util.to_two_bytes((echo_timeout >> 24) & 0xFF) \ + util.to_two_bytes((echo_timeout >> 16) & 0xFF) \ + util.to_two_bytes((echo_timeout >> 8) & 0xFF) \ + util.to_two_bytes(echo_timeout & 0xFF) data.extend(echo_timeout_arr) # pulseInTimeout self._interfaz.send_sysex(PING_READ, data)
def send_cmd_serial(connection, to, operation, operand1=0, operand2=0): operand2_str = "%s%s" % util.to_two_bytes(operand2) string = 'AF%sZ%s%s%sFA' % (to, operation, operand1, operand2_str) return connection.write(string)
def test_servo_config_min_max_pulse(self): self.board.servo_config(2, 600, 2000) data = chain([chr(0xF0), chr(0x70), chr(2)], to_two_bytes(600), to_two_bytes(2000), chr(0xF7), chr(0xE0 + 2), chr(0), chr(0)) self.assert_serial(*data)
def test_servo_config(self): self.board.servo_config(2) data = chain([chr(0xF0), chr(0x70), chr(2)], to_two_bytes(544), to_two_bytes(2400), chr(0xF7), chr(0xE0 + 2), chr(0), chr(0)) self.assert_serial(*data)
def send_cmd(to, operation, operand1=0, operand2=0): operand2_str = "%s%s" % util.to_two_bytes(operand2) string = 'AF%sZ%s%s%sFA' % (to, operation, operand1, operand2_str) print "sending %s" % string return connection.write(string)
def configServo(self, pin, min_pulse=544, max_pulse=2400): data = itertools.chain([pin], util.to_two_bytes(min_pulse), util.to_two_bytes(max_pulse), [self.robotid]) self.board.board.send_sysex(SERVO_CONFIG, data)
def set_sampling_interval(self, interval): self._interfaz.send_sysex(SAMPLING_INTERVAL, util.to_two_bytes(interval))
def moveServo(self, pin, angle): '''Pasa un ángulo entre 0 y 180 grados al servo conectado al pin indicado''' data = itertools.chain([pin], reversed(util.to_two_bytes(angle)), [self.robotid]) self.board.board.send_sysex(MOVE_SERVO, data)