2 * This file is part of Betaflight.
4 * Betaflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Betaflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Betaflight. If not, see <http://www.gnu.org/licenses/>.
20 #ifdef USE_POSITION_HOLD
23 #include "build/debug.h"
24 #include "common/maths.h"
26 #include "config/config.h"
28 #include "fc/runtime_config.h"
30 #include "flight/autopilot.h"
31 #include "flight/failsafe.h"
32 #include "flight/imu.h"
33 #include "flight/position.h"
35 #include "sensors/compass.h"
37 #include "pg/pos_hold.h"
40 typedef struct posHoldState_s
{
45 bool useStickAdjustment
;
48 static posHoldState_t posHold
;
50 void posHoldInit(void)
52 posHold
.deadband
= posHoldConfig()->pos_hold_deadband
* 0.01f
;
53 posHold
.useStickAdjustment
= posHoldConfig()->pos_hold_deadband
;
56 void posHoldCheckSticks(void)
58 // if failsafe is active, eg landing mode, don't update the original start point
59 if (!failsafeIsActive() && posHold
.useStickAdjustment
) {
60 const bool sticksDeflected
= (getRcDeflectionAbs(FD_ROLL
) > posHold
.deadband
) || (getRcDeflectionAbs(FD_PITCH
) > posHold
.deadband
);
61 setSticksActiveStatus(sticksDeflected
);
67 if (!STATE(GPS_FIX
)) {
72 !compassIsHealthy() &&
74 (!posHoldConfig()->pos_hold_without_mag
|| !canUseGPSHeading
)) {
80 void updatePosHold(timeUs_t currentTimeUs
) {
81 UNUSED(currentTimeUs
);
82 if (FLIGHT_MODE(POS_HOLD_MODE
)) {
83 if (!posHold
.isEnabled
) {
84 resetPositionControl(&gpsSol
.llh
, POSHOLD_TASK_RATE_HZ
); // sets target location to current location
85 posHold
.isControlOk
= true;
86 posHold
.isEnabled
= true;
89 posHold
.isEnabled
= false;
92 if (posHold
.isEnabled
&& posHold
.isControlOk
) {
93 posHold
.areSensorsOk
= sensorsOk();
94 if (posHold
.areSensorsOk
) {
96 posHold
.isControlOk
= positionControl(); // false only on sanity check failure
101 bool posHoldFailure(void) {
102 // used only to display warning in OSD if requested but failing
103 return FLIGHT_MODE(POS_HOLD_MODE
) && (!posHold
.isControlOk
|| !posHold
.areSensorsOk
);
106 #endif // USE_POS_HOLD