Showing posts from January, 2017

Custom Flight Controller Part 2.8: Getting the Orientation --- Processing Orientation Visualization

The last step is to visualize the orientation to check whether the orientation calculated correspond to the real orientation. Previously, Matlab was used which proved to be slow. Therefore, processing is used this time. It has been a popular choice for orientation visualization and there are a lot of ready-to-use packages to aid the development. I have chosen to follow the steps indicated in tutorial from DIY Hacking by Arvind Sanjeez.
The serial function inside the processing code provided by the tutorial has to be modified however to interface with serial communication sent from STM32. So I have configured STM32 to send quaternion in the following format.  For example, if the quaternion is [1 0 0 0], it would be printed as "Q: 1 0 0 0".  Character "Q:" can be used to check whether the data received in processing is correct. To extract the quaternion from the string received. The following code is implemented. functions like split() and trim() are inbuilt process…

Custom Flight Controller Part 2.7: Getting the Orientation --- Mag Calibration and Overall Algorithm Structure.

Magnetometer measures the direction and magnitude of magnetic field present. It is a great tool to correct yaw measurement. However, it needs calibration before it can be used to update orientation. For example, if a uncalibrated magnetometer measures 100 milliGause in x direction currently, rotating the magnetometer around z axis for 180 degrees should theoretically yield -100 milliGauss. But since the magnetometer is uncalibrated, it might give you a value like -50 millGauss. In this case, a -25 milliGauss bias should be added to the magnetometer x axis measurement so that the two measurements become +75 and -75 and are just opposite to each other.  This procedure of determining the magnetometer bias is done via magnetometer calibration. 
The method proposed in this post by Kris Winer is adopted to calibrate magnetometer in this project. Essentially, user needs to rotate the magnetometer non-stop during the calibration until the magnetomer has collected enough sample data. Then the…

Custom Flight Controller Part 2.6: Getting the Orientation --- Using Interrupt to Get Data from MPU9250

In previous method described in post Part 2.1, Arduino tries to get the measurement data from MPU9250 without checking whether a new data is available. Since Arduino is relatively slow, this method does not cause any problem as a new set of measurement is usually available before one iteration of the loop is completed. However, since STM32 is much faster, using interrupt is deemed to be a better choice. 
Therefore, with the new method, MPU9250 is programmed to send an interrupt to a GPIO pin of the STM32 microcontroller. When the microcontroller detects the interrupt, a data ready flag is set. The flag is checked before the execution of a new iteration. If the flag is set, STM32 will then fetch data from MPU9250 and perform sensor fusion. If the flag is not set, STM32 will start a new iteration. This posts will briefly explain the procedure to implement the method proposed above.
First, the interrupt needs to be enabled in MPU9250. This is done by configuring the Interrupt Enablee Re…

Custom Flight Controller Part 2.5: Getting the Orientation --- QKF on STM32

The same quaternion kalman filter described in Part 2.2 was re-written in C in IAR embedded workbench to be used on STM32 microcontroller. This posts will briefly introduce and explain the migrated code. 
Eigen, a popular c++ library for linear algebra unfortunately can not be recognized and compiled by the IAR C++ compiler. In the end, I chose CMSIS DSP Software Library, a library developed specifically for ARM Cortex-M processors, to perform matrix arithmetic. Correspondingly, “arm_math.h” has to be included in main.c. (arm_math.h is auto generated and recognized, you don't have to manually download or install it)
The basic format of initializing a matrix using CMSIS DSP Software Library looks like follows. 
After the initialization, the matrix named matrix1 is bounded to the float array matrix1_data. Any change to the matrix instance matrix1 will change the value of float array matrix1_data and vice versa.  
The library has provided all the necessary functions for basic matrix…

Custom Flight Controller Part 2.4: Getting the Orientation --- Migrate to STM32

As mentioned in the previous posts, the Matlab simulation has several issues and can only track slow rotations. Therefore, during the December holiday, I have managed to migrate the whole algorithm to a proper microcontroller of STM32F4 series and obtained much better simulation result from there.

With the new method, the microcontroller gets the data from MPU9250 using interrupt, calculates the orientation and publishes the resultant quaternion to my laptop via USB serial communication. Processing was then used to collect the quaternion data and visualize the result for testing. As the calculation of Quaternion Kalman Filter is now done by MCU instead of by Matlab, it solved the problem in previous posts.

The development board I bought was NucleoF429ZI. As the name suggests, the microcontroller it equips is STM32F429 which runs at 180MHZ which should be more than enough for our flight controller simulation. It also features a 2M Flash and includes all the necessary peripherals that …