Integrated Motor encoders

Hello My name is Jesus Rodríguez and i am from team 98782B Matbotz!!!. We are having problems with integrated encoders our counts are negative and on the debugger window they show as left= -6234 And right as 5678. How can we achieve staraightness and precision. Thank you!

P.S here is my program :
#pragma config(I2C_Usage, I2C1, i2cSensors)
#pragma config(Sensor, I2C_1, , sensorQuadEncoderOnI2CPort, , AutoAssign )
#pragma config(Sensor, I2C_2, , sensorQuadEncoderOnI2CPort, , AutoAssign )
#pragma config(Motor, port2, leftmotor, tmotorVex393_MC29, PIDControl, driveLeft, encoderPort, I2C_2)
#pragma config(Motor, port3, rightmotor, tmotorVex393_MC29, PIDControl, reversed, driveRight, encoderPort, I2C_1)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

task main()
{
int slowspeed;
int fastspeed;
slowspeed=60;
fastspeed=80;
wait1Msec(2000);

clearLCDLine(0);
clearLCDLine(1);
displayLCDString(0, 0, "R: ");
displayLCDString(1, 0, "L: ");


nMotorEncoder[rightmotor] = 0;
nMotorEncoder[leftmotor] = 0;



while(nMotorEncoder[rightmotor] < 1450)
{
    if(nMotorEncoder[leftmotor]> nMotorEncoder[rightmotor])
    {
        displayLCDNumber(0, 3, nMotorEncoder[rightmotor], 6);
        displayLCDNumber(1, 3, nMotorEncoder[leftmotor], 6);
        motor[rightmotor]=fastspeed;
        motor[leftmotor]=slowspeed;
    }
    if(nMotorEncoder[rightmotor]> nMotorEncoder[leftmotor])
    {
        displayLCDNumber(0, 3, nMotorEncoder[rightmotor], 6);
        displayLCDNumber(1, 3, nMotorEncoder[leftmotor], 6);
        motor[rightmotor]=slowspeed;
        motor[leftmotor]=fastspeed;
    }
    if(nMotorEncoder[rightmotor]==nMotorEncoder[leftmotor])
    {
        displayLCDNumber(0, 3, nMotorEncoder[rightmotor], 6);
        displayLCDNumber(1, 3, nMotorEncoder[leftmotor], 6);

        motor[rightmotor] = fastspeed;
        motor[leftmotor]    = fastspeed;
    }
    



}
nMotorEncoder[rightmotor] = 0;
nMotorEncoder[leftmotor] = 0;

while(nMotorEncoder[rightmotor] < 450)
{
    displayLCDNumber(0, 3, nMotorEncoder[rightmotor], 6);
    displayLCDNumber(1, 3, nMotorEncoder[leftmotor], 6);
    motor[rightmotor]=63;
    motor[leftmotor]=-63;

}
nMotorEncoder[rightmotor]=0;
nMotorEncoder[leftmotor]=0;

while(nMotorEncoder[rightmotor] <1400)
{
    if(nMotorEncoder[leftmotor]> nMotorEncoder[rightmotor])
    {
        displayLCDNumber(0, 3, nMotorEncoder[rightmotor], 6);
        displayLCDNumber(1, 3, nMotorEncoder[leftmotor], 6);
        motor[rightmotor]=fastspeed;
        motor[leftmotor]=slowspeed;
    }
    if(nMotorEncoder[rightmotor]> nMotorEncoder[leftmotor])
    {
        displayLCDNumber(0, 3, nMotorEncoder[rightmotor], 6);
        displayLCDNumber(1, 3, nMotorEncoder[leftmotor], 6);
        motor[rightmotor]=slowspeed;
        motor[leftmotor]=fastspeed;
    }
    if(nMotorEncoder[rightmotor]==nMotorEncoder[leftmotor])
    {
        displayLCDNumber(0, 3, nMotorEncoder[rightmotor], 6);
        displayLCDNumber(1, 3, nMotorEncoder[leftmotor], 6);

        motor[rightmotor]= fastspeed;
        motor[leftmotor]  = fastspeed;
    }


}
nMotorEncoder[leftmotor]=0;
nMotorEncoder[rightmotor]=0;

while(nMotorEncoder[rightmotor]< 450)
{
    displayLCDNumber(0, 3, nMotorEncoder[rightmotor], 6);
    displayLCDNumber(1, 3, nMotorEncoder[leftmotor], 6);
    motor[rightmotor]=63;
    motor[leftmotor]=-63;
}

nMotorEncoder[leftmotor]=0;
nMotorEncoder[rightmotor]=0;

while(nMotorEncoder[rightmotor] <1200)
{
    if(nMotorEncoder[leftmotor]> nMotorEncoder[rightmotor])
    {
        displayLCDNumber(0, 3, nMotorEncoder[rightmotor], 6);
        displayLCDNumber(1, 3, nMotorEncoder[leftmotor], 6);
        motor[rightmotor]=fastspeed;
        motor[leftmotor]=slowspeed;
    }
    if(nMotorEncoder[rightmotor]> nMotorEncoder[leftmotor])
    {
        displayLCDNumber(0, 3, nMotorEncoder[rightmotor], 6);
        displayLCDNumber(1, 3, nMotorEncoder[leftmotor], 6);
        motor[rightmotor]=slowspeed;
        motor[leftmotor]=fastspeed;
    }
    if(nMotorEncoder[rightmotor]==nMotorEncoder[leftmotor])
    {
        displayLCDNumber(0, 3, nMotorEncoder[rightmotor], 6);
        displayLCDNumber(1, 3, nMotorEncoder[leftmotor], 6);

        motor[rightmotor]= fastspeed;
        motor[leftmotor]  = fastspeed;
    }


}

nMotorEncoder[leftmotor]=0;
nMotorEncoder[rightmotor]=0;

while(nMotorEncoder[rightmotor]< 400)
{
    displayLCDNumber(0, 3, nMotorEncoder[rightmotor], 6);
    displayLCDNumber(1, 3, nMotorEncoder[leftmotor], 6);
    motor[rightmotor]=63;
    motor[leftmotor]=-63;
}

nMotorEncoder[leftmotor]=0;
nMotorEncoder[rightmotor]=0;
while(nMotorEncoder[rightmotor] <1200)
{
if(nMotorEncoder[leftmotor]> nMotorEncoder[rightmotor])
{
displayLCDNumber(0, 3, nMotorEncoder[rightmotor], 6);
displayLCDNumber(1, 3, nMotorEncoder[leftmotor], 6);
motor[rightmotor]=fastspeed;
motor[leftmotor]=slowspeed;
}
if(nMotorEncoder[rightmotor]> nMotorEncoder[leftmotor])
{
displayLCDNumber(0, 3, nMotorEncoder[rightmotor], 6);
displayLCDNumber(1, 3, nMotorEncoder[leftmotor], 6);
motor[rightmotor]=slowspeed;
motor[leftmotor]=fastspeed;
}
if(nMotorEncoder[rightmotor]==nMotorEncoder[leftmotor])
{
displayLCDNumber(0, 3, nMotorEncoder[rightmotor], 6);
displayLCDNumber(1, 3, nMotorEncoder[leftmotor], 6);

        motor[rightmotor]= fastspeed;
        motor[leftmotor]  = fastspeed;
    }


}



}

