Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/Settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -3772,7 +3772,7 @@ P gain of auto speed PID controller.

### nav_fw_bank_angle

Maximum sustained roll angle when turning in GPS assisted modes: the target bank that turn and loiter radii are planned for. Corrections may exceed it temporarily; the absolute ceiling remains max_angle_inclination_rll
Maximum sustained roll angle when turning in GPS assisted modes: the target bank that turn and loiter radii are planned for. Wind corrections on turn arcs and the loiter circle may exceed it by a fixed reserve of 30 % more lateral acceleration (e.g. 35° -> about 42°), never above max_angle_inclination_rll

| Default | Min | Max |
| --- | --- | --- |
Expand Down
6 changes: 3 additions & 3 deletions docs/development/fw-turn-debugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ blackbox logging.
| 3 | Remaining heading to the exit course | centideg | Arc coordinator, only while engaged |
| 4 | Arc bank command | centideg | Arc coordinator, only while engaged; clamped to the effective bank ceiling |
| 5 | WP-turn roll feed-forward | centideg | Feed-forward (0 when disabled, loitering, or inside the heading deadband) |
| 6 | Energy-guard bank ceiling | deg | Energy bank guard (sits at `max_angle_inclination_rll` unless the guard is reducing it) |
| 6 | Energy-guard bank ceiling | deg | Energy bank guard (sits at the bank ceiling, `nav_fw_bank_angle` plus a 30 % lateral-acceleration reserve capped by `max_angle_inclination_rll`, unless the guard is reducing it) |
| 7 | Roll ease time | ms | Arc coordinator, only while engaged (sizes the entry/exit ramps and the turn-start lead) |

Channels 2, 3, 4 and 7 hold their last value after the arc disengages; check channel 1 to know
Expand Down Expand Up @@ -54,6 +54,6 @@ rolling out of the S's second arc; `3` (idle) = S finished, coordinator handed b
A healthy COORD_FLYBY corner shows: channel 1 stepping `10 → 20 → 30 → 0`, channel 4 ramping to
the nominal bank, holding, then collapsing as channel 3 converges to 0 without overshoot. The
hand-back happens aligned (|ch3| small) and nearly level (|ch4| < 10% of nominal). Channel 4
pegged at channel 6 × 100 indicates the command is saturated at the flyable ceiling — expected
briefly downwind, a problem if sustained. Channel 6 dropping below `max_angle_inclination_rll`
pegged at about channel 6 × 100 (channel 6 is in whole degrees) indicates the command is saturated at the flyable ceiling — expected
briefly downwind, a problem if sustained. Channel 6 dropping below that bank ceiling
means the energy guard is trading bank for climb capability.
2 changes: 1 addition & 1 deletion src/main/fc/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3113,7 +3113,7 @@ groups:
field: mc.slowDownForTurning
type: bool
- name: nav_fw_bank_angle
description: "Maximum sustained roll angle when turning in GPS assisted modes: the target bank that turn and loiter radii are planned for. Corrections may exceed it temporarily; the absolute ceiling remains max_angle_inclination_rll"
description: "Maximum sustained roll angle when turning in GPS assisted modes: the target bank that turn and loiter radii are planned for. Wind corrections on turn arcs and the loiter circle may exceed it by a fixed reserve of 30 % more lateral acceleration (e.g. 35° -> about 42°), never above max_angle_inclination_rll"
default_value: 35
field: fw.max_bank_angle
min: 5
Expand Down
60 changes: 44 additions & 16 deletions src/main/navigation/navigation_fixedwing.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
#define NAV_FW_ARC_EXIT_BANK_CD 1000.0f // [centideg] ... and below this bank; the residual hands over into the tracker
#define NAV_FW_ARC_AWAY_TIMEOUT_FACTOR 1.5f // half away-circle traverse times, before the away arc is abandoned
#define NAV_FW_ARC_SHARP_TURN_CD 15000 // [centideg] beyond this the tangents explode toward the reversal -> capture only
#define NAV_FW_ARC_ENTRY_PAST_CM 1000.0f // [cm] turn-in point this far behind still flies the arc: a corner engaged slightly late
#define NAV_FW_ARC_ENTRY_PAST_R 0.2f // ... but never more than this fraction of the radius, where the arc law saturates
#define NAV_FW_BANK_RESERVE_FACTOR 1.3f // lateral-accel reserve over nav_fw_bank_angle for wind correction on arcs/loiter

