AT32F435 SD card support (#14018)
[betaflight.git] / src / main / flight / pos_hold_multirotor.c
bloba3723885e4d895456d5e51decab367c77de14c90
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 #ifndef USE_WING
22 #ifdef USE_POSITION_HOLD
24 #include "math.h"
25 #include "build/debug.h"
26 #include "common/maths.h"
28 #include "config/config.h"
29 #include "fc/core.h"
30 #include "fc/runtime_config.h"
31 #include "fc/rc.h"
32 #include "flight/autopilot.h"
33 #include "flight/failsafe.h"
34 #include "flight/imu.h"
35 #include "flight/position.h"
36 #include "rx/rx.h"
37 #include "sensors/compass.h"
39 #include "pg/pos_hold.h"
40 #include "pos_hold.h"
42 typedef struct posHoldState_s {
43 bool isEnabled;
44 bool isControlOk;
45 bool areSensorsOk;
46 float deadband;
47 bool useStickAdjustment;
48 } posHoldState_t;
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);
67 bool sensorsOk(void)
69 if (!STATE(GPS_FIX)) {
70 return false;
72 if (
73 #ifdef USE_MAG
74 !compassIsHealthy() &&
75 #endif
76 (!posHoldConfig()->pos_hold_without_mag || !canUseGPSHeading)) {
77 return false;
79 return true;
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;
90 } else {
91 posHold.isEnabled = false;
94 if (posHold.isEnabled && posHold.isControlOk) {
95 posHold.areSensorsOk = sensorsOk();
96 if (posHold.areSensorsOk) {
97 posHoldCheckSticks();
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
110 #endif // !USE_WING