@@ -27,81 +27,62 @@ void ASTRAv2_Controller_reset() {
2727 lastAttError = Vector3::Zero ();
2828}
2929
30- Vector4 ASTRAv2_Controller (Vector3 PosTarget, Vector16 X, t_constantsASTRA constantsASTRA, float dT) {
30+ Vector4 ASTRAv2_Controller (Vector3 PosTarget, Vector16 X, constantsASTRA_t constantsASTRA, float dT) {
3131 // Controller Limits
3232 float thrustMax = 1.5 * 9.8 ; // N
3333 float gimbalMax = M_PI / 18 ;
34-
35- Vector4 uMin;
36- uMin << -gimbalMax, -gimbalMax, 0.4 * thrustMax, -M_PI / 6 ;
37- Vector4 uMax;
38- uMax << gimbalMax, gimbalMax, thrustMax, M_PI / 6 ;
39-
34+ Vector4 uMin = (Vector4 () << -gimbalMax, -gimbalMax, 0.4 * thrustMax, -M_PI / 6 ).finished ();
35+ Vector4 uMax = (Vector4 () << gimbalMax, gimbalMax, thrustMax, M_PI / 6 ).finished ();
4036 Vector4 U = Vector4::Zero ();
4137
42- // // First Loop (P Loop)
38+ // First Loop (P Loop)
4339 // Position Error Vector
4440 Vector3 PosError = PosTarget - X.segment <3 >(4 );
4541
4642 // Velocity Command
47- Vector3 K_P;
48- K_P << 0.5 , 0.5 , 0.65 ;
43+ Vector3 K_P = (Vector3 () << 0.58 , 0.58 , 0.65 ).finished ();
4944 Vector3 VelTarget = K_P.cwiseProduct (PosError);
5045
5146 // Velocity Saturation Step
52- Vector3 MaxVel;
53- MaxVel << 1 , 1 , 1.5 ;
47+ Vector3 MaxVel = (Vector3 () << 1 , 1 , 1.5 ).finished ();
5448 VelTarget = VelTarget.cwiseMin (MaxVel).cwiseMax (-MaxVel);
5549
56- // // Second Loop (PI Loop)
50+ // Second Loop (PI Loop)
5751 // Velocity Error Vector
5852 Vector3 VelError = VelTarget - X.segment <3 >(7 );
5953
6054 // Integral Accumulator
61- Vector3 K_I;
62- K_I << 1.5 , 1.5 , 5 ;
63- float Leak = 0.10 ;
64- Vector3 Clamp;
65- Clamp << 1 , 1 , 2 ;
55+ Vector3 K_I = (Vector3 () << 2 , 2 , 5 ).finished ();
56+ float Leak = 0.35 ;
57+ Vector3 Clamp = (Vector3 () << 5 , 5 , 5 ).finished ();
6658
6759 // Normalize errors (0 to 1 scale)
68- Vector3 MaxAttError;
69- MaxAttError << 0.07 , 0.07 , 0.3 ;
70- Vector3 MaxVelError;
71- MaxVelError << 0.6 , 0.6 , 0.4 ;
72- Vector3 MaxRateError;
73- MaxRateError << M_PI / 5 , M_PI / 5 , M_PI / 3 ;
60+ Vector3 MaxAttError = (Vector3 () << 0.05 , 0.05 , 0.3 ).finished ();
61+ Vector3 MaxVelError = (Vector3 () << 0.3 , 0.3 , 0.3 ).finished ();
7462 Vector3 NormAttErr = lastAttError.cwiseAbs ().cwiseQuotient (MaxAttError);
7563 Vector3 NormVelErr = VelError.cwiseAbs ().cwiseQuotient (MaxVelError);
76- Vector3 NormRateErr = X.segment <3 >(10 ).cwiseAbs ().cwiseQuotient (MaxRateError);
7764
7865 // Combine errors (Vector magnitude)
79- Vector3 TotalErrorMetric = NormAttErr + NormVelErr + NormRateErr ;
66+ Vector3 TotalErrorMetric = NormAttErr + NormVelErr;
8067
8168 // Calculate Gate using Gaussian function
82- Vector3 LeakVec3;
83- LeakVec3 << Leak, Leak, Leak;
84- Vector3 Gate = (1 - Leak) * (-2 * TotalErrorMetric.cwiseAbs2 ()).array ().exp ().matrix () + LeakVec3;
69+ Vector3 Gate = (1 - Leak) * (-2 * TotalErrorMetric.cwiseAbs2 ()).array ().exp ().matrix () + (Vector3 () << Leak, Leak, Leak).finished ();
8570
8671 // Integrator
8772 K_I = K_I.cwiseProduct (Gate);
8873 VelErrorI = VelErrorI + K_I.cwiseProduct (VelError) * dT;
8974 VelErrorI = VelErrorI.cwiseMin (Clamp).cwiseMax (-Clamp);
90- K_P << 2.2 , 2.2 , 3.5 ;
75+ K_P = ( Vector3 () << 3.1 , 3.1 , 5 ). finished () ;
9176
9277 // Acceleration Target
93- Vector3 g_vec;
94- g_vec << 0 , 0 , constantsASTRA.g ;
95- Vector3 AccelTarget = K_P.cwiseProduct (VelError) + VelErrorI + g_vec;
78+ Vector3 AccelTarget = K_P.cwiseProduct (VelError) + VelErrorI + (Vector3 () << 0 , 0 , constantsASTRA.g ).finished ();
9679
9780 // Acceleration Saturation Step
98- Vector3 MaxAccelUp;
99- MaxAccelUp << 2 , 2 , 15 ;
100- Vector3 MaxAccelDown;
101- MaxAccelDown << -2 , -2 , 4 ;
81+ Vector3 MaxAccelUp = (Vector3 () << 2 , 2 , 15 ).finished ();
82+ Vector3 MaxAccelDown = (Vector3 () << -2 , -2 , 4 ).finished ();
10283 AccelTarget = AccelTarget.cwiseMin (MaxAccelUp).cwiseMax (MaxAccelDown);
10384
104- // // Kinematics Step
85+ // Kinematics Step
10586 // Compute thrust target
10687 Vector3 TargetForce_I = constantsASTRA.m * AccelTarget;
10788 U[2 ] = TargetForce_I.norm ();
@@ -111,10 +92,9 @@ Vector4 ASTRAv2_Controller(Vector3 PosTarget, Vector16 X, t_constantsASTRA const
11192 Vector3 Z_b = AccelTarget / AccelTarget.norm ();
11293
11394 // Heading reference (+X axis rolled to north)
114- Vector3 HDGRef;
115- HDGRef << 1 , 0 , 0 ;
95+ Vector3 HDGRef = (Vector3 () << 1 , 0 , 0 ).finished ();
11696 Vector3 Y_b = Z_b.cross (HDGRef);
117- Y_b = Y_b / Y_b. norm ();
97+ Y_b. normalize ();
11898
11999 // Complete the triad
120100 Vector3 X_b = Y_b.cross (Z_b);
@@ -124,24 +104,22 @@ Vector4 ASTRAv2_Controller(Vector3 PosTarget, Vector16 X, t_constantsASTRA const
124104 DCM << X_b[0 ], Y_b[0 ], Z_b[0 ], X_b[1 ], Y_b[1 ], Z_b[1 ], X_b[2 ], Y_b[2 ], Z_b[2 ];
125105 Vector4 TargetAtt = DCM_Quat_Conversion (DCM);
126106
127- // // Third Loop (LQRi)
107+ // Third Loop (LQRi)
128108 // Attitude Error computation
129- Vector4 Q_Conj;
130- Q_Conj << X[0 ], -X.segment <3 >(1 );
109+ Vector4 Q_Conj = (Vector4 () << X[0 ], -X.segment <3 >(1 )).finished ();
131110 Vector4 AttError = HamiltonianProd (Q_Conj) * TargetAtt;
132111 if (AttError[0 ] < 0 ) {
133112 AttError = -AttError;
134113 }
135114 lastAttError = AttError.segment <3 >(1 );
136115
137116 // Error accumulation and clamping
138- Clamp << 3 , 3 , 0.4 ;
117+ Clamp = ( Vector3 () << 3 , 3 , 0.4 ). finished () ;
139118 AttErrorI = AttErrorI + AttError.segment <3 >(1 ) * dT;
140119 AttErrorI = AttErrorI.cwiseMin (Clamp).cwiseMax (-Clamp);
141120
142121 // State vector and error
143- Vector9 X_Err;
144- X_Err << -AttError.segment <3 >(1 ), X.segment <3 >(10 ), AttErrorI;
122+ Vector9 X_Err = (Vector9 () << -AttError.segment <3 >(1 ), X.segment <3 >(10 ), AttErrorI).finished ();
145123
146124 // LQR Controller
147125 Vector3 u_components = -constantsASTRA.K_Att * X_Err;
@@ -150,8 +128,7 @@ Vector4 ASTRAv2_Controller(Vector3 PosTarget, Vector16 X, t_constantsASTRA const
150128 // U[2] is set above
151129 U[3 ] = u_components[2 ];
152130
153- // // Controls Saturation
154- U = U.cwiseMin (uMax).cwiseMax (uMin);
155-
131+ // Controls Saturation
132+ U = U.cwiseMax (uMin).cwiseMin (uMax);
156133 return U;
157134}
0 commit comments