How to tune PID I-term on a quadcopter

With this entry I want to initiate short series of articles showing how to tune multirotor / quadcopter PID controller. Let’s call it a continuation of Blackbox series, but this time I will not relay only on Blackbox data. Yes, I will show few examples how given scenarios looks on Blackbox logs, but all steps will be doable without Blackbox.

Before we proceed, you much understand how PID controller works and what Kp aka. P-gain, Ki aka. I-gain and Kd aka. D-gain are responsible for. There are many sources: Wikipedia, my “What is PID controller article”, great video from Bruce and many many others.

How to tune I-term of PID controller

Most PID tuning tutorials suggests to start with P tuning and then move to I. Why? Probably because it’s simpler to get UAV in flyable state. Or because P is first letter on PID. Or I have no idea. Starting with I has some advantages:

  • tuned I-term will not affect much more important Pterm tuning
  • it is simpler to tune I than P if proper method is used
  • tuned I-term will probably not change during P and D tuning

Before we proceed, a reminder of what I-term is used for and short characteristic of it:

  • I-term is responsible for correcting long time error accumulated in the past: non-even thrust from motors, wind, etc.
  • I-term should not be affected by short events like few degrees attitude correction
  • I-term should should react fast in special conditions like upside-down phase of rolls and flips
  • bigger I-gain not only means that correction will be stronger. What is more important, it also determines how fast I-term would change!
  • too low I-term causes multirotor unable to keep desired attitude. UAV will bank on its own, be sensitive to wind
  • too much I-term will introduce overshot, visible low frequency oscillations, wobble on descend
  • gentle, smooth flying for aerial photography requires much lower I-gain than aggressive acrobatic flight
  • better UAV balance (center of thrust close to center of gravity) results in lower required I-gain
  • usually, due to weight distribution, pitch axis requires more I-gain than roll axis

There are two methods of manual I-gain tuning:

  • Tuning for smooth, aerial photography, flying with bigger drones
  • Tuning for acrobatic flying with smaller drones (250 size and smaller)

How to tune I-term for smooth flying, aerial photography style

General rule for I-gain tuning for smooth flying on bigger machines (8″ or bigger propellers, > 1kg) is to keep I as low as possible. Multirotors like these are not designed for acrobatics, or aggressive flying. Their main purpose to to be stable in hover or cruise and do not wobble on descend. Higher I-gain, due to bigger (sometimes huge) inertia of both body and propellers will result in overshooting, wobbles and oscillations.

  1. Start with weight balance. Center of gravity should be as close as possible to point of thrust. Simple procedure looks like this:
    1. For pitch and roll axis find a line that crosses the frame between two point lying in the middle between motors opposing this line
    2. Try to lift multirotor using two fingers catching centerplate where line crosses it
    3. If frame tilts forward, move some weight (usually battery) back, if frame tilts left, move some weight right, and so on
    4. This does not have to perfect, but the closer you will get, the better flight performance you will have
  2. Tune roll and pitch separately. Ignore yaw for now, this can be tuned much later and usually it is not required to tune yaw at all!
  3. Tuning has to be done in Acro/Rate mode, without self leveling!
  4. Start with default PID gains. In most cases they are good enough for hover. If drone is able to take off and does not wobble on its own, you are good to go. If it is sluggish, wobbles, tilts on its own, increase P gain in 20% steps until it is hovering without much trouble. Remember, we are not tuning P yet. We just want it to be flyable.
  5. Throttle up and do a fast ascend for few meters. Then cut throttle to about 25% and descend fast. Observe what is happening to a drone. Three things can happen:
    1. If drone is unable to keep attitude and tilts is any directions, I gain on this axis is too low. Increase I gain and repeat experiment
    2. If drone keeps attitude, but wobbles on descend, I gain is too high. Decrease I gain and repeat
    3. Try to keep I gain lower than higher. That means you should concentrate on finding a spot closer to a point where UAV is unable to keep attitude that to the point where wobble appears
    4. If drone keeps attitude and does not wobbles on descend, I gain is tuned
  6. Confirm I-gain tune by doing forward-backward and left-right fast flights. If multirotor is able to keep angles, does not slowly drift or slowly wobbles during braking, congratulations, you tuned I-gain for smooth flying!

