Repository navigation
New PSG Autopilot rewrite - #2776
Conversation
This look good enough to start with and passes some basic flame tests. MechJebModuleAscentPSGAutopilot + MechJebModuleGuidanceController become the MechJebModulePSGAutopilot which is responsible for the whole launch including PSG active guidance.
- trying to better isolate the guidance code from throttle code from updating display items - fixing a problem with boosters that need to stop and be staged off from not being staged off because the next stage is a fairing or LES or something and not the decoupler for the boosters.
The _inertial field does need to be updated every tick and always updates if _inertial is V3.zero
Adds some comments around JETTISON
I want those to reflect the moment the main engines were cut. Major discrepancies in the Tgo line usually indicates root part issues or some other deviation between the rocket in the stage stats and what is actually flying, so is important in diagnosis. Alternatively some ~15 burnout TWR rocket with Abs(Tgo) < 0.02 and some fairly large Vgo just indicates inherent inaccuracy in shutting down engines on 0.02 discrete ticks, and proper operation. The Tgo/Vgo values read from the Solution after TERMINAL_RCS is done are meaningless.
cf26786 to
deb96d5
Compare
✅ Build Artifact 🚀MechJeb2-2062af1.zip (built from deb96d5) |
|
Review notes on #2776 and #2777 (dev ef54081). Method:
What the rewrite fixes
1. A burn is cut on the clock, not on the energy it has reached
When it matters: a burn that falls behind its plan, through an engine short of thrust (TestFlight's thrust loss on the Juno's A-7) or a late ignition:
Why a re-solve doesn't help: Before:
Seen in game:
Tests: 2. A late fixed booster eats the cut stage above itPlan: a fixed booster, then a stage the optimizer cuts with propellant left (JETTISON before a fixed stack), then the last stage. What happens when the booster burns past its planned end:
Results:
Before: the old controller cut that stage in TERMINAL on |r×v|, so it burned until it had the plan's energy. Test: 3. A Solution that lands during JETTISON relights the cut boosterA solve is cancelled only at the coast's start and end, at TERMINAL_STARTED and at FINISHED. Nothing cancels it at a jettison cut.
Before: the old TERMINAL_STAGING held until Test: 4. A coast skipped by #2777 can come back ahead of the burning upper stageThe setup: a COAST_BEFORE coast the optimizer leaves under
What happens once a re-solve gives that coast
Before: the old code had the same gap. But a coast shrunk to nothing used to be flown as a short coast, which set Test: Smaller
The testsEach item above has an To make a booster fall behind its plan, the tests light it late ( #2733, #2731 and #2732 look fine. For #2733 specifically: T is still its own variable, and dropping the norm row on continuity-tied points avoids the LICQ problem #2316 fixed. ReviewRegressionTests.cs/*
* Copyright Lamont Granquist, Sebastien Gaggini and the MechJeb contributors
* SPDX-License-Identifier: LicenseRef-PD-hp OR Unlicense OR CC0-1.0 OR 0BSD OR MIT-0 OR MIT OR LGPL-2.1+
*/
using System.Linq;
using MechJebLib.PSG;
using Xunit;
using static MechJebLibTest.PSGTests.AscentFlight;
namespace MechJebLibTest.PSGTests
{
/// <summary>
/// Review of #2776 / #2777: known past failure states of PSG guidance, flown through the AscentStateMachine. A test that
/// fails on dev ef540816 shows a defect. "Behind the plan" is modelled as an engine that lights late (Engine.LightAt): the
/// vessel only moves along the synthetic trajectory while it thrusts, so it reaches each planned angular momentum late.
/// </summary>
public class ReviewRegressionTests
{
private const PlanEditor.CoastKind BEFORE = PlanEditor.CoastKind.BEFORE;
private static AscentFlight ThreeStages(double booster = 60, double middle = 40, double last = 30)
{
var flight = new AscentFlight(2);
flight.AddEngine(1, 0, -1, last);
flight.AddEngine(2, 1, 0, middle);
flight.AddEngine(3, 2, 1, booster);
return flight;
}
/*
* F6: a burn cut on the clock with the vessel behind the plan. #2747 cut at the later of the planned end and the
* planned end's |r x v|; the old controller cut the precise-shutdown booster in TERMINAL on |r x v|.
*/
// a booster cut before a fixed upper stack, 5 s behind its plan, with no new Solution (the re-solves are paused for the
// last PreStageTime = 10 s of a phase anyway)
[Fact]
public void F6_JettisonCutWaitsForThePlannedAngularMomentum()
{
AscentFlight flight = ThreeStages();
flight.EngineOf(3).LightAt = MET0 + 5;
flight.Engage(Solve(flight.Plan(), MET0, Cut(50), Fixed(40), Fixed(30)));
Assert.True(flight.RunUntil(PSGState.JETTISON, 100));
// the vessel is where the plan is at its cut, or the booster leaves with 5 s of propellant more than planned
Assert.True(flight.TrajectoryT >= MET0 + 50 - DT,
$"cut at MET {flight.Met - MET0:F2} with the vessel at plan MET {flight.TrajectoryT - MET0:F2}, booster fuel left {flight.EngineOf(3).Fuel:F2} s");
}
// the same booster cut by the coast before the last stages
[Fact]
public void F6_CoastCutWaitsForThePlannedAngularMomentum()
{
AscentFlight flight = ThreeStages();
flight.EngineOf(3).LightAt = MET0 + 5;
flight.Engage(Solve(flight.Plan(BEFORE, 1), MET0, Cut(50), Coast(30), Fixed(40), Fixed(30)));
Assert.True(flight.RunUntil(PSGState.COASTING, 100));
Assert.True(flight.TrajectoryT >= MET0 + 50 - DT,
$"coast at MET {flight.Met - MET0:F2} with the vessel at plan MET {flight.TrajectoryT - MET0:F2}, booster fuel left {flight.EngineOf(3).Fuel:F2} s");
}
// A fixed booster behind its plan burns past the planned end of the short cut stage above it. The re-solves are paused
// all that time (CurrentPhaseTgo is 0, under PreStageTime), and the cut stage is jettisoned on the clock as soon as the
// stage stats show the booster gone: lateness comes straight off the cut stage, and here all of it.
[Theory]
[InlineData(3)] // the middle stage burns 7 s of its 10
[InlineData(12)] // the middle stage past its planned end before it lights: staged off unlit, full
public void F6_LateBoosterDoesNotEatTheCutStageAboveIt(double late)
{
AscentFlight flight = ThreeStages(60, 40, 30);
flight.EngineOf(3).LightAt = MET0 + late;
flight.Engage(Solve(flight.Plan(), MET0, Fixed(60), Cut(10), Fixed(30)));
Assert.True(flight.RunUntil(PSGState.JETTISON, 200), flight.Sequence());
double planAtCut = flight.TrajectoryT - MET0;
double middleFuel = flight.EngineOf(2).Fuel;
int middleBurns = flight.EngineOf(2).Burns;
Assert.True(middleBurns == 1 && planAtCut >= 70 - DT,
$"middle stage cut at MET {flight.Met - MET0:F2}, vessel at plan MET {planAtCut:F2} (planned cut 70), " +
$"middle burns {middleBurns}, fuel left {middleFuel:F2} s of 40 (planned 30)");
}
/*
* JETTISON and a Solution that lands in it
*/
// A solve started before the cut (more than PreStageTime before it) lands while JETTISON waits for KSP to stage. Its
// booster ends 3 s later than the one being cut (the vessel was behind when it started). Nothing cancels a solve at a
// jettison (only at the coast, terminal guidance and the end), so it is flown: the cut booster is throttled up again.
[Fact]
public void StaleSolutionInJettisonDoesNotRelightTheCutBooster()
{
AscentFlight flight = ThreeStages();
flight.RefuseStagingUntil = MET0 + 51;
flight.Engage(Solve(flight.Plan(), MET0, Cut(50), Fixed(40), Fixed(30)));
Assert.True(flight.RunUntil(PSGState.JETTISON, 100));
flight.Run(0.4);
Assert.Equal(PSGState.JETTISON, flight.Machine.State);
Assert.True(flight.Machine.SetSolution(Solve(flight.FlownPlan, MET0 + 39, Cut(14), Fixed(40), Fixed(30))));
flight.Fly();
Assert.True(flight.EngineOf(3).Burns == 1,
$"the cut booster lit {flight.EngineOf(3).Burns} times: {flight.Sequence()}, {flight.StagingLog()}");
}
/*
* Skipped phases (#2777)
*/
// A coast-before that a Solution skips (shorter than SKIP_TIME): the booster is cut and jettisoned, and the coast's stage
// lights. PSGSimulation never marks the coast done (MarkCoastDone runs only on COAST_ENDED) and FindStartingIndex
// starts the next solve at it, so the next Solution starts with the coast, which the optimizer (MinCoast 0) may now
// make longer than SKIP_TIME. CurrentPhase(false) is then -1 and CoastDue fires: the burning upper stage is cut for a
// coast before itself and relit (a no-relight engine, F23; the throttle cut, F56).
[Fact]
public void SkippedCoastBeforeComingBackDoesNotCutTheBurningStage()
{
AscentFlight flight = ThreeStages();
flight.Engage(Solve(flight.Plan(BEFORE, 1), MET0, Cut(50), Coast(0), Fixed(40), Fixed(30)));
Assert.True(flight.RunUntil(f => f.Met >= MET0 + 55, 100));
Assert.Equal(PSGState.BURNING, flight.Machine.State);
Assert.Equal(1, flight.Stage);
Assert.Equal(1, flight.EngineOf(2).Burns);
// flight plan stages: 0 BURN row 0, 1 BURN row 1, 2 COAST_BEFORE row 1, 3 BURN row 2 (gone)
double now = flight.Met;
Assert.True(flight.Machine.SetSolution(Solve(flight.FlownPlan, now, 2, Coast(1), Fixed(35), Fixed(30))));
flight.Fly();
Assert.True(flight.Count(AscentEvents.COAST_STARTED) == 0 && flight.EngineOf(2).Burns == 1,
$"{flight.Sequence()}; upper stage lit {flight.EngineOf(2).Burns} times");
}
// A booster the optimizer cuts with propellant left, whose remaining burn a Solution shrinks under SKIP_TIME (0.128 s in these tests) while the
// vessel is in it: guidance skips it and flies the next burn's steering and throttle on the booster, which nothing cuts
// or stages (autostaging won't drop an engine with propellant): it burns out.
[Fact]
public void SkippedCutBoosterIsNotBurnedOut()
{
AscentFlight flight = ThreeStages();
flight.Engage(Solve(flight.Plan(), MET0, Cut(0.1), Fixed(40), Fixed(30)));
flight.Run(10);
Assert.True(flight.EngineOf(3).Fuel > 59,
$"{flight.Sequence()}; booster fuel left {flight.EngineOf(3).Fuel:F2} s of 60 after 10 s, stage {flight.Stage}");
}
}
} |
|
Yeah, I deliberately avoided addressing 1 to keep the state machine simpler at "BECO", which is already getting complicated enough. I want to do stabilization before fixing everything else. This is largely about managing my own context window. If you get a thrust fault during the prestage cooldown you're also not going to get an updated optimizer solve, and you might hit the energy target, but you won't follow the actual right path, so this is about making a fault better, but you can't ever fix it. 2 is also very weird. This is a fixed burntime stack before a coast before a fixed burntime upper ending in a fully guided unfixed stage? If I'm understanding it correctly that is an incredibly low concern use case. 3 means that the PSGSImulation manager should flush a new Solution that comes in during JETTISON? That would be fine. 4 i'm guessing this edge conditions suggests that HasCoasted is the wrong API to use and that WillCoast combined with IsCoasting is the right way to query what we're doing. |
|
I pushed the PR fix it proposed as draft. About 2 it has this following comment. Still sick, so no better analysis on my side, sorry. On 2: there is no coast in it. The shape is a fixed booster, then a middle stage the optimizer cuts with propellant left, then a fixed upper stage (the test's plan is Fixed(60), Cut(10), Fixed(30)). Stock Kerbal X with a fixed Poodle and no coast has that shape: the last asparagus pair (stage 5) burns to depletion; A guard that leaves the BECO logic alone: hold a clock cut until a Solution solved after that stage lit has arrived. PSGSimulation already solves after staging (AFTER_STAGING), and that solve re-plans the cut from where the vessel actually is. The middle stage has propellant to spare in the meantime. |
replaces #2746
replaces #2747
This is almost certainly going to need more cleanup. I haven't done an actual human pass on it.