Controlling the Motors


Controlling the Motors

Introduction to the Motor class.

This class is an abstraction of a NXT motor. 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. The 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 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 basic motor methods that control movement.

Methods used in this program

Class

Method name

Notes

Motor

forward()

Start the motor rotating forward

 

backward()

Start rotating backward

 

changeDirection()

Reverse the direction of rotation

 

stop()

Stop quickly

Button

waitForPress()

Wait till any button is pressed

LCD

drawString(String str, int x, int y)

Draw a string.

What the program should do:
  1. Run motor A in the forward direction.

  2. Display FORWARD in the top line.

  3. Wait until a button is pressed.

  4. Run the motor backward.

  5. Display BACKWARD in next line.

  6. Wait until a button is pressed.

  7. Run motor A in the forward direction.

  8. Display FORWARD in the next line.

  9. Wait until a button is pressed.

  10. Stop the motor.

It is possible to write this program using each Motor method only once.

Solution

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 axel. The purpose of this program is to use the tachometer to find out how quickly the motor stops.

New methods used in this program

Class

Method name

Notes

Motor

flt()

Abbreviation for float. Turns off the power, but does not apply the brake

 

getTachoCount()

Return s the motor angle in degrees

 

resetTachocount()

Sets the counter to zero

 

setSpeed(int speed)

speed – degrees per second.. The maximum speed that can be accurately maintained is about 110 times the battery voltage.

 

int getActualSpeed()

Returns the actual motor speed (deg/sec)

LCD

drawInt()

 

What the program should do
  1. Run Motor.A forward.

  2. Wait till the tacho count reaches 360.

  3. Stop the motor.

  4. Wait until the motor has stopped.

  5. Display the tachometer reading on the on the LCD.

  6. Wait for a button press to give you time to read the screen display.

  7. Set the motor speed to 720 (the default is 360).

  8. Reset the tachometer count to zero.

  9. Repeat steps 1 through 6. Note: you can avoid duplicated code by writing a method that executes these steps.

Repeat the entire experiment by modifying your code to use flt() instead of stop(); ( flt is the abbreviation for float, a Java reserved word). This method sets the motor power to zero, but does not apply the brake.

Observe that, even using the brake, the motor does not stop immediately, because motor has inertia.

Solution

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 rotate() method.

New methods used in this program

Class

Method name

Notes

Motor

rotate(angle)

Rotates through angle degrees

 

rotateTo(angle)

Rotates to angle

 

rotateTo(angle,true)

Sets the counter to zero

What the program should do
  1. Rotate the motor one complete revolution.

  2. Display the tachometer reading on the on the LCD, row 0.

  3. Rotate the motor to angle 0.

  4. Display the tachometer reading on the on the LCD, row 1.

  5. Wait for a button press to give you time to read the LCD.

  6. Clear the LCD.

  7. Set the speed to 720.

  8. Repeat steps 1 through 6.

Note: you can avoid duplicated code by writing a method that executes these steps.

Observe 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. It applies the brake before reaching the specified angle. It then makes minor adjustments to the motor position till it is close enough.

Solution

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

Class

Method name

Notes

Motor

rotate(angle,immediateReturn)

The method returns immediately if the boolean parameter immediateReturn is true


rotateTo(angle,immediateReturn)



(boolean)isRotating()

Returns false when the motor has stopped at the specified angle

Button

int readButtons()

Returns the ID of the button pressed.

What the program should do
  1. Start a rotation of 720 degrees.

  2. While the motor is rotating, display the tacho count at the position of row 1.

  3. When a button is pressed, stop the motor.

  4. After the motor has stopped, display the tacho count in the center of the row.

  5. Wait for a button press.

  6. Start a rotation to 0.

  7. Repeat steps 2,3,5.

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

Solution

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 adjust 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 motor methods you have used so far.

New methods used in this program

Class

Method name

Notes

Motor

regulateSpeed(booleanyes)

Speed regulation is on by default.

Stopwatch

elapsed()

Returns elapsed time in milliseconds

 

reset()

Resets the watch to 0.

The Stopwatch class is in the package lejos.util.

What the program should do:
  1. Create a new stopwatch.

  2. Start all three motors running at 2 revolutions/second.

  3. Every 200 ms, display all 3 tacho count values in the same row.

  4. Repeat step 3, 8 times, using a different row each time.

  5. Write down maximum difference you see between the motor tacho counts.

  6. Turn off speed regulation.

  7. Repeat steps 1 – 5.

With speed regulation on, the motors should remain within a few degrees of each other. When it is off, the difference in angle of rotation get larger with time unless you have an unusually well matched set of motors.

Solution

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.

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.

Back to top

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)

  • boolean isRotating()

    Specifically tests if a rotate task is currently in progress

  • int getLimitAngle()

    Returns the angle to which the motor is currently rotating

  • boolean isRegulating()

    Used to test if the speed regulation is turned on.

  • int getSpeed()

    Returns the current speed setting.

  • int getActualSpeed()

    Returns the actual speed of the motor. This is updated every 100 ms, and is accurate to about +- 10 deg/sec. Use this method to detect if the motor has stalled.

  • int getPower()

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

  • int getMode()

    returns a code for the current motor status

Code

motor status

1

forward

2

backward

3

stop

4

float

The following methods are another way to get motor status:

  • boolean isForward()

  • boolean isBackward()

  • boolean isStopped()

  • boolean isFloating()

Various other motor 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.

  • 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.

  • lock()

    Ordinarily, when the motor is stopped, it has some resistance to being rotated. But in some applications, such a weight lifting robot, the force of gravity may be enough to overcome this resistance. This method causes the regulator thread to apply additional power in order to hold the motor axel in position.

Back to top