On a Blackbox log, wobble during fast descend might look like this: too much I gain during descend

Both axises: roll and pitch were affected in this case, but bigger wobble appeared on pitch axis, up to 45degrees per second! If you take a look at pitch PID graphs, you would see that I-term moved from more positive, to negative and then to positive again. P term tried to compensate, but it had to fight not only with changing conditions, but also with changing I term after original movement has been canceled out. This might not a be a perfect example, but shows general principle.

This example show what is happening in similar situation when I gain is much lower. I term stays more less flat, most of the work is done by P term.

not too much I gain during descend

How to tune I-term for acrobatic flying

If you would tune I gain using procedure from previous paragraph and went doing some rolls and flips, you would notice something bad: when copter crosses 90 degrees and begins upside-down phase, single wobble, a strong jerk, appears. Like I mentioned before, I gain does not only determines correction force. It also determines allowed speed of change when conditions changes. Imagine a copter that is slightly tail heavy. That means motors in the back have to spin slightly faster than those in the front to compensate for weight imbalance. I term does that very moment you take off and it works as long as you do not try to bring everything upside down very fast. If rear motors would still bring more trust that forward motors when drone is inverted, it would not compensate for weight imbalance. It would make thing worse than better. Our imaginary tail heavy multirotor needs less thrust in the back when flying inverted.

Flips, rolls and all other rapid maneuvers requires higher I gain to allow for faster I term change. If I term is unable to follow strong changes, single wobble or multiple wobbles would appear when passing magical 90 degrees inclination.

Another problem with method from previous paragraph is that small machines are much more wobble on descent resistant than big ones. Smaller, faster rotating propellers, less inertia, more agile. One would really have to push I gain very high to see strong wobble during descend. And it still not would do good for acrobatics. This is why, on those machines, try the following

  1. Balance multirotor like above
  2. Start with default PIDs
  3. Use only Acro/Rate mode
  4. Tune each axis separately
  5. Take off and check UAV stability during fast ascends and descends
    1. If it keep attitude but wobbles, lower I gain
    2. If it does not wobbles, but does not keep inclination, rise I gain
    3. If it does not wobbles and keeps attitude, we can move forward
    4. This step does not have to be very precise. As long and it keep inclination, you are good to go
  6. Go high. If you have FPV, this helps a lot. If not, just pay attention to drones behavior
  7. Do a fast single roll. If during transition to inverted flight (around 90 degrees) multirotor jerked, I gain is too low and I term is unable to follow the change. Raise I gain on axis and try again. The hardest thing here is to decide which axis is responsible for jerk. Sometimes it is single axis, sometimes both roll and pitch together: FPV helps a lot with this. If you really do not know, rise both. Repeat this step until roll is smooth, without visible wobble
  8. Do the same during flips. If you did everything right in previous step, this is only to confirm everything works like expected
  9. Confirm there are no wobbles during fast descends. If they appear try lowering I gains a little

Small note: I gain that allows for smooth transition to inverted flight flight depends on rotation speed and imbalance. The bigger imbalance and faster the rotation, the higher I gain would be required. So if you change rates, you might want to repeat I term tuning.

Read More

DIY wireless telemetry link for UAV

Telemetry link between UAV (drone, airplane, boat) and laptop/mobile/ground station device can be very useful. Not only to get current drone position, altitude or battery level, but also, when wireless link provides such a possibility, to update drone parameters in-flight. Some radio links, like OpenLRS provides such a possibility out of the box. They include transparent serial bridge and almost any kind of device can use it to communicate with flight controller. Unfortunately, most RC radio systems lacks this functionality and additional telemetry links have to be used. Like SiK Telemetry Radio or 3DR commercial version of it.

One can buy or one can build something by his own. Some time ago I’ve chosen the second way and decided to build my own wireless serial link to archive 2 way communication between drone and ground station software. My objectives were:

  • 433MHz since it is legal in my country
  • has to allow to use my phone with EZ-GUI, since I do not like to carry my notebook to an airfield
  • as cheap as possible

To satisfy those objectives I’ve decided as follows:

Continue reading “DIY wireless telemetry link for UAV” »

Read More

HC-12 433MHz wireless serial communication module configuration

Description

