# What PID values should I keep?

Robotics Asked by Sulaiman Ayub on January 6, 2022

I have built quadcopter but the problem is of balancing. It doesn’t goes up. I am using PID technique for balancing. But I am not finding the suitable values for PID tuning. I am using mpu6050 as a sensor. I get the accelerometer values of x and y axis and find the error from them. That is lets say if accel on x is not zero then it error cause it should be zero if balanced. I am using +-2g sensitivity scale of accelerometer. The motors I am using are DJI 920 kva. What values for kp, ki, and kd should I set? I cant set them while in flight cause it completely out of balance. This is the design. Completely homemade. I have modified it a little after this photo. An accelerometer is at 2g so at balance z will be 32768/2.

short PID()
{
short error,v;
error = desired-current;
//error/=390;
integ += error;
der = error - perror;
x = error;
x2 = integ;
x3 = der;
x* = kp;
x2* = ki;
x3* = kd;
v = kpi;x/=100;
v = kii;x2/=1000;
v = kdi;x3/=1000;
x = x+x2+x3;
//x/=390;
perror = error;
return x;
}


There are also few more questions, should I scale error or PID output? Because, the error is ranging from 0 to 16380 at 2g setting, so I am scaling it from 0 to 42. So should I divide error or PID by some value?

Okay, so developed a flight controller for this project http://kevinsheridan.info/highly-autonomous-aerial-reconnaissance-robot-proof-of-concept/

I spent 4 months trying to figure out "the right pid values". However in the end it turned out that I was implementing pid incorrectly.

Here is how to implement it. If you are trying to get the quad to stabilize itself at an angle you need to use TWO Pid controllers per axis.

Requested angle & Angle -> PID1 -> rate + requested rate into PID2-> motors

Basically use a pid for rate and for angle feeding into eachother

Answered by Trexter on January 6, 2022

I asked for your code because I've found people tend to implement PID controllers incorrectly. It doesn't really matter what your gains are, if you're not calculating the error terms correctly and/or you're not applying the gains correctly, you'll probably never get the system to work right.

That said, as Arlakt mentioned, your best bet for PID control with no system model is to try different gains. There are several methods for tuning the PID controller, but all of these methods rely on you to calculate the error terms correctly and apply the gains correctly.

## :EDIT:

You are not calculating your error terms correctly.

You have:

error = desired-current;
//error/=390;
integ += error;
der = error - perror;


Your integ error term is not an integral and your der error term is not a derivative because you have not incorporated a sample time into your calculation. You need:

integ += error*sampleTime;
der = (error - perror)/sampleTime;


Later, you have:

v=kpi;x/=100;
v=kii;x2/=1000;
v=kdi;x3/=1000;


Which I am assuming is interpreted as:

v=kpi;
x/=100;
v=kii;
x2/=1000;
v=kdi;
x3/=1000;


I do not know why you are dividing your error terms by 100 or 1000, but this is incorrect. I also do not know what v is; it is not a term used anywhere else in the function and is not returned by the function. kpi,kii,kdi are also not defined.

In any event, your entire PID controller should be:

short PID()
{
short error;
error = desired-current;
integ += error*sampleTime;
der = (error - perror)/sampleTime;
x=error;
x2=integ;
x3=der;
x*=kp;
x2*=ki;
x3*=kd;
x=x+x2+x3;
perror = error;
return x;
}


See this post I wrote yesterday on nearly identical flaws in a PID controller. In short, get sampleTime with either a delay/pause or by setting/using a timer.

Answered by Chuck on January 6, 2022

I'm sorry but it is absolutely impossible to tell you what values you should put based on the information you gave. Actually to give a real answer we would need a complete model of the system (or an approximation at least...). There are some generic models for quadcopters but parameters depend on your own system.

If you want to really understand the command of a quadcopter I recommend to read some articles like this one (I found it very quickly, maybe some others are better, but it explains how to control a quadcopter with a PID). Or at least watch this video to understand what is a PID controler.

However, if you don't want to find a model for your system, the best way to find PID parameters is empirically. You just need to know what are the consequences of each coefficient, and try to adapt them according to your system's behaviour. There is a very quick explanation for each coeff :

• P : is mainly for response speed. Too low = correction will be to slow. Too high : correction will be very fast and oversized, and your system will probably oscillate.
• I : is mainly used to cancel static error. Too low : your response will never reach the order. Too high : oscillations.
• D : is... more complex to understand. It will impact your response variation. Look at the video to have a good explanation.

Good luck !

Answered by user10198 on January 6, 2022

## Related Questions

### How do I call Matlab code into Simulink for doing a simulation for doing plots?

1  Asked on October 20, 2020 by j-d

### Library for GPS

1  Asked on September 29, 2020 by muhammed-roshan

### How can I connect arducopter to Raspberry Pi?

1  Asked on September 24, 2020 by recep-orak

### Arduino turning off when using bipolar stepper motor (L298N Driver)

1  Asked on July 26, 2020 by tushar-bandi

### Is there a way to generate power using the ocean through a floating device

1  Asked on July 21, 2020 by figbar