mbedRTOS quadcopter quadrotor RATE and ATTITUDE mode (default). Altitude Hold soon to come. pHysiX Coding 2014.

Feel free to look at the code and implement your own. Don't forget to add in the correct licences, and relevant citation for the relevant developers if you do end up following parts of my code. Sharing is caring :) PM me for queries/code.

This article will be updated slowly, whenever I get a break from rock climbing, university and work commitments. Feel free to comment or ask me questions as need be.

Refer to Wiki Page for complete documentation!

Trung Tin Ian HUA - pHysiX Coding May 2014.

SAFETY FIRST

Quadcopters are inherently dangerous! When a quadcopter is on, it is a live saw!

NEVER UNDER ANY CIRCUMSTANCES DO THE FOLLOWING:

  • Fly in a closed or confined space.
    • Always allow enough room so that the quadcopter does not get near to people or valuables, and always allow enough room to take evasive action should you lose control.
  • Touch the quadcopter when it is armed, even if the propellors are not spinning!!!
    • The last thing you need is a trip to the ER/ED at 12am because the props cut off half your pinky (personal experience).

Please be observant when flying. If it looks as if you are losing control, just bring the quadcopter back to Earth. A broken motor/prop/frame/circuit can be replaced. Damaged property or worse yet, personal injury, can be irreversible!

Obey your local laws too regarding RC models. Be sensible so that others too can enjoy this wonderful hobby.

DISCLAIMER

My code is tailored to have a general approach in terms of flight motion and control. The safety features however are specific to my build!

A quirk is that even when the mbed enters deadlock or is taken offline, the ESCs will continue to run for a period until it figures out it has lost a signal. Chances are that the ESC may even shoot to 100% power!

You are free to use and test my code, BUT I TAKE NO RESPONSIBILITY in any circumstance for any problems you encounter, including and not limited to damage of any sort and especially injury to any person or animal.

With that aside, happy flying everyone!!! :D

Results/Demonstrations

Click on image to go to 1-Build Wiki. Link to youtube video

To do/discussions (respective order of priority)

  1. Switch to body frame instead of current YPR.
  2. Implement Altitude hold!
  3. Add support for EEPROM to store tuned gains outside of default
  4. Remap keyboard input for intuitive Telemetry/control
  5. Remove Bluetooth arming dependency
  6. Add in LCD output for offline setting/debugging
  7. Add in voltage sensing alarm
  8. Build GUI using Processing for Multiwii-like control
  9. Finish this article!

Changes

