Resetting Motor Encoders in Auto

Another issue we are having is the Motor Encoder not resetting to 0 in Autonomous. At least that’s what we assume is the issue.
Our code is…



motor[port7] = 90;  //Intake moves
autoDriveDist(127,27);  //Base moves forward 

Wait1Msec(1000);

motor[port7] = 0;  //Intake stops moving 
autoDriveDist(-127,27); //Base moves back

Wait1Msec(1000); 


The robot will follow the first task, moving forward and moving the intake.
The base will stop and the intake won’t stop.
The robot doesn’t move back.

Is there a way to fix this?

What is the autoDriveDist() code?

This is the code.


void autoDriveDist (int speed, int dist)
{
	nMotorEncoder[port4] = 0;
	nMotorEncoder[port5] = 0;

	int ticks = dist/(wheelDiameter*PI) * eDriveTicks;
	while(abs(nMotorEncoder[port5]) < ticks*.6)
	{
		int rDiff = abs(nMotorEncoder[port5]) - abs(nMotorEncoder[port4]);
		int rMod = sgn(rDiff)*speed*.1;

		motor[port3] = speed;
		motor[port5] = speed;
		motor[port2] = speed + rMod;
		motor[port4] = speed + rMod;
	}

	while (abs(nMotorEncoder[port5]) < ticks)
	{
		int rDiff = abs(nMotorEncoder[port5]) - abs(nMotorEncoder[port4]);
		int rMod = sgn(rDiff)*speed*.1;

		motor[port3] = speed/3;
		motor[port5] = speed/3;
		motor[port2] = speed/3 + rMod;
		motor[port4] = speed/3 + rMod;
	}

	autoDriveDist(-speed/2, 50);
}

RobotC

It’s your last line of autoDriveDist, which makes it recursive. You’ve created an infinite loop. You’re already down to a speed of -21 when you finish the second time, so it may be pretty much stopped. Regardless of when it happens, you’ll be asking the robot to drive at a tiny speed, small enough it can’t move the robot, until it travels 50. It will never get there. So even a few iterations of this will never end. Even if they each would, you’d still have an infinite loop. You never exit autoDriveDist.