# PID Tilting Issues

Hi, my team recently attached a descoring arm to our robot, and this has caused some weight distribution issues. Our robot now tilts to the left whenever we use our PID loop. I wrote a P loop to compensate (it just takes the difference between the motor encoder counts on the right and left and subtracts from the right motors’ velocity), but it did not work. No matter how much I increased the constant it would not work. I also tried just multiplying the speed of the right motors by a constant like 0.8 or 0.6 but it kept varying based on distances. Any suggestions? Thanks!

Posting your code would help. However, one common mistake I make is setting things to int so using the constant and setting that value to an int ends up being truncated to 0. So check to make sure all of your data types are correct. If that isn’t the issue, feel free to post your code and I’ll check it out.

1 Like

When you say that your robot “tilts to the left”, do you mean that it turns to the left?

``````void pidmove(double dist){
double kp = 0.55;
double ki = 0.00001475;
double kd = 1.75;

double goal = inchesToTicks(dist);

RightFChassis.resetRotation();
RightBChassis.resetRotation();
LeftFChassis.resetRotation();
LeftBChassis.resetRotation();

double error = goal - LeftFChassis.rotation(vex::rotationUnits::deg);
double integral = 0.0;
double oldError = error;

bool check = error > 0.0;
if(dist < 0.0){
check = error < 0.0;
}

while(check){
error = goal - LeftFChassis.rotation(vex::rotationUnits::deg);

if(error < 175.0){
integral += error;
}
else{
integral = 0.0;
}

double derivative = error - oldError;
oldError = error;

double motorSpeed = (kp * error) + (ki * integral) + (kd * derivative);
if(error > (goal * 0.85)){
if(motorSpeed > 180.0){
motorSpeed = 180.0;
}
}
double help = RightFChassis.rotation(vex::rotationUnits::deg) - LeftFChassis.rotation(vex::rotationUnits::deg);
if(fabs(error) > 350.0){
RightFChassis.spin(vex::directionType::fwd, (motorSpeed * 0.8), vex::velocityUnits::rpm);
RightBChassis.spin(vex::directionType::fwd, (motorSpeed * 0.8), vex::velocityUnits::rpm);
LeftFChassis.spin(vex::directionType::fwd, (motorSpeed), vex::velocityUnits::rpm);
LeftBChassis.spin(vex::directionType::fwd, (motorSpeed), vex::velocityUnits::rpm);
}
else{
RightFChassis.spin(vex::directionType::fwd, (motorSpeed), vex::velocityUnits::rpm);
RightBChassis.spin(vex::directionType::fwd, (motorSpeed), vex::velocityUnits::rpm);
LeftFChassis.spin(vex::directionType::fwd, (motorSpeed * 0.95), vex::velocityUnits::rpm);
LeftBChassis.spin(vex::directionType::fwd, (motorSpeed * 0.95), vex::velocityUnits::rpm);
}

check = error > 0.0;
if(dist < 0.0){
check = error < 0.0;
}
}

RightFChassis.stop(vex::brakeType::brake);
RightBChassis.stop(vex::brakeType::brake);
LeftFChassis.stop(vex::brakeType::brake);
LeftBChassis.stop(vex::brakeType::brake);
}
``````

This is the code, the help variable was being used for the p loop, and the if statement was something I was just trying. @sazrocks the robot is moving forward, like it is supposed to, but it is leaning to the left even though it is supposed to be moving straight.

In order to compensate for lag on one side of the drive you should subtract a small amount of “power” from the side that is “ahead”. So if the robot is veering off to the right, you would subtract power from the left side of the drive. The amount of power you subtract is proportional to how far off course the robot is. I’ll pm you a section of code that I used to solve this issue.