I’m working on a Quadcopter for my senior design in
electrical engineering (this is the last semester). There are two problems with
this, my professors don’t allow me to use an already made code or share my code
so this makes it a little bit difficult to ask for help, but I’m going to try
to explain my program as much as I can. This might also work as a guide to
anyone trying to make their own program. (I’m using an arduino mega)


Right now, I’m having a lot of problems getting my PID
coefficients to work. (I hope that’s the only thing I need)


I’m using http://www.sparkfun.com/products/9431
6 DOF 2 gyros 1 accelerometer to get my pitch and roll angles (I also have a
magnetometer to measure the yaw but I’m not implementing that yet). In order to
get the angles I multiply the gyro reading times the difference of the time and
to get the angles from the accelerometer I use trigonometric functions
(accelerometers give the vector of the acceleration and you have to get the X
and Y angles from that vector).


After calculating the angle from the gyroscope and
accelerometer I use a kalman filter to mix both signals, you need to combine
them because a gyro keeps increasing its error with time and the accelerometer
is too noisy and gives wrong readings in certain occasions. I used to have a
complementary filter but I think the Kalman filter works better, the
complementary filter is only an equation:  compAngleX = (0.98*(compAngleX+(gyroXrate*dtime/1000)))+(0.02*(accXangle)).
I’m using almost the same script as this one: http://arduino.cc/forum/index.php/topic,58048.0.html
great project and guy by the way very helpful.

After the kalman filter I’m able to get a pretty accurate
angle in both directions going from -90 to 90 there might be two problems with
this though, I don’t actually know if I’m getting the angle measurements fast
enough because of the sensors or the script and I don’t know if I should use
low pass filters (my sensors are analog not digital). The measurements look
fast enough although my eyes can’t read at 50Hz or more so I have my doubts.

After the kalman filter I use the angles as input for a PID
control, I’m using the arduino PID library for this. This might not be good
enough I really don’t know. I know how a PID is supposed to be but I don’t
think I can do a better PID that the one in the library, the guy who wrote it
did a pretty good job ( here’s the explanation made by the author:  http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid...)


These are my settings for the PID:

My set point varies with respect of the receiver signal for
pitch and roll, the set point is 0 when I’m not touching the remote control
stick and it can go up to 35 this means that when I move the stick all the way
the PID will try to set the output (which is the actual angle of the quadcopter)
to 35 degrees in the specified direction.


The PID library is on automatic which I think means that it
will always be working, manual is when you want to turn the PID off at some


My output limits go from -90 to 90 this are the same as the
input angles, I don’t know if this will cause a problem. I didn’t want to have
more than 90 as a limit since the PID might take longer to get to the maximum
and I kind of wanted to keep the value from -90 to 90.

My sample time for the PID is 10 milliseconds which will
give 100 Hz, my ESCs run at 50 Hz so this should be fine? I’m not entirely sure;
maybe the sampling time should run at the exact frequency needed (50 Hz).


Finally, I have two sets of coefficients for the PID one set
of conservative coefficients (really small almost 0) and a set of aggressive
coefficients. The aggressive coefficients are used when the difference of the
angle and the set point is more than 10 degrees and the conservative when it is
less. I haven’t experimented with the two sets of coefficients that much, I
used to have only one set but I found out that with vibrations the sensors can
measure a little less than 10 degrees even when at an actual 0 degrees so
hopefully this will eliminate some noise and increase the speed of the motors when
it actually needs it.

Now, my biggest question comes right after this. Once I have
the value coming out of the PID I add that value (absolute value) to the value
of the throttle (the one that controls the speed of the 4 motors at the same
time) my question is: what is the recommended percentage that the direction and
balancing signals should have with respect of the maximum speed of the motors?


I actually have a potentiometer that changes the value of
the throttle (the value for direction and balancing always go from 0 to 90 when
taking absolute value) this changes the percentage of those signals.

To send the signal to the ESCs I’m using the arduino servo
library this should be working just fine, I used an oscilloscope to check that
the signals coming out of the arduino give the same width (minimum and maximum)
and frequency as my receiver of the remote control, I don’t have the values
right now but it was somewhere close to 50 Hz and 1000ms minimum width and
2000ms maximum width.

I might be calibrating the ESCs the wrong way though, what
you are supposed to do in order to calibrate them is to start the motors having
the throttle in the maximum position then after hearing certain beeps you are
supposed to lower the stick to the lowest position, this makes the ESCs know
what are your maximum and minimum widths and adjusts to make them its maximum
and minimum speed.

To make that work, my maximum value before going to the
servo library is the maximum value of the throttle this presents a problem, if
the throttle is at its maximum the motors won’t have any room to increase their
speed when the PID needs it in order to get the quad to the set point, this
shouldn’t be a big problem at first since I usually never go above the 50% of
the throttle in order to make the quad fly.

Sorry, this is getting quite long for a post. Just to make
it clearer I’m using a + configuration this means that to balance the quad I increase
the speed of the motor that is tilted down and for example when I need to go
forward the motor in the back increases its speed.


To calibrate my PID I start with only one axis (I disconnect
the other two motors and hang them) then 
I start calibrating, which I think works pretty well, the working axis
stabilizes really fast actually faster than some that I’ve seen in some videos,
but when I try all the motors at the same time I’m having a lot of problems
getting it off the ground. Let me rephrase that, it does get off the ground
somewhere around 10 cm, sometimes it wobbles a lot but it is still sort of
flying and some other times it looks like it is stable but when I try
increasing the speed a little bit more it just flips quite fast, as if it isn’t
even trying to stabilize after some point but it does stabilize when trying it
on one axis.


