I built the AC SIm to do gain tuning and now AB testing of various control algorithms. I've made some significant strides in the control loops, so I though I would detail out how it was done.
There is very little code left over from AC 1 in the current code base. When I started AC 2, I used the v1 control loops and assumed they were good. (Everyone was very happy at the time with them.) I also had no real way to measure the performance of them.
A few months ago I started to get very suspicious that the old carryover code was causing a lot of the flight headaches I've been experiencing such as aggressive flight leading to a flip. Or slow, wobbly response in descents. Max was complaining that AC2 lacked "wings". That's not a lot to go on, but it is real feedback that something needed done.
I built the SIM specifically to tackle this type of problem and here is how it was used:
In the SIM a parameter was created called g.test. A checkbox toggles the value of this param and the boolean value is used to turn on or off chunks of code. This gives me a side-by-side comparison of the impact.
Suspect code:
Attitude.pde: get_stabilize_roll() and get_stabilize_pitch();
// limit the error we're feeding to the PID
target_angle = constrain(target_angle, -2500, 2500);
This line limits the desired rates sent to the rate loop. A before and after plot shows the performance gain when this line of code is disabled. The Initial state of the copter was roll left at 450° per second. The plot shows the roll angle going to -85°, then recover to 0. Removing that line improve the recovery time from 1.15s to .75s
Suspect code 2:
static int16_t get_angle_boost(int16_t value)
{float temp = cos_pitch_x * cos_roll_x;
temp = 1.0 - constrain(temp, .5, 1.0);
int16_t output = temp * value;
return constrain(output, 0, 200);
static int16_t get_angle_boost()
{float temp = cos_pitch_x * cos_roll_x;
temp = constrain(temp, .5, 1.0);
return ((float)g.throttle_cruise / temp) - g.throttle_cruise;
This has the effect of maintaining perfect downward acceleration at all times. The reason this works is the throttle-cruise (say 500 out of 1000) is = to 9.81m/s/s of downward acceleration. We can use a linear mapping of throttle to acceleration to achieve the additional throttle necessary for the combined roll and pitch tilt.
Bonus
The last example not is not a patch but a tuning example:
For a basic setup we've been using Rate_P of .14 and Rate_D of 0. Also a Rate dampener similar to the Wii project of .15 Many folks were OK with these gains, but the performance was meh in Max and Marco's experience.
Here is a copter at 45° returning to 0° with the current gains and no patches:
With Patches:
Notice the overshoot and the bell ringing. The frequency with the above with patches is lower and that's good.
Here is the comparison of the above, but with the optimum gains:
These gains are now the default gains:
rate_P of .18
rate_D of .004
stab_D of 0
Please try these gains in the simulator and raise and lower the rate_D term. I was pretty surprised to see how tied the Rate_P and Rate_D are to each other. They cannot be tuned independently. A rate_P of .18 and Rate_D of 0 will not fly well at all. It will fast wobble in the air quite a bit.
I hope this was interesting. The SIM is currently residing at www.jasonshort.com
Jason
Comments
Great stuff
Nice work Jason.
This is just begging for neural network to find the best values for PID variables.
Fuzzy logic is a different control method which translates "human intuition" into approximate feedback gains.
It’s more suited when you lack a (accurate) mathematical model of the plant, but you know how to manual control it.
As for controlling a quad, a classic cascaded PID feedback control architecture is more suited
This is great work and will take out some more "real" testing time and damages to our copters!
are you combining this to the other simulator tool used to find bugs? (forgot the name)
I am playing with it and I really enjoy the scripted loops! hahaha I want my switch CH7 to do that!
Superb work Jason (as usual) thanks a lot. Cant wait to see these fixes in action in Firmware 2.6
Dany
www.CanadaDrones.com
Thanks Jason for your great work!