update serTcpOpen declaration to fix compile errors (#14113)
[betaflight.git] / src / main / flight / pos_hold.c
blob38e07a84552cf4b1bf40dd0aaae3e321eac895d2
1 /*
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/>.
18 #include "platform.h"
20 #ifdef USE_POSITION_HOLD
22 #include "math.h"
23 #include "build/debug.h"
24 #include "common/maths.h"
26 #include "config/config.h"
27 #include "fc/core.h"
28 #include "fc/runtime_config.h"
29 #include "fc/rc.h"
30 #include "flight/autopilot.h"
31 #include "flight/failsafe.h"
32 #include "flight/imu.h"
33 #include "flight/position.h"
34 #include "rx/rx.h"
35 #include "sensors/compass.h"
37 #include "pg/pos_hold.h"
38 #include "pos_hold.h"
40 typedef struct posHoldState_s {
41 bool isEnabled;
42 bool isControlOk;
43 bool areSensorsOk;
44 float deadband;
45 bool useStickAdjustment;
46 } posHoldState_t;
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);
65 bool sensorsOk(void)
67 if (!STATE(GPS_FIX)) {
68 return false;
70 if (
71 #ifdef USE_MAG
72 !compassIsHealthy() &&
73 #endif
74 (!posHoldConfig()->pos_hold_without_mag || !canUseGPSHeading)) {
75 return false;
77 return true;
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;
88 } else {
89 posHold.isEnabled = false;
92 if (posHold.isEnabled && posHold.isControlOk) {
93 posHold.areSensorsOk = sensorsOk();
94 if (posHold.areSensorsOk) {
95 posHoldCheckSticks();
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