Optical Shaft Encoder Programming

Hello

I am trying to program optical shaft encoders on my robot but i cannot exactly fix the problem if one wheel moves faster than another. On my program i have it written to fix that error but it goes out of control and goes into circles. Here is my program in Robot C:

while((SensorValue(left)<1920)||(SensorValue(right)<1920))
{
if ((SensorValue(left)<1920)&&(SensorValue(right)<1920))
{
motor[port1]=127;
motor[port2]=-127;
motor[port3]=127;
motor[port4]=-127;
motor[port5]=127;
motor[port6]=-127;
}
else if ((SensorValue(left)<1920)&&(SensorValue(right)>=1920))
{
//right motors off, left motors forward
motor[port1]=127;
motor[port2]=-127;
motor[port3]=127;
motor[port4]=0;
motor[port5]=0;
motor[port6]=0;
}
else if ((SensorValue(left)>=1920)&&(SensorValue(right)<1920))
{
//right motors forward, left motors off
motor[port4]=127;
motor[port5]=-127;
motor[port6]=127;
motor[port1]=0;
motor[port2]=0;
motor[port3]=0;
}
}

Thanks

So, RobotC…

Motor speeds range from -127 (full reverse) through zero (stopped) up to +127 (full forward).

Left Motors are Ports 1, 2 & 3.
Right Motors are Ports 4, 5 & 6.

So if EITHER Sensor is below 1920, we keep in the While Loop…

If Both Sensors are below 1920:
Motor Ports, 1, 3, & 5 are Full Forward, Motor Ports 2, 4, & 6 are Full Reverse.
Robot Direction is Full Forward.

If the Left Sensor is below 1920 and the Right Sensor is not, the Right Side is TOO FAST, so Right Motors OFF, Left stay ON.

If the Right Sensor is below 1920 and the Left Sensor is not, the Left Side is TOO FAST, so Left Motors OFF, Right Motors stay ON.

The moment Both Sensors goes over 1920, the While Loop exits…

Do you have this after the while loop:


    //left motors off, right motors off 
    motor[port1]=0;
    motor[port2]=0;
    motor[port3]=0;
    motor[port4]=0;
    motor[port5]=0;
    motor[port6]=0;
    

The test for the FAST SIDE means that one side has reached the Maximum (1920). The SLOW SIDE is running Full Speed, and once it reaches beyond 1920, the While Loop is exited, and the SLOW SIDE’s Motors are still running Full Forward, while the FAST SIDE’s Motors are Stopped… And it goes in circles

Also, you might make Small Changes, like reducing the Speed of the Fast Side, rather than coming to a Full Stop… This will make your Course Corrections more smoother and less abrupt…