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/>.
22 #ifdef USE_POSITION_HOLD
25 #include "build/debug.h"
26 #include "common/maths.h"
28 #include "config/config.h"
30 #include "fc/runtime_config.h"
32 #include "flight/autopilot.h"
33 #include "flight/failsafe.h"
34 #include "flight/imu.h"
35 #include "flight/position.h"
37 #include "sensors/compass.h"
39 #include "pg/pos_hold.h"
42 typedef struct posHoldState_s
{
47 bool useStickAdjustment
;
50 static posHoldState_t posHold
;
52 void posHoldInit(void)
54 posHold
.deadband
= posHoldConfig()->pos_hold_deadband
* 0.01f
;
55 posHold
.useStickAdjustment
= posHoldConfig()->pos_hold_deadband
;
58 void posHoldCheckSticks(void)
60 // if failsafe is active, eg landing mode, don't update the original start point
61 if (!failsafeIsActive() && posHold
.useStickAdjustment
) {
62 const bool sticksDeflected
= (getRcDeflectionAbs(FD_ROLL
) > posHold
.deadband
) || (getRcDeflectionAbs(FD_PITCH
) > posHold
.deadband
);
63 setSticksActiveStatus(sticksDeflected
);
69 if (!STATE(GPS_FIX
)) {
74 !compassIsHealthy() &&
76 (!posHoldConfig()->pos_hold_without_mag
|| !canUseGPSHeading
)) {
82 void updatePosHold(timeUs_t currentTimeUs
) {
83 UNUSED(currentTimeUs
);
84 if (FLIGHT_MODE(POS_HOLD_MODE
)) {
85 if (!posHold
.isEnabled
) {
86 resetPositionControl(&gpsSol
.llh
, POSHOLD_TASK_RATE_HZ
); // sets target location to current location
87 posHold
.isControlOk
= true;
88 posHold
.isEnabled
= true;
91 posHold
.isEnabled
= false;
94 if (posHold
.isEnabled
&& posHold
.isControlOk
) {
95 posHold
.areSensorsOk
= sensorsOk();
96 if (posHold
.areSensorsOk
) {
98 posHold
.isControlOk
= positionControl(); // false only on sanity check failure
103 bool posHoldFailure(void) {
104 // used only to display warning in OSD if requested but failing
105 return FLIGHT_MODE(POS_HOLD_MODE
) && (!posHold
.isControlOk
|| !posHold
.areSensorsOk
);
108 #endif // USE_POS_HOLD