1
$\begingroup$

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.

Quadcopter

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?

$\endgroup$
4
  • $\begingroup$ 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? $\endgroup$
    – Chuck
    Aug 30, 2015 at 21:11
  • $\begingroup$ 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. $\endgroup$ Aug 31, 2015 at 8:22
  • $\begingroup$ 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). $\endgroup$
    – Mark Booth
    Sep 1, 2015 at 10:57
  • 1
    $\begingroup$ What are the v=kpi, v=kii and v=kdi statements intended to do? $\endgroup$
    – Mark Booth
    Sep 1, 2015 at 11:07

3 Answers 3

2
$\begingroup$

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.

$\endgroup$
15
  • $\begingroup$ 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 $\endgroup$ Sep 1, 2015 at 5:01
  • $\begingroup$ 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. $\endgroup$ Sep 1, 2015 at 5:19
  • $\begingroup$ 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. $\endgroup$
    – Mark Booth
    Sep 1, 2015 at 11:03
  • $\begingroup$ 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. $\endgroup$
    – Chuck
    Sep 1, 2015 at 12:35
  • $\begingroup$ 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. $\endgroup$
    – Chuck
    Sep 1, 2015 at 12:38
1
$\begingroup$

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 !

$\endgroup$
0
$\begingroup$

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

have a look at my code: http://kevinsheridan.info/highly-autonomous-aerial-reconnaissance-robot-proof-of-concept/haarr-code/low-level-flight-controller-code/

$\endgroup$
2
  • $\begingroup$ Actually that is what i am doing. $\endgroup$ Aug 31, 2015 at 16:23
  • $\begingroup$ I added a link to the code for my flight controller. it might help with the rate PID values. $\endgroup$
    – Trexter
    Sep 22, 2015 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.