RevisionDateWhoCommit message
55:fc7cc7a4821c 2014-05-19 pHysiX Restructured code. Licence reproduction and citation of sources not present when code was made public. Code now withdrawn and kept private default tip
54:a36d39a90c21 2014-05-19 pHysiX PwmInRC library, removed mutex
53:c8641ecef04e 2014-05-19 pHysiX PwmInRC library updated with mutex for protection during ISR
52:53f3be66e988 2014-05-19 pHysiX Rearranged threads with correct priorities, semaphore and mutex. Timer reserved only for time critical tasks. Control loop now running at desirable rate. Code is stable (no RTOS crash), still need to test fly and retune PID!!!
51:04c6af4319e1 2014-05-19 pHysiX Code rearranged into threads. Timer reserved for time critical routines only. Implemented working semaphore and mutex
50:8a0accb23007 2014-05-19 pHysiX Semaphores implemented, currently no hanging. Need to test flight
49:c882f9135033 2014-05-19 pHysiX Changing RtosTimer functions into Threads
48:9dbdc4144f00 2014-05-19 pHysiX Altimeter and IMU changed to non-blocking I2C
47:89a7077a70d3 2014-05-18 pHysiX Broken I2C
46:b11655031d24 2014-05-17 pHysiX Beginning to implement I2C RTOS driver
45:3847d7bf8b2c 2014-05-17 pHysiX Retuned Roll PID to counter CM
44:4be5c01c6de2 2014-05-16 pHysiX Changed a few variables that were not declared volatile but are accessed by multiple threads
43:ec2146e37922 2014-05-16 pHysiX Changed PWMInRC library: Pulsewidth and Period are volatile
42:8f32f8148b57 2014-05-16 pHysiX Started to add in I2C RTOS Library
41:4e58f55fb00d 2014-05-16 pHysiX PID Rate Mode: Completely tuned; I-gain oscillation present, but dampens very quickly; Quadcopter lifts straight; PID Attitude Mode: Almost tuned; I-gain oscillation present, dampens approx. quick, but should be turned down. Quadcopter lifts straight
40:27e344adfdb9 2014-05-14 pHysiX PID stable again. Quadcopter lifting almost straight (off by 0.5deg max on back and left for rate; off by 2deg max on back and left for attitude); Need LPF or better deadbanding since transmitter has noisy sticks; Need Euler instead of YPR angles
39:02782ad251db 2014-05-14 pHysiX PID tuned; Frequency of Attitude and Rate PID decreased; ESC frequency decreased; RTOS back to stable; Increased stick gains
38:ef65533cca32 2014-05-13 pHysiX Quadcopter unstable again. ; Working on KP and KI for Rate and Attitude.; Still experimenting with frequency of Rate and Attitude loop.
37:29feef05d848 2014-05-13 pHysiX Fixed some bugs causing RTOS to crash, due to printfs being too long
36:d95e3d6f2fc4 2014-05-13 pHysiX Telemetry output completely changed; Code tidied
35:6aada2e9ffb8 2014-05-13 pHysiX Major commit. Getting ready to rearrange thread structure and function
34:228d87c45151 2014-05-12 pHysiX Code tidied
33:f88a6ee18103 2014-05-12 pHysiX Tidied code
32:7a9be7761c46 2014-05-12 pHysiX Increased PID and ESC update frequency.; ; Thread1: Only services ypr telemetry output; Thread2: Cascaded PI-PI control; Thread3: Changed UART control; Thread4: Changed calibration output and throttle, removed ESCpower output
31:3dde2201e54d 2014-05-12 pHysiX Rearranged threads and approach
30:d9b988f8d84f 2014-05-12 pHysiX WIP
29:6c03b30b59ec 2014-05-10 pHysiX Changed name of program. Still using alternative 2
28:aa72bd4ff103 2014-05-10 pHysiX Alternative2: Always run Master, but only update PV when YPR is updated
27:18b6580eb0b1 2014-05-10 pHysiX Alternative1: Run Master only when YPR is updated
26:4a3323ee36d5 2014-05-10 pHysiX Begun to implement Cascaded PID with tuned timings
25:a7cfe421cb4a 2014-05-08 pHysiX Added RATE and STABLE mode switch.; Tuned Primary and Secondary PID loops.; Quadcopter can now be flown stably indoors.; Need to add in Integral Control, and then clean up to move away from Bluetooth arming.
24:54a8cdf17378 2014-05-08 pHysiX Stable mode works. Already begun to start implementing MODE SWITCHING
23:75c0950b3592 2014-05-08 pHysiX Mbed-RTOS quadcopter using TiltyQuad IMU module.; ; Currently only flies in rate mode and is stable! Refer to Wiki for entire article. ; ; Always be careful, cautious and alert!!! Use this code at your own risk!; ; By Trung Tin Ian HUA 2014.
22:ef8aa9728013 2014-05-08 pHysiX Commented and tidied entire code for release
21:b642c18eccd1 2014-05-08 pHysiX Primary PID disabled. Secondary (rate) PID enabled. P gain tuned
20:b193a50a2ba3 2014-05-03 pHysiX Added in ability to control PID; fixed PID initialisation bug
19:bd88749c8db4 2014-05-03 pHysiX Added constraints to RC Command and ESC output
18:af657c4c3944 2014-05-03 pHysiX Better FIFO handling: Only keep max of 2 packets in FIFO, read all of FIFO each time FIFO is read
17:18c3bd016e49 2014-05-03 pHysiX Got rid of extra debug printfs. ; Implemented RCCommand restrain: 1000-2000us
16:9072cd6fa8d1 2014-05-03 pHysiX Better FIFO handling. Output YPR stable.
15:10edc6b12122 2014-05-03 pHysiX Next major change: Handling FIFO overflow
14:267368c83b6a 2014-05-02 pHysiX Reduced ESC pulse frequency to 200Hz from 400Hz since control is run up to 200Hz
13:6a983f2d42ea 2014-05-02 pHysiX Refixed usntable quaternion output. Need to optimise threads because DMP FIFO overflow is high
12:953d25061417 2014-05-02 pHysiX Added in all sensors. Need to add in EEPROM to complete control of Tilty. Finished all telemetry output and appropriate data rates.
11:f9fd410c48c2 2014-05-02 pHysiX Fixed quaternion, gyro at 200Hz.; ; Need arming sequence and calibration ability
10:ef5fe86f67fe 2014-05-01 pHysiX Fixed quaternion stability: FIFO was not serviced fast enough, and lacked correct tidying after each read
9:371950017779 2014-04-30 pHysiX Task1: Fix FIFO, or change to raw mode; Task2: Fix PID motor outputs; Task3: Need disarm/arm, calibration; Task4: Check motor mixing
8:f3e192cc714c 2014-04-30 pHysiX Experimenting with FIFO and sampling rate
7:3d28cfc4901b 2014-04-30 pHysiX Version 1.0: All systems implemented. Need to test RTOS stability, ESC behaviour during RTOS stall, and tune PID.; ; Need to implement PID based on Kp Ki Kd instead of Kp Ti Td.
6:1a5654c14b5b 2014-04-30 pHysiX Added in AUX for RC Command
5:4879ef0e6d41 2014-04-30 pHysiX Fixed bug with gyro and plane axis, added in corrected PID SP and PV
4:01921a136f58 2014-04-30 pHysiX Included RX Module read
3:605fbcb54e75 2014-04-29 pHysiX Fully implemented system. Need to test stability of RTOS, and to make sure that values are correct. ; Rate Mode only.
2:ab967d7b4346 2014-04-29 pHysiX Added in simple ESC initialise
1:43f8ac7ca6d7 2014-04-29 pHysiX Tidy file structure: threads in separate files for easy navigation
0:8c28fac22d27 2014-04-29 pHysiX MPU6050: DMP, RTOS