11 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
14 : costFunction_(costFunction)
18 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
23 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
30 bool performNumericalChecks)
32 size_t N = x_trajectory.size() - 1;
33 solve(x_trajectory, u_trajectory, A, B, N, dt, K, performNumericalChecks);
36 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
42 bool performNumericalChecks)
44 size_t N = x_trajectory.size() - 1;
49 linearizeModel(x_trajectory, u_trajectory, N, dt, derivatives, A, B);
51 solve(x_trajectory, u_trajectory, A, B, N, dt, K, performNumericalChecks);
54 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
66 for (
int i = 0;
i < N;
i++)
68 A[
i] = state_matrix_t::Identity() + dt * derivatives->getDerivativeState(x_trajectory[
i], u_trajectory[i]);
69 B[
i] = dt * derivatives->getDerivativeControl(x_trajectory[i], u_trajectory[i]);
73 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
81 bool performNumericalChecks)
83 if (x_trajectory.size() != N + 1)
85 throw std::runtime_error(
"State trajectory does not have correct length. State trajectory length: " +
86 std::to_string(x_trajectory.size()) +
", should be: " + std::to_string(N + 1));
88 if (u_trajectory.size() != N)
90 throw std::runtime_error(
"Input trajectory does not have correct length. Input trajectory length: " +
91 std::to_string(u_trajectory.size()) +
", should be: " + std::to_string(N));
95 throw std::runtime_error(
"Linearization A does not have correct length. Linearization length: " +
96 std::to_string(A.size()) +
", should be: " + std::to_string(N));
100 throw std::runtime_error(
"Linearization B does not have correct length. Linearization length: " +
101 std::to_string(B.size()) +
", should be: " + std::to_string(N));
107 costFunction_->setCurrentStateAndControl(x_trajectory[N], control_vector_t::Zero(), dt * N);
108 state_matrix_t P = costFunction_->stateSecondDerivativeTerminal();
110 if (performNumericalChecks)
112 Eigen::Matrix<
SCALAR, -1, 1> realEigVals = P.eigenvalues().real();
113 if (realEigVals.minCoeff() < 0.0)
115 std::cout <<
"P: " << std::endl << P;
116 throw std::runtime_error(
"[LQR] Q is not positive semi-definite.");
120 for (
int i = N - 1;
i >= 0;
i--)
122 costFunction_->setCurrentStateAndControl(x_trajectory[
i], u_trajectory[i], i * dt);
128 if (performNumericalChecks)
130 if (Q.minCoeff() < 0.0)
132 std::cout <<
"Q: " << std::endl << Q;
133 throw std::runtime_error(
"[LQR] Q contains negative entries.");
135 if (R.minCoeff() < 0.0)
137 std::cout <<
"R: " << std::endl << R;
138 throw std::runtime_error(
"[LQR] R contains negative entries.");
140 if (R.diagonal().minCoeff() <= 0.0)
142 std::cout <<
"R: " << std::endl << R;
143 throw std::runtime_error(
"[LQR] R contains zero entries on the diagonal.");
147 std::cout <<
"[LQR] Warning: Q is not diagonal." << std::endl;
151 std::cout <<
"[LQR] Warning: R is not diagonal." << std::endl;
156 ricattiEq_.iterateRobust(Q, R, A[i], B[i], P, K[i]);
Eigen::Matrix< SCALAR, STATE_DIM, STATE_DIM > state_matrix_t
Definition: FHDTLQR.hpp:44
const double dt
Definition: LQOCSolverTiming.cpp:18
Describes a cost function with a quadratic approximation, i.e. one that can compute first and second ...
Definition: CostFunctionQuadratic.hpp:29
Finite-Horizon Discrete Time LQR.
Definition: FHDTLQR.hpp:38
CppAD::AD< CppAD::cg::CG< double > > SCALAR
for i
Definition: mpc_unittest_plotting.m:14
FHDTLQR(std::shared_ptr< CostFunctionQuadratic< STATE_DIM, CONTROL_DIM, SCALAR >> costFunction)
Constructor.
Definition: FHDTLQR-impl.hpp:12
void designController(const state_vector_array_t &x_trajectory, const control_vector_array_t &u_trajectory, const state_matrix_array_t &A, const control_gain_matrix_array_t &B, SCALAR dt, control_feedback_array_t &K, bool performNumericalChecks=true)
design a time-varying feedback trajectory using user-provided matrices A and B
Definition: FHDTLQR-impl.hpp:24
~FHDTLQR()
Definition: FHDTLQR-impl.hpp:19
Eigen::Matrix< SCALAR, CONTROL_DIM, CONTROL_DIM > control_matrix_t
Definition: FHDTLQR.hpp:45