With any microcontroller project, you don’t get very far without software, my project is no different. In this section I will describe how the software to date works.

The software can be broken down into 3 main parts:

- The Command Line Interface
- The Kalman Filter
- The Control Loop

**The Command Line Interface**

To interface from the PC to the microcontroller I implemented a simple command line interface to be able to send commands to the microcontroller and get information from it through a COM port Terminal program such as Tera Term. I chose to do things this way so I would not need to write a second program on the PC to interface to the microcontroller. This way quadcopter can easily communicate with any computer that supports the FTDI Virtual COM Port Drivers, which looks to be everything from Windows, Mac, Linux and even Android. When a character is typed into the COM port terminal it gets sent out the COM port in this case the USB port, which goes to an FTDI USB to UART chip. For convenience I use one of these FTDI USB to Serial cables:

http://www.ftdichip.com/Support/Documents/DataSheets/Cables/DS_TTL-232R_CABLES.pdf

This then goes to the UART port on the microcontroller and causes an interrupt. In the interrupt service routine the received character is then sent back out the UART back to the computer, this echos the character back causing it to show up in the COM port terminal, the character is then stored in an array and the microcontroller waits to receive another character. If the microcontroller receives a carriage return from pressing enter on the keyboard the string is compared to a list of commands and if it matches a command it executes the code for that command. The array is then purged and the microcontroller waits to recieve another character.

Currently the software only supports very basic commands such as ones to set and store new PID constants in the EEPROM, Start and Stop the PID Loop control. Although this list may grow in the future.

I also used this interface to get the sensor and Kalman Filter data to do the graphs in previous blog posts.

**The Kalman Filter**

The next main part of the software is the Kalman Filter. The Kalman Filter is a mathematical method for combining multiple inaccurate measurements and getting a more accurate measurement. In my system I have an accelerometer and a gyroscope both which have their own quirks which make them inaccurate on their own. The accelerometer measures linear acceleration in 3 orthogonal planes (X, Y and Z). Since the earth provides 1G of acceleration towards its surface this can be uses as a tilt or angle sensor by taking the Tangent of the two linear axis’ you want to get the angle between. his works great as long as there is no vibration or other linear acceleration that would affect the reading. I also have a gyroscope which gives angular velocity in 3 orthogonal rotational axis about the sensor. If velocity is integrated over time one can calculate the position. The only problem with integrating the output of the gyroscope is that due to manufacturing process of the sensor they are not ideal and the output does not go to 0 when the sensor is stationary this causes the output of the integrator to drift slowly increasing the error over time. These can be seen in the graph below. The data was taken with the quadcopter propellers running so the accelerometer (Green) picked up all the vibration from the propellers and motors, which makes the accelerometer data on its own useless. The gyroscope (red) on the other hand had no problem with the vibration but you can see over time it had a downward trend due to the integrator drift. The output of the Kalman Filter has the best of both worlds it has little noise like the gyroscope but it doesn’t drift like the accelerometer.

**The Control Loop**

Currently to control the angle of the Quadcopter I am using a PID or Proportional, Integral, Derivative controller. I am currently experimenting on it in one axis and once that is working well it should be easy to implement in other axis’. Right now I just want the quadcopter to maintain an angle of 0°. Shown below is the basic block diagram of a PID control loop.

A control loop has 4 main signals (Shown above):

**Setpoint:** Where you want tobe.
**Output:** Where you currently are.
**Feedback:** Usually sensor data transduced from your output.
**Error:** How far you are from where you want to be.

In a PID control loop you have a set point which in my system I want the quadcopter to maintain an angle of 0° so it remains flat o the set point is 0°. I can measure the angle from the data of the accelerometer and gyroscope which has one through the Kalman filter as described above; this provides the feedback. The feedback angle is subtracted from the set point to product the Error which goes to the PID controller. The error is then multiplied by KP which is the proportional gain constant, the derivative of the error which essentially is the change in the error divided by time (Current_Error – Prev_Error)/Time_Step is multiplied by KD; the derivative gain constant, and the integral of the error is multiplied by KI; the integral gain constant. The output of the proportional part is added to the output of the derivative and integral parts and then this goes to the system as the control signal; in my system this is the throttle signal to the motors which adjusts the output.

Essentially what the three parts of the system; proportional, derivative and integral do are:

**Proportional:** Corrects current error; the larger the error, the larger the output of the proportional section which moves the output toward the setpoint as it moves to the setpoint the output of the proportional section gets smaller slowing the output down.
**Derivative:** Corrects future error; the Derivative takes the change in error overtime which in my system is essentially the angular velocity of the output. Now if the angular velocity was high when the error is near zero the output will just overshoot causing oscillation. The derivative section slows down the output thus reducing overshoot and oscillation.
**Integral:** Corrects Previous error; the Integral adds up error over time so if there is error in one direction for a long period of time the output of the integrator will continually add up until the output reaches or passes the set point. his can be used to eliminate steady state error where the system is close but not at the set point and not moving so the output of the proportional section isn’t enough to overcome the force holding it back such as friction or gravity and since it is not moving the derivative is zero and therefore the derivative section does not help. Overtime the error will add up in the integrator increasing the control signal and moving the system to the set point.

Andrew