-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfiniteLQRLinearTracking.cpp
66 lines (55 loc) · 1.95 KB
/
InfiniteLQRLinearTracking.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "InfiniteLQRLinearTracking.h"
#include "Simulator.h"
#include "TrajectoryGeneration.h"
#include "Constants.h"
#include <cmath>
#include <iostream>
#include <stdexcept>
#include <optional>
#include <memory>
#include "RiccatiSolvers.h"
namespace Controllers {
InfiniteLQRLinearTrackingVehicle::InfiniteLQRLinearTrackingVehicle(
Simulator::PV state,
double targetSMA,
std::shared_ptr<Trajectory> targetTrajectory,
Eigen::Matrix<double, 6, 6> Q,
Eigen::Matrix<double, 3, 3> R): Simulator::Vehicle(state) {
// Target SMA needed to construct the linearized A matrix
this->targetSMA = targetSMA;
this->targetTrajectory = targetTrajectory;
double n = std::sqrt(MU_EARTH/std::pow(this->targetSMA, 3));
// System Dynamics
A << 0.0, 0.0, 0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 1.0,
3*n*n, 0.0, 0.0, 0.0, 2*n, 0.0,
0.0, 0.0, 0.0, -2*n, 0.0, 0.0,
0.0, 0.0, -1*n*n, 0.0, 0.0, 0.0;
// Control matrix
B << 0.0, 0.0, 0.0,
0.0, 0.0, 0.0,
0.0, 0.0, 0.0,
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0;
this->Q = Q;
this->R = R;
auto P = solveRiccatiEigen(A, B, Q, R);
if(!P) {
throw std::runtime_error("Riccati Iteration failed to converge.");
}
this->K = this->R.inverse() * this->B.transpose() * P.value();
}
Eigen::Vector3d InfiniteLQRLinearTrackingVehicle::getControl(
[[maybe_unused]] double t,
[[maybe_unused]] const Simulator::Vehicle& target) {
// Construct the control
Simulator::Vector6d state = this->getRtn(target.getPv());
Simulator::Vector6d targetState = this->targetTrajectory->getTargetState(t);
Simulator::Vector6d error = state - targetState;
Eigen::Vector3d control = -1 * this->K * error;
// This control is acceleration
return control;
}
} // namespace Controllers