# The logic of implementing an Auto-level function in a PID flight controller

So I have multi-rotor with a basic PID controller, that keeps its axis stable through the gyroscope. However, the multi rotor, does not keep its height or position. So I would like to use an accelerometer for keeping its rough position (auto level). I want to use both the gyro and accelerometer, but how would the accelerometer values be used, is it implemented through the PID the same ways the gyro values are (degrees per second, which is the rate I used to calculate PID)? And then adjusting the esc through that?? I am confused at that part (the basic logic for using the accelerometer values)

• Can you please clarify your question? You have a PID controller that uses gyro data to do what? Is the PID controller for auto-leveling and you want to also use accelerometer data? Do you want to replace gyro data with accelerometer data? I can't tell what exactly your question is or what you're trying to do. – Chuck Jan 28 '16 at 14:06
• Please edit your question with this additional info and delete the comment. – Ben Jan 29 '16 at 1:51

A gyro gives angular velocity in degrees or radians per second.

An accelerometer gives acceleration in feet or meters per second squared.

The key to keeping a reference position is getting from an acceleration to a position. This is done with by integrating the acceleration twice. The first integration gets from acceleration to velocity, then the second integration gets from velocity to distance.

So, if you were interested in implementing these integrations on a digital device, then you would use numerical integration in a manner as follows:

velocity = 0; % Set initial velocity
position = 0; % Set initial position
startTime = <get timer data>;
<main loop>
endTime = <get timer data>;
sampleTime = endTime - startTime;
startTime = endTime;

acceleration = <get accelerometer data>;
velocity = velocity + acceleration*sampleTime;
position = position + velocity*sampleTime;

positionProportionalError = positionReference - position;
<loop>


Once you have the positionProportionalError, you can integrate/derive the other two errors for PID and use that for your PID control.