Hello all,

First, **THIS IS EXPERIMENTAL FOR SIMULATION ONLY.** Video coming shortly of me using it in the simulator.

I have a functional first attempt at automatic P gain tuning. I'm calling it **Pretty Good Gains** (pun intended). There is definitely room for improvement, but here is how it works:

PID controllers usually end up with an output graph that looks something like this (from wikipedia):

As you can see by the above graph the most well tuned P gain value in this controller is 2. Why? Because it arrives at its target value the fastest with the least amount of overshoot and oscillation. One can see this in practice when flying a multi-copter and the PIDs are too high producing an oscillation. I have read a lot of different PID tuning strategies looking for something that I could replicate in code. I came across something called the Good Gain Method. Essentially this method of tuning requires the user to set the Integral and Derivative gains to zero and first focus on the P gain. Adjust the P gain until the least amount of oscillation is seen with an acceptable arrival time at the target value. If the P value is too low, the curve will take too long to get to the target and if the P value is too high you will see the more violent oscillations.

How do we do this in code? Well, first I require the user to input a disturbance into the system (ie. pitch right) and then wait for the user's input to return to zero (center stick). Now we have our starting point (the disturbance) and our target (zero pitch, or stable hover). As the copter attempts to return to a stable hover the error values follow a path similar to the curve above. As this is happening I measure the distance between the first peak and the first valley of that curve and how long it took to produce that peak and valley. Now we have our starting point. When another disturbance is introduced we can compare its peak/valley to the previous oscillation and decide which direction to move the P gain. Once that is done enough times, the P value is tuned.

Right now I move the P gain value in increments of 10%, but I can see changing that to adapt to the degree of the oscillation with increasing granularity as the P gain improves. I have tested this in the HIL simulator and it seems to work well. It works best if you set the P values a bit too high and let the auto-tune lower it.

I am attaching a zip of the files I modified. Please give me feedback and discuss my method. Again, **Pretty Good Gains** means that this isn't perfect. It also doesn't address the Integral gain... but I have plans for that too.

## Comments

Just finished some improvements that will lower the number of iterations required to get a pretty good gain value.

1. When the new gain value degrades the performance, the next iteration uses the best gain that has been found as a starting point instead of the previous gain. Should save 1 iteration.

2. I scale the amount of change applied to the gain over time. Should speed up the initial ball park gain and improve resolution with more iterations.

3. The gain shift value, which is how much we shift the gain per iteration, will reset if the auto tuning mode is toggled off and then on again.

I will test this in the simulator tonight and if it looks good I will tackle the I term.

Doug: Yes. Sorry for the graph I used. The graph actually contradicts my comments about the P value. I do understand that a higher P value will produce more violent oscillations. Fortunately, the code does not care about any assumptions related to the P value because it is essentially a feedback loop. If raising the P value lowers the oscillation it will continue to raise the P value as for some reason it must be helping. Will this actually happen? No... but my point is that it doesn't really matter in this case.

I didn't really mean for this to be an adaptive controller system. The risks involved with having this autotuning mode on all the time are far too high for exactly the reasons you listed above. I also agree with you that this is best suited for a controlled environment (indoors no wind) because this system does not account for the potential noise. I will ponder that after I have this working well in a controlled environment.

Thanks for the suggestion on the I and D gain calculations. I did stumble across some of that information while reading up on the good gain method. I think those methods are good for achieving a starting point, but obviously its just an estimation. I hope to be able to apply this feedback loop approach to the I term as well. I think the most ideal system for auto tuning would be an offline method that uses a set of harvested data from a flight, but I don't know enough about PID control theory to implement that.

Hello Adam,

A few comments:

The form of the response of a system with PID feedback control is highly dependent on the system dynamics. I would have to say based on my experience with a variety of mechanical systems using PID control, Wikipedia could lead you a bit astray with their example plot. I would consider that a pretty atypical result in that with constant I and D gain they are suggesting that you will see less overshoot with higher P gain. This is true with the gains they have chosen (and some example system), but generally you will see I gains that are on the order of a magnitude less than the P gain, and you will see the opposite relation - that higher P gain typically produces more overshoot. Their opposite relation is due to an (generally unrealistic) choice of I gain.

Also, you are describing an in-flight adaptive controller. There is a large body of knowledge on the subject. Generally it is acknowledged that it is a hard thing to do well, and there is risk involved. One poor iterative choice on a gain can pretty swiftly result in a crash. Use of such a technique is best restricted to controlled conditions - EG don't leave it on all the time, but just use it when conditions are very benign (i.e. no wind).

Finally, there are several (fairly old) pieces of literature on direct calculation of I and D gains from the P gain and oscillatory frequency. You might want to check those out.

Video as promised: https://www.youtube.com/watch?v=vXO0-9T1b_A

Sounds promising Adam. I will be reading through your changes. Good luck with the implementation on the I term- this will go a long way to improving the "universal autopilot"!