Skip to content

Commit

Permalink
Merge pull request #6708 from avsaase/avs-automatic-servo-trim
Browse files Browse the repository at this point in the history
Automatic servo trim
  • Loading branch information
avsaase authored May 8, 2021
2 parents 7d28d9c + 1a6f96c commit bd3cd69
Show file tree
Hide file tree
Showing 13 changed files with 183 additions and 25 deletions.
1 change: 1 addition & 0 deletions docs/Settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@
| serialrx_halfduplex | AUTO | | | Allow serial receiver to operate on UART TX pin. With some receivers will allow control and telemetry over a single wire. |
| serialrx_inverted | OFF | | | Reverse the serial inversion of the serial RX protocol. When this value is OFF, each protocol will use its default signal (e.g. SBUS will use an inverted signal). Some OpenLRS receivers produce a non-inverted SBUS signal. This setting supports this type of receivers (including modified FrSKY). |
| serialrx_provider | _target default_ | | | When feature SERIALRX is enabled, this allows connection to several receivers which output data via digital interface resembling serial. See RX section. |
| servo_autotrim_rotation_limit | 15 | 1 | 60 | Servo midpoints are only updated when total aircraft rotation is less than this threshold [deg/s]. Only applies when using `feature FW_AUTOTRIM`. |
| servo_center_pulse | 1500 | PWM_RANGE_MIN | PWM_RANGE_MAX | Servo midpoint |
| servo_lpf_hz | 20 | 0 | 400 | Selects the servo PWM output cutoff frequency. Value is in [Hz] |
| servo_protocol | PWM | | | An option to chose the protocol/option that would be used to output servo data. Possible options `PWM` (FC servo outputs), `SERVO_DRIVER` (I2C PCA9685 peripheral), `SBUS` (S.Bus protocol output via a configured serial port) |
Expand Down
1 change: 1 addition & 0 deletions src/main/build/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,6 @@ typedef enum {
DEBUG_ALTITUDE,
DEBUG_GYRO_ALPHA_BETA_GAMMA,
DEBUG_SMITH_PREDICTOR,
DEBUG_AUTOTRIM,
DEBUG_COUNT
} debugType_e;
2 changes: 1 addition & 1 deletion src/main/fc/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ static const char * const featureNames[] = {
"", "RSSI_ADC", "LED_STRIP", "DASHBOARD", "",
"BLACKBOX", "", "TRANSPONDER", "AIRMODE",
"SUPEREXPO", "VTX", "", "", "", "PWM_OUTPUT_ENABLE",
"OSD", "FW_LAUNCH", NULL
"OSD", "FW_LAUNCH", "FW_AUTOTRIM", NULL
};

/* Sensor names (used in lookup tables for *_hardware settings and in status command output) */
Expand Down
1 change: 1 addition & 0 deletions src/main/fc/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
FEATURE_PWM_OUTPUT_ENABLE = 1 << 28,
FEATURE_OSD = 1 << 29,
FEATURE_FW_LAUNCH = 1 << 30,
FEATURE_FW_AUTOTRIM = 1 << 31,
} features_e;

