Unofficial reply: Driving Straight with encoders help

@RednaxNewo -

  1. it’s not obvious, but the RobotC tech support channel is only answered by RobotC staff, and the community cannot respond to your request if you put it there.
  2. whenever you post code, please put opening and closing "
" tags around your entire program, or use highlight the text and press the </> button in the toolbar. People are more likely to help you if they can read your code easily.

See comments in code below, starting with "BLP" (BigLeslieP):

// BLP: you shouldn’t need this line; on my end, one can use abs(whatever)
// without having to define like this
#define abs(X) ((X < 0) ? -1 * X : X)

void driveStraightDistance(int tenthsOfIn, int masterPower) {
int tickGoal = (42 * tenthsOfIn) / 10;

//This will count up the total encoder ticks despite the fact that the encoders are constantly reset.
// BLP: why are you contstantly resetting the encoders? You should reset them once, here, before the while
// loop, just once each time you call this function
// per comments below, you’d delete this line
int totalTicks = 0;

int slavePower = masterPower - 5;
int error = 0;
// BLP: Normally Kp is a fraction, and one multiplies the error by Kp, but that’s just
// a technicality
int kp = 5;

// BLP: just setting them once here is fine
SensorValue[leftEncoder] = 0;
SensorValue[rightEncoder] = 0;

//Monitor ‘totalTicks’, instead of the values of the encoders which are constantly reset.
// BLP: change this to while(abs(SensorValue[leftEncoder]) < tickGoal)
while(abs(totalTicks) < tickGoal) {
//Proportional algorithm to keep the robot going straight.
motor[leftMotor] = masterPower;
motor[rightMotor] = slavePower;
motor[frontLeftMotor] = masterPower;
motor[frontRightMotor] = slavePower;

  error = SensorValue[leftEncoder] - SensorValue[rightEncoder];

  // BLP: as mentioned above, usually you multiply error by Kp
  // BLP: I think this is where you are going wrong ; you want to set
  // slavePower = masterPower + error/Kp, not add the adjustment to what
  // the slavePower already is
  slavePower += error / kp;

  // totally skip this part; it's not needed in each iteration
  SensorValue[leftEncoder] = 0;
  SensorValue[rightEncoder] = 0;


  // BLP: totally skip this step; not needed
  totalTicks+= SensorValue[leftEncoder];