コード例 #1
0
ファイル: video.py プロジェクト: v1ckxy/pycozmo
def pycozmo_program(cli: pycozmo.client.Client):

    global last_im

    # Raise head.
    angle = (pycozmo.robot.MAX_HEAD_ANGLE.radians -
             pycozmo.robot.MIN_HEAD_ANGLE.radians) / 2.0
    cli.set_head_angle(angle)

    # Register to receive new camera images.
    cli.add_handler(pycozmo.event.EvtNewRawCameraImage, on_camera_image)

    # Enable camera.
    pkt = pycozmo.protocol_encoder.EnableCamera()
    cli.conn.send(pkt)

    while True:

        if last_im:

            # Get last image.
            im = last_im

            # Resize from 320x240 to 128x32.
            im = im.resize((128, 32))
            # Convert to binary image.
            im = im.convert('1')

            # Display the result image.
            cli.display_image(im)

        # Run with 25 FPS.
        time.sleep(1 / 25)
コード例 #2
0
ファイル: display_image.py プロジェクト: zhangrelay/pycozmo
def pycozmo_program(cli: pycozmo.client.Client):

    angle = (pycozmo.robot.MAX_HEAD_ANGLE.radians - pycozmo.robot.MIN_HEAD_ANGLE.radians) / 2.0
    cli.set_head_angle(angle)
    time.sleep(1)

    # Load image
    im = Image.open(os.path.join(os.path.dirname(__file__), "..", "assets", "pycozmo.png"))
    # Convert to binary image.
    im = im.convert('1')

    cli.display_image(im, 10)
コード例 #3
0
ファイル: camera.py プロジェクト: pajp/cozmo
def pycozmo_program(cli: pycozmo.client.Client):

    cli.conn.add_handler(pycozmo.protocol_encoder.RobotState, on_robot_state, one_shot=True)
    cli.conn.add_handler(pycozmo.protocol_encoder.RobotPoked, on_robot_poked)
    cli.conn.add_handler(pycozmo.protocol_encoder.FallingStarted, on_robot_falling_started)
    cli.conn.add_handler(pycozmo.protocol_encoder.FallingStopped, on_robot_falling_stopped)
    cli.conn.add_handler(pycozmo.protocol_encoder.ButtonPressed, on_button_pressed)
    cli.add_handler(pycozmo.event.EvtRobotPickedUpChange, on_robot_picked_up)
    cli.add_handler(pycozmo.event.EvtRobotChargingChange, on_robot_charging)
    cli.add_handler(pycozmo.event.EvtCliffDetectedChange, on_cliff_detected)
    cli.add_handler(pycozmo.event.EvtRobotWheelsMovingChange, on_robot_wheels_moving)


    heart = Image.open("hjarta.png")
    heart = heart.convert('1')
    eyes = Image.open("eyes.png")
    eyes = eyes.convert('1')

    cli.display_image(eyes)
    
    angle = (pycozmo.robot.MAX_HEAD_ANGLE.radians - pycozmo.robot.MIN_HEAD_ANGLE.radians) / 2.0
    #cli.set_head_angle(angle)
    cli.set_head_angle(0)

    pkt = pycozmo.protocol_encoder.EnableCamera(enable=True)
    cli.conn.send(pkt)
    pkt = pycozmo.protocol_encoder.EnableColorImages(enable=True)
    cli.conn.send(pkt)

    # Wait for image to stabilize.
    time.sleep(1.0)

    cli.add_handler(pycozmo.event.EvtNewRawCameraImage, on_camera_image, one_shot=False)
    speed = 100
    cli.drive_wheels(lwheel_speed=-speed,rwheel_speed=-speed, duration=0.5)
    cli.drive_wheels(lwheel_speed=speed,rwheel_speed=speed, duration=1.0)
    cli.drive_wheels(lwheel_speed=speed,rwheel_speed=speed, duration=3.0)
    time.sleep(1)
    cli.display_image(heart)
    time.sleep(1)
    cli.display_image(eyes)
    
    turnduration=1.38
    cli.drive_wheels(lwheel_speed=speed,rwheel_speed=-speed, duration=turnduration)
    cli.drive_wheels(lwheel_speed=speed,rwheel_speed=speed, duration=2.25)
    time.sleep(1)
    cli.drive_wheels(lwheel_speed=speed,rwheel_speed=-speed, duration=turnduration)
    cli.display_image(heart)
    cli.drive_wheels(lwheel_speed=-speed,rwheel_speed=-speed, duration=1.7)
    cli.display_image(eyes)    
    time.sleep(1)
コード例 #4
0
def pycozmo_program(cli: pycozmo.client.Client):

    angle = (pycozmo.robot.MAX_HEAD_ANGLE.radians -
             pycozmo.robot.MIN_HEAD_ANGLE.radians) / 2.0
    cli.set_head_angle(angle)
    time.sleep(1)

    # Render a 128x64 procedural face with default parameters.
    f = pycozmo.procedural_face.ProceduralFace()
    im = f.render()

    # The Cozmo protocol expects a 128x32 image, so take only the even lines.
    np_im = np.array(im)
    np_im2 = np_im[::2]
    im2 = Image.fromarray(np_im2)

    cli.display_image(im2, 10)
コード例 #5
0
def pycozmo_program(cli: pycozmo.client.Client):

    angle = (pycozmo.robot.MAX_HEAD_ANGLE.radians -
             pycozmo.robot.MIN_HEAD_ANGLE.radians) / 2.0
    cli.set_head_angle(angle)
    time.sleep(1)

    # Generate random dots.
    dots = []
    for i in range(NUM_DOTS):
        x = random.randint(0, WIDTH)
        y = random.randint(0, HEIGHT)
        vx = random.randint(-MAX_SPEED, MAX_SPEED)
        vy = random.randint(-MAX_SPEED, MAX_SPEED)
        dot = Dot(x, y, vx, vy)
        dots.append(dot)

    while True:

        # Create a blank image.
        im = Image.new("1", (128, 32), color=0)

        # Draw lines.
        draw = ImageDraw.Draw(im)
        for a, b in itertools.combinations(dots, 2):
            draw.line((a.x, a.y, b.x, b.y), width=LINE_WIDTH, fill=1)

        # Move dots.
        for dot in dots:
            dot.x += dot.vx
            dot.y += dot.vy
            if dot.x <= DOT_SIZE:
                dot.x = DOT_SIZE
                dot.vx = abs(dot.vx)
            elif dot.x >= WIDTH - DOT_SIZE:
                dot.x = WIDTH - DOT_SIZE
                dot.vx = -abs(dot.vx)
            if dot.y <= DOT_SIZE:
                dot.y = DOT_SIZE
                dot.vy = abs(dot.vy)
            elif dot.y >= HEIGHT - DOT_SIZE:
                dot.y = HEIGHT - DOT_SIZE
                dot.vy = -abs(dot.vy)

        cli.display_image(im, DELAY)