HC-12 are cheap 433MHz wireless serial port communication modules with a range up to 1800m in open space. Each costs about $5 when bought from China, and 2 of them can create wireless UART link that can be used, for example, to transfer telemetry data from UAV. Or drive IoT device. Or connect sensors. Or whatever else one can think of.

HC-12 433MHz wireless serial communication module

It is based on SI4463 RF chip, has build in microcontroller, can be configured using AT commands and allows to use external antenna. Working frequency is divided into 100 channels starting from 433,4MHz up to 473,0MHz with 400kHz channel separation. Maximum output power is 100mW (20dBm) and receiver sensitivity differs from -117dBm to -100dBm, depending on transmission speed. It accepts 3,2V-5,5V power supply and can be used with 3.3V and 5V UART voltage devices (5V safe). Continue reading “HC-12 433MHz wireless serial communication module configuration” »

Read More

Improving cheap radio range

Radio range of my first drone, UDI 829A was pretty pathetic. OK, it was (still is, I only have to finally replace motors) pretty indestructible, quite stable and reasonably priced. But effective radio range as quite short. More less 50 meters. Above that limit strange things started to happen. The reason was pretty simple. Just take a look at this picture.

Antenna too short

Can you see the transmitter antenna? Yeap, that it this short cable. Not only it does not go into “antenna cover” of the receiver, it is also horizontally polarized and when receiver is held in a normal way, pointing into a drone, it emits almost no signal in this direction. This and an additional single whip antenna on the receiver makes it virtually impossible to have a good radio range.

So, I’ve decided to fix that and install external antenna that would work with vertical linear polarization and actually emits some power in drone’s direction.
This tutorial shows how to do it for UDI 829A, but will work almost all cheap drones. Their transmitters are very similar inside and as long as there is antenna pad or connector, it can be done. Continue reading “Improving cheap radio range” »

Read More

Connecting ultrasonic rangefinder (Sonar) to Cleanflight

Keeping constant altitude with a drone is not a trivial task. Specially, if drone is supposed to keep give altitude very precisely few meters above the ground. One of the reasons for it is difficulty of reading precise altitude. Barometer can drift when atmospheric pressure changes and can produce a lot of noise. GPS is very inaccurate when dealing with altitude. One of the options is to use some kind of rangefinder. Ultrasonic for example. Cleanflight and its derivatives supports exactly one kind of those: cheap HC-SR04 sonar. There are plans to integrate different sonars, but none of official builds supports it yet.

dav

Continue reading “Connecting ultrasonic rangefinder (Sonar) to Cleanflight” »

Read More

WiFi telemetry for Cleanflight with EZ-GUI and ESP8266

Some time ago I have written a tutorial how to setup Bluetooth based telemetry link between Cleanflight and PC or smartphone. It’s simple and it works. But it has some disadvantages. For example, connection is very very slow and has a limited range: up to 10 meters. While it is enough to change PIDs before flight or plan a mission with EZ-GUI, it is not enough to have a real, usable and reliable, telemetry link.

ESP8266 ESP-01 Version 2

Luckily, Bluetooth is not the only cheap radio protocol we can use for purposes like this. Why not to use WiFi? Since market saw ESP8266 some time ago, cheap programmable WiFi modules became a reality. There are cheap, they are simple to use and can be programmed to do much more than just act as Access Point or network client. There are many alternative firmwares. For example ones that provides transparent bridges via TCP to allow pass serial ports over WiFi. Almost like serial over Bluetooth, but using WiFi instead. In theory that gives more range, higher speed and lower delay. Why not to use it to connect Android smartphone to drone flight controller and have nice an cheap telemetry solution? Exactly, why not. In this example how to do it with Android EZ-GUI and Flip32 running Cleanflight 1.12, but exactly the same trick can be used for Betaflight, SPracingF3, iNav, Baseflight, Naze32, APM, Pixhawx, MultiWii. If anything is using UART, it can also use ESP8266 as serial bridge over WiFi. Continue reading “WiFi telemetry for Cleanflight with EZ-GUI and ESP8266” »

Read More

iNav: Cleanflight learned how to do missions

In my recent post I mentioned that iNav flight controller software (fork of Cleanflight) introduced missions. Missions are preprogrammed waypoints that drone will fly to in specified order and/or do specified action at each of them. For example, if pilot wants to make a video on specified route, he does not have to pilot his drone all the time. He enters waypoints and lets machine do everything else. In this entry I will show how to configure iNav to do missions.

