Herunterladen Diese Seite drucken

LEGO MINDSTORMS Education EV3 MicroPython Erste Schritte Seite 48

Werbung

Erste Schritte mit LEGO
#!/usr/bin/env pybricks-micropython
from
pybricks
import
from
pybricks.ev3devices
from
pybricks.parameters
from
pybricks.tools
# The colored objects are either red, green, blue, or yellow.
POSSIBLE_COLORS =(Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW)
# Initialize the motors that drive the conveyor belt and eject the objects.
belt_motor = Motor(Port.D)
feed_motor = Motor(Port.A)
# Initialize the Touch Sensor. It is used to detect when the belt motor
# has moved the sorter module all the way to the left.
touch_sensor = TouchSensor(Port.S1)
# Initialize the Color Sensor. It is used to detect the color of the objects.
color_sensor = ColorSensor(Port.S3)
# This is the main loop. It waits for you to scan and insert 8 colored objects.
# Then it sorts them by color. Then the process starts over and you can scan
# and insert the next set of colored objects.
while True:
# Get the feed motor in the correct starting position.
# This is done by running the motor forward until it stalls. This
# means that it cannot move any further. From this end point, the motor
# rotates backward by 180 degrees. Then it is in the starting position.
feed_motor.run_until_stalled(120)
feed_motor.run_angle(450, -180)
# Get the conveyor belt motor in the correct starting position.
# This is done by first running the belt motor backward until the
# touch sensor becomes pressed. Then the motor stops, and the the angle is
# reset to zero. This means that when it rotates backward to zero later
# on, it returns to this starting position.
belt_motor.run(-500)
while not
touch_sensor.pressed():
pass
belt_motor.stop()
wait(1000)
belt_motor.reset_angle(0)
# Clear all the contents from the display.
brick.display.clear()
# When we scan the objects, we store all the color numbers in a list.
# We start with an empty list. It will grow as we add colors to it.
color_list = []
# This loop scans the colors of the objects. It repeats until 8 objects
# are scanned and placed in the chute. This is done by repeating the loop
# while the length of the list is still less than 8.
while
len(color_list) < 8:
# Show an arrow that points to the color sensor.
brick.display.image(ImageFile.RIGHT)
MINDSTORMS
®
®
as
ev3brick
brick
import
Motor, TouchSensor, ColorSensor
import
Port, Button, Color, ImageFile, SoundFile
import
wait
LEGO, the LEGO logo and MINDSTORMS are trademarks of the LEGO Group.
©2019 The LEGO Group.
Education EV3 MicroPython
Version 1.0.0
(Fortsetzung auf der nächsten Seite)
46

Werbung

loading