Skip to content

Commit

Permalink
Merge pull request cleanflight#726 from iNavFlight/reset-maghold-on-arm
Browse files Browse the repository at this point in the history
MagHold controller reset on arm and MAGHOLD enable
  • Loading branch information
DzikuVx committed Nov 1, 2016
2 parents 6d02467 + 1b8c11d commit 14dc698
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/main/fc/mw.c
Expand Up @@ -230,6 +230,8 @@ void mwArm(void)
ENABLE_ARMING_FLAG(WAS_EVER_ARMED);
headFreeModeHold = DECIDEGREES_TO_DEGREES(attitude.values.yaw);

resetMagHoldHeading(DECIDEGREES_TO_DEGREES(attitude.values.yaw));

#ifdef BLACKBOX
if (feature(FEATURE_BLACKBOX)) {
serialPort_t *sharedBlackboxAndMspPort = findSharedSerialPort(FUNCTION_BLACKBOX, FUNCTION_MSP);
Expand Down Expand Up @@ -399,8 +401,8 @@ void processRx(void)
if (sensors(SENSOR_ACC) || sensors(SENSOR_MAG)) {
if (IS_RC_MODE_ACTIVE(BOXMAG)) {
if (!FLIGHT_MODE(MAG_MODE)) {
resetMagHoldHeading(DECIDEGREES_TO_DEGREES(attitude.values.yaw));
ENABLE_FLIGHT_MODE(MAG_MODE);
updateMagHoldHeading(DECIDEGREES_TO_DEGREES(attitude.values.yaw));
}
} else {
DISABLE_FLIGHT_MODE(MAG_MODE);
Expand Down Expand Up @@ -656,4 +658,3 @@ void taskUpdateRxMain(void)
updatePIDCoefficients(&currentProfile->pidProfile, currentControlRateProfile, &masterConfig.rxConfig);
isRXDataNew = true;
}

9 changes: 7 additions & 2 deletions src/main/flight/pid.c
Expand Up @@ -88,6 +88,7 @@ extern bool motorLimitReached;
extern float dT;

int16_t magHoldTargetHeading;
static pt1Filter_t magHoldRateFilter;

// Thrust PID Attenuation factor. 0.0f means fully attenuated, 1.0f no attenuation is applied
static float tpaFactor;
Expand Down Expand Up @@ -354,6 +355,12 @@ void updateMagHoldHeading(int16_t heading)
magHoldTargetHeading = heading;
}

void resetMagHoldHeading(int16_t heading)
{
updateMagHoldHeading(heading);
pt1FilterReset(&magHoldRateFilter, 0.0f);
}

int16_t getMagHoldHeading() {
return magHoldTargetHeading;
}
Expand Down Expand Up @@ -394,8 +401,6 @@ uint8_t getMagHoldState()
*/
float pidMagHold(const pidProfile_t *pidProfile)
{

static pt1Filter_t magHoldRateFilter;
float magHoldRate;

int16_t error = DECIDEGREES_TO_DEGREES(attitude.values.yaw) - magHoldTargetHeading;
Expand Down
1 change: 1 addition & 0 deletions src/main/flight/pid.h
Expand Up @@ -95,4 +95,5 @@ enum {
};

void updateMagHoldHeading(int16_t heading);
void resetMagHoldHeading(int16_t heading);
int16_t getMagHoldHeading();

0 comments on commit 14dc698

Please sign in to comment.