Beispiel #1
0
def main():
    
    logging.basicConfig(level=logging.INFO)

    global motor_state
   

    try:
        motor_state = 0
        hub = MoveHub()

        hub.button.subscribe(button_callback)

        #hub.vision_sensor.subscribe(callback, mode=VisionSensor.COLOR_DISTANCE_FLOAT)

        while True:

            moveslow(hub)

            #if motor_state ==  1:
            #    hub.button.unsubscribe()
            #    hub.disconnect()

            #if not hub.connection.is_alive():
            #    connect()
            #    hub.vision_sensor.subscribe(callback, mode=VisionSensor.COLOR_DISTANCE_FLOAT)
            #    print("Reconnection!")
            #else:
            #    print("Connected!")
            sleep(3)

    except:
        print("ex!")
        sleep(5)
        main()
        
    finally:
        #hub.vision_sensor.unsubscribe(callback)
        #hub.disconnect()
        hub.disconnect()
Beispiel #2
0
                    if known_face_name != nameOld:
                        shake_head()
                        say_text('Hello, ' + known_face_name)
                    nameOld = known_face_name
                else:
                    # If an unknown face has been detected for a few times, add it to the list
                    unknown_face_count += 1
                    if unknown_face_count > unknown_face_limit:
                        unknown_face_count = 0
                        add_face(face_encoding)

        process_this_frame = not process_this_frame


if __name__ == '__main__':
    logging.basicConfig(
        level=logging.INFO,
        format='%(relativeCreated)d\t%(levelname)s\t%(name)s\t%(message)s')
    try:
        train_faces()
        say_text('Hi, I\'m Verny! Please push on the green button')
        movehub = MoveHub()
        movehub.vision_sensor.subscribe(wave_callback)
        dramatic_turn()
        do_image_recognition()
    finally:
        # Release handle to the webcam
        video_capture.release()
        say_text('Okay, goodbye!')
        movehub.disconnect()
Beispiel #3
0
class Joystick(object):
    def __init__(self):
        super(Joystick, self).__init__()
        self._on_joystick = set()
        self._hub = MoveHub()

        self._reset_sensors()

        self.button_pressed = False
        self._hub.button.subscribe(self._on_btn)

        self._angle_A = 0
        self._on_motor_a(self._on_a)

        self.angle_B = 0
        self.on_rotation(self._on_b)

        self._angle_C = 0
        self._on_motor_c(self._on_c)

        logging.info("Done initializing")

    def disconnect(self):
        self._hub.disconnect()

    def _reset_sensors(self):
        logging.info("Resetting motor encoders")
        self._hub.motor_A.preset_encoder()
        self._hub.motor_B.preset_encoder()
        self._hub.motor_external.preset_encoder()

    def on_button(self, callback):
        """
        Notifies about button state change. ``callback(state)`` gets single bool parameter
        """
        def wrapper(state):
            if state in (0, 1):
                callback(bool(state))

        self._hub.button.subscribe(wrapper)

    def _on_motor_a(self, callback):
        def wrapper(angle):
            logging.debug("Raw angle: %s", angle)
            spread = 25
            angle = _clamp(-spread, angle, spread)
            callback(angle)

        self._hub.motor_A.subscribe(wrapper)

    def on_rotation(self, callback):
        """
        Notifies about B motor rotation. ``callback(state)`` gets single int parameter from 0 to 359
        """
        def wrapper(angle):
            logging.debug("Raw angle: %s", angle)
            val = angle % 360
            callback(val if val >= 0 else 360 - val)

        self._hub.motor_B.subscribe(wrapper)

    def _on_motor_c(self, callback):
        def wrapper(angle):
            logging.debug("Raw angle: %s", angle)
            spread = 25
            angle = _clamp(-spread, angle, spread)
            callback(angle)

        self._hub.motor_external.subscribe(wrapper)

    def _on_btn(self, state):
        self.button_pressed = bool(state)

    def _on_a(self, angle):
        logging.debug("A rotated: %s", angle)
        self._angle_A = angle
        self._calc_joystick()

    def _on_b(self, angle):
        logging.debug("B rotated: %s", angle)
        self.angle_B = angle

    def _on_c(self, angle):
        logging.debug("C rotated: %s", angle)
        self._angle_C = angle
        self._calc_joystick()

    def on_joystick(self, callback):
        """
        Notifies about joystick change. ``callback(speed, direction)`` gets parameters:
        - ``speed`` - float value from 0.0 to 1.0
        - ``direction`` - int value from 0 to 359

        """
        self._on_joystick.add(callback)

    def _calc_joystick(self):
        norm_a = self._angle_A / 25
        norm_b = self._angle_C / 25
        logging.info("%s / %s", self._angle_A, self._angle_C)
        logging.info("%s / %s", norm_a, norm_b)
        speed = math.sqrt(norm_a**2 + norm_b**2) / math.sqrt(2)
        speed = _clamp(-1.0, speed, 1.0)

        maxsize = sys.maxsize if norm_a >= 0 else -sys.maxsize
        direction = math.atan(norm_a / norm_b if norm_b else maxsize)
        direction *= 180 / math.pi
        if norm_a >= 0 and norm_b >= 0:
            logging.info("Sector 1")
            direction = 90 - direction
        elif norm_a < 0 and norm_b >= 0:
            logging.info("Sector 2")
            direction = 90 - direction
        elif norm_a < 0 and norm_b < 0:
            logging.info("Sector 3")
            direction = 270 - direction
        else:
            logging.info("Sector 4")
            direction = 270 - direction

        for callback in self._on_joystick:
            callback(speed, 359 - int(direction))
