Skip to content

Commit 3802269

Browse files
authored
Merge pull request #9429 from JulioCesarMatias/TrackBackToHeader
[RTH TrackBack] Create .c and .h files for this feature
2 parents 38f5d4c + 318d298 commit 3802269

File tree

9 files changed

+251
-169
lines changed

9 files changed

+251
-169
lines changed

.vscode/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
"chrono": "c",
44
"cmath": "c",
55
"ranges": "c",
6+
"navigation.h": "c",
7+
"rth_trackback.h": "c"
68
"platform.h": "c",
79
"timer.h": "c",
810
"bus.h": "c"

src/main/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,8 @@ main_sources(COMMON_SRC
558558
navigation/navigation_rover_boat.c
559559
navigation/sqrt_controller.c
560560
navigation/sqrt_controller.h
561+
navigation/rth_trackback.c
562+
navigation/rth_trackback.h
561563

562564
sensors/barometer.c
563565
sensors/barometer.h

src/main/common/maths.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#define RAD (M_PIf / 180.0f)
3737

3838
#define DEGREES_TO_CENTIDEGREES(angle) ((angle) * 100)
39-
#define CENTIDEGREES_TO_DEGREES(angle) ((angle) / 100.0f)
39+
#define CENTIDEGREES_TO_DEGREES(angle) ((angle) * 0.01f)
4040

4141
#define CENTIDEGREES_TO_DECIDEGREES(angle) ((angle) / 10.0f)
4242
#define DECIDEGREES_TO_CENTIDEGREES(angle) ((angle) * 10)
@@ -54,11 +54,11 @@
5454
#define RADIANS_TO_DECIDEGREES(angle) (((angle) * 10.0f) / RAD)
5555

5656
#define RADIANS_TO_CENTIDEGREES(angle) (((angle) * 100.0f) / RAD)
57-
#define CENTIDEGREES_TO_RADIANS(angle) (((angle) / 100.0f) * RAD)
57+
#define CENTIDEGREES_TO_RADIANS(angle) (((angle) * 0.01f) * RAD)
5858

5959
#define CENTIMETERS_TO_CENTIFEET(cm) (cm / 0.3048f)
6060
#define CENTIMETERS_TO_FEET(cm) (cm / 30.48f)
61-
#define CENTIMETERS_TO_METERS(cm) (cm / 100.0f)
61+
#define CENTIMETERS_TO_METERS(cm) (cm * 0.01f)
6262

6363
#define METERS_TO_CENTIMETERS(m) (m * 100)
6464

src/main/fc/multifunction.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
#pragma once
2626

27+
#include <stdbool.h>
28+
2729
#ifdef USE_MULTI_FUNCTIONS
2830

2931
extern uint8_t multiFunctionFlags;

src/main/navigation/navigation.c

Lines changed: 14 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555

5656
#include "navigation/navigation.h"
5757
#include "navigation/navigation_private.h"
58+
#include "navigation/rth_trackback.h"
5859

5960
#include "rx/rx.h"
6061

@@ -258,10 +259,8 @@ static void resetJumpCounter(void);
258259
static void clearJumpCounters(void);
259260

260261
static void calculateAndSetActiveWaypoint(const navWaypoint_t * waypoint);
261-
static void calculateAndSetActiveWaypointToLocalPosition(const fpVector3_t * pos);
262262
void calculateInitialHoldPosition(fpVector3_t * pos);
263263
void calculateFarAwayTarget(fpVector3_t * farAwayPos, int32_t bearing, int32_t distance);
264-
static bool isWaypointReached(const fpVector3_t * waypointPos, const int32_t * waypointBearing);
265264
bool isWaypointAltitudeReached(void);
266265
static void mapWaypointToLocalPosition(fpVector3_t * localPos, const navWaypoint_t * waypoint, geoAltitudeConversionMode_e altConv);
267266
static navigationFSMEvent_t nextForNonGeoStates(void);
@@ -273,11 +272,6 @@ bool validateRTHSanityChecker(void);
273272
void updateHomePosition(void);
274273
bool abortLaunchAllowed(void);
275274

276-
static bool rthAltControlStickOverrideCheck(unsigned axis);
277-
278-
static void updateRthTrackback(bool forceSaveTrackPoint);
279-
static fpVector3_t * rthGetTrackbackPos(void);
280-
281275
/*************************************************************************************************/
282276
static navigationFSMEvent_t navOnEnteringState_NAV_STATE_IDLE(navigationFSMState_t previousState);
283277
static navigationFSMEvent_t navOnEnteringState_NAV_STATE_ALTHOLD_INITIALIZE(navigationFSMState_t previousState);
@@ -1257,16 +1251,11 @@ static navigationFSMEvent_t navOnEnteringState_NAV_STATE_RTH_INITIALIZE(navigati
12571251
setDesiredPosition(&navGetCurrentActualPositionAndVelocity()->pos, posControl.actualState.yaw, NAV_POS_UPDATE_XY | NAV_POS_UPDATE_Z | NAV_POS_UPDATE_HEADING);
12581252

12591253
return NAV_FSM_EVENT_SWITCH_TO_RTH_LANDING; // NAV_STATE_RTH_HOVER_PRIOR_TO_LANDING
1260-
}
1261-
else {
1262-
// Switch to RTH trackback
1263-
bool trackbackActive = navConfig()->general.flags.rth_trackback_mode == RTH_TRACKBACK_ON ||
1264-
(navConfig()->general.flags.rth_trackback_mode == RTH_TRACKBACK_FS && posControl.flags.forcedRTHActivated);
1265-
1266-
if (trackbackActive && posControl.activeRthTBPointIndex >= 0 && !isWaypointMissionRTHActive()) {
1267-
updateRthTrackback(true); // save final trackpoint for altitude and max trackback distance reference
1254+
} else {
1255+
if (rthTrackBackIsActive() && rth_trackback.activePointIndex >= 0 && !isWaypointMissionRTHActive()) {
1256+
rthTrackBackUpdate(true); // save final trackpoint for altitude and max trackback distance reference
12681257
posControl.flags.rthTrackbackActive = true;
1269-
calculateAndSetActiveWaypointToLocalPosition(rthGetTrackbackPos());
1258+
calculateAndSetActiveWaypointToLocalPosition(getRthTrackBackPosition());
12701259
return NAV_FSM_EVENT_SWITCH_TO_NAV_STATE_RTH_TRACKBACK;
12711260
}
12721261

@@ -1381,36 +1370,8 @@ static navigationFSMEvent_t navOnEnteringState_NAV_STATE_RTH_TRACKBACK(navigatio
13811370
return NAV_FSM_EVENT_SWITCH_TO_EMERGENCY_LANDING;
13821371
}
13831372

1384-
if (posControl.flags.estPosStatus >= EST_USABLE) {
1385-
const int32_t distFromStartTrackback = calculateDistanceToDestination(&posControl.rthTBPointsList[posControl.rthTBLastSavedIndex]) / 100;
1386-
#ifdef USE_MULTI_FUNCTIONS
1387-
const bool overrideTrackback = rthAltControlStickOverrideCheck(ROLL) || MULTI_FUNC_FLAG(MF_SUSPEND_TRACKBACK);
1388-
#else
1389-
const bool overrideTrackback = rthAltControlStickOverrideCheck(ROLL);
1390-
#endif
1391-
const bool cancelTrackback = distFromStartTrackback > navConfig()->general.rth_trackback_distance ||
1392-
(overrideTrackback && !posControl.flags.forcedRTHActivated);
1393-
1394-
if (posControl.activeRthTBPointIndex < 0 || cancelTrackback) {
1395-
posControl.rthTBWrapAroundCounter = posControl.activeRthTBPointIndex = -1;
1396-
posControl.flags.rthTrackbackActive = false;
1397-
return NAV_FSM_EVENT_SWITCH_TO_NAV_STATE_RTH_INITIALIZE; // procede to home after final trackback point
1398-
}
1399-
1400-
if (isWaypointReached(&posControl.activeWaypoint.pos, &posControl.activeWaypoint.bearing)) {
1401-
posControl.activeRthTBPointIndex--;
1402-
1403-
if (posControl.rthTBWrapAroundCounter > -1 && posControl.activeRthTBPointIndex < 0) {
1404-
posControl.activeRthTBPointIndex = NAV_RTH_TRACKBACK_POINTS - 1;
1405-
}
1406-
calculateAndSetActiveWaypointToLocalPosition(rthGetTrackbackPos());
1407-
1408-
if (posControl.activeRthTBPointIndex - posControl.rthTBWrapAroundCounter == 0) {
1409-
posControl.rthTBWrapAroundCounter = posControl.activeRthTBPointIndex = -1;
1410-
}
1411-
} else {
1412-
setDesiredPosition(rthGetTrackbackPos(), 0, NAV_POS_UPDATE_XY | NAV_POS_UPDATE_Z | NAV_POS_UPDATE_BEARING);
1413-
}
1373+
if (rthTrackBackSetNewPosition()) {
1374+
return NAV_FSM_EVENT_SWITCH_TO_NAV_STATE_RTH_INITIALIZE;
14141375
}
14151376

14161377
return NAV_FSM_EVENT_NONE;
@@ -2408,7 +2369,7 @@ static bool getLocalPosNextWaypoint(fpVector3_t * nextWpPos)
24082369
* Check if waypoint is/was reached.
24092370
* waypointBearing stores initial bearing to waypoint
24102371
*-----------------------------------------------------------*/
2411-
static bool isWaypointReached(const fpVector3_t * waypointPos, const int32_t * waypointBearing)
2372+
bool isWaypointReached(const fpVector3_t *waypointPos, const int32_t *waypointBearing)
24122373
{
24132374
posControl.wpDistance = calculateDistanceToDestination(waypointPos);
24142375

@@ -2768,12 +2729,13 @@ void updateHomePosition(void)
27682729
* Climb First override limited to Fixed Wing only
27692730
* Roll also cancels RTH trackback on Fixed Wing and Multirotor
27702731
*-----------------------------------------------------------*/
2771-
static bool rthAltControlStickOverrideCheck(unsigned axis)
2732+
bool rthAltControlStickOverrideCheck(uint8_t axis)
27722733
{
27732734
if (!navConfig()->general.flags.rth_alt_control_override || posControl.flags.forcedRTHActivated ||
27742735
(axis == ROLL && STATE(MULTIROTOR) && !posControl.flags.rthTrackbackActive)) {
27752736
return false;
27762737
}
2738+
27772739
static timeMs_t rthOverrideStickHoldStartTime[2];
27782740

27792741
if (rxGetChannelValue(axis) > rxConfig()->maxcheck) {
@@ -2812,110 +2774,6 @@ static bool rthAltControlStickOverrideCheck(unsigned axis)
28122774
return false;
28132775
}
28142776

2815-
/* --------------------------------------------------------------------------------
2816-
* == RTH Trackback ==
2817-
* Saves track during flight which is used during RTH to back track
2818-
* along arrival route rather than immediately heading directly toward home.
2819-
* Max desired trackback distance set by user or limited by number of available points.
2820-
* Reverts to normal RTH heading direct to home when end of track reached.
2821-
* Trackpoints logged with precedence for course/altitude changes. Distance based changes
2822-
* only logged if no course/altitude changes logged over an extended distance.
2823-
* Tracking suspended during fixed wing loiter (PosHold and WP Mode timed hold).
2824-
* --------------------------------------------------------------------------------- */
2825-
static void updateRthTrackback(bool forceSaveTrackPoint)
2826-
{
2827-
static bool suspendTracking = false;
2828-
bool fwLoiterIsActive = STATE(AIRPLANE) && (NAV_Status.state == MW_NAV_STATE_HOLD_TIMED || FLIGHT_MODE(NAV_POSHOLD_MODE));
2829-
if (!fwLoiterIsActive && suspendTracking) {
2830-
suspendTracking = false;
2831-
}
2832-
2833-
if (navConfig()->general.flags.rth_trackback_mode == RTH_TRACKBACK_OFF || FLIGHT_MODE(NAV_RTH_MODE) || !ARMING_FLAG(ARMED) || suspendTracking) {
2834-
return;
2835-
}
2836-
2837-
// Record trackback points based on significant change in course/altitude until
2838-
// points limit reached. Overwrite older points from then on.
2839-
if (posControl.flags.estPosStatus >= EST_USABLE && posControl.flags.estAltStatus >= EST_USABLE) {
2840-
static int32_t previousTBTripDist; // cm
2841-
static int16_t previousTBCourse; // degrees
2842-
static int16_t previousTBAltitude; // meters
2843-
static uint8_t distanceCounter = 0;
2844-
bool saveTrackpoint = forceSaveTrackPoint;
2845-
bool GPSCourseIsValid = isGPSHeadingValid();
2846-
2847-
// start recording when some distance from home, 50m seems reasonable.
2848-
if (posControl.activeRthTBPointIndex < 0) {
2849-
saveTrackpoint = posControl.homeDistance > METERS_TO_CENTIMETERS(50);
2850-
2851-
previousTBCourse = CENTIDEGREES_TO_DEGREES(posControl.actualState.cog);
2852-
previousTBTripDist = posControl.totalTripDistance;
2853-
} else {
2854-
// Minimum distance increment between course change track points when GPS course valid - set to 10m
2855-
const bool distanceIncrement = posControl.totalTripDistance - previousTBTripDist > METERS_TO_CENTIMETERS(10);
2856-
2857-
// Altitude change
2858-
if (ABS(previousTBAltitude - CENTIMETERS_TO_METERS(posControl.actualState.abs.pos.z)) > 10) { // meters
2859-
saveTrackpoint = true;
2860-
} else if (distanceIncrement && GPSCourseIsValid) {
2861-
// Course change - set to 45 degrees
2862-
if (ABS(wrap_18000(DEGREES_TO_CENTIDEGREES(DECIDEGREES_TO_DEGREES(gpsSol.groundCourse) - previousTBCourse))) > DEGREES_TO_CENTIDEGREES(45)) {
2863-
saveTrackpoint = true;
2864-
} else if (distanceCounter >= 9) {
2865-
// Distance based trackpoint logged if at least 10 distance increments occur without altitude or course change
2866-
// and deviation from projected course path > 20m
2867-
float distToPrevPoint = calculateDistanceToDestination(&posControl.rthTBPointsList[posControl.activeRthTBPointIndex]);
2868-
2869-
fpVector3_t virtualCoursePoint;
2870-
virtualCoursePoint.x = posControl.rthTBPointsList[posControl.activeRthTBPointIndex].x + distToPrevPoint * cos_approx(DEGREES_TO_RADIANS(previousTBCourse));
2871-
virtualCoursePoint.y = posControl.rthTBPointsList[posControl.activeRthTBPointIndex].y + distToPrevPoint * sin_approx(DEGREES_TO_RADIANS(previousTBCourse));
2872-
2873-
saveTrackpoint = calculateDistanceToDestination(&virtualCoursePoint) > METERS_TO_CENTIMETERS(20);
2874-
}
2875-
distanceCounter++;
2876-
previousTBTripDist = posControl.totalTripDistance;
2877-
} else if (!GPSCourseIsValid) {
2878-
// if no reliable course revert to basic distance logging based on direct distance from last point - set to 20m
2879-
saveTrackpoint = calculateDistanceToDestination(&posControl.rthTBPointsList[posControl.activeRthTBPointIndex]) > METERS_TO_CENTIMETERS(20);
2880-
previousTBTripDist = posControl.totalTripDistance;
2881-
}
2882-
2883-
// Suspend tracking during loiter on fixed wing. Save trackpoint at start of loiter.
2884-
if (distanceCounter && fwLoiterIsActive) {
2885-
saveTrackpoint = suspendTracking = true;
2886-
}
2887-
}
2888-
2889-
// when trackpoint store full, overwrite from start of store using 'rthTBWrapAroundCounter' to track overwrite position
2890-
if (saveTrackpoint) {
2891-
if (posControl.activeRthTBPointIndex == (NAV_RTH_TRACKBACK_POINTS - 1)) { // wraparound to start
2892-
posControl.rthTBWrapAroundCounter = posControl.activeRthTBPointIndex = 0;
2893-
} else {
2894-
posControl.activeRthTBPointIndex++;
2895-
if (posControl.rthTBWrapAroundCounter > -1) { // track wraparound overwrite position after store first filled
2896-
posControl.rthTBWrapAroundCounter = posControl.activeRthTBPointIndex;
2897-
}
2898-
}
2899-
posControl.rthTBPointsList[posControl.activeRthTBPointIndex] = posControl.actualState.abs.pos;
2900-
2901-
posControl.rthTBLastSavedIndex = posControl.activeRthTBPointIndex;
2902-
previousTBAltitude = CENTIMETERS_TO_METERS(posControl.actualState.abs.pos.z);
2903-
previousTBCourse = GPSCourseIsValid ? DECIDEGREES_TO_DEGREES(gpsSol.groundCourse) : previousTBCourse;
2904-
distanceCounter = 0;
2905-
}
2906-
}
2907-
}
2908-
2909-
static fpVector3_t * rthGetTrackbackPos(void)
2910-
{
2911-
// ensure trackback altitude never lower than altitude of start point
2912-
if (posControl.rthTBPointsList[posControl.activeRthTBPointIndex].z < posControl.rthTBPointsList[posControl.rthTBLastSavedIndex].z) {
2913-
posControl.rthTBPointsList[posControl.activeRthTBPointIndex].z = posControl.rthTBPointsList[posControl.rthTBLastSavedIndex].z;
2914-
}
2915-
2916-
return &posControl.rthTBPointsList[posControl.activeRthTBPointIndex];
2917-
}
2918-
29192777
/*-----------------------------------------------------------
29202778
* Update flight statistics
29212779
*-----------------------------------------------------------*/
@@ -3589,7 +3447,7 @@ static void mapWaypointToLocalPosition(fpVector3_t * localPos, const navWaypoint
35893447
geoConvertGeodeticToLocal(localPos, &posControl.gpsOrigin, &wpLLH, altConv);
35903448
}
35913449

3592-
static void calculateAndSetActiveWaypointToLocalPosition(const fpVector3_t * pos)
3450+
void calculateAndSetActiveWaypointToLocalPosition(const fpVector3_t *pos)
35933451
{
35943452
// Calculate bearing towards waypoint and store it in waypoint bearing parameter (this will further be used to detect missed waypoints)
35953453
if (isWaypointNavTrackingActive() && !(posControl.activeWaypoint.pos.x == pos->x && posControl.activeWaypoint.pos.y == pos->y)) {
@@ -3682,7 +3540,7 @@ bool isWaypointNavTrackingActive(void)
36823540
// is set from current position not previous WP. Works for WP Restart intermediate WP as well as first mission WP.
36833541
// (NAV_WP_MODE flag isn't set until WP initialisation is finished, i.e. after calculateAndSetActiveWaypoint called)
36843542

3685-
return FLIGHT_MODE(NAV_WP_MODE) || (posControl.flags.rthTrackbackActive && posControl.activeRthTBPointIndex != posControl.rthTBLastSavedIndex);
3543+
return FLIGHT_MODE(NAV_WP_MODE) || (posControl.flags.rthTrackbackActive && rth_trackback.activePointIndex != rth_trackback.lastSavedIndex);
36863544
}
36873545

36883546
/*-----------------------------------------------------------
@@ -3739,9 +3597,7 @@ void applyWaypointNavigationAndAltitudeHold(void)
37393597
// ensure WP missions always restart from first waypoint after disarm
37403598
posControl.activeWaypointIndex = posControl.startWpIndex;
37413599
// Reset RTH trackback
3742-
posControl.activeRthTBPointIndex = -1;
3743-
posControl.flags.rthTrackbackActive = false;
3744-
posControl.rthTBWrapAroundCounter = -1;
3600+
resetRthTrackBack();
37453601

37463602
return;
37473603
}
@@ -4273,7 +4129,7 @@ void updateWaypointsAndNavigationMode(void)
42734129
updateWpMissionPlanner();
42744130

42754131
// Update RTH trackback
4276-
updateRthTrackback(false);
4132+
rthTrackBackUpdate(false);
42774133

42784134
//Update Blackbox data
42794135
navCurrentState = (int16_t)posControl.navPersistentId;

src/main/navigation/navigation.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,9 @@ float geoCalculateMagDeclination(const gpsLocation_t * llh); // degrees units
579579
// Select absolute or relative altitude based on WP mission flag setting
580580
geoAltitudeConversionMode_e waypointMissionAltConvMode(geoAltitudeDatumFlag_e datumFlag);
581581

582+
void calculateAndSetActiveWaypointToLocalPosition(const fpVector3_t *pos);
583+
bool isWaypointReached(const fpVector3_t * waypointPos, const int32_t * waypointBearing);
584+
582585
/* Distance/bearing calculation */
583586
bool navCalculatePathToDestination(navDestinationPath_t *result, const fpVector3_t * destinationPos); // NOT USED
584587
uint32_t distanceToFirstWP(void);
@@ -631,6 +634,7 @@ bool isEstimatedAglTrusted(void);
631634

632635
void checkManualEmergencyLandingControl(bool forcedActivation);
633636
float updateBaroAltitudeRate(float newBaroAltRate, bool updateValue);
637+
bool rthAltControlStickOverrideCheck(uint8_t axis);
634638

635639
int8_t navCheckActiveAngleHoldAxis(void);
636640
uint8_t getActiveWpNumber(void);

src/main/navigation/navigation_private.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@
4545
#define MC_LAND_DESCEND_THROTTLE 40 // RC pwm units (us)
4646
#define MC_LAND_SAFE_SURFACE 5.0f // cm
4747

48-
#define NAV_RTH_TRACKBACK_POINTS 50 // max number RTH trackback points
49-
5048
#define MAX_POSITION_UPDATE_INTERVAL_US HZ2US(MIN_POSITION_UPDATE_RATE_HZ) // convenience macro
5149
_Static_assert(MAX_POSITION_UPDATE_INTERVAL_US <= TIMEDELTA_MAX, "deltaMicros can overflow!");
5250

@@ -428,12 +426,6 @@ typedef struct {
428426
timeMs_t wpReachedTime; // Time the waypoint was reached
429427
bool wpAltitudeReached; // WP altitude achieved
430428

431-
/* RTH Trackback */
432-
fpVector3_t rthTBPointsList[NAV_RTH_TRACKBACK_POINTS];
433-
int8_t rthTBLastSavedIndex; // last trackback point index saved
434-
int8_t activeRthTBPointIndex;
435-
int8_t rthTBWrapAroundCounter; // stores trackpoint array overwrite index position
436-
437429
/* Internals & statistics */
438430
int16_t rcAdjustment[4];
439431
float totalTripDistance;

0 commit comments

Comments
 (0)