quadcopter issue

HI guys
Im working on a quadcopter since last december, it's completely homamade, I have projected the frame, the transmitter, the receiver and the FC.
So far it works great, but the only problem is that I don't have Autoleveling mode, I really don't know from where to start the code, for now the quad fly using just the gyro. I'd like to implement the autolevel by combining gyro and accelerometer. Can I use the same PID criterion that I've used for the gyro to the accelerometer+gyro system? actually I have tried that way but it doesn't work.
How can I do it? is there any algorithm? can somebody explain it to me please? I don't need a code which is already prepared, I would like to create.
Thanks a lot

What do you use as the Setpoint of your PID control loops?
What do you use at the Input of your PID control loops?

If you use the joystick input for the Setpoint and the current roll/pitch/yaw as your Input then you would just use the accelerometer to get a better roll/pitch/yaw for your Inputs.

yes I use the joystick for the three axis, I get the inputs in microseconds, and translate them to degrees per second, then I calculate the the error with the gyro acceleration rate. But this way it just conpensate for the angular accelerations.
I would like to keep it always leveled when i don't give any inputs.

kevin1312:
I get the inputs in microseconds, and translate them to degrees per second,

Try translating to "degrees from vertical" for roll and pitch. You can keep "degrees per second" for yaw. That will allow you to use the accelerometer to measure "degrees from vertical" and use the joystick to control direction.

So you say, to get directly the degrees, by integrating the acceleration and feed this value to the PID?

kevin1312:
So you say, to get directly the degrees, by integrating the acceleration and feed this value to the PID?

No need to integrate. For small values of pitch and roll it just takes some math to get tilt angles. A quick Google search turned up this page:
https://www.dfrobot.com/wiki/index.php?title=How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing