A starting example
This is a simple example which:
- beeps,
- moves forward 100mm (10cm),
- moves backward 100mm,
- turns 360° on the spot,
- beeps again.
Each of these steps happens to correspond
with a line of code:
ev3.speaker.beep()
robot.straight(100)
robot.straight(-100)
robot.turn(360)
ev3.speaker.beep()
Complete program
Here is the complete program, complete with headings
marking the major parts of the program structure.
You can see on lines 44, 47, and 50 there are lines
of code that are commented out because we don’t need
them, but they might be handy in the future!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
| #!/usr/bin/env pybricks-micropython
# ^ Shebang line above ^
##############################################
# Docstring
##############################################
"""
Example LEGO® MINDSTORMS® EV3 Robot Educator Driving Base Program
-----------------------------------------------------------------
This program requires LEGO® EV3 MicroPython v2.0.
Download: https://education.lego.com/en-us/support/mindstorms-ev3/python-for-ev3
Building instructions can be found at:
https://education.lego.com/en-us/support/mindstorms-ev3/building-instructions#robot
"""
##############################################
# Import code
##############################################
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import (
Motor,
TouchSensor,
ColorSensor,
InfraredSensor,
UltrasonicSensor,
GyroSensor
)
from pybricks.parameters import Port, Stop, Direction, Button, Color
from pybricks.tools import wait, StopWatch, DataLog
from pybricks.robotics import DriveBase
from pybricks.media.ev3dev import SoundFile, ImageFile
##############################################
# Setup
##############################################
# Initialize the EV3 Brick.
ev3 = EV3Brick()
# Initialize the motors.
left_motor = Motor(Port.B)
right_motor = Motor(Port.C)
# Initialize the drive base.
robot = DriveBase(left_motor, right_motor, wheel_diameter=55.5, axle_track=104)
# Initialize the Touch Sensor.
# touch_sensor = TouchSensor(Port.S1)
# Initialize the Colour Sensor.
# color_sensor = ColorSensor(Port.S3)
# Initialize the ultrasonic sensor.
# ultrasonic_sensor = UltrasonicSensor(Port.S4)
##############################################
# Program body
##############################################
ev3.speaker.beep()
robot.straight(100)
robot.straight(-100)
robot.turn(360)
ev3.speaker.beep()
|