1

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?

Milan
  • 207
  • 2
  • 10
  • 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? – Chuck 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. – Sulaiman Ayub Aug 31 '15 at 08: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). – Mark Booth Sep 01 '15 at 10:57
  • 1
    What are the v=kpi, v=kii and v=kdi statements intended to do? – Mark Booth Sep 01 '15 at 11:07

3 Answers3

2

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.

Chuck
  • 16,061
  • 2
  • 18
  • 47
  • 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 – Sulaiman Ayub Sep 01 '15 at 05: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. – Sulaiman Ayub Sep 01 '15 at 05: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. – Mark Booth Sep 01 '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. – Chuck Sep 01 '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. – Chuck Sep 01 '15 at 12:38
  • @MarkBooth is saying that instead of taking (error - perror)/sampleTime and then multiplying that by kd, you can take (error - perror) and multiply that by kd/sampleTime, then if sampleTime is constant then the effective derivative gain becomes kd_eff = kd/sampleTime. HOWEVER, you need to know what the sample time is, AND this trick only works for the integral and proportional gains - you should not be trying to modify the proportional gain term this way because the proportional term is time-independent. – Chuck Sep 01 '15 at 12:43
  • If you are deriving your gains empirically then sampleTime is irrelevant (it isn't mentioned in the Manual Tuning section of the wiki article you linked to). Only if you are trying to set gains based on something like the Ziegler–Nichols method would you need to factor in time constants directly. Also, you are assuming floating point arithmetic is always better. If you have a low end microcontroller, an integer arithmetic loop at a higher rate will be better than a floating point loop at a rate which is too low to achieve stability. – Mark Booth Sep 01 '15 at 14:05
  • Ok sample time will be in secs, millisecond or what. – Sulaiman Ayub Sep 01 '15 at 14:08
  • If you are deriving your gains empirically and your sample time is constant then it is irrelevant. If you can't keep a steady sample time, however, then your error terms are going to be inconsistent. The sample time should be measured in seconds - 0.01 for 10ms, etc. I don't know how you can do PID control with integer-only math. That said, if floating point operations are especially expensive, you can clock your process for a while and evaluate the mean and standard deviation of the result. – Chuck Sep 01 '15 at 14:30
  • If your standard deviation is large (more than a few percent) relative to the mean, then you probably need to run the clock all the time and use an exact sample time. 99 percent of samples should fall within +/- 3 standard deviations, so if your standard deviation is, say, 2 percent of your mean, then that means that your sample time varies by +/- 6 percent of your mean, for a total sample-to-sample variation of up to 12%. – Chuck Sep 01 '15 at 14:37
  • Oh dear, I feel an 'Ah, kids these days' welling up. *8') If you don't have deterministic, no jitter, constant time PID loop handling then you have more problems than calculating your per cycle sample time. Sample time should be as close to constant as is possible, the softer the real time requirements of your system are the further away it can be from constant, but the less aggressively you can tune your system. – Mark Booth Sep 01 '15 at 15:46
  • Anyway, this probably isn't helping to improve your answer, so we should take this to [chat]. I would suggest that you incorporate any thought which might be useful into your answer and we can tidy up (delete) these comments. – Mark Booth Sep 01 '15 at 15:47
  • I am not taking derivative into account. Just using pi. And i think sample time can doesnt matter when you carefully select kp,ki. – Sulaiman Ayub Sep 01 '15 at 16:15
  • Nope, I'm good. I don't know that any of these comments added much that I haven't already written. Feel free to delete away. – Chuck Sep 01 '15 at 19:44
  • Last comment - @SulaimanAyub - you are using derivative. That's your x3 term. I started writing in Robotics chat. – Chuck Sep 01 '15 at 19:45
1

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 !

0

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/

Trexter
  • 11
  • 3