Controlling the Motors
Controlling the Motors

Introduction to the Motor class.

This Motor class provides access to the NXT motors. To be useful, a motor must be connected to one of the three NXT motor ports. This class provides an instance for each port. They are: Motor.A, Motor.B and Motor.C.

Each of these three objects is an instance of the class NXTRegulatedMotor. This class provides methods for controlling the motor, and for finding out what the motor is doing.

This tutorial contains a set of five programs for you to write. With them, you can perform experiments to understand how the NXT motor performs. They are simple enough so you don’t need much Java experience (beyond loops) to write them. Finally, there is a discussion of other motor methods , not used in the programs, that you might find useful.

Program 1 - Basic movement controls.

This program uses the most basic motor methods that control movement. They are listed below, together with other methods you will need for this program.

Methods used in this program


Method name


NXTRegulatedMotor, e.g. Motor.A


Start the motor rotating forward



Start rotating backward



Stop quickly



Wait till any button is pressed


drawString(String str, int x, int y)

Draw a string at position x in row y

What the program should do:
  1. Display "Program 1 " in row 0 of the LCD.
  2. Wait for a button to be pressed
  3. Run motor A in the forward direction.
  4. Display FORWARD in the top line.
  5. Wait until a button is pressed.
  6. Run the motor backward.
  7. Display BACKWARD in next line.
  8. Wait until a button is pressed.
  9. Stop the motor.


Back to top

Program 2 - Using the Tachometer.

The NXT motor has a built in tachometer that keeps track of the current angle (in degrees) of the motor axle. The purpose of this experiment is to find out how quickly the motor stops. The program will attempt to rotate the motor exactly 4 revolutions. It uses two different ideas to accomplish this goal. The first idea set the motor speed at 2 revolutions per second and stop after two seconds. The second idea is to stop the motor after 4 revolutions, as measured by the tachometer.

New methods used in this program


Method name


NXTRegulatedMotor, e.g. Motor.A getTachoCount() Returns the motor angle in degrees.


setSpeed(int speed) speed – degrees per second.. The maximum speed that can be accurately maintained is about 110 times the battery voltage.
Delay msDelay(int interval)

Pause the program for interval milliseconds

What the program should do
  1. Display the program number as before.
  2. Set the motor speed to 2 rev/sec
  3. Run Motor.A forward.
  4. Wait for 2 seconds.
  5. Display the motor angle on the LCD. (what should it be?)
  6. Stop the motor.
  7. Display the tachometer reading on the on the next line LCD.
  8. Start the motor rotating backward.
  9. Wait till the tacho count reaches 0.
  10. Display the tacho count on the next line.
  11. Stop the motor.
  12. Display the tacho count on the next line.
  13. Wait for a button press so you can read the LCD.

Observe that in the first attempt, the stop instruction is issued before the motort has quite completed the 4 revolutions in 2 seconds, and that the it does not stop immediatel because of inertia.


Back to top

Program 3 - Accurate rotation control.

The Motor class has a regulator thread that runs all the time. It has two principle jobs, one of which is to stop the motor at a specified angle. This program will test the accuracy of the methods to accurately control the rotation. The methods have two versions. The basic method returns only when the rotation is complete, the other returns immediately but the motor stops when the rotation is completed.

New methods used in this program


Method name


NXTRegulatedMotor, e.g. Motor.A

rotate(int angle)

Rotates through angle degrees


rotateTo(int angle)

Rotates to angle

What the program should do
  1. Display the program name and wait for a button press.
  2. Rotate the motor 4 complete revolutions.
  3. Display the tachometer reading on the on the LCD.
  4. Rotate the motor to angle 0.
  5. Display the tachometer reading on the on the LCD, next row
  6. Wait for a button press.

The motor usually stops within 1 degree of the specified angle if the motor regulator is doing its job. It works by calculating how far the motor will continue to turn after the brake has been applied and applies the brake before reaching the specified angle.

Observe: Once the motor has stopped if you try to turn it by hand it will resist you and will even return nack to the stopped position. This is because when the regulator will continue to control the motor at its stopped position.


Back to top

Program 4.Interrupting rotation

Sometimes you will want the motor stop (or do something else) before it reaches the specified angle. This program will detect a button press to interrupt the rotation task if you press a button soon enough. The rotate() methods will not return until the motor has stopped at the target angle. But the new methods in this program can return immediately. The motor will still stop at the specified angle unless a new motor method is called in the meantime.

New methods used in this program


Method name


NXTRegulatedMotor, e.g. Motor.A

rotate(int angle,boolean immediateReturn)

The method returns immediately if the boolean parameter immediateReturn is true

