Rise
The Vieneo Province
Loading...
Searching...
No Matches
AfcsControl.h
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <cmath>
4namespace AfcsControl {
5inline float Clamp(float v,float a,float b) {return (std::clamp)(v,a,b);}
6inline float Lerp(float a,float b,float t) {return a+(b-a)*t;}
7inline float Radians(float degrees) {return degrees*0.01745329251994329577f;}
8// Existing Rise PIT controller shared with the independent ascent plant.
9// qz uses the client's Vieneo aerodynamic model; these are existing AFCS gains.
11 bool apWasEngaged=false;
14 void Reset() {*this={};}
15 Result Step(float pitchAtt,float desiredPitch,float stick,float qz,float f_augment,float elapsed) {
16 const float dt=Clamp(elapsed,1.0f/240.0f,1.0f/20.0f);
17 const float pitchError = desiredPitch - pitchAtt;
18 const float rcsAuth = Clamp(f_augment, 0.0f, 1.0f);
19 const float qRef = 60.0f;
20 const float aeroAuth = Clamp(qz / qRef, 0.0f, 1.0f);
21 const float auth = Clamp(aeroAuth + rcsAuth, 0.0f, 1.0f);
22 if (!apWasEngaged)
23 {
24 pitchRateFilt = 0.0f;
25 rateI = Clamp(stick, -0.8f, 0.8f);
26 apPitchLastInput = stick;
27 apPitchLastAttitude = pitchAtt;
28 }
29 apWasEngaged = true;
30 float pitchRateRaw = (pitchAtt - apPitchLastAttitude) / dt; // rad/sec
31 // Filter: slightly faster with aero authority
32 const float tauRate = Lerp(0.16f, 0.10f, aeroAuth);
33 pitchRateFilt += (pitchRateRaw - pitchRateFilt) * (dt / (tauRate + dt));
34 const float maxRateFar = Radians(2.5f); // rad/sec
35 const float maxRateNear = Radians(0.6f);
36 const float errForFull = Radians(5.0f);
37 float rateScale = Clamp(fabsf(pitchError) / errForFull, 0.0f, 1.0f);
38 float maxRate = Lerp(maxRateNear, maxRateFar, rateScale);
39 // "Response time" behavior via proportional mapping (more stable than pitch-error integral here)
40 const float outerGain = 0.65f; // (rad/sec) per rad -> approx 1.5s time constant
41 float desiredRate = Clamp(pitchError * outerGain, -maxRate, maxRate);
42 float rateError = desiredRate - pitchRateFilt; // rad/sec
43 // Normalize rate error so gains are intuitive (unitless)
44 float rateErrNorm = (maxRateFar > 1e-6f) ? (rateError / maxRateFar) : 0.0f;
45 // Proportional gain in stick per (normalized rate error)
46 const float KpStick = Lerp(0.35f, 0.90f, auth);
47 // Integrator gain in stick per second per (normalized rate error)
48 // Lower with more authority to avoid "stiff" oscillation
49 const float KiStick = Lerp(0.22f, 0.08f, auth);
50 // P term
51 float uP = KpStick * rateErrNorm;
52 // Pre-sat command
53 float uUnsat = uP + rateI;
54 // Saturate to stick limits
55 float uSat = Clamp(uUnsat, -1.0f, 1.0f);
56 // --- Anti-windup: only integrate if not saturated, or if error drives OUT of saturation ---
57 bool saturated = (uSat != uUnsat);
58 bool drivesOut =
59 (!saturated) ||
60 ((uSat >= 1.0f) && (rateErrNorm < 0.0f)) ||
61 ((uSat <= -1.0f) && (rateErrNorm > 0.0f));
62 if (drivesOut)
63 {
64 rateI += KiStick * rateErrNorm * dt;
65 rateI = Clamp(rateI, -0.8f, 0.8f);
66 }
67 // Recompute after I update (optional but keeps logging truthful)
68 uUnsat = uP + rateI;
69 uSat = Clamp(uUnsat, -1.0f, 1.0f);
70 saturated = (uSat != uUnsat);
71 const float maxSlewPerSec = 0.45f;
72 float maxStep = maxSlewPerSec * dt;
73 float delta = Clamp(uSat - apPitchLastInput, -maxStep, maxStep);
74 stick = apPitchLastInput + delta;
75 // Save state
76 apPitchLastInput = stick;
77 apPitchLastAttitude = pitchAtt;
78 return {stick,pitchError,desiredRate,rateError,uP,uUnsat,uSat,KpStick,KiStick,maxRate,saturated};
79 }
80};
81inline float RollInput(float desired,float actual,float rate,float previous,float aggression,float weight,float dt) {
82 const float desiredRate=Clamp((desired-actual)/3,-Radians(10),Radians(10));
83 const float correction=(desiredRate-rate)*3*aggression*weight;
84 return Clamp(previous+Clamp(correction-previous,-.25f*dt,.25f*dt),-1,1);
85}
86}
float Radians(float degrees)
Definition AfcsControl.h:7
float Clamp(float v, float a, float b)
Definition AfcsControl.h:5
float RollInput(float desired, float actual, float rate, float previous, float aggression, float weight, float dt)
Definition AfcsControl.h:81
float Lerp(float a, float b, float t)
Definition AfcsControl.h:6
Result Step(float pitchAtt, float desiredPitch, float stick, float qz, float f_augment, float elapsed)
Definition AfcsControl.h:15