![]() |
![]() |
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
What the program should do:
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
What the program should do
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. 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
What the program should do
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 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 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 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 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. What the program should do:
The motors should remain within 1 or 2 degrees of each
other, once the target speed is attained. 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 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. Some Other Motor MethodsFinding out what the motor is doing
Various other motor methods
NXTMotor methods
|