Rise
The Vieneo Province
Loading...
Searching...
No Matches
OrbitEntryControl.h
Go to the documentation of this file.
1#pragma once
2#include "OrbitEntryPhysics.h"
4#include "OrbitEntryProfile.h"
5
6namespace Orbital { namespace EntryControl {
7struct Protection {
8 bool skip=false,load=false;
9 double skipClear=0,loadClear=0;
10 static bool Track(bool risk,bool recovered,bool& active,double& clear,double dt) {
11 if(risk) {active=true;clear=0;}
12 else if(active) {
13 clear=recovered?clear+(std::clamp)(dt,0.0,1.0):0;
14 if(clear>=2) {active=false;clear=0;}
15 }
16 return active;
17 }
18};
19inline SurfacePlanning::Kinematic Reference(const Plan& p,double t,double spin,double gm)
20{
21 const auto& e=p.entry;
22 if(t<e.coastSeconds) {
23 State s;
24 if(Propagate(e.departure,t-e.coastSeconds,gm,s)) return {s,SurfacePlanning::Gravity(s.r,gm)};
25 }
26 if(t<=p.end) return EntryProfile::PoweredReference(p,(std::max)(t,e.coastSeconds),gm);
27 // Continue the handoff ground track, including its descent. Wind must not
28 // be added again: the planned ground velocity already includes the crab.
29 const Vec up=Unit(e.handoff.r),ground=e.handoff.v-Cross({0,0,spin},e.handoff.r);
30 const double vertical=Dot(ground,up),horizontal=Norm(ground-up*vertical),dt=t-p.end;
31 const double radius=Norm(e.handoff.r);
32 const double floor=e.handoffFpa<0&&p.arrival.valid?Norm(p.arrival.gate):radius-1.0;
33 const double nextRadius=(std::max)(floor,radius+vertical*dt);
34 const Vec axis=Unit(Cross(up,ground));
35 const double angle=std::abs(vertical)>1e-9?horizontal/vertical*std::log(nextRadius/radius):horizontal/radius*dt;
36 auto turn=[&](Vec v){return Rotate(Rotate(v,axis,angle),{0,0,1},spin*dt);};
37 const Vec r=turn(up)*nextRadius,tangent=Unit(Cross(turn(axis),Unit(r))),g=turn(ground),v=Cross({0,0,spin},r)+g;
38 return {{r,v},Cross({0,0,spin},Cross({0,0,spin},r))+Cross({0,0,spin},g)*2-
39 Unit(r)*(horizontal*horizontal/nextRadius)+tangent*(horizontal*vertical/nextRadius)};
40}
41inline SurfacePlanning::Kinematic InCurrentFrame(const Plan& p,double t,double spin,double gm)
42{
43 auto k=Reference(p,t,spin,gm);
44 auto rotate=[&](Vec v){return Rotate(v,{0,0,1},-spin*t);};
45 k.state={rotate(k.state.r),rotate(k.state.v)};k.acceleration=rotate(k.acceleration);return k;
46}
47inline Vec Bounded(Vec v,double bound) {const double n=Norm(v);return n>bound?v*(bound/n):v;}
48struct MainCommand {double lever=0;bool reverse=false;};
49inline MainCommand MainEngine(double acceleration,double forward,double reverse,double output,double reverser,
50 bool reverseCommand=false)
51{
52 // No force request is an idle, not a direction change. Keep the last
53 // bucket command through ALIGN, stale guidance and zero-demand crossings;
54 // retracting a loaded reverse bucket can produce an unwanted forward pulse.
55 if(acceleration==0) return {0,reverseCommand};
56 const bool wantsReverse=acceleration<0;
57 const double target=wantsReverse?1:0;
58 // Idle before moving the bucket, then wait for its two-second travel. Never
59 // demand forward power through a reverse bucket (or reverse through forward).
60 if(std::abs(reverser-target)>.001) return {0,output>.01?reverser>.5:wantsReverse};
61 const double authority=wantsReverse?reverse:forward;
62 return {authority>0?(std::clamp)(std::abs(acceleration)/authority,0.0,1.0):0,wantsReverse};
63}
64inline Vec TrackingAcceleration(const Input& in,const SurfacePlanning::Kinematic& reference,Vec integral)
65{
66 // 12 s velocity response and 30 s position response leave room for the
67 // finite main-engine spool. Integral learns small unmodeled aero forces.
68 return reference.acceleration+Bounded((reference.state.v-in.own.v)/12,.006)+
69 Bounded((reference.state.r-in.own.r)/900,.003)+integral;
70}
71inline Vec ProtectHandoffAltitude(const Input& in,const Plan& p,Vec acceleration)
72{
73 const double radius=Norm(in.own.r),altitude=radius-in.radius;
74 const Vec up=Unit(in.own.r);const double vertical=Dot(in.own.v,up);
75 const double curvature=(Dot(in.own.v,in.own.v)-vertical*vertical)/radius;
76 // Critically damp the remaining height with the existing 12 s velocity
77 // response. A 20 m numerical/actuator capture margin is small beside the
78 // 50 m handoff tolerance. This acts before reaching the selected altitude;
79 // it never clamps position or invents additional actuator authority.
80 const double clearance=altitude-p.entry.handoffAltitudeKm-(p.entry.handoffFpa<0?-.15:.02);
81 // Descending final handoff may cross the selected altitude; retain a
82 // bounded floor if speed/path capture fails instead of chasing it down.
83 if(p.entry.handoffFpa<0&&clearance>1) return acceleration;
84 const double capture=-clearance/(12*12)-2*vertical/12;
85 return acceleration+up*((std::max)(0.0,capture-Dot(acceleration,up)-curvature));
86}
87inline Vec EntryAcceleration(const Input& in,const Plan& p,double elapsed,
88 const SurfacePlanning::Kinematic& reference,Vec integral)
89{
90 Vec acceleration=TrackingAcceleration(in,reference,integral);
91 if(elapsed>=p.end-30) {
92 // The frozen endpoint contains the wind at planning time. Capture the
93 // selected TAS in today's wind, not that stale inertial speed. Keep the
94 // planned ground-track direction and the cross-track/height feedback.
95 const Vec up=Unit(in.own.r),rotation=Cross({0,0,in.spin},in.own.r);
96 const Vec ground=reference.state.v-Cross({0,0,in.spin},reference.state.r);
97 const Vec along=Unit(p.entry.handoffFpa<0?ground:ground-up*Dot(ground,up)),wind=EntryPhysics::Wind(in,in.own.r);
98 const double tailwind=Dot(wind,along),crosswind2=(std::max)(0.0,Dot(wind,wind)-tailwind*tailwind);
99 if(crosswind2<p.entry.handoffSpeedKms*p.entry.handoffSpeedKms) {
100 const double speed=tailwind+std::sqrt(p.entry.handoffSpeedKms*p.entry.handoffSpeedKms-crosswind2);
101 const double wanted=Dot(reference.acceleration,along)+Dot(rotation+along*speed-in.own.v,along)/12;
102 acceleration=acceleration+along*(wanted-Dot(acceleration,along));
103 }
104 if(p.entry.handoffFpa==0) {
105 // FINAL HOLD captures altitude; a runway handoff keeps descending.
106 const double radius=Norm(in.own.r),vertical=Dot(in.own.v,up);
107 const double curvature=(Dot(in.own.v,in.own.v)-vertical*vertical)/radius;
108 const double capture=-(radius-in.radius-p.entry.handoffAltitudeKm-.02)/(12*12)-2*vertical/12;
109 const double radial=(std::max)(-.003,capture)-curvature;
110 acceleration=acceleration+up*(radial-Dot(acceleration,up));
111 }
112 }
113 return p.entry.handoffFpa<0&&elapsed<p.end+1?acceleration:ProtectHandoffAltitude(in,p,acceleration);
114}
115inline Vec LimitForce(const Input& in,const EntryPhysics::Demand& d,bool measured=true)
116{
117 const double density=std::exp(-std::abs(Norm(in.own.r)-in.radius)/in.scaleHeight);
118 const double efficiency=(std::max)(.01,1+(in.seaLevelEfficiency-1)*density);
119 const double f=(std::clamp)(d.main,-1.0,1.0)*(d.main>=0?in.entryForwardKN:in.entryReverseKN)*efficiency;
120 const double u=(std::clamp)(d.normal,-1.0,1.0)*(d.normal>=0?in.entryUpKN:in.entryDownKN);
121 return EntryEnvelope::Limit(in,d,d.nose*f+d.up*u+d.side*((std::clamp)(d.lateral,-1.0,1.0)*in.entrySideKN),measured);
122}
123inline double SpeedHold(const Input& in,double desiredTas,double& integral,double dt,bool approach=false)
124{
125 const auto air=EntryPhysics::Atmosphere(in,in.own,in.entryVehicle.aoa,true);
126 double error=desiredTas-Norm(air.velocity);
127 integral=(std::clamp)(integral+error*(std::clamp)(dt,0.0,1.0)/600,-.002,.002);
129 const Vec natural=SurfacePlanning::Gravity(in.own.r,in.gm)+aero;
130 if(approach) {
131 // Start unloading reverse before final speed is reached. The measured
132 // force still acts during spool/bucket travel; ignoring it undershoots
133 // Vref and then demands a violent forward recovery.
134 const double actual=Dot(natural+in.entryVehicle.deliveredAcceleration,Unit(air.velocity));
135 error-=actual*(in.entryVehicle.spoolSeconds*.5+2);
136 }
137 double acceleration=error/(approach?6:15)+integral-Dot(natural,in.entryVehicle.nose);
139 acceleration=(std::min)(acceleration,-.003);integral=0;
140 }
141 const double efficiency=(std::max)(.01,1+(in.seaLevelEfficiency-1)*air.density);
142 if(approach) {
143 const Vec observed=in.entryVehicle.aeroValid?in.entryVehicle.aeroAcceleration:aero;
144 const double aeroFore=in.entryVehicle.loadValid?in.entryVehicle.bodyLoadG.z*FlightLoads::G-
147 acceleration=(std::clamp)(acceleration,-bound-aeroFore,bound-aeroFore);
148 }
149 return (std::clamp)(acceleration,-in.entryReverseKN*efficiency/in.massKg,in.entryForwardKN*efficiency/in.massKg);
150}
151inline Guidance Command(const Input& in,const Plan& p,double elapsed,Vec& integral,double dt,Protection* protection=nullptr)
152{
153 Guidance g;g.valid=true;g.entryControl=true;g.epoch=in.now;
155 const auto air=EntryPhysics::Atmosphere(in,in.own,EntryPhysics::EntryAero(in).angle,true);
156 const auto ref=InCurrentFrame(p,elapsed,in.spin,in.gm);
157 g.positionErrorKm=Norm(ref.state.r-in.own.r);g.entrySpeedError=Norm(air.velocity)-p.entry.handoffSpeedKms;
158 const Vec along=Unit(air.velocity),up=Unit(in.own.r);
159 g.pointing=along*std::cos(EntryPhysics::EntryAero(in).angle)+
160 Unit(up-along*Dot(up,along))*std::sin(EntryPhysics::EntryAero(in).angle);
162 if(elapsed<p.entry.coastSeconds) {
163 // Pre-align throughout the last 120 s of coast. No translational thrust.
164 if(p.entry.coastSeconds-elapsed>120) {g.phase=Phase::Wait;g.attitude=false;}
165 g.status=L"ENTRY ALIGN / COAST";return g;
166 }
167 integral=Bounded(integral+(ref.state.r-in.own.r)*(dt/60000),.0015);
168 Vec acceleration=EntryAcceleration(in,p,elapsed,ref,integral);
169 auto demand=EntryPhysics::Forces(in,in.own,acceleration,in.massKg,true);
170 g.accelerationCommand=LimitForce(in,demand)/in.massKg;
171 g.thrust=true;g.status=L"POWERED ENTRY";
172 if(Norm(in.own.r)-in.radius<100) g.phase=Phase::Arrive;
173 // Local protection predicts through engine response rather than reacting
174 // only after an IAS/AFT limit is crossed. The committed full profile was
175 // checked separately; this forecast starts from actual mass, state and AFT.
176 Input forecast=in;double heat=in.entryVehicle.aftC;
178 bool entered=elapsed>=p.entry.entrySeconds||Norm(in.own.r)-in.radius<OrbitalClearanceKm;
183 in.entryVehicle.nose,in.bodyUp));
187 constexpr double h=2;
188 for(int j=1;j<=24;++j) {
189 const auto future=InCurrentFrame(p,elapsed+j*h,in.spin,in.gm);
190 // Forecast vectors stay in the current inertial axes for this 48 s look-ahead.
191 auto reference=future;auto rotate=[&](Vec v){return Rotate(v,{0,0,1},in.spin*j*h);};
192 reference.state={rotate(future.state.r),rotate(future.state.v)};reference.acceleration=rotate(future.acceleration);
193 const auto d=EntryPhysics::Forces(forecast,forecast.own,EntryAcceleration(forecast,p,elapsed+j*h,reference,integral),forecast.massKg);
194 auto predicted=forecast;predicted.entryVehicle.aeroValid=false;
195 const Vec desired=LimitForce(predicted,d,false)/forecast.massKg;
196 delivered=delivered+Bounded(desired-delivered,in.entryForwardKN/in.massKg*h/(std::max)(1.0,in.entryVehicle.spoolSeconds));
197 const auto a=EntryPhysics::Atmosphere(forecast,forecast.own,EntryPhysics::EntryAero(in).angle);
198 heat=EntryPhysics::Heat(in.entryVehicle,a,heat,h);
199 g.entryForecastAftC=(std::max)(g.entryForecastAftC,heat);
201 const Vec total=SurfacePlanning::Gravity(forecast.own.r,in.gm)+delivered+
202 EntryPhysics::AerodynamicForce(forecast,a,EntryPhysics::EntryAero(in),forecast.own.r)/forecast.massKg;
203 g.entryForecastLoadFraction=(std::max)(g.entryForecastLoadFraction,EntryEnvelope::Fraction(forecast,forecast.own,total,d));
204 forecast.own.r=forecast.own.r+forecast.own.v*h+total*(h*h/2);forecast.own.v=forecast.own.v+total*h;
205 entered=entered||(Norm(forecast.own.r)-in.radius<OrbitalClearanceKm&&Dot(forecast.own.v,Unit(forecast.own.r))<0);
206 // Do not classify endpoint capture after the planned handoff as an
207 // entry skip. Heat and load forecasts remain active if capture is late.
208 if(elapsed+j*h<=p.end) g.entrySkipRisk=g.entrySkipRisk||EntryEnvelope::Skip(forecast.own,in.radius,entered);
209 }
211 // Prefer losing the requested time over driving through an envelope.
212 // Brake nose-first with reverse mains and arrest descent with bounded
213 // normal thrust. Do not flip tail-first or drop control into a hot coast.
214 const double radial=Dot(in.own.v,up);
215 acceleration=ProtectHandoffAltitude(in,p,along*(-in.entryReverseKN/in.massKg*.9)+up*((std::max)(0.0,-radial)/20));
216 demand=EntryPhysics::Forces(in,in.own,acceleration,in.massKg,true);
217 g.accelerationCommand=LimitForce(in,demand)/in.massKg;
218 g.entryLimited=true;g.status=L"ENTRY LIMIT / BRAKING";integral={};
219 }
220 if(protection) g.entrySkipRisk=Protection::Track(g.entrySkipRisk,Dot(in.own.v,up)<=.001,
221 protection->skip,protection->skipClear,dt);
222 if(g.entrySkipRisk) {
223 // Keep the existing nose-into-flow attitude. Reduce excess vertical
224 // lift with bounded translation while the speed/heat guard may brake.
225 const double vertical=Dot(in.own.v,up);
226 const double curvature=(Dot(in.own.v,in.own.v)-vertical*vertical)/Norm(in.own.r);
229 Vec total=SurfacePlanning::Gravity(in.own.r,in.gm)+specific;
230 // A forecast climb must not suppress the lift needed to arrest an
231 // existing sink. Dampen toward a small descent through engine response;
232 // request downward acceleration only as the vertical state permits.
233 const double desired=-curvature+(-.005-vertical)/20;
234 total=total+up*((std::min)(0.0,desired-Dot(total,up)));
235 // Skip prevention cannot trade an altitude-capture undershoot for a
236 // smaller predicted climb. Retain the bounded sink-arrest floor.
237 if(p.entry.handoffFpa==0||elapsed>=p.end+1) total=ProtectHandoffAltitude(in,p,total);
238 demand=EntryPhysics::Forces(in,in.own,total,in.massKg,true);
239 g.accelerationCommand=LimitForce(in,demand)/in.massKg;
240 g.entryLimited=true;g.status=L"SKIP LIMIT / RECOVER";integral={};
241 }
243 if(protection) loadLimited=Protection::Track(loadLimited,g.entryForecastLoadFraction<.90,
244 protection->load,protection->loadClear,dt);
245 if(loadLimited) {
246 g.entryLimited=true;g.status=L"LOAD LIMIT / RECOVER";integral={};
247 }
248 if(EntryProfile::Coasting(p.entry,elapsed)&&!g.entryLimited) {
249 g.status=L"ENTRY COAST / TRIM";
250 // Idle within a small tracking corridor; otherwise retain bounded trim.
251 // Pre-align for capture just as for the initial deorbit burn. Envelope
252 // protection above remains active even while the pilot can look around.
253 if(g.positionErrorKm<.5&&Norm(ref.state.v-in.own.v)<.005&&
255 g.thrust=false;g.accelerationCommand={};integral={};
256 g.status=L"ENTRY TRANSFER COAST";
257 if(p.entry.transferCoastEnd-elapsed>120) {g.phase=Phase::Coast;g.attitude=false;}
258 }
259 }
260 const double altitude=Norm(in.own.r)-in.radius;
261 const bool descending=p.entry.handoffFpa<0;
262 const double referenceVertical=Dot(ref.state.v,Unit(ref.state.r));
263 if(elapsed>=p.end-10&&std::abs(altitude-p.entry.handoffAltitudeKm)<(descending?.1:.05)&&
264 std::abs(g.entrySpeedError)<.005&&
265 (descending?Dot(in.own.v,up)<0&&std::abs(Dot(in.own.v,up)-referenceVertical)<.01:std::abs(Dot(in.own.v,up))<.001)&&
266 g.positionErrorKm<5&&
267 !g.entryLimited&&Angle(in.entryVehicle.nose,g.pointing)<5*Pi/180) {
269 g.status=descending?L"ENTRY COMPLETE / FPA NAV SPD":L"ENTRY COMPLETE / ALT HDG SPD";
270 }
271 return g;
272}
273}}
constexpr double Fore
Definition FlightLoads.h:13
constexpr double G
Definition FlightLoads.h:13
Vec Bounded(Vec v, double bound)
Guidance Command(const Input &in, const Plan &p, double elapsed, Vec &integral, double dt, Protection *protection=nullptr)
double SpeedHold(const Input &in, double desiredTas, double &integral, double dt, bool approach=false)
SurfacePlanning::Kinematic InCurrentFrame(const Plan &p, double t, double spin, double gm)
Vec LimitForce(const Input &in, const EntryPhysics::Demand &d, bool measured=true)
Vec EntryAcceleration(const Input &in, const Plan &p, double elapsed, const SurfacePlanning::Kinematic &reference, Vec integral)
MainCommand MainEngine(double acceleration, double forward, double reverse, double output, double reverser, bool reverseCommand=false)
Vec ProtectHandoffAltitude(const Input &in, const Plan &p, Vec acceleration)
Vec TrackingAcceleration(const Input &in, const SurfacePlanning::Kinematic &reference, Vec integral)
SurfacePlanning::Kinematic Reference(const Plan &p, double t, double spin, double gm)
double Fraction(const Input &in, Vec g)
constexpr double Reserve
bool Skip(State state, double radius, bool entered)
Vec Limit(const Input &in, const EntryPhysics::Demand &d, Vec force, bool measured=true)
Vec BodyG(Vec specific, Vec nose, Vec up)
Vec AerodynamicForce(const Input &in, const Air &air, const AeroPoint &aero, Vec radial)
Vec Wind(const Input &in, Vec position)
AeroPoint AeroAt(const Input &in, double angle)
constexpr double IasReserve
constexpr double AftLimitC
Air Atmosphere(const Input &in, State state, double angle=0, bool measured=false)
Demand Forces(const Input &in, State state, Vec acceleration, double mass, bool measured=false)
const AeroPoint & EntryAero(const Input &in)
double Heat(const EntryVehicle &v, const Air &air, double aftC, double seconds)
SurfacePlanning::Kinematic PoweredReference(const Plan &p, double t, double gm)
bool Coasting(const EntryEstimate &e, double seconds)
Vec Gravity(Vec r, double gm)
Definition OrbitProfile.h:5
double Angle(Vec a, Vec b)
Definition OrbitMath.h:21
Vec Unit(Vec a)
Definition OrbitMath.h:20
constexpr double OrbitalClearanceKm
Definition OrbitState.h:38
Vec Rotate(Vec v, Vec axis, double angle)
Definition OrbitMath.h:22
double Dot(Vec a, Vec b)
Definition OrbitMath.h:16
constexpr double Pi
Definition OrbitMath.h:10
double Norm(Vec a)
Definition OrbitMath.h:18
bool Propagate(State initial, double t, double gm, State &result)
Definition OrbitMath.h:67
Vec Cross(Vec a, Vec b)
Definition OrbitMath.h:17
bool reverse
double lever
double loadClear
bool skip
static bool Track(bool risk, bool recovered, bool &active, double &clear, double dt)
double skipClear
bool load
double transferCoastEnd
Definition OrbitState.h:207
double handoffAltitudeKm
Definition OrbitState.h:196
double entrySeconds
Definition OrbitState.h:195
double handoffSpeedKms
Definition OrbitState.h:196
double handoffFpa
Definition OrbitState.h:197
double coastSeconds
Definition OrbitState.h:195
double main
double normal
Vec up
double lateral
Vec nose
Vec side
double aftC
Definition OrbitState.h:86
double aoa
Definition OrbitState.h:88
double spoolSeconds
Definition OrbitState.h:87
bool loadValid
Definition OrbitState.h:90
Vec bodyLoadG
Definition OrbitState.h:91
bool aeroValid
Definition OrbitState.h:96
Vec nose
Definition OrbitState.h:92
Vec deliveredAcceleration
Definition OrbitState.h:92
Vec aeroAcceleration
Definition OrbitState.h:97
double entryHandoffSpeedKms
Definition OrbitState.h:310
std::wstring status
Definition OrbitState.h:315
double entryHandoffAltitudeKm
Definition OrbitState.h:310
double entryForecastLoadFraction
Definition OrbitState.h:312
double entryForecastAftC
Definition OrbitState.h:314
double entryForecastIasFraction
Definition OrbitState.h:314
EntryVehicle entryVehicle
Definition OrbitState.h:123
double entrySideKN
Definition OrbitState.h:111
double entryReverseKN
Definition OrbitState.h:111
double entryDownKN
Definition OrbitState.h:111
double seaLevelEfficiency
Definition OrbitState.h:109
double scaleHeight
Definition OrbitState.h:101
double entryForwardKN
Definition OrbitState.h:111
EntryEstimate entry
Definition OrbitState.h:233
ArrivalPreview arrival
Definition OrbitState.h:235