Skip to content

Commit 44f08dc

Browse files
committed
rename controller to controller_and_estimator, add missing reset, Vec13->19
1 parent 7144fb3 commit 44f08dc

File tree

5 files changed

+20
-43
lines changed

5 files changed

+20
-43
lines changed

lib/controller/controller.cpp renamed to lib/controller/controller_and_estimator.cpp

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#include "controller.h"
1+
#include "controller_and_estimator.h"
22
#include "matlab_funcs.h"
33

4-
namespace Controller {
4+
namespace ControllerAndEstimator {
55
constantsASTRA_t constantsASTRA;
66
Matrix18_18 P;
77
Matrix9_9 Flight_P;
8-
Vector13 x_est;
8+
Vector19 x_est;
99
Vector3 lastEMA;
1010
Matrix9_4 dnf_X;
1111
Matrix9_4 dnf_Y;
@@ -14,17 +14,13 @@ bool last_GND;
1414

1515
unsigned long last_call_time; // ms
1616

17-
void begin() {
18-
reset_controller_state();
19-
}
20-
21-
void reset_controller_state() {
17+
void init_controller_and_estimator_constants() {
2218
constantsASTRA.g = 9.8015;
2319
constantsASTRA.m = 1.2490;
2420
constantsASTRA.mag << -0.4512, 0, 0.8924;
25-
constantsASTRA.Q << 2.500083e-06, 0, 0, 0, 0, 0, 0, 0, 0, -2.500000e-07, 0, 0, 0, 0, 0, 0, 0, 0, //
26-
0, 2.500083e-06, 0, 0, 0, 0, 0, 0, 0, 0, -2.500000e-07, 0, 0, 0, 0, 0, 0, 0, //
27-
0, 0, 2.500083e-06, 0, 0, 0, 0, 0, 0, 0, 0, -2.500000e-07, 0, 0, 0, 0, 0, 0, //
21+
constantsASTRA.Q << 1.000017e-06, 0, 0, 0, 0, 0, 0, 0, 0, -2.500000e-07, 0, 0, 0, 0, 0, 0, 0, 0, //
22+
0, 1.000017e-06, 0, 0, 0, 0, 0, 0, 0, 0, -2.500000e-07, 0, 0, 0, 0, 0, 0, 0, //
23+
0, 0, 1.000017e-06, 0, 0, 0, 0, 0, 0, 0, 0, -2.500000e-07, 0, 0, 0, 0, 0, 0, //
2824
0, 0, 0, 2.083349e-09, 0, 0, 6.250078e-07, 0, 0, 0, 0, 0, -2.083333e-09, 0, 0, 0, 0, 0, //
2925
0, 0, 0, 0, 2.083349e-09, 0, 0, 6.250078e-07, 0, 0, 0, 0, 0, -2.083333e-09, 0, 0, 0, 0, //
3026
0, 0, 0, 0, 0, 2.083349e-09, 0, 0, 6.250078e-07, 0, 0, 0, 0, 0, -2.083333e-09, 0, 0, 0, //
@@ -45,13 +41,12 @@ void reset_controller_state() {
4541
constantsASTRA.R.block<3, 3>(0, 0) = Matrix3_3::Identity() * 0.100;
4642
constantsASTRA.R.block<3, 3>(3, 3) = Matrix3_3::Identity() * 0.100;
4743

48-
constantsASTRA.K_Att << 1.341480e+00, -4.423127e-16, -2.263115e-17, 1.949556e-01, -3.652184e-17, -5.666956e-17, -6.582806e-01, 5.461372e-16, -2.780114e-16, //
49-
4.924545e-16, 1.341480e+00, -3.901911e-16, 5.099383e-17, 1.949556e-01, -3.192517e-16, -2.775558e-16, -6.582806e-01, -2.514962e-16, //
50-
-4.006925e-16, 5.609321e-16, 4.035990e+00, -5.718917e-17, 1.014174e-16, 2.059154e+00, 3.453548e-16, -4.435380e-16, -1.581139e+00; //
51-
//
44+
constantsASTRA.K_Att << 8.722500e-01, -1.643694e-16, -8.894669e-16, 1.345039e-01, 7.189700e-19, -7.736332e-17, -4.787136e-01, 3.300943e-16, 4.539530e-16, //
45+
6.198463e-16, 8.722500e-01, -1.023859e-15, 4.915936e-17, 1.345039e-01, -2.388831e-16, -1.440612e-15, -4.787136e-01, 3.240570e-16, //
46+
-9.959333e-17, 7.261800e-16, 4.035990e+00, -9.370403e-17, 2.685714e-17, 2.059154e+00, 7.242069e-16, 2.974059e-16, -1.581139e+00; //
5247

5348
P = 1 * Matrix18_18::Identity();
54-
x_est = Vector13::Zero();
49+
x_est = Vector19::Zero();
5550
x_est[0] = 1;
5651
lastEMA = Vector3::Zero();
5752
dnf_X = Matrix9_4::Ones();
@@ -84,6 +79,7 @@ Controller_Output get_controller_output(Controller_Input ci) {
8479

8580
if (!ci.GND_val && last_GND) { // we left GND this frame
8681
Flight_P = P.block<9, 9>(0, 0);
82+
ASTRAv2_Controller_reset(); // reset integral gains in the controller itself
8783
}
8884

8985
if (ci.GND_val) {
@@ -92,8 +88,7 @@ Controller_Output get_controller_output(Controller_Input ci) {
9288
x_est = FlightEstimator(x_est, constantsASTRA, z, dT, Flight_P, ci.new_gps_packet);
9389
}
9490

95-
Vector3 EMA_G = EMA_Gyros(z, lastEMA);
96-
Vector16 X = StateAUG(x_est, EMA_G);
91+
Vector16 X = StateAUG(x_est.segment<13>(0), z.segment<3>(3));
9792
Vector3 TargetPos;
9893
TargetPos << ci.target_pos_north, ci.target_pos_west, ci.target_pos_up;
9994
Vector4 raw_co = ASTRAv2_Controller(TargetPos, X, constantsASTRA, dT);
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ struct Controller_Input {
3535
float target_pos_up;
3636
};
3737

38-
namespace Controller {
39-
void begin();
40-
void reset_controller_state();
38+
namespace ControllerAndEstimator {
39+
void init_controller_and_estimator_constants();
4140
Controller_Output get_controller_output(Controller_Input ci);
42-
}; // namespace Controller
41+
}; // namespace ControllerAndEstimator

lib/controller/matlab_helpers.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,6 @@ Matrix4_4 HamiltonianProd(Vector4 q) {
1919
return M;
2020
}
2121

22-
// Basic Exponential Moving Average Implementation to pre - process % measurements.
23-
Vector3 ExpMovingAvg(Vector3 Input, Vector3 Last, float Alpha) {
24-
// Filter difference equation
25-
return Alpha * Input + (1 - Alpha) * Last;
26-
}
27-
28-
Vector3 EMA_Gyros(Vector15 Y, Vector3 &lastEMA) {
29-
30-
// Extract Gyros
31-
Vector3 gyros = Y.segment<3>(3);
32-
33-
// Exponential Moving Avg Step
34-
Vector3 EMA_G = ExpMovingAvg(gyros, lastEMA, 0.3);
35-
lastEMA = EMA_G;
36-
return EMA_G;
37-
}
38-
3922
Vector16 StateAUG(Vector13 XKF, Vector3 G) {
4023
// Change Filter State to Controls State
4124
Vector16 X;

lib/trajectory_following/TrajectoryFollower.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "SDCard.h"
1010
#include "TrajectoryLoader.h"
1111
#include "TrajectoryLogger.h"
12-
#include "controller.h"
12+
#include "controller_and_estimator.h"
1313
#include "elapsedMillis.h"
1414
#include "gimbal_servos.h"
1515

@@ -33,7 +33,7 @@ void follow_trajectory() {
3333
Mag::beginMeasurement();
3434

3535
Point last_gps_pos = {-1, -1, -1}; // first packet will be marked as new
36-
Controller::reset_controller_state();
36+
ControllerAndEstimator::init_controller_and_estimator_constants();
3737

3838
while (!Mag::isMeasurementReady()) {
3939
Router::println("Waiting on mag...");
@@ -126,7 +126,7 @@ void follow_trajectory() {
126126

127127
ci.GND_val = !has_left_ground;
128128

129-
Controller_Output co = Controller::get_controller_output(ci);
129+
Controller_Output co = ControllerAndEstimator::get_controller_output(ci);
130130
float thrust_perc;
131131
float diffy_perc;
132132
Prop::get_prop_perc(co.thrust_N, co.roll_rad_sec_squared, &thrust_perc, &diffy_perc);

lib/trajectory_following/TrajectoryLogger.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include "controller.h"
3+
#include "controller_and_estimator.h"
44

55
namespace TrajectoryLogger {
66
void create_trajectory_log(const char *filename);

0 commit comments

Comments
 (0)