Shop our Airplanes Products Drone Products Sales
Thread Tools
Feb 09, 2014, 06:46 PM
Whoop the planet!
benedikt's Avatar

MultiWii: manual, in-flight PID tuning via AUX channels

A while ago, I hacked some MultiWii code together to allow the adjustment of PID values in flight. It works beautifully!
I thought I'd kick off a discussion on this topic to make more people using it and to improve the code over time.

Credit goes to teslahead over on the MultiWii forums, who has done most of the work. I just adjusted it for my needs and trimmed the value ranges for the Pocket Quad.

Here is the exec summary:
  • PID values change the how the flight controller reacts to external forces and to control inputs. "Wrong" PID values can make a multicopter over- or under-react to pilot input, not compensate enough for external input (e.g. wind force) or compensate too much and enter the OoD (Oscillation of Death ).
  • Usually, PID values are programmed into the flight controller from the board manufacturer. In MultiWii, we are (un)-lucky enough (depending on your perspective) to have the ability (and responsibility) to set these values ourselves. This is often enough a frustrating trial and error game.
  • Dynamic PID adjustment allows the pilot to change those values on the fly (!) and adapt the flight characteristics quickly to flight conditions and the task on hand.

E.g. a sporty fly-around requires crisp and sharp values:
Pocket Cam - Pylon Racing on the Playground (4 min 2 sec)

and a sedated camera cruise asks for softer values:
Beautiful Belly @ Somersby Falls (2 min 17 sec)

Lets get cracking:
In the MultiWii standard code, there is the TPA (Throttle PID Attenuation) function. It is supposed to adjust PID values according to throttle input, to stiffen up the aiframe during high acceleration, or soften it during descents. This kicked off the idea for me, because it goes into the right direction, but the implementation was too limited to achieve what I needed.
So I started started off with simply replacing the throttle input with an AUX channel, and adjust TPA manually in flight.
Having achieved that, I was looking for further tuning options, to affect the P, I and D values for gyro and accelerometer separately. Teslaheads code came handy, so I just had to do some c&p to get the basics sorted.

Here is whats needed:
at the very end of void annexCode(), add the following:
  #if defined(DIAL_TUNING_BG)

    #if defined(POT_G)
      int ku = ((constrain(rcData[POT_G],1000,2000) -1000) << 1) >> 6 ; // * 20/640;
      int kp = ku + 8; //range: 1.0-3.7[PIDROLL].P8 = kp;[PIDPITCH].P8 = kp;
      ku = ((constrain(rcData[POT_G],1000,2000) -1000) << 1) >> 6 ; // * 20/640;
      kp = (ku * 2.7)+22 ;  //range: 10-37
	   //kp = ku * 6 ;  //range: 4-65
           //kp = ku * 10 ; //range: 7-100[PIDROLL].D8 = (kp * 3) >> 3;[PIDPITCH].D8 = (kp * 3) >> 3;
    #if defined(POT_A)
      ku = ((constrain(rcData[POT_A],1000,2000) -1000) << 1) >> 6 ; // * 20/640;
      kp = ku*3 + 8; //range: 1.4-9.5
      //int kp = 3*ku + 16;[PIDLEVEL].P8 = kp;
At the end of config.h, add the following:
  #define DIAL_TUNING_BG
  #define POT_G AUX2
  #define POT_A AUX3
Assign 2 AUX channels (ideally pots) to AUX2/3 (or whatever channels you want).
If you flashed this code and connect the board to the MW-GUI, you now twist the knob and when you hit "READ", the current PID values are shown in the GUI.
By changing the "kp=" equations, you can adjust the PID value ranges.
By adding or removing "conf.PID[" lines, you can change what AUX channel is affected what PID values.

I have decided to have channel (AUX2) control Gyro P (1.0 -3.7) and D (10-37) at the same time, and AUX3 control Acc P (1.4-9.5) only.

I hope the above is enough to get others interested in this PID tuning method, kick off some discussions about this, and make it a useful tool for all you propellerheads out there. My programming skills are very basic, but I will try to clarify and answer any questions that come up. My hope that some real code gurus get a sniff of this and will own this thread shortly.
Latest blog entry: this is the Micro Motor Museum!
Sign up now
to remove ads between posts
Feb 10, 2014, 08:01 AM
Registered User
ever came across ?
Feb 10, 2014, 05:11 PM
Whoop the planet!
benedikt's Avatar
hmm.. the github thread is primarily talking about RC rate and expo. PID settings are just briefly mentioned and dont seem to be in focus.
I found it much easier to adjust rates and expo on the transmitter. Instead of landing, connecting the board to the GUI, changing values and test, I rather change values in flight. But once you have experienced smoothly adjustable PIDs, rate and expo setting feel "old" anyway
Im running full linear rates on all sticks, and adjust behaviour to my needs purely via PIDs. That doesn't only impact reaction to stick input, but also all other reaction of the airframe (e.g. how quickly it returns to level in Angle mode, and how it reacts to side wind). A quick twist on the knobs turns an aggressive racer into a sleepy cruiser or anywhere in between. Different relations of gyro to acc values provide further interesting potential to spice up the flight experience.
Last edited by benedikt; Feb 11, 2014 at 04:58 AM.
Feb 11, 2014, 04:16 AM
Registered User
Ok, thanks for the explanation. Sounds like your code is a better solution compared to what they're trying to achive.
Would you mind creating a pull request for this feature so it can get a proper look and comments from the developper community ?
Feb 11, 2014, 04:54 AM
Whoop the planet!
benedikt's Avatar
this exact idea is floating around in the MultiWii forum since 2012.
It is so simple that even someone like myself with only very basic programming skills and very very basic knowledge of this particular code can punch it together.
Dont you think that if any of the core developers of MultiWii (my deepest thanks and respect, gents!) would have the slightest interest in dynamic PIDs, it would have been done long time ago?

