sorting bird #944
MichaelVerschaeve
started this conversation in
Show and tell
Replies: 2 comments 5 replies
-
This looks like a very nice program. You can play around with sensor.detectable_colors(...) to try to get better color matching if the default is giving you trouble. Some Python style tips to make this easier to read (at least for people who are used to reading Python):
from pybricks.hubs import MoveHub
from pybricks.pupdevices import Motor, ColorDistanceSensor
from pybricks.parameters import Button, Color, Direction, Port, Side, Stop
from pybricks.robotics import DriveBase
from pybricks.tools import wait, StopWatch
hub = MoveHub()
motor_a = Motor(Port.A,Direction.COUNTERCLOCKWISE)
motor_b = Motor(Port.B,Direction.CLOCKWISE, [[24, 12], [8, 28]])
sensor = ColorDistanceSensor(Port.C)
BIRD_HEIGHT = 200
def reset():
# part A: spin away, close beak, tilt up
# motor_a.run_angle(200,-300)
motor_a.run_time(200, 300, Stop.HOLD, False)
motor_b.run_time(200, 300)
motor_a.run_time(-150, 300)
# part B: down and close until stall, then up to default angle
motor_a.run_until_stalled(100, duty_limit=40)
motor_a.run_angle(200, -BIRD_HEIGHT)
# part C
motor_a.reset_angle(0)
motor_a.run(-50)
angle_b_start = motor_b.angle()
motor_b.run_until_stalled(-50, duty_limit=40)
motor_a.hold()
angle_b_stop = motor_b.angle()
correction = -motor_a.angle() + angle_b_stop - angle_b_start
print("correction:", correction)
motor_a.run_angle(200, 5 + correction)
motor_b.run_angle(200, 5)
motor_a.reset_angle(0)
motor_b.reset_angle(0)
wait(1000)
def spin(degree):
rel = degree - motor_b.angle()
motor_a.run_angle(400, rel, Stop.HOLD, wait=False)
motor_b.run_target(400, degree)
def pick_and_place(degree):
motor_a.run_angle(100, BIRD_HEIGHT)
spin(0)
motor_a.run_angle(100, -BIRD_HEIGHT)
spin(degree)
motor_a.run_angle(100, BIRD_HEIGHT)
motor_a.run_angle(100, -BIRD_HEIGHT)
# Main program
reset()
spin(60)
while True:
hub.light.on(Color.VIOLET)
wait(100)
color = sensor.color()
if color == Color.RED:
hub.light.on(color)
pick_and_place(74)
elif color == Color.YELLOW:
hub.light.on(color)
pick_and_place(114)
elif color == Color.GREEN:
hub.light.on(color)
pick_and_place(154)
elif color == Color.WHITE:
hub.light.on(color)
pick_and_place(185) |
Beta Was this translation helpful? Give feedback.
4 replies
-
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I coded the BrickPecker from The Lego Boost Activity Book by Daniele Benedettelli with PyBricks:
brickpecker.mp4
I'm new to this, so any suggestions for improvements are appreciated.
Issue I'm having:
Beta Was this translation helpful? Give feedback.
All reactions