2 * This file is part of INAV Project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
6 * You can obtain one at http://mozilla.org/MPL/2.0/.
8 * Alternatively, the contents of this file may be used under the terms
9 * of the GNU General Public License Version 3, as described below:
11 * This file is free software: you may copy, redistribute and/or modify
12 * it under the terms of the GNU General Public License as published by the
13 * Free Software Foundation, either version 3 of the License, or (at your
14 * option) any later version.
16 * This file is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19 * Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see http://www.gnu.org/licenses/.
31 #include "build/debug.h"
32 #include "common/utils.h"
33 #include "fc/rc_controls.h"
34 #include "fc/config.h"
35 #include "flight/mixer.h"
36 #include "navigation/navigation.h"
37 #include "navigation/navigation_private.h"
39 static bool isYawAdjustmentValid
= false;
40 static int32_t navHeadingError
;
42 static void update2DPositionHeadingController(timeUs_t currentTimeUs
, timeDelta_t deltaMicros
)
44 static timeUs_t previousTimeMonitoringUpdate
;
45 static int32_t previousHeadingError
;
46 static bool errorIsDecreasing
;
48 int32_t targetBearing
= calculateBearingToDestination(&posControl
.desiredState
.pos
);
51 * Calculate NAV heading error
52 * Units are centidegrees
54 navHeadingError
= wrap_18000(targetBearing
- posControl
.actualState
.yaw
);
56 // Slow error monitoring (2Hz rate)
57 if ((currentTimeUs
- previousTimeMonitoringUpdate
) >= HZ2US(NAV_FW_CONTROL_MONITORING_RATE
)) {
58 // Check if error is decreasing over time
59 errorIsDecreasing
= (ABS(previousHeadingError
) > ABS(navHeadingError
));
61 // Save values for next iteration
62 previousHeadingError
= navHeadingError
;
63 previousTimeMonitoringUpdate
= currentTimeUs
;
66 posControl
.rcAdjustment
[YAW
] = processHeadingYawController(deltaMicros
, navHeadingError
, errorIsDecreasing
);
69 void applyRoverBoatPositionController(timeUs_t currentTimeUs
)
71 static timeUs_t previousTimePositionUpdate
; // Occurs @ GPS update rate
72 static timeUs_t previousTimeUpdate
; // Occurs @ looptime rate
74 const timeDelta_t deltaMicros
= currentTimeUs
- previousTimeUpdate
;
75 previousTimeUpdate
= currentTimeUs
;
77 // If last position update was too long in the past - ignore it (likely restarting altitude controller)
78 if (deltaMicros
> HZ2US(MIN_POSITION_UPDATE_RATE_HZ
)) {
79 previousTimeUpdate
= currentTimeUs
;
80 previousTimePositionUpdate
= currentTimeUs
;
81 resetFixedWingPositionController();
85 // Apply controller only if position source is valid. In absence of valid pos sensor (GPS loss), we'd stick in forced ANGLE mode
86 if ((posControl
.flags
.estPosStatus
>= EST_USABLE
)) {
87 // If we have new position - update velocity and acceleration controllers
88 if (posControl
.flags
.horizontalPositionDataNew
) {
89 const timeDelta_t deltaMicrosPositionUpdate
= currentTimeUs
- previousTimePositionUpdate
;
90 previousTimePositionUpdate
= currentTimeUs
;
92 if (deltaMicrosPositionUpdate
< HZ2US(MIN_POSITION_UPDATE_RATE_HZ
)) {
93 update2DPositionHeadingController(currentTimeUs
, deltaMicrosPositionUpdate
);
95 resetFixedWingPositionController();
98 // Indicate that information is no longer usable
99 posControl
.flags
.horizontalPositionDataConsumed
= 1;
102 isYawAdjustmentValid
= true;
105 isYawAdjustmentValid
= false;
109 void applyRoverBoatPitchRollThrottleController(navigationFSMStateFlags_t navStateFlags
, timeUs_t currentTimeUs
)
111 UNUSED(currentTimeUs
);
113 rcCommand
[PITCH
] = 0;
115 if (navStateFlags
& NAV_CTL_POS
) {
117 if (navStateFlags
& NAV_AUTO_WP_DONE
) {
119 * When WP mission is done, stop the motors
122 rcCommand
[THROTTLE
] = feature(FEATURE_REVERSIBLE_MOTORS
) ? reversibleMotorsConfig()->neutral
: motorConfig()->mincommand
;
124 if (isYawAdjustmentValid
) {
125 rcCommand
[YAW
] = posControl
.rcAdjustment
[YAW
];
128 rcCommand
[THROTTLE
] = constrain(navConfig()->fw
.cruise_throttle
, motorConfig()->mincommand
, motorConfig()->maxthrottle
);
133 void applyRoverBoatNavigationController(navigationFSMStateFlags_t navStateFlags
, timeUs_t currentTimeUs
)
135 if (navStateFlags
& NAV_CTL_EMERG
) {
137 rcCommand
[PITCH
] = 0;
139 rcCommand
[THROTTLE
] = failsafeConfig()->failsafe_throttle
;
140 } else if (navStateFlags
& NAV_CTL_POS
) {
141 applyRoverBoatPositionController(currentTimeUs
);
142 applyRoverBoatPitchRollThrottleController(navStateFlags
, currentTimeUs
);