#!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import Motor, TouchSensor, ColorSensor
from pybricks.parameters import Port, Stop, Direction
from pybricks.tools import wait
# Initialize the EV3 Brick
ev3 = EV3Brick()
# Configure the gripper motor on Port A with default settings.
gripper_motor = Motor(Port.A)
elbow_motor = Motor(Port.B, Direction.COUNTERCLOCKWISE, [8, 40])
base_motor = Motor(Port.C, Direction.COUNTERCLOCKWISE, [12, 36])
elbow_motor.control.limits(speed=60, acceleration=120)
base_motor.control.limits(speed=60, acceleration=120)
base_switch = TouchSensor(Port.S1)
# Set up the Color Sensor. This sensor detects when the elbow
# is in the starting position. This is when the sensor sees the
# white beam up close.
elbow_sensor = ColorSensor(Port.S3)
elbow_motor.run_time(-30, 1000)
elbow_motor.run(15)
while elbow_sensor.reflection() < 32:
wait(10)
elbow_motor.reset_angle(0)
elbow_motor.hold()
base_motor.run(-60)
while not base_switch.pressed():
wait(10)
base_motor.reset_angle(0)
base_motor.hold()
gripper_motor.run_until_stalled(200, then=Stop.COAST, duty_limit=50)
gripper_motor.reset_angle(0)
gripper_motor.run_target(200, -90)
def robot_pick(position):
base_motor.run_target(60, position)
# Lower the arm.
elbow_motor.run_target(60, -40)
# Close the gripper to grab the wheel stack.
gripper_motor.run_until_stalled(200, then=Stop.HOLD, duty_limit=50)
# Raise the arm to lift the wheel stack.
elbow_motor.run_target(60, 0)
def robot_release(position):
base_motor.run_target(60, position)
# Lower the arm to put the wheel stack on the ground.
elbow_motor.run_target(60, -40)
# Open the gripper to release the wheel stack.
gripper_motor.run_target(200, -90)
# Raise the arm.
elbow_motor.run_target(60, 0)
# Play three beeps to indicate that the initialization is complete.
for i in range(3):
ev3.speaker.beep()
wait(100)
LEFT = 160
MIDDLE = 100
RIGHT = 40
while True:
# Move a wheel stack from the left to the middle.
robot_pick(LEFT)
robot_release(MIDDLE)
# Move a wheel stack from the right to the left.
robot_pick(RIGHT)
robot_release(LEFT)
# Move a wheel stack from the middle to the right.
robot_pick(MIDDLE)
robot_release(RIGHT)
銜接主流的程式語言
EV3 機器人程式除了以 Labview based 的 Mindstorms-G 圖像程式,也可相容其他主流程式軟體,例:C++、Python、Scratch、Ev3basic 等等。