Requirements

  • iNav compatible drone flight controller flashed with iNav software. Currently supported boards are: Naze32, Flip32, CC3D, SPracingF3, Sparky, RDMO
  • GPS module connected to flight controller board. This example shows how to do it for Flip32, but it will work for all other boards, only UART pins might be different
  • Magnetometer connected and properly calibrated
  • Barometer connected
  • EZ-GUI Ground Station Android application that will act as Mission Planner. Official Cleanflight Configurator does not support this function yet
  • Bluetooth telemetry. Some time ago I have written how to do it for Flip32, but it will work almost the same for all other flight controllers, only UART pins might be different

Continue reading “iNav: Cleanflight learned how to do missions” »

Read More

Smartport telemetry for Cleanflight

With their latest transmitters and receivers FrSky changed telemetry protocol. XJT module, Taranis radios, X8R, X4R and X4RSB are using SmartPort telemetry protocol. And that creates few problems. First of all, SmartPort is a serial protocol. That means, flight controller has to have free serial port to connect S.Port device. Second of all, TX and RX lines shares the same wire. The work in half-duplex mode. Third of all, SmartPort signal levels are inverted: logical 0 is in HIGH state, logical 1 is in high state. All of that combined, connecting SmartPort receiver to flight controller and sending telemetry data is not so easy to archive. Specially on FCs without hardware inverters. That applies to most popular STM32F1 devices like Naze32 and Flip32. Not only numer of UARTs there is limited, but also they lack hardare inverters. Of curse, everything is possible and hardware solution for Cleanflight, Naze32 and SmartPort telemetry can be found here.

Luckily, there is simpler solution for Cleanflight that uses SoftSerial and does not require any hardware hacks besides special wire. Requirements:

  • Cleanflight capable flight controller (STM32F1 or STM32F3),
  • SmartPort enabled receiver: FrSky X8R, X6R, X4R, X4RSB,
  • Possibility to enable SoftSerial. Depending on FC type, different fetures like Parallel PWM, Sonar, LED Strip or Current Meter collides with SoftSerial functionality. Check documentation first. In case of Naze32/Flip32 WS2812b LED strips and Parallel PWM can not be used.

Continue reading “Smartport telemetry for Cleanflight” »

Read More

Programming ATtiny85 and ATtiny45 with Arduino IDE

What is ATtiny

ATtiny is a fimily of microcontrollers by Atmel, the same company that provides ATmega series used widely in “real” Arduinos. Comparing to ATmega, ATtinys are much simpler, smaller (usually), with less features. But also cheaper, easier to connect, using less energy, and trust me, in many many cases you do not need 32kB of flash memory. If, for example, you want to build a device that will beep every 10 minutes which microcontroller would you use: huge DIP-28 ATmega328P from Arduino UNO R3 or small DIP-8 ATtiny25 that ususes way less power and costs around 1EUR? I would use ATtiny.

ATtiny85 as light sensor with I2C bus

There are many microcontrollers in ATtiny family. In this tutorial and all future in this series I will concentrate on ATtiny85 with 8kB of flash memory. There are 2 simpler versions of it: ATtiny25 and ATtiny45 with respectively 2kB and 4kB of flash, but price difference between them is so small, that I see no point of trying to use them. When buoght from China, it might be even possible to buy ATtiny85 cheaper than its smaller brothers. Continue reading “Programming ATtiny85 and ATtiny45 with Arduino IDE” »

Read More

Detecting Cleanflight PID tuning issues with Blackbox: not enough P

This is third part of Cleanflight PID tuning tutorial with Blackbox. Previously I’ve showed examples of:

This time it is time for something slightly different: not enough P gain. Usually this problem can be identified without any log analysis. Symptoms are quite visible: multirotor is sluggish during maneuvers, has a tendency to change attitude on its own, constant course corrections are required. In worse cases, it is unflyable. But how does it look like on Blackbox logs.

First of all, symptoms are not so clearly visible. There are no huge oscillations for example. Zoomed out log might event look good on a first glance. For example like this:

blackbox pid tuning not enough P overview

Continue reading “Detecting Cleanflight PID tuning issues with Blackbox: not enough P” »

Read More