Beispiel #4
0
        log.info("Voltage: %s", value)

    movehub.current.subscribe(callback1, mode=Current.CURRENT_L, granularity=0)
    movehub.current.subscribe(callback1, mode=Current.CURRENT_L, granularity=1)

    movehub.voltage.subscribe(callback2, mode=Voltage.VOLTAGE_L, granularity=0)
    movehub.voltage.subscribe(callback2, mode=Voltage.VOLTAGE_L, granularity=1)
    time.sleep(5)
    movehub.current.unsubscribe(callback1)
    movehub.voltage.unsubscribe(callback2)


def demo_all(movehub):
    demo_voltage(movehub)
    demo_led_colors(movehub)
    demo_motors_timed(movehub)
    demo_motors_angled(movehub)
    demo_port_cd_motor(movehub)
    demo_tilt_sensor_simple(movehub)
    demo_tilt_sensor_precise(movehub)
    demo_color_sensor(movehub)
    demo_motor_sensors(movehub)


if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)

    hub = MoveHub()
    demo_all(hub)
    hub.disconnect()
class Joystick(object):
    RANGE_A = 40
    RANGE_C = 30

    def __init__(self):
        super(Joystick, self).__init__()
        self._on_joystick = set()
        self.button_pressed = False
        self._angle_A = 0
        self.angle_B = 0
        self._angle_C = 0

        print("Starting search for Joystick...")
        self._hub = MoveHub()
        self._reset_sensors()
        self._hub.button.subscribe(self._on_btn)
        self._on_motor_a(self._on_a)
        self.on_rotation(self._on_b)
        self._on_motor_c(self._on_c)

        print("Joystick is ready")

    def disconnect(self):
        print("Joystick disconnects")
        self._hub.disconnect()

    def _reset_sensors(self):
        logging.info("Resetting motor encoders")
        self._hub.motor_A.preset_encoder()
        self._hub.motor_B.preset_encoder()
        self._hub.motor_external.preset_encoder()

    def on_button(self, callback):
        """
        Notifies about button state change. ``callback(state)`` gets single bool parameter
        """
        def wrapper(state):
            if state in (0, 1):
                callback(bool(state))

        self._hub.button.subscribe(wrapper)

    def _on_motor_a(self, callback):
        def wrapper(angle):
            logging.debug("Raw angle: %s", angle)
            angle = _clamp(-self.RANGE_A, angle, self.RANGE_A)
            callback(angle)

        self._hub.motor_A.subscribe(wrapper)

    def on_rotation(self, callback):
        """
        Notifies about B motor rotation. ``callback(state)`` gets single int parameter from 0 to 359
        """
        def wrapper(angle):
            logging.debug("Raw angle: %s", angle)
            val = angle % 360
            val = val if val >= 0 else 360 - val - 1
            val = 359 - val
            callback(val)

        self._hub.motor_B.subscribe(wrapper)

    def _on_motor_c(self, callback):
        def wrapper(angle):
            logging.debug("Raw angle: %s", angle)
            angle = _clamp(-self.RANGE_C, angle, self.RANGE_C)
            callback(angle)

        self._hub.motor_external.subscribe(wrapper)

    def _on_btn(self, state):
        self.button_pressed = bool(state)

    def _on_a(self, angle):
        logging.debug("A rotated: %s", angle)
        self._angle_A = angle
        self._calc_joystick()

    def _on_b(self, angle):
        logging.debug("B rotated: %s", angle)
        self.angle_B = angle

    def _on_c(self, angle):
        logging.debug("C rotated: %s", angle)
        self._angle_C = angle
        self._calc_joystick()

    def on_joystick(self, callback):
        """
        Notifies about joystick change. ``callback(speed, direction)`` gets parameters:
        - ``speed`` - int value from 0 to 10
        - ``direction`` - int value from 0 to 359

        """
        self._on_joystick.add(callback)

    def _calc_joystick(self):
        norm_a = self._angle_A / self.RANGE_A
        norm_b = self._angle_C / self.RANGE_C
        logging.debug("%s / %s", self._angle_A, self._angle_C)
        logging.debug("%s / %s", norm_a, norm_b)
        speed = math.sqrt(norm_a**2 + norm_b**2)  # / math.sqrt(2)
        speed = _clamp(-1.0, speed, 1.0)

        maxsize = sys.maxsize if norm_a >= 0 else -sys.maxsize
        direction = math.atan(norm_a / norm_b if norm_b else maxsize)
        direction *= 180 / math.pi
        if norm_a >= 0 and norm_b >= 0:
            direction = 90 - direction
        elif norm_a < 0 and norm_b >= 0:
            direction = 90 - direction
        elif norm_a < 0 and norm_b < 0:
            direction = 270 - direction
        else:
            direction = 270 - direction

        for callback in self._on_joystick:
            callback(int(round(10 * speed)), int(direction))