Quadcopter Software Overview

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:


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.


Quadcopter Controller Rev 0 Revised

Well I eventually got the PCBs back from being made and built it up. I then powered up the board and there was smoke…..

Turns out I made a mistake choosing the 5V regulator. It turns out he one I used Microchip MCP1826S has a max input voltage of 6V and I tried to power it with 12V…oops.  But I can replace it with the good old LM7805 which I had laying around. Afterwards I noticed that even if the MCP1826S would accept a 12V input, with the power dissipation of a linear regulator stepping down 12V to 5V it would have gotten very hot possibly overheated. This is shown below.

Since the input is 12V and the out put is 5V that means there is 7V across the pass transistor of the linear regulator. According to the datasheeet for the MCP1826S the thermal resistance of the junction temperature to ambient temperature for the SOT223 package is 62°C/W

The max curent of the circuit should be about 110mA so:

LDO Temperture CalculationSo using the Thermal Resistance from the LDO datasheet of 62°C/W I get a junction temperature of 70.74°C which is hot but not enough to over-heat the chip but the 62°C/W was assuming a 4 layer PCB which wold be more thermally dissipative than my 2 layer PCB so the actual temperature will be higher than 70.74°C, Also it will heat up the PCB which could affect the sensor accuracy since they drift with changes is die temperature. To be safe on the next revision of the board I will use the LM7805 in a TO-220 package which has a much lower Ambient to unction Thermal Resistance and it is a through hole part so it will not heat up the PCB as much.

There were other problems with this revision that will have to be cleared up on the next revision:

  • I used a level shifter on the PWM output to shift the 3.3V PWM from the microcontroller to 5V since I read in some forums that some ESCs do not work with 3.3V logic. The only problem is it turns out the level shifter I chose only works with voltages 3.3V and lower…oops guess I need to double check the datasheets before I send these things off. Luckily the ESCs I have work with 3.3V so I can mod these boards to work.
  • I wasn’t too happy with the noise performance of the analog sensors the output of the Kalman filter was still bouncing around 1-3 degrees when the system was at rest. I bought a breakout board from Sparkfun with a digital accelerometer and digital gyroscope and got much better results. Changing to a digital sensors also allos me to use 1 3-axis gyroscope chip and I can remove some other parts like the analog reference. The noise performance is shown in the graphs below:




Quadcopter Controller Design Rev 0

Now that I have a microcontroler I can desgin the rest of the controller. For the sensors I chose to use:

  • X and Y axis Gyroscope: Invensense IDG-500
  • Z axis Gyroscope: Invensense ISZ-500
  • 3 axis Accelerometer: Analog Devices ADXL335
  • Pressure Sensor: Bosch BMP085
  • Compass: Honeywell HMC5883


The Accelerometer and Gyroscopes will be used to control the rotational axis’. I chose to use analog snsors to beable to have the fastest update rate. Had I used I2C digital sensors the max possible update rate would be much slower since the ACD conversion time i significantly less than the time it takes to read data from an I2C sensor at their max baudrate of 400kHz. Thee are some downsides I am going to have to live with since I am using the analog sensors I need to have a stable and accurate volatge reference for the ADC, in whch I chose the:

National Semiconductor LM4132EMF-3.3

  • 0.5% Tolerance
  • 30ppm/°C  Temperature Coefficient
  • 50ppm Long Term Stability

Other downsides are to the anaog sensors are the 10bit ADC on the micro controller, which only gives 1024 counts which could be a problem. Also Analog sensors are going to be more suceptable to EMI which cold be thrown off from the brushless motors. Even though the downsides I finished the design with the analog sensors and sent the Gerber files off for the boards to be made.

Quadcopter Controller Rev 0

Quadcopter Controller Rev 0




Quadcopter Microcontroller Selection Cont.

I also did another test to compare the speed of the two chips. I wrote the Kalman filter code that I will use in the quadcopter and ran it in MPLABSIM for the PIC32 and DsPIC both running at 80Mhz. I go he following results:

Kalman FIlter Simulated on DsPIC
Kalman Filter Simulated on DsPIC
Kalman FIlter Simulated on PIC32
Kalman Filter Simulated on PIC32

As you can see from the Stopwatch column the PIC32 uses less than half the instruction cycles for the same code of the DsPIC. Also the PIC32 ran the same code 5 times faster than the DsPIC. Speed can be important if I want to have high update rates on the PID loop so from all this I chose to use the PIC32 on my quadcopter controller.




Quadcopter Microcontroller Slection

The controller for the quadcopter is the brains of the operation. The first part of the design is to choose a microcontroller. Since I am most familiar with the Microchip IDE and I already own the microchip Tools.

I chose two candidates from Microchip:

Device Flash Memory RAM Timers Output Compare ADC UART I2C
dsPIC33FJ256GP710A 256KB 30KB 9 8 12 bit 2 2
PIC32MX795F512L 512KB Program + 12KB Bootloader 128KB 5 5 10 bit 6 4

Comparing the two:

  • Flash Memory: The PIC32 wins here 512KB vs 256KB, I may or maynot need the extra memory but since I am more of a hardware guy than a software guy it wont hurt to have extra memory.
  • RAM: The PIC32 here wins again 128KB vs 30KB, this could be important because I could use the RAM to log data on the PIC32 on the DsPIC I would probably have to use external serial memory such as FRAM which is expensive and the transfer rate would be much slower than the on-board RAM.
  • Timers: This is bascally a tie although the DsPIC has more Timers I probably only need 2: 1 for the PID loop and 1 for the PWM.
  • Output Compare: This is bascally a tie although the DsPIC has more Output Compare Channels, I only need 4: 1 for each motor.
  • ADC: The DsPIC wins here hands down a 12bit ADC gives you 4 times the resolution
  • UART: This is bascally a tie although the PIC32 has more UART Channels, I only need 1
  • I2C: This is bascally a tie although the PIC32 has more I2C Channels, I only need 1 or 2

Continued on next post.


Quadcopter Overview

For my final project for the BCIT Bachelors of in Electronics I am building a Quadcopter. This is a project I have been working on or the past few months and have decided to start documenting it. Below is a simple block diagram of the major components of the Quadcopter:

Quadcopter Block Diagram

It consists of:

  • A Quadcopter Controller which will be a custom PCB that I will design
  • 4 Electronic Speed Controllers which are brush-less motor drivers to interface between the Quadcopter Controller and the brush-less motors, which will be purchased
  • 4 Brush-less Motors which will be purchased

The main goal of this project is to get one axis of the quadcopter to work stably under feedback. Ultimately I would like the Quadcopter to hover or fly stably.