Apollo  6.0
Open source self driving car software
mpc_osqp.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2019 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 
17 #pragma once
18 
19 #include <algorithm>
20 #include <limits>
21 #include <memory>
22 #include <utility>
23 #include <vector>
24 
25 #include "Eigen/Eigen"
26 #include "osqp/osqp.h"
27 
28 #include "cyber/common/log.h"
29 
30 namespace apollo {
31 namespace common {
32 namespace math {
33 class MpcOsqp {
34  public:
45  MpcOsqp(const Eigen::MatrixXd &matrix_a, const Eigen::MatrixXd &matrix_b,
46  const Eigen::MatrixXd &matrix_q, const Eigen::MatrixXd &matrix_r,
47  const Eigen::MatrixXd &matrix_initial_x,
48  const Eigen::MatrixXd &matrix_u_lower,
49  const Eigen::MatrixXd &matrix_u_upper,
50  const Eigen::MatrixXd &matrix_x_lower,
51  const Eigen::MatrixXd &matrix_x_upper,
52  const Eigen::MatrixXd &matrix_x_ref, const int max_iter,
53  const int horizon, const double eps_abs);
54 
55  // control vector
56  bool Solve(std::vector<double> *control_cmd);
57 
58  private:
59  void CalculateKernel(std::vector<c_float> *P_data,
60  std::vector<c_int> *P_indices,
61  std::vector<c_int> *P_indptr);
62  void CalculateEqualityConstraint(std::vector<c_float> *A_data,
63  std::vector<c_int> *A_indices,
64  std::vector<c_int> *A_indptr);
65  void CalculateGradient();
66  void CalculateConstraintVectors();
67  OSQPSettings *Settings();
68  OSQPData *Data();
69  void FreeData(OSQPData *data);
70 
71  template <typename T>
72  T *CopyData(const std::vector<T> &vec) {
73  T *data = new T[vec.size()];
74  memcpy(data, vec.data(), sizeof(T) * vec.size());
75  return data;
76  }
77 
78  private:
79  Eigen::MatrixXd matrix_a_;
80  Eigen::MatrixXd matrix_b_;
81  Eigen::MatrixXd matrix_q_;
82  Eigen::MatrixXd matrix_r_;
83  Eigen::MatrixXd matrix_initial_x_;
84  const Eigen::MatrixXd matrix_u_lower_;
85  const Eigen::MatrixXd matrix_u_upper_;
86  const Eigen::MatrixXd matrix_x_lower_;
87  const Eigen::MatrixXd matrix_x_upper_;
88  const Eigen::MatrixXd matrix_x_ref_;
89  int max_iteration_;
90  size_t horizon_;
91  double eps_abs_;
92  size_t state_dim_;
93  size_t control_dim_;
94  size_t num_param_;
95  int num_constraint_;
96  Eigen::VectorXd gradient_;
97  Eigen::VectorXd lowerBound_;
98  Eigen::VectorXd upperBound_;
99 };
100 } // namespace math
101 } // namespace common
102 } // namespace apollo
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
MpcOsqp(const Eigen::MatrixXd &matrix_a, const Eigen::MatrixXd &matrix_b, const Eigen::MatrixXd &matrix_q, const Eigen::MatrixXd &matrix_r, const Eigen::MatrixXd &matrix_initial_x, const Eigen::MatrixXd &matrix_u_lower, const Eigen::MatrixXd &matrix_u_upper, const Eigen::MatrixXd &matrix_x_lower, const Eigen::MatrixXd &matrix_x_upper, const Eigen::MatrixXd &matrix_x_ref, const int max_iter, const int horizon, const double eps_abs)
Solver for discrete-time model predictive control problem.
bool Solve(std::vector< double > *control_cmd)
Definition: mpc_osqp.h:33