From e6e25525bc1a92150174ac0842938ce6a0282b25 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 8 Aug 2012 13:13:30 +1000 Subject: [PATCH] AP_Param: avoid saving values within 0.01% of the default value this saves some more space due to rounding errors --- libraries/AP_Common/AP_Param.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/libraries/AP_Common/AP_Param.cpp b/libraries/AP_Common/AP_Param.cpp index ef8c61485a..250bf08ad2 100644 --- a/libraries/AP_Common/AP_Param.cpp +++ b/libraries/AP_Common/AP_Param.cpp @@ -626,9 +626,23 @@ bool AP_Param::save(void) } // if the value is the default value then don't save - if (phdr.type <= AP_PARAM_FLOAT && - cast_to_float((enum ap_var_type)phdr.type) == PGM_FLOAT(&info->def_value)) { - return true; + if (phdr.type <= AP_PARAM_FLOAT) { + float v1 = cast_to_float((enum ap_var_type)phdr.type); + float v2; + if (ginfo != NULL) { + v2 = PGM_FLOAT(&ginfo->def_value); + } else { + v2 = PGM_FLOAT(&info->def_value); + } + if (v1 == v2) { + return true; + } + if (phdr.type != AP_PARAM_INT32 && + (fabs(v1-v2) < 0.0001*fabs(v1))) { + // for other than 32 bit integers, we accept values within + // 0.01 percent of the current value as being the same + return true; + } } if (ofs+type_size((enum ap_var_type)phdr.type)+2*sizeof(phdr) >= _eeprom_size) {