Robot Educator
The education version of the Lego EV3 software proposes various example programs in a sequence called ‘Robot Educator’. You can see those programs and my commentaries HERE. You can also see HERE how the same problems can be solved in EV3 Python, if you're interested.
Let’s see how to get the same functionality using EV3 Basic. Let’s not forget that EV3 Basic can also do other kinds of program that the Lego software is incapable of doing. And above all, let’s not forget that the main reason to use EV3 Basic is that it is the simplest way to do textual programming on the EV3, and learning textual programming is a much better preparation for a career in programming than learning Lego’s non-standard icon-based programming system. As for all exercises on this site, it is assumed that you are using the official ‘Lego ‘Driving Base’ model, or something very similar, with motors attached to ports B and C. The 'Driving Base' model is also called the 'Educator Vehicle'. Building instructions for making the Driving Base model from the education version of the EV3 kit can be viewed or downloaded HERE. Building instructions for making the Driving Base model from the home (retail) version of the EV3 kit can be viewed HERE. The program solutions below can be copied into Small Basic so that you can test them, if you wish.
Further down this page you can find the 'Basics' exercises. Once you have completed them, follow these links for the 'Hardware' and 'Beyond Basics' exercises:
BASICS
Lesson 1 of the official Lego lesson sequence is about configuring blocks and is not relevant to EV3 Basic and therefore not discussed here.
Lesson 2: Straight Move
Objective:
Make the robot advance such that the wheels rotate 720° (50% speed, apply brake when movement terminated). Actually the Lego program makes the robot advance for two wheel rotations but this is equivalent to 720°. To keep EV3 Basic as simple as possible, it does not include the option of programming the motors to rotate through a given number of rotations, therefore you will need to convert rotations to degrees (which you can do in the program, of course).
Wait one second
Make the robot move backwards such that the wheels rotate 720° (50% speed, apply brake when movement terminated)
Wait one second
Make the robot advance for one second (50% speed, apply brake when movement terminated)
EV3 Basic solution:
Motor.Move ("BC", 50, 720, "True")
Program.Delay(1000)
Motor.Move ("BC", -50, 720, "True")
Program.Delay(1000)
Motor.Start("BC", 50)
Program.Delay(1000)
Motor.Stop("BC","True")
For comparison, here is the solution as it appears within the standard Lego icon-based programming environment:
You can find the Lego solutions to the other challenges HERE.
Notes:
This solution, and most that follow, uses the standard EV3 Basic commands for motor control (Motor.Move, Motor.Start, Motor.Schedule). These commands set the ‘speed’ rather than the ‘power’ of the motors. In EV3 Basic ‘speed’ and ‘power’ have slightly different meanings, as explained elsewhere on this site.
In the third line above, you could also set the power to be positive and the angle to be negative but in EV3 Basic it is good practice to obtain reverse movement by making the power negative rather than the angle negative because some other commands ignore a negative sign in the angle value.
Lesson 3: Curved Move
Objective:
Make the robot do a hard turn right (on the spot) with 685° wheel rotation (speed 40, brake on). The left wheel will turn forwards through that angle and the right wheel will turn backwards through the same angle so that the robot itself turns on the spot. Note that a hard turn on the spot like this corresponds to a 'steering' setting of +/-100 in the Move Steering block of the standard Lego software.
Wait one second
Medium turn right (equivalent to a 'steering' setting of 50 in the Move Steering block of the standard Lego software, with only one wheel rotating) with a wheel rotation of 1380° (speed 40, brake on).
Wait one second
Gentle turn right. A 'steering' setting of 25 in the Move Steering block of the standard Lego software means that the right wheel with turn half as fast as the left wheel. Speed 50 for the faster (left) wheel, so the right wheel will need a speed of 25. Brake on. The faster-turning wheel should rotate through two rotations (720°) so the wheel rotating half as fast will rotate 360° in the same time.
Solution:
To make two motors move simultaneously with different speeds, such as when following a curved path or turning on the spot, the best option is to use the command Motor.MoveSync(ports, speed1, speed2, degrees, brake).
ports: Names of exactly TWO motor ports (for example "BC")
speed1: Speed value from -100 (full reverse) to 100 (full forward) of the motor with the lower port letter.
speed2: Speed value from -100 (full reverse) to 100 (full forward) of the motor with the higher port letter.
degrees: The angle that the faster motor should rotate (just as for the 'move tank' block in the standard Lego software).
Note that this function causes program execution to pause until the movement completes.
Motor.MoveSync("BC", 50, -50, 685, "True")
Program.Delay(1000)
Motor.Move ("B", 40, 1380, "True")
Program.Delay(1000)
Motor.MoveSync("BC", 50, 25, 720, "True")
Notes:
Motor.MoveSync has similar functionality to the 'Move Steering' block in the standard Lego EV3 software.
It was not necessary to use Motor.MoveSync for the second movement since only one wheel was moving.
Lesson 4: Tank Move
Objective:
The intended movement in the official Lego lesson 4 is exactly the same as in lesson 3 above, so the solution has already been given. In the Lego lessons, the difference is that lesson 3 is to be achieved using the 'Move Steering' block while lesson 4 is to be solved using the 'Move Tank' block. EV3 Basic always uses the 'Move Tank' approach, specifying the speed that each wheel should have, but the two approaches are equivalent and it is not very difficult to figure out the speed settings that correspond to a certain 'steering value'.
Lesson 5: Move Object
Objective:
This program uses the medium motor to lower the robot's gripper bar, trapping an object, then it does a medium left turn backwards, pulling the object with it, then it raises the gripper bar. EV3 Basic makes no distinction between the large and medium motors - they should both work fine. It is assumed that the medium motor is attached to port A.
Make the medium motor (on port A) turn 100° at -30% speed and apply the brake. In the standard 'driving base' model the medium motor is attached to a gripper which is assumed to start in a vertical position - this block will cause the gripper to be lowered to the ground, hopefully enclosing an object within its grip as it does so.
Robot makes a medium left turn backwards. The left wheel should rotate backwards 360° with speed -50 and the right wheel should not rotate. The brake option is on.
Make the medium motor turn 100° at +30% speed, and apply the brake. This will cause the gripper to be raised back into the vertical position.
Solution:
Motor.Move ("A", -30, 100, "True")
Motor.Move ("B", -50, 360, "True")
Motor.Move ("A", 30, 100, "True")
Notes:
How easy that was!
Lesson 6: Stop at line
Objective:
This exercise assumes that a colour sensor is attached to port 3 and is suspended just above the ground, pointing at the ground. This program turns on the right motor (making the robot turn left), then waits until the detected brightness falls below a certain value (because the sensor has passed over a black line, for example) then turns off the motor.
Start the right motor moving with speed 50.
Wait until the color sensor detects that the brightness has fallen beneath a certain threshold value
Turn off the motor and apply the brake
Solution:
Motor.Start("C", 30)
While Sensor.ReadPercent(3)>50
Endwhile
Motor.Stop("C", "True")
Notes:
Note how the 'wait until' structure becomes a While loop in EV3 Basic.
Lesson 7: Stop at Angle
This exercise uses the gyro sensor, which is assumed to be connected to port 2. Note that the gyro sensor is included in the education version of the EV3 but not in the home version, so if you have the home version you will not be able to test this program (it's always possible to buy additional sensors). In mode 0 the gyro sensor measures the angle in which it is pointing, relative to the angle that is was at when the program was launched. It's very important to keep the Gyro Sensor and EV3 steady when connecting the cable and during start-up of the EV3, otherwise the gyro reading will continually wander away from the correct value. If you are not sure that this condition was met then simply unplug the sensor from port 2 and then reconnect it before running the program, while ensuring that the EV3 and the gyro sensor are perfectly still.
Objective:
Start the left motor with speed 40 to initiate a medium turn right.
Wait until the gyro sensor detects that the robot has turned (at least) 45° in the positive direction (to the right). Note that the gyro measures the rotation of the robot itself and not the rotation of the motors or the wheels.
Then the robot moves straight ahead with speed 50 until the wheels have turned through one rotation (360°).
Solution:
'Connect gyro sensor to port 2
Sensor.SetMode(2,0) 'set port 2 to mode 0: measure angle in degrees
Motor.Start("B", 40)
While Sensor.ReadRawValue(2,0) < 45
EndWhile
Motor.Move ("BC", 50, 360, "True") 'advance 360° (one revolution)
Lesson 8: Stop at Object
This project will use the ultrasound sensor, which is assumed for this exercise to be on port 4. If you don't have the ultrasound sensor (because you bought the home edition of the EV3) you could perhaps substitute the IR sensor but it won't be possible to get exact distances because the IR sensor does not return exact distances. Also, the code will be different if you use the IR sensor because with the ultrasound sensor Sensor.ReadRaw is used and with IR sensor (in mode 0: distance) Sensor.ReadPercent is used. This difference means other small changes need to be made to the code. See note 2 for a solution using the infrared sensor.
Objective:
The robot will move forward in a straight line (speed=50) until it has moved (at least) 11 cm closer to the reflecting object in front of it, then it will stop and pause for one second, then it will back up continuously (speed=-50) until it detects that it has moved (at least) 6 cm away from the object, then it will stop. (Note that if the robot moves 11 cm closer and then 6 cm further away it will then be 5 cm from its starting position.)
Solution (using the ultrasound sensor):
First we need to ensure that the sensor is in mode 0: measure distance in mm (not cm). To obtain the distance reading from the ultrasound sensor, Sensor.ReadRawValue(port number, 0) should be used.
'Connect ultrasonic sensor to port 4
Sensor.SetMode(4,0) 'set port 4 to mode 0: distance in mm
StartDistance = Sensor.ReadRawValue(4,0) 'Distance in mm
Motor.Start("BC", 50)
While Sensor.ReadRawValue(4,0) > (StartDistance - 110) '11cm=110mm
EndWhile
Motor.Stop("BC", "True")
Program.Delay(1000)
Motor.Start("BC", -50) 'reverse with speed= -50
While Sensor.ReadRawValue(4,0) < (StartDistance - 50) '5cm=50mm
EndWhile
Motor.Stop("BC", "True")
Notes:
I find that the sensor can give unreliable readings (due to unwanted reflections) if the robot is on a hard, highly reflective surface or if the sensor is mounted too close to the floor.
If you use the infrared sensor in place of the ultrasound sensor then you must also modify the code since these sensors use different functions to obtain the sensor reading and since the ultrasound sensor returns millimeters while the infrared sensor returns centimeters. Here is the same program, modified for the IR sensor.
'Connect infrared sensor on port 4
Sensor.SetMode(4,0) 'set port 4 to mode 0: distance in cm
StartDistance = Sensor.ReadPercent(4) 'Returns distance in cm
Motor.Start("BC", 50) 'Move forward at speed 50
While Sensor.ReadPercent(4)>(StartDistance-11)
'robot hasn't yet moved 11cm closer
EndWhile
Motor.Stop("BC", "True") 'Brake on
Program.Delay(1000) ' pause 1 second
Motor.Start("BC", -50) 'reverse with speed= -50
While Sensor.ReadPercent(4)<(StartDistance-5)
'robot hasn't yet moved 6cm further away
EndWhile
Motor.Stop("BC", "True")
Now that you have complete the 'Basics' exercises you are ready to tackle the 18 'Beyond Basics' exercises which begin with Beyond Basics exercises 1-6.