Ill be honest: I had a look at the forking and pulling on github, and I'm not 100% sure what do to. Im not even sure if "baseflight" is the right place, because the above is from an older (16bit?) code. And I have right now so many things on my plate, that I just cant spend time working myself into yet another topic, even if you think its super simple.
If you know how to pull (!) this in front of the right people, would you mind taking it from here?
Latest blog entry: this is the Micro Motor Museum!
Feb 11, 2014, 06:04 AM
Registered User
I don't know, I'm not really used to forums, I would definitely not use them to find and implement new features, especially when the author use github.
I guess multiwii developpers should add a HOW_TO_CONTRIBUTE file in their repo with instructions on how to use github and branches to contribute.

oh no baseflight is definitely not the correct project
I'll probably use github to reference and eventually PR some changes some of you have done.
I'll just have to dig forums to salvage bits of code, to split huge patches into individual ones, and try to identify the authors...
how convenient ! (and don't expect the multiwii main developpers to do it for you, pretty sure they like to fly as well...)
Last edited by larzac; Feb 11, 2014 at 08:34 AM. Reason: s/really/not really/
Apr 16, 2014, 02:20 AM
Registered User
X_Marine's Avatar
Thanks for the info, it will be very helpful to MultiWii users.
I have never used the MultiWii fc but after watching I will have to check into it soon.

I also now understand your desire for the best brushed motors which aids many of us in search of performance.

Have to issue some kudos on your flying and nice job putting the videos together as well.

Many thanks
Last edited by X_Marine; Apr 16, 2014 at 02:22 AM. Reason: brushed**
Aug 06, 2014, 05:04 AM
Team WarpSquad
luca biada's Avatar
wow, love the idea of pid tuning from radio. i think it's better than the lcd display in kk boards. every fc should have this function. great hack
Aug 06, 2014, 08:48 AM
Registered User
Machy2k's Avatar
Now if I just had the right setup on my Devo 7e
Aug 06, 2014, 09:29 AM
Registered User
Not as convenient as adjusting from the radio, but you can adjust the PID values on the fly using the bluetooth dongle and a cell phone.
Aug 10, 2014, 08:58 PM
Registered User
I've decided to try this out since the bradwii autotune didn't work out for me and this seems like it would help with the tuning.

Couple questions:

1. I only have aux1 and aux2 available, so I'd like to have the MW default to horizon mode instead of having to use another switch to turn horizon mode on. Is that easy to change in the MW code?

2. The code here changes PID values for both gyro and acc separately (?), but the multiwii config only has a single set of PIDs for roll, pitch, yaw, etc. Can you explain the reasoning for this and how in general it affects things?

Aug 10, 2014, 09:21 PM
Whoop the planet!
benedikt's Avatar
1. no prob. You dont even need to change the code. Just go into the MWConf and tick all three checkboxes in the horizon row, for a random AUX channel. E.g. if you select that high/mid/low AUX1 enables horizon, it will be always active and you dont need a channel for mode switching.

2. the whole theory of that would go beyond my lunch break but here is a brief:
There are two sets of PIDs for the gyro loop (roll/pitch). When your copter is symmetrical, it makes sense always change roll and pitch simultaneously. I have decided to change gyro P and D proportionally. That seems to give good results.
And then there is one set of PIDs for the level loop. (Look further down in the GUI). I only change P here. The rest doesnt seem to do much.
Its hard to explain what it does.. changing the values in the air was my way to explain it to myself..
Level P is the main value I adjust (not applicable if you fly rate mode). This determines the stick response and beneral back-to-center force. More level P will make it more responsive, but also twitchier and wind sensitive. Too much level P and you get oscillations.
Gyro P and D work in the other direction. A high level P can be compensated/supported by higher gyro P and D values.
I usually dial the gyro in and when I got good gyro values for a certain setup, I usually leave it at that and adjust level P to conditions/mood.
Latest blog entry: this is the Micro Motor Museum!
Aug 14, 2014, 05:57 PM
Registered User
Does this play nicely with

The multiwii thread implies that the ranges of PID values change with that setting.
Aug 14, 2014, 07:32 PM
Whoop the planet!
benedikt's Avatar
I have no idea.
I assume the way of injecting values remains the same, but you probably have to adjust the value ranges.
Let us know if you try it!
Latest blog entry: this is the Micro Motor Museum!
Aug 14, 2014, 11:08 PM
Registered User
Tried it... broke a prop. :-)

Seems to fly better with default values compared to PID_CONTROLLER 1 though. More stable in horizon mode.

They have switched things around according to the thread. LEVEL_I in the gui is used for LEVEL_P in HORIZON mode. LEVEL_P in the gui is only used for LEVEL in angle mode (along with gyro pids).