HobbyKing.com New Products Flash Sale
Reply
Thread Tools
Old Feb 09, 2014, 07:46 PM
proud to be orange!
benedikt's Avatar
Australia, NSW, Avoca Beach
Joined Jul 2013
1,822 Posts
Mini-HowTo
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:
Code:
  #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
      conf.pid[PIDROLL].P8 = kp;
      conf.pid[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
      conf.pid[PIDROLL].D8 = (kp * 3) >> 3;
      conf.pid[PIDPITCH].D8 = (kp * 3) >> 3;
    #endif
    
    #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; 
      conf.pid[PIDLEVEL].P8 = kp;
      
    #endif
  #endif
  
  }
At the end of config.h, add the following:
Code:
  #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.
benedikt is online now Find More Posts by benedikt
RCG Plus Member
Latest blog entry: The interactive AlienWii manual
Reply With Quote
Sign up now
to remove ads between posts
Old Feb 10, 2014, 09:01 AM
Registered User
Joined Jan 2014
21 Posts
ever came across https://github.com/multiwii/baseflight/pull/29 ?
larzac is offline Find More Posts by larzac
Reply With Quote
Old Feb 10, 2014, 06:11 PM
proud to be orange!
benedikt's Avatar
Australia, NSW, Avoca Beach
Joined Jul 2013
1,822 Posts
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.
benedikt is online now Find More Posts by benedikt
RCG Plus Member
Last edited by benedikt; Feb 11, 2014 at 05:58 AM.
Reply With Quote
Old Feb 11, 2014, 05:16 AM
Registered User
Joined Jan 2014
21 Posts
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 ?
larzac is offline Find More Posts by larzac
Reply With Quote
Old Feb 11, 2014, 05:54 AM
proud to be orange!
benedikt's Avatar
Australia, NSW, Avoca Beach
Joined Jul 2013
1,822 Posts
larzac,
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?
benedikt is online now Find More Posts by benedikt
RCG Plus Member
Latest blog entry: The interactive AlienWii manual
Reply With Quote
Old Feb 11, 2014, 07:04 AM
Registered User
Joined Jan 2014
21 Posts
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...)
larzac is offline Find More Posts by larzac
Last edited by larzac; Feb 11, 2014 at 09:34 AM. Reason: s/really/not really/
Reply With Quote
Old Apr 16, 2014, 03:20 AM
Registered User
X_Marine's Avatar
United States, TX, Houston
Joined Sep 2012
76 Posts
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
X/BillyD..
.
X_Marine is offline Find More Posts by X_Marine
Last edited by X_Marine; Apr 16, 2014 at 03:22 AM. Reason: brushed**
Reply With Quote
Old Aug 06, 2014, 06:04 AM
Team WarpSquad
luca biada's Avatar
Joined Jun 2014
549 Posts
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
luca biada is offline Find More Posts by luca biada
Reply With Quote
Old Aug 06, 2014, 09:48 AM
Registered User
Machy2k's Avatar
Ft Lauderdale, FL.
Joined Jan 2007
2,254 Posts
Now if I just had the right setup on my Devo 7e
Machy2k is offline Find More Posts by Machy2k
Reply With Quote
Old Aug 06, 2014, 10:29 AM
Registered User
Joined Mar 2013
106 Posts
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.
wobbletop is offline Find More Posts by wobbletop
Reply With Quote
Old Aug 10, 2014, 09:58 PM
Registered User
Joined Mar 2013
106 Posts
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?

Thanks!
wobbletop is offline Find More Posts by wobbletop
Reply With Quote
Old Aug 10, 2014, 10:21 PM
proud to be orange!
benedikt's Avatar
Australia, NSW, Avoca Beach
Joined Jul 2013
1,822 Posts
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.
benedikt is online now Find More Posts by benedikt
RCG Plus Member
Latest blog entry: The interactive AlienWii manual
Reply With Quote
Old Aug 14, 2014, 06:57 PM
Registered User
Joined Mar 2013
106 Posts
Does this play nicely with
#define PID_CONTROLLER 2
?

The multiwii thread implies that the ranges of PID values change with that setting.

http://www.multiwii.com/forum/viewto...tart=10#p37387
wobbletop is offline Find More Posts by wobbletop
Reply With Quote
Old Aug 14, 2014, 08:32 PM
proud to be orange!
benedikt's Avatar
Australia, NSW, Avoca Beach
Joined Jul 2013
1,822 Posts
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!
benedikt is online now Find More Posts by benedikt
RCG Plus Member
Latest blog entry: The interactive AlienWii manual
Reply With Quote
Old Aug 15, 2014, 12:08 AM
Registered User
Joined Mar 2013
106 Posts
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).
wobbletop is offline Find More Posts by wobbletop
Reply With Quote
Reply


Thread Tools

Similar Threads
Category Thread Thread Starter Forum Replies Last Post
Help! MultiWii PID tuning help? xoxota Multirotor Electronics 11 Nov 30, 2012 05:34 AM
Discussion RushduinoOSD- Now able to EDIT multiwii PID via the OSD!!! JohnyGab Multirotor Electronics 89 Oct 15, 2012 03:17 PM
Question Multiwii 2.1 Tricopter PID tuning vpb Multirotor Electronics 5 Aug 06, 2012 09:22 AM
Discussion MultiWii 2.1 PID Settings via Graupner HoTT FlyinGizm0 Multirotor Talk 0 Jul 27, 2012 06:54 AM