Пример #1
0
    def _attach_device(self, dev_type, port):
        if port in PORTS and dev_type in DEVICE_TYPES:
            log.info("Attached %s on port %s", DEVICE_TYPES[dev_type],
                     PORTS[port])
        else:
            log.warning("Attached device 0x%x on port 0x%x", dev_type, port)

        if dev_type == DEV_MOTOR:
            self.devices[port] = EncodedMotor(self, port)
        elif dev_type == DEV_IMOTOR:
            self.motor_external = EncodedMotor(self, port)
            self.devices[port] = self.motor_external
        elif dev_type == DEV_DCS:
            self.color_distance_sensor = ColorDistanceSensor(self, port)
            self.devices[port] = self.color_distance_sensor
        elif dev_type == DEV_LED:
            self.devices[port] = LED(self, port)
        elif dev_type == DEV_TILT_SENSOR:
            self.devices[port] = TiltSensor(self, port)
        elif dev_type == DEV_AMPERAGE:
            self.devices[port] = Amperage(self, port)
        elif dev_type == DEV_VOLTAGE:
            self.devices[port] = Voltage(self, port)
        else:
            log.warning("Unhandled peripheral type 0x%x on port 0x%x",
                        dev_type, port)
            self.devices[port] = Peripheral(self, port)
def play_scenario(movehub):
    """Play a scenario
    
    movehub : The hub where motors are connected to
    """
    
    motor_a = Motor(movehub, movehub.PORT_A)
    motor_b = Motor(movehub, movehub.PORT_B)
    motor_c = EncodedMotor(movehub, movehub.PORT_C)
    
    # motor_c.subscribe(show_angle_value, mode=EncodedMotor.SENSOR_ANGLE)
    
    print("Reset angle:")
    reset_angle(motor_c)
    sleep(2)
    
    print("Forward:")
    forward(motor_a, motor_b, motor_c)
    sleep(1)
    
    print("Backward:")
    backward(motor_a, motor_b, motor_c)
    sleep(1)
    
    print("Stop")
    stop_moving(motor_a, motor_b)
    
    print("Left:")
    go_left(motor_c)
    sleep(2)

    print("Right:")
    go_right(motor_c)
    sleep(2)
Пример #3
0
 def __init__(cls):
     """Create the connection with the car"""
     cls.connection = get_connection_gatt(hub_mac=cls.MY_MOVEHUB_ADD)
     try:
         # The motors
         cls.movehub = MoveHub(cls.connection)
         cls.front_motor = Motor(cls.movehub, cls.movehub.PORT_A)
         cls.back_motor = Motor(cls.movehub, cls.movehub.PORT_B)
         cls.directionnal_motor = EncodedMotor(cls.movehub,
                                               cls.movehub.PORT_C)
         cls.old_angle = cls.DEFAULT_ANGLE
     except:
         cls.movehub = None
         cls.front_motor = None
         cls.back_motor = None
         cls.directionnal_motor = None
         cls.instance = None
         cls.connection = None
         cls.old_angle = None
    def test_motor_sensor(self):
        hub = HubMock()
        motor = EncodedMotor(hub, MoveHub.PORT_C)
        hub.peripherals[MoveHub.PORT_C] = motor

        vals = []

        def callback(*args):
            vals.append(args)

        hub.connection.notification_delayed('0a004702020100000001', 0.1)
        motor.subscribe(callback)

        hub.connection.notification_delayed("0800450200000000", 0.1)
        hub.connection.notification_delayed("08004502ffffffff", 0.2)
        hub.connection.notification_delayed("08004502feffffff", 0.3)
        time.sleep(0.4)

        hub.connection.notification_delayed('0a004702020000000000', 0.1)
        motor.unsubscribe(callback)
        hub.connection.wait_notifications_handled()

        self.assertEqual([(0, ), (-1, ), (-2, )], vals)
    def test_motor(self):
        hub = HubMock()
        motor = EncodedMotor(hub, MoveHub.PORT_D)
        hub.peripherals[MoveHub.PORT_D] = motor

        hub.connection.notification_delayed('050082030a', 0.1)
        motor.start_power(1.0)
        self.assertEqual(b"07008103110164", hub.writes.pop(1)[1])

        hub.connection.notification_delayed('050082030a', 0.1)
        motor.stop()
        self.assertEqual(b"0c0081031109000064647f03", hub.writes.pop(1)[1])

        hub.connection.notification_delayed('050082030a', 0.1)
        motor.set_acc_profile(1.0)
        self.assertEqual(b"090081031105e80300", hub.writes.pop(1)[1])

        hub.connection.notification_delayed('050082030a', 0.1)
        motor.set_dec_profile(1.0)
        self.assertEqual(b"090081031106e80300", hub.writes.pop(1)[1])

        hub.connection.notification_delayed('050082030a', 0.1)
        motor.start_speed(1.0)
        self.assertEqual(b"090081031107646403", hub.writes.pop(1)[1])

        hub.connection.notification_delayed('050082030a', 0.1)
        motor.stop()
        self.assertEqual(b"0c0081031109000064647f03", hub.writes.pop(1)[1])

        logging.debug("\n\n")
        hub.connection.notification_delayed('0500820301', 0.1)
        hub.connection.notification_delayed('050082030a', 0.2)
        motor.timed(1.0)
        self.assertEqual(b"0c0081031109e80364647f03", hub.writes.pop(1)[1])

        hub.connection.notification_delayed('0500820301', 0.1)
        hub.connection.notification_delayed('050082030a', 0.2)
        motor.angled(180)
        self.assertEqual(b"0e008103110bb400000064647f03", hub.writes.pop(1)[1])

        hub.connection.notification_delayed('050082030a', 0.2)
        motor.preset_encoder(-180)
        self.assertEqual(b"0b0081031151024cffffff", hub.writes.pop(1)[1])

        hub.connection.notification_delayed('0500820301', 0.1)
        hub.connection.notification_delayed('050082030a', 0.2)
        motor.goto_position(0)
        self.assertEqual(b"0e008103110d0000000064647f03", hub.writes.pop(1)[1])

        hub.connection.wait_notifications_handled()