Rise
The Vieneo Province
Loading...
Searching...
No Matches
OrbitAttitude.h
Go to the documentation of this file.
1#pragma once
2#include "OrbitState.h"
3
4namespace Orbital
5{
6// Existing Rise rotational RCS force limits (propulsion's legacy force units).
7// These are shared with the physical actuators, not new spacecraft performance.
8constexpr double PitchRcsForce=870*134, YawRcsForce=870*200, RollRcsForce=870*60;
9struct BodyFrame { Vec right{1,0,0},up{0,1,0},forward{0,0,1}; };
10inline Vec InBody(const BodyFrame& f,Vec v) { return {Dot(f.right,v),Dot(f.up,v),Dot(f.forward,v)}; }
11inline bool ValidFrame(const BodyFrame& f)
12{
13 return Finite(f.right)&&Finite(f.up)&&Finite(f.forward)&&
14 std::abs(Norm(f.right)-1)<.01&&std::abs(Norm(f.up)-1)<.01&&
15 std::abs(Norm(f.forward)-1)<.01&&Norm(Cross(f.right,f.up)-f.forward)<.01;
16}
17// Rotation logarithm, including the 180-degree case where a cross-product-only
18// controller has zero error. Result is a right-handed rotation in body axes.
19inline Vec FrameError(const BodyFrame& current,const BodyFrame& target)
20{
21 const Vec x=InBody(current,target.right),y=InBody(current,target.up),z=InBody(current,target.forward);
22 double qw,qx,qy,qz;
23 const double trace=x.x+y.y+z.z;
24 if(trace>0) { const double s=2*std::sqrt(1+trace); qw=s/4; qx=(y.z-z.y)/s; qy=(z.x-x.z)/s; qz=(x.y-y.x)/s; }
25 else if(x.x>y.y&&x.x>z.z) { const double s=2*std::sqrt((std::max)(0.0,1+x.x-y.y-z.z)); qw=(y.z-z.y)/s; qx=s/4; qy=(y.x+x.y)/s; qz=(z.x+x.z)/s; }
26 else if(y.y>z.z) { const double s=2*std::sqrt((std::max)(0.0,1+y.y-x.x-z.z)); qw=(z.x-x.z)/s; qx=(y.x+x.y)/s; qy=s/4; qz=(z.y+y.z)/s; }
27 else { const double s=2*std::sqrt((std::max)(0.0,1+z.z-x.x-y.y)); qw=(x.y-y.x)/s; qx=(z.x+x.z)/s; qy=(z.y+y.z)/s; qz=s/4; }
28 Vec q{qx,qy,qz}; if(qw<0) { qw=-qw; q=q*(-1); }
29 const double n=Norm(q); return n>1e-10?q*(2*std::atan2(n,qw)/n):Vec{};
30}
32{
34 Vec rates, acceleration; // actual right-handed body rates and available rad/s2
35 double effort=0; // installed RCS health * electrical power, [0,1]
36};
37inline Vec RotationAuthority(double weight,double pitchInertia,double yawInertia,double rollInertia,
38 double arm,double span,double leftWing,double rightWing,double effort,double wear)
39{
40 if(!(weight>0&&pitchInertia>0&&yawInertia>0&&rollInertia>0)) return {};
41 const double factor=effort*wear/weight;
42 return {PitchRcsForce*arm*factor/pitchInertia,YawRcsForce*arm*factor/yawInertia,
43 RollRcsForce*span*.5*(leftWing+rightWing)*factor/rollInertia};
44}
45inline Vec RotationCommand(const BusState& data,double now,double available=1)
46{
47 const auto& g=data.guidance;
48 if(!g.valid||!g.attitude||now<data.sourceTime||now-data.sourceTime>=.5||
49 !Finite(g.rotationCommand)||!std::isfinite(available)) return {};
50 available=(std::clamp)(available,0.0,1.0);
51 return {(std::clamp)(g.rotationCommand.x,-available,available),
52 (std::clamp)(g.rotationCommand.y,-available,available),(std::clamp)(g.rotationCommand.z,-available,available)};
53}
54inline Vec HorizonRotation(State own,double spin)
55{
56 // Parallel-transport the local vertical along the measured path, including
57 // planetary rotation. No forced heading, bank, or pitch for free coast.
58 const double rr=Dot(own.r,own.r);
59 return rr>1&&Finite(own.r)&&Finite(own.v)&&std::isfinite(spin)?
60 Cross(own.r,own.v-Cross({0,0,spin},own.r))/rr:Vec{};
61}
62inline Vec CoastRotationCommand(const BusState& data,double now,const RotationState& in,Vec pilot)
63{
64 const auto& g=data.guidance;
65 if(!g.valid||(g.phase!=Phase::Wait&&g.phase!=Phase::Coast)||now<data.sourceTime||now-data.sourceTime>=.5||
66 !Finite(g.coastRotation)||!Finite(pilot)||!ValidFrame(in.frame)||!Finite(in.rates)||
67 !Finite(in.acceleration)||!(in.effort>0&&in.acceleration.x>0&&in.acceleration.y>0&&in.acceleration.z>0)) return {};
68 // Same manual rate scale as Rise's augmentation (limiterC = 30 deg/s).
69 // With the controls centered, follow curvature rather than damping to an
70 // inertially fixed attitude. Pilot commands are offsets from that rate.
71 const Vec wanted=InBody(in.frame,g.coastRotation)+Vec{-pilot.x,pilot.y,-pilot.z}*(Pi/6);
72 const Vec error=wanted-in.rates;
73 auto effort=[&](double error,double acceleration) { return (std::clamp)(error/(.5*acceleration),-1.0,1.0)*in.effort; };
74 return {-effort(error.x,in.acceleration.x),effort(error.y,in.acceleration.y),-effort(error.z,in.acceleration.z)};
75}
77{
78 bool initialized=false,ready=false,vertical=false;
80 double settled=0;
82public:
83 void Reset() { *this={}; }
84 // Rise control policy: gentle 5 deg/s slew, 2 deg / 0.5 deg/s capture,
85 // 5 deg / 2 deg/s release. Physical authority still comes from the vehicle.
86 static constexpr double SlewRate=5*Pi/180;
87 bool Step(Guidance& g,const RotationState& in,Vec radial,double dt)
88 {
90 if(!g.valid) { Reset(); return true; }
91 // Ordinary AFCS owns the control surfaces and its IAS-faded augmentation.
92 // Never mix a wings-level orbital yaw demand into a banked HDG turn.
93 if(g.ascentControl&&g.ascentAircraft) { Reset();return true; }
94 if(!ValidFrame(in.frame)||!Finite(in.rates)||!Finite(in.acceleration)||
95 !(in.acceleration.x>0&&in.acceleration.y>0&&in.acceleration.z>0&&in.effort>0)||
96 !Finite(radial)||Norm(radial)<.5||!std::isfinite(dt)||dt<=0) return false;
97 dt=(std::min)(dt,1.0);
99 // The pilot may look around while the route remains armed. Start a
100 // fresh alignment only when the executor requests another burn.
101 Reset(); g.attitude=false; g.thrust=false; return true;
102 }
103 // Do not chase a noisy near-zero terminal velocity vector through a
104 // half-turn. Small trims use the existing bounded translation controls.
106 Vec requested=Norm(g.pointing)>.5?Unit(g.pointing):(initialized?nose:in.frame.forward);
107 if(trim) requested=nose;
108 if(!initialized||Angle(nose,requested)>5*Pi/180) { ready=false; settled=0; }
109 nose=requested; previous=g.phase;
110 const Vec radialUp=Unit(radial),projected=radialUp-nose*Dot(radialUp,nose);
111 const double levelLength=Norm(projected);
112 vertical=vertical?levelLength<.2:levelLength<.1;
113 Vec up=vertical?(initialized?levelUp:in.frame.up):projected;
114 up=up-nose*Dot(up,nose);
115 if(Norm(up)<.01) up=in.frame.right-nose*Dot(in.frame.right,nose);
116 BodyFrame desired; desired.forward=nose; desired.right=Unit(Cross(up,nose));
117 desired.up=Cross(nose,desired.right); levelUp=desired.up; initialized=true;
118 const Vec error=FrameError(in.frame,desired);
119 const double angle=Norm(error),rate=Norm(in.rates);
120 // Entry allocates continuous force in the measured body frame. Once
121 // captured, a small correcting rotation must not interrupt braking
122 // merely because it crosses the impulsive burn's 2 deg/s rate gate.
123 // Initial capture and the 5-degree attitude release remain mandatory.
124 if(angle>5*Pi/180||rate>(g.entryControl?SlewRate:2*Pi/180)) { ready=false; settled=0; }
125 if(angle<2*Pi/180&&rate<.5*Pi/180) settled+=dt; else settled=0;
126 if(settled>=.5) ready=true;
127 auto demand=[&](double e,double w,double a) {
128 const double desiredRate=std::copysign((std::min)({SlewRate,1.5*std::abs(e),.6*std::sqrt(2*a*std::abs(e))}),e);
129 return (std::clamp)((desiredRate-w)/((std::max)(.5,2*dt)*a),-1.0,1.0)*in.effort;
130 };
131 // Rise stores inverse-orientation rates; positive yaw torque subtracts
132 // yawRadPerSec, whereas positive pitch/roll torque adds their rates.
133 g.rotationCommand={-demand(error.x,in.rates.x,in.acceleration.x),
134 demand(error.y,in.rates.y,in.acceleration.y),-demand(error.z,in.rates.z,in.acceleration.z)};
135 g.pointing=nose; g.attitude=true;
136 if(!ready&&!g.ascentControl) {
137 const double a=(std::min)({in.acceleration.x,in.acceleration.y,in.acceleration.z});
138 g.alignmentSeconds=angle/SlewRate+2*std::sqrt(angle/a)+rate/a+2;
139 g.phase=Phase::Align; g.status=L"DOR ALIGN"; g.thrust=false;
140 }
141 return true;
142 }
143};
144}
static constexpr double SlewRate
bool Step(Guidance &g, const RotationState &in, Vec radial, double dt)
constexpr double YawRcsForce
double Angle(Vec a, Vec b)
Definition OrbitMath.h:21
Vec CoastRotationCommand(const BusState &data, double now, const RotationState &in, Vec pilot)
Vec HorizonRotation(State own, double spin)
Vec Unit(Vec a)
Definition OrbitMath.h:20
Vec FrameError(const BodyFrame &current, const BodyFrame &target)
Vec InBody(const BodyFrame &f, Vec v)
double Dot(Vec a, Vec b)
Definition OrbitMath.h:16
constexpr double RollRcsForce
constexpr double PitchRcsForce
constexpr double Pi
Definition OrbitMath.h:10
bool ValidFrame(const BodyFrame &f)
Vec RotationAuthority(double weight, double pitchInertia, double yawInertia, double rollInertia, double arm, double span, double leftWing, double rightWing, double effort, double wear)
double Norm(Vec a)
Definition OrbitMath.h:18
Vec RotationCommand(const BusState &data, double now, double available=1)
Vec Cross(Vec a, Vec b)
Definition OrbitMath.h:17
bool Finite(Vec a)
Definition OrbitMath.h:19
double alignmentSeconds
Definition OrbitState.h:303
std::wstring status
Definition OrbitState.h:315