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
What the program should do:
It is possible to write this program using each Motor method only once. 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
What the program should do
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. 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
What the program should do
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. Back to topProgram 4.Interrupting rotationSometimes 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
What the program should do
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. Back to topProgram 5: Regulating motor speedThe 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
The Stopwatch class is in the package lejos.util. What the program should do:
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. Back to topWhen 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 topOther Motor MethodsFinding out what the motor is doing
The following methods are another way to get motor status:
Various other motor methods
|