# Pidhelp

So I have this inertial sensor turn pid and the robot tends to drift due to the inertial sensor being imperfect I was told that I could find the pattern in the sensor that causes the robot to drift and correct It using math in the code .Does anyone know how I can do that?

here is the the code{

double posNeg180(double angle){
while(angle > 180){
angle -= 360;
}
while(angle <= -180){
angle += 360;
}
return angle;
}
double turnpid(double desiredValue){
double kp=0.9; //This number gets the bot to move

double ki=0.00005; //This prevents it from stopping

double kd=0.1; // This stops it at the end

double prevErrror = 0;

double derivitive = 0.0;

double totalError=0;

double error=posNeg180(desiredValue - inertials.yaw());

while(std::abs(error) > 4){
double angle=(inertials.yaw());

``````prevErrror=error;

error=posNeg180(desiredValue - angle);

derivitive=error-prevErrror;

totalError+=error;

double motorPower = error*kp + derivitive*kd + totalError*ki;

Lrw.spin(directionType::fwd,motorPower,velocityUnits::pct);

Lfw.spin(directionType::fwd,motorPower,velocityUnits::pct);

Rrw.spin(directionType::fwd,motorPower,velocityUnits::pct);

Rfw.spin(directionType::fwd,motorPower,velocityUnits::pct);

``````

}
Lrw.stop();
Lfw.stop();
Rrw.stop();
Rfw.stop();

return 1;

};

}end the code
//////

How do you think you should do that?

What is the behavior, exactly, that you are observing?

What is the desired behavior?

Why do you think this is happening?

How can you test if your theory of â€śwhy is this happeningâ€ť is correct?

1 Like

2 Likes
``````double posNeg180(double angle){
while(angle > 180){
angle -= 360;
}
while(angle <= -180){
angle += 360;
}
return angle;
}
double turnpid(double desiredValue){
double kp=0.9; //This number gets the bot to move

double ki=0.00005; //This prevents it from stopping

double kd=0.1; // This stops it at the end

double prevErrror = 0;

double derivitive = 0.0;

double totalError=0;

double error=posNeg180(desiredValue - inertials.yaw());

while(std::abs(error) > 4){
double angle=(inertials.yaw());

```
prevErrror=error;

error=posNeg180(desiredValue - angle);

derivitive=error-prevErrror;

totalError+=error;

double motorPower = error*kp + derivitive*kd + totalError*ki;

Lrw.spin(directionType::fwd,motorPower,velocityUnits::pct);

Lfw.spin(directionType::fwd,motorPower,velocityUnits::pct);

Rrw.spin(directionType::fwd,motorPower,velocityUnits::pct);

Rfw.spin(directionType::fwd,motorPower,velocityUnits::pct);

```

}
Lrw.stop();
Lfw.stop();
Rrw.stop();
Rfw.stop();

return 1;

};``````

Formatted

4 Likes
1. start the code
2. calibrate the imu
3. turn the robot 100 times
4. see how many degrees is the imu off from being at 0
5. repeat this 10 times and average the number of degrees off
6. given n, the degrees by which the imu is off, scale all imu reading by 36000 / (36000-n)
3 Likes

sorry I donâ€™t know how to make code tags so I did that to make it more clear

``````this is a code block
you literally just start the code with [code] on line one
and end it with [/code] at the end``````

even better, you can actually type outside the code block in one message, instead of making 2-4 messages anytime you have a question.

3 Likes

ftfy

4 Likes

oh yeah, except for when youâ€™ve been toldâ€¦ like, 2 weeks agoâ€¦ or yesterday, where you could edit your own post and see exactly how itâ€™s formattedâ€¦

5 Likes

Iâ€™ve been holding off on jumping in, but at this point, Iâ€™m just gonna say it.

At this point, you need to stop asking for help. Nobody is going to continuously read through your plethora of un-readable unformatted code and directly give you code that will do what you want it to do. Everybody here has provided good resources that you should reference on your original post (not the other 3 you posted after that) and you just need to get reading and do your own research in understanding what your code actually does rather than copying and pasting blindly.

Not to mention, James locked the previous thread for a reason and at a certain point, you should either talk to your mentor (if you have one) or find someone who will properly teach you code instead of spamming the forum with your low quality posts. The fact that you continue to ignore this and decide to make the same post 3 more times whenever you reach a point in each of the threads where you need to put in slightly any effort in troubleshooting your own code or researching what a PID actually shows that you need to re-evaluate your attitude in approaching coding.

I wish you the best of luck in fixing your PID and coding issues, but you arenâ€™t going to find it here. Why not utilize your sister teams to help you? I have definitely seen 46X active in one of these threads.

16 Likes