rotateTo(int angle,booleanimmediateReturn)


Returns false when the motor has stopped at the specified angle

What the program should do
  1. Display the program number.
  2. Start a rotation of 4 revolutions backward.
  3. While the motor is rotating, display the tacho count.
  4. When a button is pressed, stop the motor.
  5. After the motor has stopped, display the tacho count in another row.
  6. Wait for a button press.

Observe: if you press the button before the rotation is complete, the motor will stop without completing its rotation. Otherwise, the stop() method has no effect.


Back to top

Program 5: Regulating motor speed

The other principle task of the regulator thread is to control the motor speed. One reason for doing this is that a two wheel vehicle will only travel in a straight line if both motors run at the same speed.(obviously). The standard Lego software solves this problem by directly synchronizing two motors. NXJ takes a different approach: keeping each motor rotation synchronized to the system clock. The regulator compares the tacho count (minus its reference count) with speed times elapsed time, and adjusts the power to keep these two quantities closely matched. The regulator resets its reference count and its elapsed time to zero and begins its comparison again whenever you call any of the methods you have used so far. This program allows you to experiment with the effectiveness of speed regulation in keeping motors synchroniced.
You will not need any new methods for this program.

What the program should do:
  1. Display the program number as usual.
  2. Set the speed of all 3 motors at 2 revolutions/sec.
  3. Start all 3 motors rotating through 2 revolutions simultaneously.
  4. Every 200 ms, display all 3 tacho count values in a row.
  5. Repeat step 3, 8 times, using a different row each time.
  6. Wait for a button press while you write down the array of numbers (perhaps in a spread sheet)

The motors should remain within 1 or 2 degrees of each other, once the target speed is attained.
Speed regulation depends on increasing or decreasing power. But if the motor is the motor cannot acheive the desired speed even at maximum power, speed regulation is impossible. The maximum speed for reliable reglation is approximately 100 times the battery voltage. You should repeat the experiment with target speed of 1000 deg/sec to verify this.


Back to top
When might you want to turn off speed regulation?

In some robots, the motor speed should not be constant but changed in response to a sensor reading as for example in a line follower or a balancing robot. If the speed corrections happen frequently, there is no advantage in the regulator thread using CPU cycles in adjusting the motor power to maintain constant speed between adjustments. To use an unregulated Motor you must create an instance of the class NXTMotor. For example:

	   NXTMotor m1 = new NXTMotor(MotorPort.A);

NXTMotor has many of the methods of NXTRegulatedMotor, but instead of the setSpeed method, it has a setPower method, and, as it has no regulation thread, it does not support any of the rotate methods.

When should you use speed regulation?

If you specify a very slow speed, the power to maintain it may not be enough overcome the motor internal friction, so the motor never moves. In this case, a call to rotate() will never return. But with speed regulation on, the regulator will keep increasing the power until the motor comes up to speed.

Can you mix the two methods of control?

Yes you can. Simply create an instance of both NXTRegulatedMotor and NXTMotor classes for the same motor port. When you want to use setPower simply turn off motor regulation by calling suspendRegulation on the NXTRegulatedMotor instance.

Back to top

Some Other Motor Methods

Finding out what the motor is doing
  • boolean isMoving();

    This is useful to test if the motor has finished rotating. isMoving() returns true when the Motor is moving for any reason (e.g. forward() or backward() have been called or a rotate() task is in progress)

  • int getLimitAngle()

    Returns the angle to which the motor is currently rotating

  • int getSpeed()

    Returns the current speed setting.

  • int getActualSpeed()

    Returns the actual speed of the motor.

  • boolean isStalled()

    Tells you if the motor is stalled or if the regulation of the motor speed has failed (have you asked it to go faster then is possible?).

Various other motor methods
  • resetTachoCount()

    This method not only sets the tacho count to 0 but also resets the origin used by the regulator thread in deciding when to stop a rotation task.

  • void setAcceleration(int acceleration)

    Lets you control how fast the motor speed will change from one speed to another. The acceleration is set in degrees per second per second. Use small values (try 200 or 500) to have your robot smoothly accelerate up to speed.

  • void getAcceleration()

    Returns the current acceleration value for the motor

  • suspendRegulation()

    Turns of the regulation of the motor. Use this method if you want to mix regulated and unregulated control of the same motor.

Back to top

NXTMotor methods
  • setPower(int aPower)

    Used to control motor power directly. Use a value between 0 and 100. Don’t call this method if speed regulation is turned on because then the regulator thread is continuously adjusting the power.

  • int getPower()

    returns the current power setting in the range of 0 to 100.

Back to top