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);
258259static void clearJumpCounters (void );
259260
260261static void calculateAndSetActiveWaypoint (const navWaypoint_t * waypoint );
261- static void calculateAndSetActiveWaypointToLocalPosition (const fpVector3_t * pos );
262262void calculateInitialHoldPosition (fpVector3_t * pos );
263263void calculateFarAwayTarget (fpVector3_t * farAwayPos , int32_t bearing , int32_t distance );
264- static bool isWaypointReached (const fpVector3_t * waypointPos , const int32_t * waypointBearing );
265264bool isWaypointAltitudeReached (void );
266265static void mapWaypointToLocalPosition (fpVector3_t * localPos , const navWaypoint_t * waypoint , geoAltitudeConversionMode_e altConv );
267266static navigationFSMEvent_t nextForNonGeoStates (void );
@@ -273,11 +272,6 @@ bool validateRTHSanityChecker(void);
273272void updateHomePosition (void );
274273bool abortLaunchAllowed (void );
275274
276- static bool rthAltControlStickOverrideCheck (unsigned axis );
277-
278- static void updateRthTrackback (bool forceSaveTrackPoint );
279- static fpVector3_t * rthGetTrackbackPos (void );
280-
281275/*************************************************************************************************/
282276static navigationFSMEvent_t navOnEnteringState_NAV_STATE_IDLE (navigationFSMState_t previousState );
283277static 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 ;
0 commit comments