To anyone making their own quad you need the PID control it
really changes things I tried it first with no PID and you get an unstable
system and I really mean unstable, instead of decreasing the angle it just
keeps increasing more and more, it’s kind of fun to watch…


Anyways, is there something that I’m missing? Some secret
kind of PID control no one ever talks about or am I making a mistake or having
a wrong approach somewhere?


I hope someone can give me some hints and that this post
helps someone. If anyone has any question I’ll try to answer as fast as I can.


Views: 10489

Reply to This

Replies to This Discussion

It looks like you are in the final steps of your project. But just in case it can be of some help, I am working on the physical modeling of the quadcopter in the state space following this text book:


This discussion is helping me too.

Thank you very much.


Hey Javio,


Looks like an interesting book, I'll have to see if I can look through it sometime.


As far as state space goes, I've gone through making a block diagram of the attitude angle control loop, and the only non-linearity is where the propeller acts upon the envrionment.


Since a propeller directly outputs acceleration, and not airspeed, you have to go through some serious math when trying to linearize it. I did however find a useful equation:


w_craft = w_prop * (prop_pitch / radius)


w_craft = angular speed of the craft

w_prop = angular speed of the propeller

prop_pitch = The propellers pitch ( or distance per 1 revolution of the propeller)

radius = the distance from the motor to the center of mass. Perpendicular to the propeller's thrust vector



You can then extend this equation to all motors working on a given axis ( whatever you've defined it as ). 


Note that this equation is actually a limit, but for small changes in w_prop, it can be assumed linear and instantanious.


I've got the math around here somewhere, but chances are your going through the same thing right about now. :P


I don't know if it is allowed by the forum policy or not but I fund the book in this Internet link:


and I attach it to this post. Hope this is of some help and don't forget to buy it if you like it in:


To the moderator: If this post is inappropriate, feel free to delete it.




Hi Allen, hope your project is going well. Ive had some busy weeks, sorry i havent posted anything. Anyways my quadcopter is flying, it flied one week before it was due lol.

I didnt use anything similar to the equations you just posted anywhere actually im not entirely sure what they are for. My quad still needs some work to be as stable as many projects out there but i think im going to take a break from quadcopters for a while x_x .

I appreciate all the help you gave me and im here if I could be of any help. Here is the final flight I made with the quadcopter,



@ Javio Thanks for the info.

Hey Eric,

Things are going to be slow for some time to come, I finally bit the bullet and decided to go all out with the design of the software. It's looking very promising now, but I need to get the programming details ironed out as well as hunt for a couple of coders to help get the grunt work done.

Thumbs up on the flight! It looks like the pilot was doing most of the work though (instead of the MCU), lol.

||Absolutely. The final output to each motor should be the collective plus the output of the attitude controller for that motor. The collective would be varied by a seperate height controller routine.

what is collective? i still dont understand on it.
please make it more clear, what are the inputs for PID for each motor? and what is the output for each motor?

thanks in advance

Don't think of having a PID for each motor, rather, think of having a PID for each quantity that you want to control. For instance, the heading controller/compensator is a single PID that controls the heading angle. It does this by making a differential value to clockwise (CQ) and counter-clockwise (CCQ) spinning motors. The value has the same magnitude, but different polarity between the CQ and CCQ motors, i.g. an output of +1 to CQ motors would mean an output of -1 to CCQ motors at the same time.

This should, in effect, make the craft rotate CCQ, which by convention is a positive rotation. (CQ motors make the craft spin CCQ, review Newton's laws of motion if confused).

The output of the heading PID would the be added to the outputs of the pitch and roll PID's, and finally also the collective. The pitch and roll PID's will also be differential just like the heading PID - same value but different polarity.

The collective can have it's own PID, which should at least control height rate (the rate the craft climbs or descends). It differs from the attitude and heading PID's in the fact that it is not differential - it has the same value and polarity applied to all motors.

The collective is a control taken from traditional helicopters, which also use the term "collective." For both, it means "collective thrust," which is the total amount of thrust (force) generated by the rotor(s), and is what determines the speed and acceleration of your craft.

For quadrotors, the collective can be thought of a baseline motor speed to which all other PID's either increase or decrease the speed of each motor.

Finally, the input to all of these PID's should come directly from your receiver, or from a more advanced system that would also control linear positions and/or velocities.

If you still don't quite understand what's going on, do a bit more research on linear control systems. Keep in mind that quadrotors have direct control over their attitude and heading, but have indirect control over velocities and positioning.

thanks a lot Allen,
so i made i big mistake by thinking like that,
now, i re-program my quad based on pid like you said 

Hey Allen, I saw in one of your threads you had done a presentation at Texas A&M, are you still in school there? I'm a senior here at Texas A&M and was wondering if maybe you knew of any local spots for info on these things. I'm wanting to make a pretty sturdy one for aerial video footage, but I'm not sure about the local laws for something like this. I've done my homework though and (I'm sure you pro's will spit on me for this idea), but I think I want to go with the pre-built dji naza controller (w/ or w/out gps?).


This is a study of filtering out Gyro filter from Quadcopter by analyzing real data.

hello, i just want to ask how to relate the 6 dof output to change the quadcopter motor's speed so it would stabilize the copter?

Are you looking to make your own program, or are you just looking for something to get you flying?

Reply to Discussion


© 2019   Created by Chris Anderson.   Powered by

Badges  |  Report an Issue  |  Terms of Service