From b4c5f31b1703afa4de6e9c4772aa32a3b7536e8f Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 17 Apr 2014 17:20:40 +1000 Subject: [PATCH] APM_Control: reduce the number of parameter saves in autotune don't save a parameter unless it has changed by 0.1% --- libraries/APM_Control/AP_AutoTune.cpp | 21 +++++++++++++++++---- libraries/APM_Control/AP_AutoTune.h | 2 ++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/libraries/APM_Control/AP_AutoTune.cpp b/libraries/APM_Control/AP_AutoTune.cpp index db5a4be835..67224791b4 100644 --- a/libraries/APM_Control/AP_AutoTune.cpp +++ b/libraries/APM_Control/AP_AutoTune.cpp @@ -260,16 +260,29 @@ void AP_AutoTune::check_save(void) last_save_ms = hal.scheduler->millis(); } +/* + set a float and save a float if it has changed by more than + 0.1%. This reduces the number of insignificant EEPROM writes + */ +void AP_AutoTune::save_float_if_changed(AP_Float &v, float value) +{ + float old_value = v.get(); + v.set(value); + if (value <= 0 || fabsf((value-old_value)/value) > 0.001f) { + v.save(); + } +} + /* save a set of gains */ void AP_AutoTune::save_gains(const ATGains &v) { current = last_save; - current.tau.set_and_save_ifchanged(v.tau); - current.P.set_and_save_ifchanged(v.P); - current.I.set_and_save_ifchanged(v.I); - current.D.set_and_save_ifchanged(v.D); + save_float_if_changed(current.tau, v.tau); + save_float_if_changed(current.P, v.P); + save_float_if_changed(current.I, v.I); + save_float_if_changed(current.D, v.D); current.rmax.set_and_save_ifchanged(v.rmax); current.imax.set_and_save_ifchanged(v.imax); last_save = current; diff --git a/libraries/APM_Control/AP_AutoTune.h b/libraries/APM_Control/AP_AutoTune.h index a977fcb143..94eaad6d5c 100644 --- a/libraries/APM_Control/AP_AutoTune.h +++ b/libraries/APM_Control/AP_AutoTune.h @@ -85,6 +85,8 @@ private: void write_log_headers(void); void write_log(float servo, float demanded, float achieved); + + void save_float_if_changed(AP_Float &v, float value); }; #endif // __AP_AUTOTUNE_H__