1) resets the rotation counter on motor B;
2) backs up until the color sensor detects BLACK;
3) grabs the rotation sensor value;
4) coasts motor B;
5) waits until motor B is not moving;
6) reads out the current rotation value;
7) goes forward the distance read in step 6;
The rotation counter at the end of step 7 is never 0. Never, ever. It varies, somewhat, with the power levels going both directions, but is never 0. It varies from 4 (power = 10) to 80 (power = 40). And it is always overshoot.
I've even put in long waits, seeing if that would help. It didn't.
The error is real, because the robot always stops beyond where it started.
Anyone have any suggestions? I've developed a work around. But, I would prefer to have the motor stop at 0.
Code: Select all
// Back up to the long line.
SetSensorColorFull(S3);
ResetRotationCount(OUT_B);
Wait(10);
RampUp(OUT_B, 30, 180);
until (1 == SensorValue(S3))
{
ContinueRun(OUT_B, 30);
Yield();
}
fDistance1 = MotorRotationCount(OUT_B);
SetSensorColorNone(S3);
Coast(OUT_B);
WaitUntilStopped(OUT_B);
Wait(1000);
NumOut(0,LCD_LINE2,fDistance1); // distance measured to line
NumOut(0,LCD_LINE3,MotorRotationCount(OUT_B)); // to be moved
// Drive forward that same distance it backed up.
RotateMotor(OUT_B, 30, -MotorRotationCount(OUT_B));
WaitUntilStopped(OUT_B);
Wait(1000);
NumOut(35,LCD_LINE3,MotorRotationCount(OUT_B)); // residual angle - should be 0.
// Because the residual angle is never 0, adjust the measured distance by that amount.
// So, the distance will be from HERE to the line.
fDistance1 -= MotorRotationCount(OUT_B);
NumOut(70,LCD_LINE3,fDistance1); // New distance to line
Code: Select all
void WaitUntilStopped(byte output)
{
// Wait until the motor has stopped turning.
long rc = MotorRotationCount(output);
long oldrc = rc+1;
while (oldrc <> rc) {
oldrc = rc;
Wait(100); // adjust this wait time to see what impact it has on accuracy.
rc = MotorRotationCount(output);
}
}