Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

G-Tune implementation and Harakiri pid updates #568

Merged
merged 17 commits into from Oct 6, 2015

Conversation

MJ666
Copy link
Member

@MJ666 MJ666 commented Feb 28, 2015

Implement G-Tune (ported from Harakiri). Documentation can be found here:

https://github.com/MJ666/cleanflight/blob/Harakiri_PID_update/docs/Gtune.md

G-Tune is tested in a quite some test flights and is working much more saver and less intrusive than the CF Autotune. It should work on all PID controllers (PID 0, PID 3 and PID 5 are tested). Please see also here #560.

Make two formerly hardcoded parameter for the Harakiri PID configurable. The first one has helped some users with there tuning problem to eliminate some wobble.

    set pid5_maincuthz = 12     [1-50Hz] Cut Off Frequency for D term of main Pid controller
    set pid5_oldyw = 0          [0/1] 0 = multiwii 2.3 yaw (default), 1 = older yaw

Added an yaw constrain if yaw stick commands are given to PID 3, PID 4 and PID 5 controllers. Recommendation by Crashpilot1000.

if (!time_skip[axis]) AvgGyro[axis] = 0;
time_skip[axis]++;
if (time_skip[axis] > 0) {
if (axis == FD_YAW) AvgGyro[axis] += 32 * ((int16_t)gyroData[axis] / 32); // Chop some jitter and average
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this really work in practice? What if value is near rounding boundary?

It just looks wrong to me ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not touch the algorithms. This is a strait port from Harakiri code. At least the result are flyable P values on my copters. Outside with wind I had sometimes wobble but home in the living room the resulting P values are absolut stable and quite different from the starting ones.

@hydra
Copy link
Contributor

hydra commented Mar 1, 2015

looking forward to giving this a test, thanks @MJ666

time_skip[axis] = 0;
if (axis == FD_YAW) {
threshP = 20;
error = -AvgGyro[axis];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making the value negative has no influence on rest of code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was also wondering. But have not touched it.

@ledvinap
Copy link
Contributor

ledvinap commented Mar 1, 2015

It would be nice to add blockbox logging for GTune. It can help to explain what it does ...

@MJ666
Copy link
Member Author

MJ666 commented Mar 1, 2015

I have not dived into the BlackBox code yet. What i can see in the Autotune code it looks to be quite complex. May be @thenickdude can help here?

@thenickdude
Copy link
Contributor

For Blackbox logging you need to decide on exactly which variables need to be logged and when they need to be logged, define a struct to hold that data in blackbox_fielddefs.h, and write the code that'll serialise those bytes in blackboxLogEvent. Then you call blackboxLogEvent with your populated event structure when one of your new events needs to be recorded.

And the number one thing to keep in mind is that the available bandwidth for events that will be logged on every control loop iteration is very small. The typical main frame logged on every iteration is about 22 bytes long, so if your event frame is 10 bytes long, you've already increased your logging bandwidth requirement during g-tune by about 50%. And the event frame has a 2 byte header as well!

The hardest part is deciding what needs to be logged since this needs a very good handle on how your algorithm works, so that you know exactly what variables capture useful state that you want to inspect. If you don't have a specific debugging goal in mind then logging everything will just be a waste of time.

@ledvinap
Copy link
Contributor

ledvinap commented Mar 2, 2015

GTune is calculated every 16th cycle now, so bandwidth requirements are not that high. AvgGyro[] and two bits for inc/dec p (or new P value) should be enough to see how GTune works ...

@thenickdude
Copy link
Contributor

thenickdude commented Mar 2, 2015 via email

@MJ666
Copy link
Member Author

MJ666 commented Mar 2, 2015

Will take a look at this.

UPDATE: Have added BlackBox recording for G-Tune. Untested since I don't have the related hardware for testing. May be someone can take a look.

@hydra
Copy link
Contributor

hydra commented Mar 2, 2015

@MJ666 are you sure you dont have the hardware to log it? You just need a CC3D or a Full NAZE (with flash) now and you can log to the onboard flash chip.

@MJ666
Copy link
Member Author

MJ666 commented Mar 2, 2015

@hydra you are right I have an WarpQuad with an NAZE rev5. But testing may take a while since I can only go to the flying field at the weekend and the weather is still a limiting factor here. I have posted a Naze HEX in the RCG thread. May be someone can provide feedback more early. I also still have an problem with my WarpQuad which need to be resolved first.

@digitalentity
Copy link
Contributor

@MJ666, @hydra may I have a suggestion: maybe after G-Tune finally makes it to CF we can remove 'Autotune' alltogether to free up flash for other usefull features, i.e. full-blown navigation with waypoints and RTL? I haven't tested G-Tune yet, however ability to automatically tune PIDs during normal flight looks very promising.

@hydra
Copy link
Contributor

hydra commented Mar 3, 2015

Agreed. Bradwii autotune was an interesting experiment that never really worked. The code will remain in the git history should we need it again. However it was never properly debugged either, analysis of autotune flight data logs was next on my list after the serial port cleanup work I'm doing...

@ledvinap
Copy link
Contributor

ledvinap commented Mar 3, 2015

IMO GTune won't work either. The assumptions from accompanying posts are questionable and not reflected in code, the algorithm is IMO just magic juggling of external disturbances. It may have some result for quad with right time constants, but that is mostly coincidence (or it works because some principle that is not expected by the algorithm).

Adding blackbox logs should help understanding GTune ...

@MJ666
Copy link
Member Author

MJ666 commented Mar 3, 2015

BlackBox logging is added and should work (but not tested yet).

@digitalentity
Copy link
Contributor

I don't think that working GTune is a mere coincidence. This method it is somewhat similar to Ziegler–Nichols method of tuning a PID controller. Both use random external disturbances as a destabilization factor, both start with zero P value, both increase P to the edge of oscillation and act on that. There are differences, but the similarity of the two can not be ignored.

@ledvinap
Copy link
Contributor

ledvinap commented Mar 3, 2015

@digitalentity : Ziegler–Nichols method finds ultimate gain (and frequency) and backs off quite a lot.

GTune increases gain if sampled angular rate is increasing fast enough and decreases gain if it is decreasing (with identical but negative threshold). The rate of P decrease is about half. It also decreases gain when rotation direction changes, but only if change occurred at right time in sampling interval(this is probably random) and change is steep enough.

If ultimate frequency is low enough, this should result in P going to infinity. But it may work if oscillation frequency is higher than GTune sampling - sampling will reject P-caused oscillation and resulting accumulated value may stay within limits (no change to P).

But I may be wrong .. blackbox log will be great tool to analyze this ...

@MJ666
Copy link
Member Author

MJ666 commented Mar 3, 2015

From my testing 25-30 flights mostly with PID5 I always get very flyable results. since I always ended up near 7:0 which is the default limit. I raised to to 10 to see what happens. But I still end op with 6.3 to 6.7 with Roll and pitch and around 7.0 with yaw. most of this flights in a room which will not give much disturbance. My outside flights with changing wind conditions causing some light oscillations in between. But usually the final results where always good flyable P values. The individual results have some variation (up to 1.0) and are different between the PID controllers. I never ended up in something unflyable. With PID2 I onl dis some basic testing since this PID is very aggressive with standard settings and the challenge is the get it stabilized in the air to start tuning. Anyhow looks to be @strips had some good success also with PID2 already here #560:

http://www.rcgroups.com/forums/showpost.php?p=30941917&postcount=4078

@ledvinap
Copy link
Contributor

ledvinap commented Mar 3, 2015

It may be interesting to change sampling time (https://github.com/cleanflight/cleanflight/pull/568/files#diff-f948c4e72f6fd563317e98aa58c3e2bcR131). If is is decreased too far, P term will saturate to maximum. Not sure about other extreme, but with high enough value it may tune P to minimum value where quad is controllable (unless crash comes first).

My quad (450 frame) seems to oscillate around 8Hz (guessing from log), so if you have something similar or smaller/lighter, P caused oscillation will be rejected by GTune sampling.

GTune may actually work (as long as some conditions are met), but if my speculation is correct, same result may be obtained in much cleaner and robust way

@MJ666
Copy link
Member Author

MJ666 commented Mar 3, 2015

During my test with PC2 is even tuned P down and hit the lower limits. may be this is related to the aggressive flight behavior which is already hard to control.

@MJ666
Copy link
Member Author

MJ666 commented Mar 3, 2015

Here is an tuning example for my Nano Hexacopter PC5 all other values are CF defaults:

Axis | Start value | G-Tune result
Pitch | 4.0 | 4.3
Roll | 4.0 | 7.7
Yaw | 8,5 | 8.2

The possible range was set from 2.0 to 10.0

@MJ666 MJ666 force-pushed the Harakiri_PID_update branch 5 times, most recently from f20e539 to 5d6924f Compare March 10, 2015 06:27
Fix code style
- add two new CLI paramaters "gtune_settle_time" and
"gtune_average_cycles"
- the settle time is not depending on looptime anymore
- updated default setting to cover e wider range of copters
- remove lower limit for P value for CLI (Zero P is now posible, but
schould be used with care)
- Documentation updates
Flight tested with PID3 and PID5 (still functional without negative side
effects)
Additional code style updates of gtune.c
@smiling-Jack
Copy link

Hello, please can you rebase with 1.10 and share a new Naze build ?

@MJ666
Copy link
Member Author

MJ666 commented Oct 6, 2015

I rebased the code recently. Unfortunately the configuration space for the Naze an the AlienWii F1 target is full and the firmware will not build anymore for this two targets. One temporary solution would be to remove one other feature like GPS to get an G-Tune capable firmware build.

@MJ666 MJ666 closed this Oct 6, 2015
@MJ666 MJ666 reopened this Oct 6, 2015
@smiling-Jack
Copy link

Ah ok, do you have a naze build without gps for me ?
Am 06.10.2015 10:28 schrieb "MJ666" notifications@github.com:

I rebased the code recently. Unfortunately the configuration space for the
Naze an the AlienWii F1 target is full and the firmware will not build
anymore for this two targets. One temporary solution would be to remove one
other feature like GPS to get an G-Tune capable firmware build.


Reply to this email directly or view it on GitHub
#568 (comment)
.

@MJ666
Copy link
Member Author

MJ666 commented Oct 6, 2015

I don't have acces to my development system for the next days. Why not use the latest hex I published here for tuning and use the tuned values with the latest stable afterwards?

@smiling-Jack
Copy link

The old download links are death.

Ok i try my luck to build it on my own.

@hydra
Copy link
Contributor

hydra commented Oct 6, 2015

I'm merging this now.

FYI. I'm only enabling this on 256KB targets due to current requirement for configuration storage. On the 256K targets the last 4KB of EEPROM will be reserved for configuration.

We can look to address this in a future pull request.

@hydra
Copy link
Contributor

hydra commented Oct 6, 2015

gtune also fits on CJMCU.
this is because the CJMCU doesn't use servos or LEDs and thus requires less configuration storage.

@hydra hydra merged commit f15cedd into cleanflight:master Oct 6, 2015
@hydra
Copy link
Contributor

hydra commented Oct 6, 2015

merged

@NullVoxPopuli
Copy link

what pid controllers does this work for? / will it work on betaflight's luxfloat?

@digitalentity
Copy link
Contributor

digitalentity commented Oct 6, 2015 via email

@digitalentity
Copy link
Contributor

Sorry, didn't notice that it has been already done, please disregard my previous comment =)

@MJ666
Copy link
Member Author

MJ666 commented Oct 7, 2015

@hydra cool finaly the PR made it.
@NullVoxPopuli it was tested to work for all PID controllers. Since we have BlackBox and the many new improvements from Betaflight tuning is getting much more easy. There are some changes in scaling of parameters in Betaflight and may be some newer testing would be helpful.

@MJ666 MJ666 deleted the Harakiri_PID_update branch October 15, 2015 16:22
martinbudden added a commit to martinbudden/cleanflight that referenced this pull request Jun 23, 2016
@coveralls
Copy link

coveralls commented Feb 9, 2017

Coverage Status

Changes Unknown when pulling f15cedd on MJ666:Harakiri_PID_update into ** on cleanflight:master**.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet