Large motor

In this section, you will learn how to control a large motor, in particular how to initialize one and make it rotate for a specified interval of time. LeJOS, to its credit, has been designed with generalization in mind and therefore, the concepts presented here apply to all motors (that come with EV3 and NXT both). In most cases, substituting the relevant class is all that is required to make a piece of code work for another kind of motor.

Our first program will control a large motor connected to port A of EV3, making it rotate forward for three seconds. The first step is to set up the project directory structure with a folder named Motor acting as the project root:

Motor
    | ---- src
              | ---- Motor.java
    | ---- build.gradle
    | ---- ev3
       | ---- DBusJava
       | ---- ev3classes

This is the standard structure of a LeJOS EV3 project that is compiled using Gradle. The source code we write to control the motor is placed in the src subfolder, and it is called Motor.java. Next, we have the build.gradle file that is used by Gradle to compile the project. Finally, we copy the ev3 folder (we created in the last chapter), which contains the LeJOS API source code (the DBusJava and ev3classes folders), into the project directory. This allows us to incorporate this code into our project.

The build.gradle file

The build.gradle file for this project is the standard one we described in the last chapter. The only difference is that we change the value of the main_class variable to Motor, which is the name we will give to the main class we define in the src/Motor.java file. Here is the content of build.gradle:

apply plugin: 'java'

def main_class = "Motor"

sourceSets {
  main {
    java {
      srcDirs = ['src', 'ev3/DBusJava/src', 'ev3/ev3classes/src']
    }

    dependencies {
      compile files('ev3/ev3classes/lib/jna-3.2.7.jar')
    }

    jar {
      manifest {
        attributes("Main-Class": main_class, "Class-Path": "/home/root/lejos/lib/ev3classes.jar /home/root/lejos/libjna/usr/share/java/jna.jar")
      }
    }
  }
}

The Motor.java file

In src/Motor.java, we write the actual code that will control the large motor connected to EV3. We present the code in full before we explain it in greater detail:

import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.utility.Delay;


public class Motor
{
    public static void main(String[] args)
    {
        log("Program Starting");

        EV3LargeRegulatedMotor motor = new EV3LargeRegulatedMotor(MotorPort.A);
        motor.setSpeed(500);

        log("Forward motion");
        motor.forward();

        Delay.msDelay(3000);

        log("Stopping motor");
        motor.stop();

        log("Program Ending");
    }


    private static void log(String msg)
    {
      System.out.println("log>	" + msg);
    }
}

As is customary, the Java file starts with the following import statements that pull in the classes from other parts of the source code that are used in this file:

import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.utility.Delay;

We import three classes: EV3LargeRegulatedMotor (which is used to control a large motor), MotorPort (which represents one of the four motor ports A to D on the top face of EV3), and a Delay class (which is used to pause the execution, similar to a clock icon in EV3's visual programming language).

Next, we define the main class of the project (and of this file, which is named after it):

public class Motor
{

Inside this class, at the bottom, we define a simple logging function (the order of methods/functions and members/variables inside a Java class don't matter as they are compiled before they are used):

private static void log(String msg)
{
  System.out.println("log>	" + msg);
}

The log() function takes a string and prints it to the terminal. It uses the built-in System.out.println function to do so. We prepend the message with the string "log> ", which means every message printed using log() will start with log> followed by a tab (whitespace). This will allow us to easily distinguish between messages we log and other messages and errors printed by the program as it executes.

The log messages are printed on the terminal and are only accessible if the program is executed from a shell (using SSH). The primary purpose of the log statements is debugging programs while you develop them. The log messages of programs executed directly from EV3 are inaccessible to us.

The other function in the class is main(). This is the entry point for execution of the program. The first line that is executed is a log statement that indicates that the program has started:

public static void main(String[] args)
{
  log("Program Starting");

Next, we initiate an object of the EV3LargeRegulatedMotor class:

EV3LargeRegulatedMotor motor = new EV3LargeRegulatedMotor(MotorPort.A);

This involves creating a variable named motor whose type is EV3LargeRegulatedMotor. This means motor can refer (or store) only an object of the EV3LargeRegulatedMotor class. We create such an object using the keyword new:

new EV3LargeRegulatedMotor(MotorPort.A);

The preceding statement creates such an object. We pass in the argument MotorPort.A, which tells the class that the large motor we want to control is connected to port A of EV3. Now, the variable motor corresponds to a large motor connected to port A and we can use it to control said motor. The first instruction we give it is to set the speed at which it should rotate:

motor.setSpeed(500);

This tells the motor that when it rotates, it should do so at a speed of 500 degrees per second (approximately 1.4 rotations per second). We set the motor spinning with the following statement:

log("Forward motion");
motor.forward();

The first line prints a log message informing us that the motor is about to start spinning in the forward direction. The second line instructs the motor to spin at the specified speed in the forward direction. The rotation continues until the motor is instructed to stop.

Just like in the visual programming language, we introduce a small pause/delay for the interval of time we want the motor to rotate. To pause the execution for three seconds, we issue the following command:

Delay.msDelay(3000);

After pausing execution for 3000 milliseconds (three seconds), we instruct the motor to stop:

log("Stopping motor");
motor.stop();

Finally, we print a final log to indicate that the program execution is coming to an end:

log("Program Ending");

Compile, deploy, and execute

Once the program is complete, we need to compile it (to create a .jar file), deploy (transfer) it to EV3, and execute it. The compilation is done using Gradle (the gradle build command) and you can use scp (SSH) to transfer it to EV3. Finally, you can execute it directly from EV3 (in which case you won't see the logs) or from the shell (terminal) using SSH.

Since the compile-deploy-execute cycle is at the core of programming for EV3 and must be repeated countless times as you develop a robot, it is easier if it is automated. Appendix A, The Make Utility, discusses how to use the make utility to do just that. With a properly configured Makefile, the entire process can be condensed into a single command: make run.

More motor commands

So far, you learned how to set a motor's speed, instruct it to rotate in the forward direction, and stop it. There are a multitude of other commands associated with a large motor, which provide additional functionality. We will cover most of the important ones in upcoming chapters. The best place to learn about all of the commands associated with an EV3 component is to read through the LeJOS EV3 API documentation at http://www.lejos.org/ev3/docs/.

In the documentation, you can click on the package lejos.hardware.motor and then choose the EV3LargeRegulatedMotor class to learn about all of the methods associated with this class, which you can call to get information from and control a large motor connected to EV3. This documentation is an important resource for anyone programming an EV3 robot using LeJOS.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.114.221