// FW energy/altitude bank guard thresholds (conservative; observable via DEBUG_FW_TURN)
#define NAV_FW_GUARD_PHI_FLOOR_DEG 15.0f // minimum effective bank limit
Expand Down Expand Up @@ -427,6 +430,7 @@ void resetFixedWingPositionController(void)
// transition, e.g. RTH -> CRUISE) seed from the last applied command so the level-off/turn change
// is eased; after a pilot-flown phase seed neutral so a roll-out in progress is not re-commanded.
fwRollSmoothSeedCd = ((micros() - fwLastNavRollCmdTimeUs) < MAX_POSITION_UPDATE_INTERVAL_US) ? fwLastNavRollCmdCd : 0.0f;
fwLastNavRollCmdCd = fwRollSmoothSeedCd; // the arc engage and the handback blend from it: a stale bank must not seed them
fwRollSmoothReseed = true;

pt1FilterSetCutoff(&fwCrossTrackErrorRateFilterState, NAV_FW_CROSSTRACK_RATE_CUTOFF_HZ);
Expand Down Expand Up @@ -521,13 +525,27 @@ static float applyFwRollInSmoothing(float rollTargetCd, timeDelta_t deltaMicros,
return out;
}

// Hard roll ceiling [deg]: the global angle-mode limit (max_angle_inclination_rll)
// Hard roll ceiling [deg]: max_angle_inclination_rll is a stick-feel limit and must not become a nav bank
static float getFwBankCeilingDeg(void)
{
return (float)pidProfile()->max_angle_inclination[FD_ROLL] / 10.0f;
static float ceilingDeg;
static int16_t cachedInclination;
static uint8_t cachedBank;

const uint8_t bank = navConfig()->fw.max_bank_angle;
const int16_t inclination = pidProfile()->max_angle_inclination[FD_ROLL];
// Called every PID loop: redo the trig only when a setting changes
if (bank != cachedBank || inclination != cachedInclination) {
const float bankRad = DEGREES_TO_RADIANS((float)bank);
const float reserveDeg = RADIANS_TO_DEGREES(atan2_approx(NAV_FW_BANK_RESERVE_FACTOR * sin_approx(bankRad), cos_approx(bankRad)));
ceilingDeg = MIN(reserveDeg, (float)inclination / 10.0f);
cachedBank = bank;
cachedInclination = inclination;
}
return ceilingDeg;
}

