Instructional Material: Exercise: Drive Forward and Stop

Now its time write more interesting Java programs for the EV3. Start Eclipse on your PC. When Eclipse is ready, the EV3 Exercises project you created in the HelloWorld lesson should appear in the project explorer on the left side of the screen.

Click on the project to open the project's tree of folders. One will be src, which is the directory where the project source files are stored. Right click on src and select New and then Package. This will display the New Package dialog. Enter the package name ev3.exercises. We are going to put each of our exercise (sample) programs into this package. This organizes all of the exercises in a single package. This is a simpler and more tidy way to organize things rather than have a separate project for each exercise.

The new package will appear under src in the project tree. Right-click the package and select New, Class. On  the New Class dialog, enter DriveForward for the class name. Check the box to create a static void main method stub. Click Finish. This will create the DriveForward.java file with the new class in it and open that class file in the editing area. You are ready to program!

Here is the Java code we will be working with. Move your cursor over the code and a Copy button will appear upper right. Click to copy the code below and replace the code in the DriveForward.java file completely. 

Lets look at this code in detail:
 
First we see the package statement that puts our class into the package we created.
 
Next we see some import statements importing classes from the leJOS API hardware namespace. These classes will give us access to the EV3 and attached devices.
 
Next we display some information on the EV3 LCD panel that will show when our program is ready to run. We also use the Button class to use the LED lights that surround the EV3 buttons to flash the LEDs green when we are ready to drive. The Button class has a lot of methods that give us access to all of the EV3 buttons and the LEDs. Note that when using the println method, if the characters \n appear in the message, they generate a new line (or carriage return) request and the following text will start on the next line down.
 
Next we use the Sound class to control the EV3 speaker. Here we call a method that generates a series of rising tones to let you know when the program is ready to drive.
 
Next we use another Button class method to wait for any button to be pressed to start the driving portion of the program.
 
Once a button  is pressed, we create two UnregulatedMotor objects, one for each motor on our robot. An UnregulatedMotor is a simple motor that you control by setting the power level, 0-100%. The RegulatedMotor class is a more complex but more powerful way to control motors and we will explore it later.
 
There are 4 motor control ports or connections on the EV3. When you create a motor object to control a motor you indicate which port the motor is plugged into in the motor object constructor. LeJOS provides an enum called MotorPort with members A, B, C and D corresponding to the EV3 motor ports.
 
We then set the two motor objects to 50% power. The motors will start turning. Now we wait for 2 seconds (2000 milliseconds) allowing the robot to drive forward and then set the motors to 0% power. Setting power to zero causes the motors to stop with a braking effect so the stop is quick and the motors will resist movement. You can also call a method on the motor objects that cause them to coast or float leading to a gentler stop and the motors will not resist movement.
 
Finally we free the motor objects by closing them and generate a sound that is several tones descending to signal that the program  is done.
 
Once you have the code copied into the file in Eclipse, connect your EV3 to your PC with USB cable and then click the green arrow button on the Eclipse toolbar to compile and download the program. The EV3 should restart and the program should run. As a challenge, look at the Unregulated Motor class to see how you can change the program to drive backwards.
 
Note: On first connection of the EV3 with your PC, the EV3 will try install a driver for communication. If that install fails, go to Control Panel, Device Manager, and locate the device in error on the device list. Right click and select update driver and choose the option to check Windows Update for the driver. The correct driver should then be installed.

 

Material Type: 
Lab Activity
Education Level: 
Middle School
High School
Focus Subject: 
Computing / Computer Science
Robotics Software
HW Platform: 
EV3
SW Platform: 
Java
Interactivity Style: 
Mixed
Audience: 
Learner