We are using two optical shaft encoders to make our robot drive straight, and we did this by adjusting one wheel to the other in a master-slave setup. We have been having issues with the while loop not running fast enough… we think. Everytime we run our robot and try to have the right encoder adjust to the left encoder to move in a straight line, it takes much longer than we think it should. It impacts the robot’s ability to drive in a straight line, and its veering off course, taking too long to adjust from the previous data. Is there a way to change the frequency of the while loop, or is there something else wrong with our code?
{
float Lencoder = LeftEnc.rotation(rotationUnits::deg);
float Rencoder = RightEnc.rotation(rotationUnits::deg);
float Lvel = 50;
float Rvel = 50;
float whileCounter = 0;
while (1)
{
whileCounter = whileCounter + 1;
Lencoder = LeftEnc.rotation(rotationUnits::deg);
Rencoder = RightEnc.rotation(rotationUnits::deg);
Controller1.Screen.clearScreen();
Controller1.Screen.setCursor(1, 1);
Controller1.Screen.print("LVe = %3.0f" ,Lencoder);
Controller1.Screen.setCursor(2, 1);
Controller1.Screen.print("RVe = %3.0f" ,Rencoder);
Controller1.Screen.setCursor(3, 1);
Controller1.Screen.print("LV = %3.0f" ,Lvel);
Controller1.Screen.setCursor(3, 11);
Controller1.Screen.print("RV = %3.0f" ,Rvel);
Controller1.Screen.setCursor(1, 14);
Controller1.Screen.print("%3.0f" ,whileCounter);
if(Rencoder > Lencoder)
{
Rvel = Rvel - 1;
}
if (Rencoder < Lencoder)
{
Rvel = Rvel + 1;
}
LeftMotors.spin(vex::directionType::fwd, Lvel, vex::velocityUnits::pct);
RightMotors.spin(vex::directionType::fwd, Rvel, vex::velocityUnits::pct);
}
}