codecraft dunedin, 2015-03-04, blackbox feature for cleanflight, nicholas sherlock
TRANSCRIPT
Introduction to quadcopters
Four sets of propellers, motors and
motor speed controllers
One radio receiver
One flight controller
- STM32 running at 72MHz
- 20kB RAM, 128kB of flash
- Sensors:
- Gyroscope
- Accelerometer
- Magnetometer
- Barometer
- GPS
- Cleanflight firmware
Cleanflight’s inheritance
Originally: MultiWii, using an 8-bit microcontroller and the
gyro/accelerometer package from a Wii remote
Baseflight, a port of MultiWii to a faster 32-bit CPU
Cleanflight, a fork of Baseflight which adds significant
features and improves code abstraction. Adds support for
new targets including faster CPUs with hardware floating
point and more RAM.
Introduction to quadcopter
flight
Target rotation rate in three axesGyroscope
measurement of 3-axis
rotation rate
Diff
3-axis error
Introduction to quadcopter
flight3-axis error
PID controller
3-axis correction Motor mixer
4 different motor
commands to be
sent to the 4
motor speed
controllers
Throttle position
PID controller
Takes the rotation rate error on 3 axes as the input and
produces 3 axes of corrections.
Three corrective terms computed for each axis:
P – Proportional. A fixed portion of the error signal.
I – Integral. Sum of the error over time.
D – Differential. The slope of the error.
Self-leveling flight modes
Accelerometer can be used to work out which way is down.
Angle flight mode:
Transmitter stick sets an angle which the quadcopter should be
sitting at.
PID controller is changed to compute corrections based on the
angle instead of the rotation rate.
Horizon flight mode:
A mixture of the acro flight mode and the angle flight mode
When the stick is near the centre, the angle flight mode is used.
When the stick is near the edge, the acro flight mode is used.
PID tuningNeed to tune the controller’s response according to your
craft’s aerodynamics, motors, props and other factors.
Each axis has a factor for tuning each of the P, I and D
corrections.
The main loopwhile (true) {
sticks = readReceiver();gyros = readGyroscopes();
corrections = runPIDController(sticks, gyros);
motorCommands = computeMotorMixer(corrections, sticks.throttle);
sendCommandsToMotors(motorCommands);}
The main loopwhile (true) {
sticks = readReceiver();gyros = readGyroscopes();
corrections = runPIDController(sticks, gyros);
motorCommands = computeMotorMixer(corrections, sticks.throttle);
sendCommandsToMotors(motorCommands);
logBlackboxState(sticks, gyro, corrections, motorCommands);}
What is logged?
Loop start time in microseconds
4-axis stick positions
3-axis gyroscope, 3-axis accelerometer
Barometer altitude
3-axis magnetometer
Battery voltage and current
P, I and D corrections for each axis
Motor commands for each motor
GPS position and altitude
Where is it logged?
Logged to an onboard
OpenLog logging device which
stores it onto a microSD card.
However maximum write
speed is currently about
14,000B/s
And we want to log 20 – 30
variables at 500Hz which is
10,000 – 15,000 readings /
second.
Log compression
Need to reduce the number of bytes per frame, so:
Create a keyframe (intraframe, “I frame”) once every 32
frames. These don’t depend on any previous frame so they
allow recovery from dropped frames.
For the other frames, store them as deltas from the previous
frame (interframes, “P frames”). Deltas are smaller in
magnitude than the original values.
Integer compression
We want the small integers left over after subtracting the
previous frame to have small encodings.
Variable byte encoding:
Use the high bit of each byte as a “more bytes follow” flag
Write the integer out in little-endian order.
7-bits 0aaa aaaa => 0aaa aaaa
8-bits baaa aaaa => 1aaa aaaa 0000 000b
14-bits 00bb bbbb baaa aaaa => 1aaa aaaa 0bbb
bbbb
Tag/group compression
Tag 2 3S32 encoding - Store a 2 tag which says how many
bits the largest of the next 3 signed 32 bit fields require to
encode.
• 2 bits per field ss11 2233
• 4 bits per field ss00 1111 2222 3333
• 6 bits per field ss11 1111 0022 2222 0033 3333
• 32 bits per field sstt tttt followed by fields of various byte
counts
Compression results
Log 1 of 1, start 00:16.376, end 09:39.730, duration 09:23.353
StatisticsI frames 7313 49.5 bytes avg 362032 bytes totalP frames 226677 22.8 bytes avg 5169085 bytes totalE frames 2 9.0 bytes avg 18 bytes totalFrames 233990 23.6 bytes avg 5531117 bytes totalData rate 415Hz 9820 bytes/s 78600 baud
1 frames failed to decode, rendering 8 loop iterations unreadable. 26 iterations are missing in total (62ms, 0.01%)
Links
Cleanflight firmware – http://github.com/cleanflight/
My YouTube channel - @thenickdude
Multicopter subreddit – http://reddit.com/r/multicopter
RCGroups rc flying forum – http://rcgroups.com/