Austin Schuh | 1d73136 | 2022-02-22 13:57:55 -0800 | [diff] [blame^] | 1 | #include "y2022/control_loops/superstructure/catapult/catapult.h" |
| 2 | |
| 3 | #include "Eigen/Dense" |
| 4 | #include "Eigen/Sparse" |
| 5 | #include "aos/realtime.h" |
| 6 | #include "aos/time/time.h" |
| 7 | #include "glog/logging.h" |
| 8 | #include "osqp++.h" |
| 9 | #include "osqp.h" |
| 10 | #include "y2022/control_loops/superstructure/catapult/catapult_plant.h" |
| 11 | |
| 12 | namespace y2022 { |
| 13 | namespace control_loops { |
| 14 | namespace superstructure { |
| 15 | namespace catapult { |
| 16 | namespace chrono = std::chrono; |
| 17 | |
| 18 | namespace { |
| 19 | osqp::OsqpInstance MakeInstance( |
| 20 | size_t horizon, Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> P) { |
| 21 | osqp::OsqpInstance instance; |
| 22 | instance.objective_matrix = P.cast<c_float>().sparseView(); |
| 23 | |
| 24 | instance.constraint_matrix = |
| 25 | Eigen::SparseMatrix<c_float, Eigen::ColMajor, osqp::c_int>(horizon, |
| 26 | horizon); |
| 27 | instance.constraint_matrix.setIdentity(); |
| 28 | |
| 29 | instance.lower_bounds = |
| 30 | Eigen::Matrix<c_float, Eigen::Dynamic, 1>::Zero(horizon, 1); |
| 31 | instance.upper_bounds = |
| 32 | Eigen::Matrix<c_float, Eigen::Dynamic, 1>::Ones(horizon, 1) * 12.0; |
| 33 | return instance; |
| 34 | } |
| 35 | } // namespace |
| 36 | |
| 37 | MPCProblem::MPCProblem(size_t horizon, |
| 38 | Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> P, |
| 39 | Eigen::Matrix<double, Eigen::Dynamic, 1> accel_q, |
| 40 | Eigen::Matrix<double, 2, 2> Af, |
| 41 | Eigen::Matrix<double, Eigen::Dynamic, 2> final_q) |
| 42 | : horizon_(horizon), |
| 43 | accel_q_(std::move(accel_q.cast<c_float>())), |
| 44 | Af_(std::move(Af.cast<c_float>())), |
| 45 | final_q_(std::move(final_q.cast<c_float>())), |
| 46 | instance_(MakeInstance(horizon, std::move(P))) { |
| 47 | instance_.objective_vector = |
| 48 | Eigen::Matrix<c_float, Eigen::Dynamic, 1>(horizon, 1); |
| 49 | settings_.max_iter = 25; |
| 50 | settings_.check_termination = 25; |
| 51 | settings_.warm_start = 0; |
| 52 | auto status = solver_.Init(instance_, settings_); |
| 53 | CHECK(status.ok()) << status; |
| 54 | } |
| 55 | |
| 56 | void MPCProblem::SetState(Eigen::Matrix<c_float, 2, 1> X_initial, |
| 57 | Eigen::Matrix<c_float, 2, 1> X_final) { |
| 58 | X_initial_ = X_initial; |
| 59 | X_final_ = X_final; |
| 60 | instance_.objective_vector = |
| 61 | X_initial(1, 0) * accel_q_ + final_q_ * (Af_ * X_initial - X_final); |
| 62 | |
| 63 | auto status = solver_.SetObjectiveVector(instance_.objective_vector); |
| 64 | CHECK(status.ok()) << status; |
| 65 | } |
| 66 | |
| 67 | bool MPCProblem::Solve() { |
| 68 | const aos::monotonic_clock::time_point start_time = |
| 69 | aos::monotonic_clock::now(); |
| 70 | osqp::OsqpExitCode exit_code = solver_.Solve(); |
| 71 | const aos::monotonic_clock::time_point end_time = aos::monotonic_clock::now(); |
| 72 | VLOG(1) << "OSQP solved in " |
| 73 | << std::chrono::duration<double>(end_time - start_time).count(); |
| 74 | solve_time_ = std::chrono::duration<double>(end_time - start_time).count(); |
| 75 | // TODO(austin): Dump the exit codes out as an enum for logging. |
| 76 | // |
| 77 | // TODO(austin): The dual problem doesn't appear to be converging on all |
| 78 | // problems. Are we phrasing something wrong? |
| 79 | |
| 80 | // TODO(austin): Set a time limit so we can't run forever, and signal back |
| 81 | // when we hit our limit. |
| 82 | return exit_code == osqp::OsqpExitCode::kOptimal; |
| 83 | } |
| 84 | |
| 85 | void MPCProblem::WarmStart(const MPCProblem &p) { |
| 86 | CHECK_GE(p.horizon(), horizon()) |
| 87 | << ": Can only copy a bigger problem's solution into a smaller problem."; |
| 88 | auto status = solver_.SetPrimalWarmStart(p.solver_.primal_solution().block( |
| 89 | p.horizon() - horizon(), 0, horizon(), 1)); |
| 90 | CHECK(status.ok()) << status; |
| 91 | status = solver_.SetDualWarmStart(p.solver_.dual_solution().block( |
| 92 | p.horizon() - horizon(), 0, horizon(), 1)); |
| 93 | CHECK(status.ok()) << status; |
| 94 | } |
| 95 | |
| 96 | CatapultProblemGenerator::CatapultProblemGenerator(size_t horizon) |
| 97 | : plant_(MakeCatapultPlant()), |
| 98 | horizon_(horizon), |
| 99 | Q_final_( |
| 100 | (Eigen::DiagonalMatrix<double, 2>().diagonal() << 10000.0, 10000.0) |
| 101 | .finished()), |
| 102 | As_(MakeAs()), |
| 103 | Bs_(MakeBs()), |
| 104 | m_(Makem()), |
| 105 | M_(MakeM()), |
| 106 | W_(MakeW()), |
| 107 | w_(Makew()), |
| 108 | Pi_(MakePi()), |
| 109 | WM_(W_ * M_), |
| 110 | Wmpw_(W_ * m_ + w_) {} |
| 111 | |
| 112 | std::unique_ptr<MPCProblem> CatapultProblemGenerator::MakeProblem( |
| 113 | size_t horizon) { |
| 114 | return std::make_unique<MPCProblem>( |
| 115 | horizon, P(horizon), accel_q(horizon), Af(horizon), |
| 116 | (2.0 * Q_final_ * Bf(horizon)).transpose()); |
| 117 | } |
| 118 | |
| 119 | const Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> |
| 120 | CatapultProblemGenerator::P(size_t horizon) { |
| 121 | CHECK_GT(horizon, 0u); |
| 122 | CHECK_LE(horizon, horizon_); |
| 123 | return 2.0 * (WM_.block(0, 0, horizon, horizon).transpose() * Pi(horizon) * |
| 124 | WM_.block(0, 0, horizon, horizon) + |
| 125 | Bf(horizon).transpose() * Q_final_ * Bf(horizon)); |
| 126 | } |
| 127 | |
| 128 | const Eigen::Matrix<double, Eigen::Dynamic, 1> CatapultProblemGenerator::q( |
| 129 | size_t horizon, Eigen::Matrix<c_float, 2, 1> X_initial, |
| 130 | Eigen::Matrix<c_float, 2, 1> X_final) { |
| 131 | CHECK_GT(horizon, 0u); |
| 132 | CHECK_LE(horizon, horizon_); |
| 133 | return 2.0 * X_initial(1, 0) * accel_q(horizon) + |
| 134 | 2.0 * ((Af(horizon) * X_initial - X_final).transpose() * Q_final_ * |
| 135 | Bf(horizon)) |
| 136 | .transpose(); |
| 137 | } |
| 138 | |
| 139 | const Eigen::Matrix<double, Eigen::Dynamic, 1> |
| 140 | CatapultProblemGenerator::accel_q(size_t horizon) { |
| 141 | return 2.0 * ((Wmpw_.block(0, 0, horizon, 1)).transpose() * Pi(horizon) * |
| 142 | WM_.block(0, 0, horizon, horizon)) |
| 143 | .transpose(); |
| 144 | } |
| 145 | |
| 146 | const Eigen::Matrix<double, 2, 2> CatapultProblemGenerator::Af(size_t horizon) { |
| 147 | CHECK_GT(horizon, 0u); |
| 148 | CHECK_LE(horizon, horizon_); |
| 149 | return As_.block<2, 2>(2 * (horizon - 1), 0); |
| 150 | } |
| 151 | |
| 152 | const Eigen::Matrix<double, 2, Eigen::Dynamic> CatapultProblemGenerator::Bf( |
| 153 | size_t horizon) { |
| 154 | CHECK_GT(horizon, 0u); |
| 155 | CHECK_LE(horizon, horizon_); |
| 156 | return Bs_.block(2 * (horizon - 1), 0, 2, horizon); |
| 157 | } |
| 158 | |
| 159 | const Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> |
| 160 | CatapultProblemGenerator::Pi(size_t horizon) { |
| 161 | CHECK_GT(horizon, 0u); |
| 162 | CHECK_LE(horizon, horizon_); |
| 163 | return Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>(Pi_).block( |
| 164 | horizon_ - horizon, horizon_ - horizon, horizon, horizon); |
| 165 | } |
| 166 | |
| 167 | Eigen::Matrix<double, Eigen::Dynamic, 2> CatapultProblemGenerator::MakeAs() { |
| 168 | Eigen::Matrix<double, Eigen::Dynamic, 2> As = |
| 169 | Eigen::Matrix<double, Eigen::Dynamic, 2>::Zero(horizon_ * 2, 2); |
| 170 | for (size_t i = 0; i < horizon_; ++i) { |
| 171 | if (i == 0) { |
| 172 | As.block<2, 2>(0, 0) = plant_.A(); |
| 173 | } else { |
| 174 | As.block<2, 2>(i * 2, 0) = plant_.A() * As.block<2, 2>((i - 1) * 2, 0); |
| 175 | } |
| 176 | } |
| 177 | return As; |
| 178 | } |
| 179 | |
| 180 | Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> |
| 181 | CatapultProblemGenerator::MakeBs() { |
| 182 | Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> Bs = |
| 183 | Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>::Zero(horizon_ * 2, |
| 184 | horizon_); |
| 185 | for (size_t i = 0; i < horizon_; ++i) { |
| 186 | for (size_t j = 0; j < i + 1; ++j) { |
| 187 | if (i == j) { |
| 188 | Bs.block<2, 1>(i * 2, j) = plant_.B(); |
| 189 | } else { |
| 190 | Bs.block<2, 1>(i * 2, j) = |
| 191 | As_.block<2, 2>((i - j - 1) * 2, 0) * plant_.B(); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | return Bs; |
| 196 | } |
| 197 | |
| 198 | Eigen::Matrix<double, Eigen::Dynamic, 1> CatapultProblemGenerator::Makem() { |
| 199 | Eigen::Matrix<double, Eigen::Dynamic, 1> m = |
| 200 | Eigen::Matrix<double, Eigen::Dynamic, 1>::Zero(horizon_, 1); |
| 201 | for (size_t i = 0; i < horizon_; ++i) { |
| 202 | m(i, 0) = As_(1 + 2 * i, 1); |
| 203 | } |
| 204 | return m; |
| 205 | } |
| 206 | |
| 207 | Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> |
| 208 | CatapultProblemGenerator::MakeM() { |
| 209 | Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> M = |
| 210 | Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>::Zero(horizon_, |
| 211 | horizon_); |
| 212 | for (size_t i = 0; i < horizon_; ++i) { |
| 213 | for (size_t j = 0; j < horizon_; ++j) { |
| 214 | M(i, j) = Bs_(2 * i + 1, j); |
| 215 | } |
| 216 | } |
| 217 | return M; |
| 218 | } |
| 219 | |
| 220 | Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> |
| 221 | CatapultProblemGenerator::MakeW() { |
| 222 | Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> W = |
| 223 | Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>::Identity(horizon_, |
| 224 | horizon_); |
| 225 | for (size_t i = 0; i < horizon_ - 1; ++i) { |
| 226 | W(i + 1, i) = -1.0; |
| 227 | } |
| 228 | W /= std::chrono::duration<double>(plant_.dt()).count(); |
| 229 | return W; |
| 230 | } |
| 231 | |
| 232 | Eigen::Matrix<double, Eigen::Dynamic, 1> CatapultProblemGenerator::Makew() { |
| 233 | Eigen::Matrix<double, Eigen::Dynamic, 1> w = |
| 234 | Eigen::Matrix<double, Eigen::Dynamic, 1>::Zero(horizon_, 1); |
| 235 | w(0, 0) = -1.0 / std::chrono::duration<double>(plant_.dt()).count(); |
| 236 | return w; |
| 237 | } |
| 238 | |
| 239 | Eigen::DiagonalMatrix<double, Eigen::Dynamic> |
| 240 | CatapultProblemGenerator::MakePi() { |
| 241 | Eigen::DiagonalMatrix<double, Eigen::Dynamic> Pi(horizon_); |
| 242 | for (size_t i = 0; i < horizon_; ++i) { |
| 243 | Pi.diagonal()(i) = |
| 244 | std::pow(0.01, 2.0) + |
| 245 | std::pow(0.02 * std::max(0.0, (20 - ((int)horizon_ - (int)i)) / 20.), |
| 246 | 2.0); |
| 247 | } |
| 248 | return Pi; |
| 249 | } |
| 250 | |
| 251 | Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> |
| 252 | CatapultProblemGenerator::MakeP() { |
| 253 | return 2.0 * (M_.transpose() * W_.transpose() * Pi_ * W_ * M_ + |
| 254 | Bf(horizon_).transpose() * Q_final_ * Bf(horizon_)); |
| 255 | } |
| 256 | |
| 257 | } // namespace catapult |
| 258 | } // namespace superstructure |
| 259 | } // namespace control_loops |
| 260 | } // namespace y2022 |