Drive Function Issue (Robotc)

Hey, recently I’ve been having an issue with creating a straight drive function. It works perfectly when moving forward, but when moving backward it goes about a tile or two too far. Any idea why? Everything should be self-explanatory. For the first motion, I am using jpearman’s slew rate code.

void drive(float inches, int angle)
{
	float distance = inches*inchconstant;
	float gyrokp = 0.25;
	int error;
	int rotation = (angle*10);
	int correction;
	SensorValue[LeftQuad] = 0;
	int direction;

	startTask(MotorSlewRateTask);

	if(distance > 0)
	{

		while(distance - SensorValue[LeftQuad] >= 600)
		{
			error = rotation - SensorValue[Gyro];
			correction = error*gyrokp;

			Slewleft(60 - correction);
			Slewright(60 + correction);
		}

		stopTask(MotorSlewRateTask);

		while(distance - SensorValue[LeftQuad] >= 50)
		{
			error = rotation - SensorValue[Gyro];
			correction = error*gyrokp;

			leftdrive(30 - (correction/2));
			rightdrive(30 + (correction/2));
		}
		leftdrive(-40);
		rightdrive(-40);
		wait1Msec(60);
		motors(0);
	}
	else
	{

		while(distance - SensorValue[LeftQuad] <= 600)
		{
			error = rotation - SensorValue[Gyro];
			correction = error*gyrokp;

			Slewleft(-60 - correction);
			Slewright(-60 + correction);
		}

		stopTask(MotorSlewRateTask);

		while(distance - SensorValue[LeftQuad] <= 50)
		{
			error = rotation - SensorValue[Gyro];
			correction = error*gyrokp;

			leftdrive(-30 - (correction/2));
			rightdrive(-30 + (correction/2));
		}
		leftdrive(40);
		rightdrive(40);
		wait1Msec(60);
		motors(0);
	}
}

Does anyone have any ideas as to why it’s not working?

check this logic


while(distance - SensorValue[LeftQuad] <= 600)

perhaps -600 ? same for the second test, perhaps -50.