Skip to content

Writing a Basic Motor Control Run

johnmeshulam edited this page Nov 10, 2019 · 2 revisions

In this first example, we will create a program that spins a meduim motor 420 degrees forward, waits a few seconds, and then spins it back. We will be using the armMotor on port A, that we defined in the Defining Hardware tutorial.

The first step is creating a new class in our user.runs package. Let's call this class Run1.

public class Run1 {

}

In order for this class to be a valid run, it needs to extend the RobotRun class, and implement 2 methods:

  1. A constructor. This is a simple constructor that sets the run's display name, which will show up on the brick.
  2. a public void method named runInstructions. This is where our run code will go. Your code should now look something like this:
public class Run1 extends RobotRun {

    public Run1(String name) {
	super(name);
    }

    @Override
    public void runInstructions() {
	//Your code here
    }

}

Now that we've set up our basic run template, lets write the actual code. Our program flow is very simple:

  1. Rotate the motor 420 degrees

  2. Wait 2 seconds

  3. Rotate the motor back 420 degrees

In FLL-leJOS code, this can be written using 3 simple lines:

    RobotMap.getMotor("armMotor").rotateDegrees(0.5, 420, true); //rotate the motor forwards at 50% speed
    
    Wait.waitForSeconds(2.0); //wait 3 seconds
    
    RobotMap.getMotor("armMotor").rotateDegrees(-0.5, 420, true); //rotate the motor backwards at 50% speed

Our final run should look like this:

public class Run1 extends RobotRun{

	public Run1(String name) {
		super(name);
	}

	@Override
	public void runInstructions() {
		RobotMap.getMotor("armMotor").rotateDegrees(0.5, 420, true);
		
		Wait.waitForSeconds(3.0);
		
		RobotMap.getMotor("armMotor").rotateDegrees(-0.5, 420, true);
	}

}

Clone this wiki locally