typedef struct systemConfig_s {
Expand Down
12 changes: 11 additions & 1 deletion src/main/fc/fc_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ typedef struct emergencyArmingState_s {

timeDelta_t cycleTime = 0; // this is the number in micro second to achieve a full loop, it can differ a little and is taken into account in the PID loop
static timeUs_t flightTime = 0;
static timeUs_t armTime = 0;

EXTENDED_FASTRAM float dT;

Expand Down Expand Up @@ -838,8 +839,12 @@ void taskMainPidLoop(timeUs_t currentTimeUs)

if (ARMING_FLAG(ARMED) && (!STATE(FIXED_WING_LEGACY) || !isNavLaunchEnabled() || (isNavLaunchEnabled() && (isFixedWingLaunchDetected() || isFixedWingLaunchFinishedOrAborted())))) {
flightTime += cycleTime;
armTime += cycleTime;
updateAccExtremes();
}
if (!ARMING_FLAG(ARMED)) {
armTime = 0;
}

gyroFilter();

Expand Down Expand Up @@ -901,7 +906,7 @@ void taskMainPidLoop(timeUs_t currentTimeUs)

if (isMixerUsingServos()) {
servoMixer(dT);
processServoAutotrim();
processServoAutotrim(dT);
}

//Servos should be filtered or written only when mixer is using servos or special feaures are enabled
Expand Down Expand Up @@ -958,6 +963,11 @@ float getFlightTime()
return (float)(flightTime / 1000) / 1000;
}

float getArmTime()
{
return (float)(armTime / 1000) / 1000;
}

void fcReboot(bool bootLoader)
{
// stop motor/servo outputs
Expand Down
1 change: 1 addition & 0 deletions src/main/fc/fc_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ void emergencyArmingUpdate(bool armingSwitchIsOn);

bool isCalibrating(void);
float getFlightTime(void);
float getArmTime(void);

void fcReboot(bool bootLoader);
6 changes: 5 additions & 1 deletion src/main/fc/fc_msp_box.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,11 @@ void initActiveBoxIds(void)
if (!feature(FEATURE_FW_LAUNCH)) {
activeBoxIds[activeBoxIdCount++] = BOXNAVLAUNCH;
}
activeBoxIds[activeBoxIdCount++] = BOXAUTOTRIM;

if (!feature(FEATURE_FW_AUTOTRIM)) {
activeBoxIds[activeBoxIdCount++] = BOXAUTOTRIM;
}

#if defined(USE_AUTOTUNE_FIXED_WING)
activeBoxIds[activeBoxIdCount++] = BOXAUTOTUNE;
#endif
Expand Down
7 changes: 6 additions & 1 deletion src/main/fc/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ tables:
"VIBE", "CRUISE", "REM_FLIGHT_TIME", "SMARTAUDIO", "ACC",
"ERPM", "RPM_FILTER", "RPM_FREQ", "NAV_YAW", "DYNAMIC_FILTER", "DYNAMIC_FILTER_FREQUENCY",
"IRLOCK", "CD", "KALMAN_GAIN", "PID_MEASUREMENT", "SPM_CELLS", "SPM_VS600", "SPM_VARIO", "PCF8574", "DYN_GYRO_LPF", "FW_D", "IMU2", "ALTITUDE",
"GYRO_ALPHA_BETA_GAMMA", "SMITH_PREDICTOR"]
"GYRO_ALPHA_BETA_GAMMA", "SMITH_PREDICTOR", "AUTOTRIM"]
- name: async_mode
values: ["NONE", "GYRO", "ALL"]
- name: aux_operator
Expand Down Expand Up @@ -1215,6 +1215,11 @@ groups:
description: "On tricopter mix only, if this is set to ON, servo will always be correcting regardless of armed state. to disable this, set it to OFF."
default_value: ON
type: bool
- name: servo_autotrim_rotation_limit
description: "Servo midpoints are only updated when total aircraft rotation is less than this threshold [deg/s]. Only applies when using `feature FW_AUTOTRIM`."
default_value: 15
min: 1
max: 60

- name: PG_CONTROL_RATE_PROFILES
type: controlRateConfig_t
Expand Down
4 changes: 3 additions & 1 deletion src/main/flight/mixer.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ void mixerUpdateStateFlags(void);
void mixerResetDisarmedMotors(void);
void mixTable(const float dT);
void writeMotors(void);
void processServoAutotrim(void);
void processServoAutotrim(const float dT);
void processServoAutotrimMode(void);
void processContinuousServoAutotrim(const float dT);
void stopMotors(void);
void stopPwmAllMotors(void);

Expand Down
16 changes: 16 additions & 0 deletions src/main/flight/pid.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,22 @@ void pidResetErrorAccumulators(void)
}
}

void pidReduceErrorAccumulators(int8_t delta, uint8_t axis)
{
pidState[axis].errorGyroIf -= delta;
pidState[axis].errorGyroIfLimit -= delta;
}

float getTotalRateTarget(void)
{
return sqrtf(sq(pidState[FD_ROLL].rateTarget) + sq(pidState[FD_PITCH].rateTarget) + sq(pidState[FD_YAW].rateTarget));
}

float getAxisIterm(uint8_t axis)
{
return pidState[axis].errorGyroIf;
}

static float pidRcCommandToAngle(int16_t stick, int16_t maxInclination)
{
stick = constrain(stick, -500, 500);
Expand Down
3 changes: 3 additions & 0 deletions src/main/flight/pid.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ extern int32_t axisPID_P[], axisPID_I[], axisPID_D[], axisPID_Setpoint[];
void pidInit(void);
bool pidInitFilters(void);
void pidResetErrorAccumulators(void);
void pidReduceErrorAccumulators(int8_t delta, uint8_t axis);
float getAxisIterm(uint8_t axis);
float getTotalRateTarget(void);
void pidResetTPAFilter(void);

struct controlRateConfig_s;
Expand Down
Loading

0 comments on commit bd3cd69

Please sign in to comment.