// Output bank ceiling [deg]: hard ceiling reduced by the energy guard; nav_fw_bank_angle is only the planning target
// Output bank ceiling [deg]: hard ceiling reduced by the energy guard
static float getFwEffectiveBankLimit(void)
{
const float ceiling = getFwBankCeilingDeg();
Expand Down Expand Up @@ -936,18 +954,10 @@ static void fwArcPlanCornerTurn(const fwArcCtx_t *c, bool capped)
fwPlanTurn(c->v, &plan);
const float omegaNomCds = DEGREES_TO_CENTIDEGREES(RADIANS_TO_DEGREES(c->v / plan.r)); // v/R == g*tan(phi)/v
const float psiTmp = 0.5f * omegaNomCds * (plan.tEaseMs / 1000.0f);
if (capped || ABS(c->hdgErrToLeg) > NAV_FW_ARC_SHARP_TURN_CD) {
// Capped or near-reversal: no valid tangent circle - fly the bounded capture directly
fwTurn.arc.engaged = true;
fwTurn.arc.bankCmd = c->lastNavRollCmdCd; // blend from the current command: no engage step
fwTurn.arc.rampMs = 0.0f;
fwTurn.arc.rampStartCd = 0.0f;
fwTurn.arc.r = plan.r; // capture ignores it, but the away timeout is sized from it
fwTurn.arc.dir = (c->hdgErrToLeg > 0) ? 1 : -1;
fwTurn.arc.phiNomCd = plan.phiNomCd;
fwTurn.arc.tEaseMs = plan.tEaseMs;
fwArcCaptureToLeg(c->legBearing);
} else if (2.0f * psiTmp < (float)ABS(c->hdgErrToLeg)) { // enough turn left for a steady arc between the ease ramps
if (!capped && ABS(c->hdgErrToLeg) <= NAV_FW_ARC_SHARP_TURN_CD) {
if (!(2.0f * psiTmp < (float)ABS(c->hdgErrToLeg))) { // too little turn for a steady arc between the ease ramps
return;
}
const int8_t dirTmp = (c->hdgErrToLeg > 0) ? 1 : -1;
// Centre = intersection of both legs shifted R inside (inscribed circle), so the exit lands ON the out-leg
const float cogRad = CENTIDEGREES_TO_RADIANS((float)c->cog);
Expand All @@ -963,8 +973,26 @@ static void fwArcPlanCornerTurn(const fwArcCtx_t *c, bool capped)
cx = p1x;
cy = p1y;
}
fwArcEngageRampIn(&plan, dirTmp, cx, cy, c->legBearing, c->lastNavRollCmdCd);
// Turn-in tangency well behind us: the arc law would start far outside the circle and saturate the bank
if (fwAlongLegCm(cx, cy, p1x, p1y, d1x, d1y) >= -MIN(NAV_FW_ARC_ENTRY_PAST_CM, NAV_FW_ARC_ENTRY_PAST_R * plan.r)) {
fwArcEngageRampIn(&plan, dirTmp, cx, cy, c->legBearing, c->lastNavRollCmdCd);
return;
}
if (!navConfig()->fw.wp_tracking_accuracy) {
return; // no leg line to capture without tracking: the heading controller flies straight at the waypoint
}
}

// Capped, near-reversal or turn-in point well behind: no reachable tangent circle - fly the bounded capture directly
fwTurn.arc.engaged = true;
fwTurn.arc.bankCmd = c->lastNavRollCmdCd; // blend from the current command: no engage step
fwTurn.arc.rampMs = 0.0f;
fwTurn.arc.rampStartCd = 0.0f;
fwTurn.arc.r = plan.r; // capture ignores it, but the away timeout is sized from it
fwTurn.arc.dir = (c->hdgErrToLeg > 0) ? 1 : -1;
fwTurn.arc.phiNomCd = plan.phiNomCd;
fwTurn.arc.tEaseMs = plan.tEaseMs;
fwArcCaptureToLeg(c->legBearing);
}

static void fwArcPlanNewLeg(const fwArcCtx_t *c, bool capped)
Expand Down
6 changes: 6 additions & 0 deletions src/main/navigation/navigation_fixedwing_turn_math.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ float fwOffLegCm(float px, float py, float qx, float qy, float ux, float uy)
return (px - qx) * (-uy) + (py - qy) * ux;
}

// Signed along-track position [cm] of p ahead of q on the line with unit direction (ux, uy)
float fwAlongLegCm(float px, float py, float qx, float qy, float ux, float uy)
{
return (px - qx) * ux + (py - qy) * uy;
}

// Ground distance covered while the roll-in ramp builds the bank (k calibrated in flight)
float fwRollInLeadCm(float v, float tMs)
{
Expand Down
1 change: 1 addition & 0 deletions src/main/navigation/navigation_fixedwing_turn_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ float fwSmoothBlend(float from, float to, float p);
float fwSlewToward(float cur, float target, float maxStep);
float fwArcMaxStepCd(float phiCd, float tEaseMs, float dtMs);
float fwOffLegCm(float px, float py, float qx, float qy, float ux, float uy);
float fwAlongLegCm(float px, float py, float qx, float qy, float ux, float uy);
float fwRollInLeadCm(float v, float tMs);
57 changes: 57 additions & 0 deletions src/test/unit/navigation_fixedwing_turn_math_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ static float ref_offLegCm(float px, float py, float qx, float qy, float legRad)
return (px - qx) * (-sin_approx(legRad)) + (py - qy) * cos_approx(legRad);
}

