# What PID values should I keep?

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?

• What have you tried for PID gains already? Can you post your code? Can you post a drawing of your quad copter? You say, "the problem is of balancing," but then you say, "it doesn't go up." What do you mean? Aug 30 '15 at 21:11
• By saying "it doesnt goes up" means that due to unbalanced it doesnt goes up and goes drfting. And the missing part in question is that i control the motor speed with 42 values. That is it has 42 speed steps. On 42 it is at maximum speed. Aug 31 '15 at 8:22
• On stack exchange, it is better to edit your question to add information requested in comments, rather than adding more comments. Comments are for helping to improve questions and answers, and are distracting, so we try to keep them to a minimum. If all of the information needed to answer the question is contained within it, the comments can be tidied up (deleted). Sep 1 '15 at 10:57
• What are the v=kpi, v=kii and v=kdi statements intended to do? Sep 1 '15 at 11:07

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.

• Is sample time a must. And how much delay for sampletime is fine. I am using 8051uc. And the term kp/100, ki/1000 means that i am dividng the kp and ki,. It means that if kp is 1 so it is .01. I used this code for to have more kp samples, when i increase kp through remote so it will increase .01 not 1. Because in ucontrollers floating values are hard to implement. So to make kp foating i used this division Sep 1 '15 at 5:01
• And in the other post you have mentioned that higher sampletime will have bad result and lower will have good result. So then not using it will be best, correct me if i am wrong. Sep 1 '15 at 5:19
• The /=100 and /=1000 is just a rudimentary way to implement gains as a fixed point value. Unless you have a pretty beefy microcontroller with fast floating point support you want to use integer arithmetic for your PID loop. Similarly, if sample time is fixed, you should be able to avoid dividing by the sample time too, it just makes your gains less obvious, and invalidates them if you do change your sample time. Sep 1 '15 at 11:03
• Dividing a gain does not get you "more samples". I believe if you are wanting to define a variable as a float you can either type cast it as a float or I think you can add a ".0" to the end of a number defining it - so where "1" might be integer "1.0" would be a float. Sep 1 '15 at 12:35
• I believe @SulaimanAyub is misunderstanding the use of the sample time - you need to divide by the sample time in order to calculate the derivative error correctly. The derivative error "tells" the controller how quickly the error is changing - if the error is increasing quickly, then the P and I terms aren't doing enough so the derivative term picks up to help out. Similarly, when error starts decreasing quickly, the controller is about to overshoot the setpoint, so the derivative term tries to start the controller "backing off" so it hits the setpoint with no error. Sep 1 '15 at 12:38

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 !

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

• Actually that is what i am doing. Aug 31 '15 at 16:23
• I added a link to the code for my flight controller. it might help with the rate PID values. Sep 22 '15 at 13:14