SaveTrim is additive, not absolute

Hello,

The autotrim page explains how to trim the APM:
copter.ardupilot.com/wiki/autotrim/

However, it seems that the trim parameter is additive - Meaning that saving the trim twice will add double the trim you wanted to add. I guess that makes sense if you understand the AHRS_TRIM parameter, but it could cause some trouble.

Is it possible to add the following notice right after no. 5?
“NOTICE: SaveTrim is additive - Meaning that saving the trim twice will add double the trim you wanted to add. Be careful not to save the trim twice!”

Thanks
Nitay

I do not know what makes you think what you write, but here is the actual code:

// save_trim - adds roll and pitch trims from the radio to ahrs
static void save_trim()
{
// save roll and pitch trim
float roll_trim = ToRad((float)g.rc_1.control_in/100.0f);
float pitch_trim = ToRad((float)g.rc_2.control_in/100.0f);
ahrs.add_trim(roll_trim, pitch_trim);
Log_Write_Event(DATA_SAVE_TRIM);
gcs_send_text_P(SEVERITY_HIGH, PSTR(“Trim saved”));
}

// auto_trim - slightly adjusts the ahrs.roll_trim and ahrs.pitch_trim towards the current stick positions
// meant to be called continuously while the pilot attempts to keep the copter level
static void auto_trim()
{
if(auto_trim_counter > 0) {
auto_trim_counter–;

    // flash the leds
    AP_Notify::flags.save_trim = true;

    // calculate roll trim adjustment
    float roll_trim_adjustment = ToRad((float)g.rc_1.control_in / 4000.0f);

    // calculate pitch trim adjustment
    float pitch_trim_adjustment = ToRad((float)g.rc_2.control_in / 4000.0f);

    // make sure accelerometer values impact attitude quickly
    ahrs.set_fast_gains(true);

    // add trim to ahrs object
    // save to eeprom on last iteration
    ahrs.add_trim(roll_trim_adjustment, pitch_trim_adjustment, (auto_trim_counter == 0));

    // on last iteration restore leds and accel gains to normal
    if(auto_trim_counter == 0) {
        ahrs.set_fast_gains(false);
        AP_Notify::flags.save_trim = false;
    }
}

}
// add_trim - adjust the roll and pitch trim up to a total of 10 degrees
void AP_AHRS::add_trim(float roll_in_radians, float pitch_in_radians, bool save_to_eeprom)
{
Vector3f trim = _trim.get();

// add new trim
trim.x = constrain_float(trim.x + roll_in_radians, ToRad(-AP_AHRS_TRIM_LIMIT), ToRad(AP_AHRS_TRIM_LIMIT));
trim.y = constrain_float(trim.y + pitch_in_radians, ToRad(-AP_AHRS_TRIM_LIMIT), ToRad(AP_AHRS_TRIM_LIMIT));

// set new trim values
_trim.set(trim);

// save to eeprom
if( save_to_eeprom ) {
    _trim.save();
}

Hi,

The line:

Adds the new trim to the current trim. Meaning that if you save the trim twice, you add twice the value to each channel.

Also, here’s the relevant line from ahrs.add_trim

void AP_AHRS::add_trim(float roll_in_radians, float pitch_in_radians, bool save_to_eeprom)
{
    Vector3f trim = _trim.get();

    // add new trim
    trim.x = constrain_float(trim.x + roll_in_radians, ToRad(-AP_AHRS_TRIM_LIMIT), ToRad(AP_AHRS_TRIM_LIMIT));
    trim.y = constrain_float(trim.y + pitch_in_radians, ToRad(-AP_AHRS_TRIM_LIMIT), ToRad(AP_AHRS_TRIM_LIMIT));

...

So if I understoof the code correctly - The trim seems to be adding up.

Thanks
Nitay