-32768 to 32767 degrees. This corresponds to about 91 rotations in either the forward or
reverse directions. In long running programs, the programmer should periodically reset the
encoder values to zero to avoid an over
ow.
Consider the instructions in Listing 7.2. This program will cause the robot to move
forward in a straight line through exactly to rotations of the drive wheels. The rst two
# pragma config (Motor , motorC , Left , tmotorNormal , PIDControl )
# pragma config (Motor , motorA , Right , tmotorNormal , PIDControl )
task main () {
nSyncedMotors = synchAC ; // Left motor slaved to right motor
nSyncedTurnRatio = +100; // Left right motors same rate
nMotorEncoder [ Right ]=0; // Reset right motor encoder to 0
nMotorEncoderTarget [ Right ] = 720; // Stops after 720 degs
motor [ Right ] = 50; // Right motor moves at 50% power
while ( nMotorRunState [ Right ]== runStateRunning ){} // Hold
}
Listing 7.2: This program will cause the tri-bot to move forward in a straight line through
exactly two rotations of the drive wheels.
instructions synchronize the motors and cause them to run at the same speed. Next, we set
the motor encoder value for the right motor to zero and use the nMotorEncoderTarget[]
array to set a precise stopping point at 720. Setting the target does not start the motor,
but it does cause the motor to stop when the motor encoder value reaches 720. We then
start the motor at 50% speed and use a while-loop to hold the program execution until the
rotations are complete.
It is reasonable to wonder why, in Listing 7.2, we do not omit the encoder target instruc-
tion and simply stop the motors by setting the motor power to 0 after the while-loop exits.
Indeed, this approach does work. However, particularly at high speeds, stopping in this
manner can be quite rough causing the robot to rock back and forth and perhaps overshoot
(or undershoot) the desired number of rotations.
Setting the nMotorEncoderTarget[] causes the motor to slow to a stop at the specied
encoder value in a precise way regardless of the motor speed. It should be noted that the
target ignores the direction of rotation so that a value of 720 will stop the motors at either
720. The sign of the target value does play a role however. Using a negative value disables
the precision stopping algorithm and is akin to the rough stopping method alluded to earlier.
There are times when each is appropriate. Using the precise stopping algorithm will
drain more power from the batteries in the long run. In the case of attempting to deter-
mine the eective circumference of a robot's wheels, the precise stopping algorithm is more
appropriate.

No comments:
Post a Comment