// New helper, replaced nothing: same sum as the inline along-track terms in the coordinator
static float ref_alongLegCm(float px, float py, float qx, float qy, float legRad)
{
return (px - qx) * cos_approx(legRad) + (py - qy) * sin_approx(legRad);
}

// P17 @784, 935, 1008
static float ref_rollInLeadCm(float v, float tMs)
{
Expand Down Expand Up @@ -376,3 +382,54 @@ TEST(NavFwTurnMathUnittest, RollInLead)
}
}
}

TEST(NavFwTurnMathUnittest, AlongLeg)
{
const float px = 12345.0f, py = -6789.0f;
const float qx = -2200.0f, qy = 33300.0f;
for (int32_t cd = 0; cd < 36000; cd += 37) {
float ux, uy;
fwBearingUnit(cd, &ux, &uy);
const float legRad = CENTIDEGREES_TO_RADIANS((float)cd);
EXPECT_BITS_EQ(fwAlongLegCm(px, py, qx, qy, ux, uy), ref_alongLegCm(px, py, qx, qy, legRad));
}
}

// Corner-turn planner chain: along-course distance from the aircraft to the inscribed circle's turn-in tangency
static float cornerEntryAlongCm(float px, float py, int32_t cog, float wpx, float wpy, int32_t leg, float r)
{
const float dir = (wrap_18000(leg - cog) > 0) ? 1.0f : -1.0f;
float d1x, d1y, d2x, d2y, p1x, p1y, p2x, p2y, cx, cy;
fwBearingUnit(cog, &d1x, &d1y);
fwBearingUnit(leg, &d2x, &d2y);
fwPerpOffset(px, py, r, CENTIDEGREES_TO_RADIANS((float)cog), dir, &p1x, &p1y);
fwPerpOffset(wpx, wpy, r, CENTIDEGREES_TO_RADIANS((float)leg), dir, &p2x, &p2y);
if (!fwLineIntersect(p1x, p1y, d1x, d1y, p2x, p2y, d2x, d2y, 0.087f, &cx, &cy)) {
cx = p1x;
cy = p1y;
}
return fwAlongLegCm(cx, cy, p1x, p1y, d1x, d1y);
}

TEST(NavFwTurnMathUnittest, CornerEntryTangency)
{
// WP mode entered 103 deg off the leg, the leg starting at the aircraft: tangency 1.26 R behind
const float r = 7580.0f;
const float px = 22396.0f, py = -13081.0f;
float ux, uy;
fwBearingUnit(16440, &ux, &uy);
const float behind = cornerEntryAlongCm(px, py, 6150, px + 30000.0f * ux, py + 30000.0f * uy, 16440, r);
EXPECT_LT(behind, 0.0f);
EXPECT_NEAR(behind, -r * tanf(DEGREES_TO_RADIANS(102.9f / 2.0f)), 50.0f);

// Regular FLY_BY corner, 90 deg left: on the inbound leg, switched at roll-in lead + R*tan(45 deg) before the WP
const float r2 = 4000.0f;
const float lead = fwRollInLeadCm(2000.0f, 950.0f);
const float wp0x = 50000.0f, wp0y = 20000.0f;
const float ahead = cornerEntryAlongCm(wp0x - (lead + r2), wp0y, 0, wp0x, wp0y - 40000.0f, 27000, r2);
EXPECT_GT(ahead, 0.0f);
EXPECT_NEAR(ahead, lead, 20.0f);

// Near-parallel fallback (outside the planner's 30..150 deg range): exactly zero, which must still engage
EXPECT_GE(cornerEntryAlongCm(px, py, 22500, px - 5000.0f, py + 9000.0f, 22700, r), 0.0f);
}
Loading