ATmega Quad Controller

ATmega328P flight controller development process

Download .zip Download .tar.gz View on GitHub

I started this project in order to learn all the programming skills required to build an ATmega328P-controlled quadcopter. I am starting with the very basics in order to understand everything I make during the project. My main motivation is to find a specific subject related to the flight-controller design of which I can write my masters thesis on. So far I have a few ideas, but I need to invest more time to really find what would be the best subject to work on. I am really interested in different flight control algorithms, inertial measurment filtering methods and guidance and navigation in general.

As a first objective I decided to design the prototype flight controller based on Arduino Uno. With a working prototype flight controller I will design my own board with ATmega328P. For inertial measurements I am using the Invensense MPU6050 IMU.

The controller design was greatly inspired by Joop Brokking's Youtube channel and blog. I find his YMFC-3D quadcopter video tutorials of great help and will refer to them often during the project. The design is also based on YMFC-3D. I have just received most of the components for the quadcopter and will post the parts list with images as soon as I assemble it.

I used many libraries from other Github users. All references to the authors are under each subsection of the readme file where the library is used.

My starting point was the development of the serial graphing monitor. This is a necessary tool for signal analysis during the development. First I wrote graphing monitor with Python 2.7 and later with C#. Both serial graphing programs are described below.

Serial Graphing Monitor

1. Serial Graphing Monitor - Python 2.7

is used to visualize the data sent via serial communication. I use it to analyze the data sent from the MPU6050 IMU connected to Arduino. Current code is written for Python 2.7. If you want to run it on Python 3 you need to change a couple of things (For example: -Py 2: from Queue import Queue -Py 3: from queue import queue).

 

In order to run the code you need to install the following libraries:

  • PyQtGraph
  • PyQt 4.8+ or PySide
  • NumPy
  • PySerial
Place the following files in a folder:
  • DataMonitor.py
  • globals.py
  • ComMonitor.py
  • SP_logo.png
  • COMPORT.png
  • BAUDRATE.png

To start the program run DataMonitor.py. The program is written to receive maximum three separate values at a time from serial. For now it is programmed so all three values need to be packed in a single line, for example, if you want to plot readings from all X, Y, and Z accelerometer axis you should write them to serial like this "AccX AccY AccZ\n". If only one or two values are written to serial, then only one or two data sets will be plotted. Code was inspired by MBA7's SerialPort-RealTime-Data-Plotter and Eli Bendersky work.

2. Serial Graphing Monitor - C# Windows Forms

was written after I developed the graphing program in Python. I found a desire to improve my knowledge of C# so I used this opportunity to write a program I needed.

In order to run the program you need to install ZedGraph.

Libraries for ATmega328 and Arduino

1. AK_MPU6050_lib for ATmega328:

I am working on a library to use MPU6050 IMU with ATmega328P chip. As of September 19, 2016 the library contains the following functions:

  • MPU6050_init()
  • MPU6050_test_I2C()
  • MPU6050_get_accel()
  • MPU6050_get_gyro()
  • MPU6050_set_sleepMode()
  • MPU6050_set_gyroFS()
  • MPU6050_set_accelFS()
  • MPU6050_set_clockSource()

The AK_MPU6050_lib uses I2c-master-lib by g4lvanix for I2C communication with the MPU6050 chip.

2. USART library for ATmega328:

This is a slightly modified version of a USART library written by Elliot Williams for his AVR Programming book. I must note that I have learned most of my AVR programming knowledge from this book and I highly recommend it.

Servo motor control

9/20/2016 I started working on controlling servo motors and ESCs with ATmega328P. Quadcopters don't use servo motors, but achive the control solely with the motors, but for the sake of understanding the principals I used a servo motor, since it gave me a visual feedback.

Servo motors turn to the desired position by receiving a 1000-2000us long pulse at a period of about 20ms (50Hz). 1500ms pulse should in fact turn the servo to its center position, while 2000ms and 1000ms pulses to its extremes. At first I thought about using the PWM signal with 50Hz frequency, but I decided to try and write my own, time-measuring, function similar to what Arduino's function micros() does. With that I could controll the servo in a similar way what Joop Brokking does in this video.. This method eliminates the need for an additional interrupt routine, which might mess things up like shown in the video. At first I tried to supply the servo with 1000-2000us pulses every 4ms (250kHz), because I had no idea about how servos worke. Soon enough I have realized that this was not the best for the motor, becaus it made strange noises. This in fact would work with fast ESC designed for quadcopter, since some of them can be controlled with frequencies as high as 400KHz. I have also tried controlling the servo with a much slower frequency - 10Hz, just to see how it behaved. With such a slow frequency, the accumulated voltage in the servo was very low and correspondingly the servo turne slowly.

Because the flight control program already uses an interrupt routine that reads RC transmitter signals, I am not sure what would be the effect of adding another interrupt routine called every 4ms (for updating the ESCs). As mentioned previously, the method Joop Brokking used on his YMFC-3D avoids adding thi interrupt. I will have to conduct more reaserch and testing to find the best solution on how to control the ESCs. If anyone knows more about this subject, I would be more than happy if they could provide additional information.

Author: alex.kraljic@gmail.com

There is still a lot to fix, add, and improve, so any suggestions and corrections are welcome!