Encoder counts can be negative, that is not a problem. If you move the wheel in one direction from its starting locale, it will count up clicks. Move it back to where it started and then some, and it will be negative. In the sample code below, you’ll see that for driving straight, it’s fine to just key off the absolute value of the encoder reading.

I see you’re writing the value to the LCD. Do the values on the LCD match the debugger?

Step 1: turn everything off: unplug computer from joystick or cortex; turn off joysticks; turn off cortex, quit RobotC, then turn everything on again / reconnect.

I’m sure that the information below does not solve all of your problems, but they are important issues to making your program actually work.

ISSUE #1
Other problems aside, you have a pretty fundamental flaw in your code structure (credit to @jpearman on this topic). You are setting motor levels multiple times in each iteration of the loop. It’s not good for the motors, your drive will be jerky, etc.

Also, you should not be checking every situation every time through the loop. If L is > than R, there’s no need to check if L is < R or L == R. Use If/else if statements, as written below (“ELSE” in caps below for emphasis only).

To fix this flaw, create a variable to hold your desired motor power. Modify your multi-level checking while-loops to have this structure:

// just once, at the top of the program
int motorLeftPower;
int motorRightPower;

while(nMotorEncoder[rightmotor] < ####) {
   if L > R {
     motorRightPower = fastspeed;
     motorLeftPower = slowspeed;
   }
   ELSE if R > L  {
     motorRightPower = slowspeed;
     motorLeftPower = fastspeed;
   }
   ELSE if R == L {
     motorRightPower = fastspeed;
     motorLeftPower = fastspeed;
   }

   // Set the motor power here, at the very end of the loop, to whatever your
   // checks above determined was the desired speed.

   motor[right] = motorRightPower;
   motor[left] = motorLeftPower;

   // a short wait here gives the cortex time to actually deliver your instructions
   // to the motor before your next set of checks
  wait 50ms  
}

Here’s the pseudo code of what your robot is doing; is this your plan? It seems kinda weird, but maybe there are other things happening on the robot in each of these time periods?

  1. drive forward for 1450 clicks, keeping left & right sides even
  2. drive forward 450 clicks at 63 power
  3. drive forward 1400 clicks, keeping L & R even
  4. drive forward 450 clicks at 63 power
  5. drive forward 1200 clicks, keeping L&R even
  6. drive forward 400 clicks at 63 power
  7. drive forward 1200 clicks, keeping L&R even

ISSUE #2
You have to go and get the encoder values inside each while loop. They’re not just constantly updated in the background.

Usually, you’d check them at the end of the loop, so that you have values ready to go in the next iteration of the loop:

int rightEncoderValue;
int leftEncoderValue;

// get encoder values once, before all of these while loops so you have something to start with.
rightEncoderValue = abs(nMotorEncoder[rightmotor]);
leftEncoderValue = abs(nMotorEncoder[leftmotor]);

// then each loop is like this, using the variables you defined above:
while leftEncoderValue > rightEncoderValue {

   check stuff
   do stuff with motors

   small wait

   // get updated encoder counts to be ready for the next loop
   rightEncoderValue = abs(nMotorEncoder[rightmotor]);
   leftEncoderValue = abs(nMotorEncoder[leftmotor]);
}

Hope some or all of this helps.

Leslie