Apollo 11.0
自动驾驶开放平台
piecewise_jerk_problem.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 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
21#pragma once
22
23#include <tuple>
24#include <utility>
25#include <vector>
26
27#include "osqp/osqp.h"
28
29namespace apollo {
30namespace planning {
31
32/*
33 * @brief:
34 * This class solve an optimization problem:
35 * x
36 * |
37 * | P(s1, x1) P(s2, x2)
38 * | P(s0, x0) ... P(s(k-1), x(k-1))
39 * |P(start)
40 * |
41 * |________________________________________________________ s
42 *
43 * we suppose s(k+1) - s(k) == s(k) - s(k-1)
44 *
45 * Given the x, x', x'' at P(start), The goal is to find x0, x1, ... x(k-1)
46 * which makes the line P(start), P0, P(1) ... P(k-1) "smooth".
47 */
48
50 public:
51 PiecewiseJerkProblem(const size_t num_of_knots, const double delta_s,
52 const std::array<double, 3>& x_init);
53
54 virtual ~PiecewiseJerkProblem() = default;
55
56 void set_x_bounds(std::vector<std::pair<double, double>> x_bounds);
57
58 void set_x_bounds(const double x_lower_bound, const double x_upper_bound);
59
60 void set_dx_bounds(std::vector<std::pair<double, double>> dx_bounds);
61
62 void set_dx_bounds(const double dx_lower_bound, const double dx_upper_bound);
63
64 void set_ddx_bounds(std::vector<std::pair<double, double>> ddx_bounds);
65
66 void set_ddx_bounds(const double ddx_lower_bound,
67 const double ddx_upper_bound);
68
69 void set_dddx_bound(const double dddx_bound) {
70 set_dddx_bound(-dddx_bound, dddx_bound);
71 }
72
73 void set_dddx_bound(const double dddx_lower_bound,
74 const double dddx_upper_bound) {
75 dddx_bound_.first = dddx_lower_bound;
76 dddx_bound_.second = dddx_upper_bound;
77 }
78
79 void set_weight_x(const double weight_x) { weight_x_ = weight_x; }
80
81 void set_weight_dx(const double weight_dx) { weight_dx_ = weight_dx; }
82
83 void set_weight_ddx(const double weight_ddx) { weight_ddx_ = weight_ddx; }
84
85 void set_weight_dddx(const double weight_dddx) { weight_dddx_ = weight_dddx; }
86
87 void set_scale_factor(const std::array<double, 3>& scale_factor) {
88 scale_factor_ = scale_factor;
89 }
90
97 void set_x_ref(const double weight_x_ref, std::vector<double> x_ref);
98
105 void set_x_ref(std::vector<double> weight_x_ref_vec,
106 std::vector<double> x_ref);
107
114 void set_towing_x_ref(const double weight_towing_x_ref,
115 std::vector<double> towing_x_ref);
116
117 void set_towing_x_ref(std::vector<double> weight_towing_x_ref_vec,
118 std::vector<double> towing_x_ref);
119
120 void set_end_state_ref(const std::array<double, 3>& weight_end_state,
121 const std::array<double, 3>& end_state_ref);
122
123 virtual bool Optimize(const int max_iter = 4000);
124
125 const std::vector<double>& opt_x() const { return x_; }
126
127 const std::vector<double>& opt_dx() const { return dx_; }
128
129 const std::vector<double>& opt_ddx() const { return ddx_; }
130
131 protected:
132 // naming convention follows osqp solver.
133 virtual void CalculateKernel(std::vector<c_float>* P_data,
134 std::vector<c_int>* P_indices,
135 std::vector<c_int>* P_indptr) = 0;
136
137 virtual void CalculateOffset(std::vector<c_float>* q) = 0;
138
139 virtual void CalculateAffineConstraint(std::vector<c_float>* A_data,
140 std::vector<c_int>* A_indices,
141 std::vector<c_int>* A_indptr,
142 std::vector<c_float>* lower_bounds,
143 std::vector<c_float>* upper_bounds);
144
145 virtual OSQPSettings* SolverDefaultSettings();
146
147 bool FormulateProblem(OSQPData* data);
148
149 void FreeData(OSQPData* data);
150
151 bool CheckLowUpperBound(const std::vector<c_float>& lower,
152 const std::vector<c_float>& upper);
153
154 template <typename T>
155 T* CopyData(const std::vector<T>& vec) {
156 T* data = new T[vec.size()];
157 memcpy(data, vec.data(), sizeof(T) * vec.size());
158 return data;
159 }
160
161 protected:
162 size_t num_of_knots_ = 0;
163
164 // output
165 std::vector<double> x_;
166 std::vector<double> dx_;
167 std::vector<double> ddx_;
168
169 std::array<double, 3> x_init_;
170 std::array<double, 3> scale_factor_ = {{1.0, 1.0, 1.0}};
171
172 std::vector<std::pair<double, double>> x_bounds_;
173 std::vector<std::pair<double, double>> dx_bounds_;
174 std::vector<std::pair<double, double>> ddx_bounds_;
175 std::pair<double, double> dddx_bound_;
176
177 double weight_x_ = 0.0;
178 double weight_dx_ = 0.0;
179 double weight_ddx_ = 0.0;
180 double weight_dddx_ = 0.0;
181
182 double delta_s_ = 1.0;
183
184 bool has_x_ref_ = false;
185 double weight_x_ref_ = 0.0;
186 std::vector<double> x_ref_;
187 // un-uniformed weighting
188 std::vector<double> weight_x_ref_vec_;
189
190 bool has_towing_x_ref_ = false;
191 std::vector<double> towing_x_ref_;
192 std::vector<double> weight_towing_x_ref_vec_;
193
194 bool has_end_state_ref_ = false;
195 std::array<double, 3> weight_end_state_ = {{0.0, 0.0, 0.0}};
196 std::array<double, 3> end_state_ref_;
197};
198
199} // namespace planning
200} // namespace apollo
bool CheckLowUpperBound(const std::vector< c_float > &lower, const std::vector< c_float > &upper)
void set_x_ref(const double weight_x_ref, std::vector< double > x_ref)
Set the x ref object and the uniform x_ref weighting
const std::vector< double > & opt_x() const
void set_dddx_bound(const double dddx_lower_bound, const double dddx_upper_bound)
std::vector< std::pair< double, double > > x_bounds_
void set_scale_factor(const std::array< double, 3 > &scale_factor)
virtual void CalculateKernel(std::vector< c_float > *P_data, std::vector< c_int > *P_indices, std::vector< c_int > *P_indptr)=0
T * CopyData(const std::vector< T > &vec)
void set_end_state_ref(const std::array< double, 3 > &weight_end_state, const std::array< double, 3 > &end_state_ref)
virtual void CalculateOffset(std::vector< c_float > *q)=0
void set_dddx_bound(const double dddx_bound)
void set_x_bounds(std::vector< std::pair< double, double > > x_bounds)
virtual bool Optimize(const int max_iter=4000)
void set_weight_ddx(const double weight_ddx)
void set_dx_bounds(std::vector< std::pair< double, double > > dx_bounds)
const std::vector< double > & opt_dx() const
const std::vector< double > & opt_ddx() const
void set_towing_x_ref(const double weight_towing_x_ref, std::vector< double > towing_x_ref)
Set towing x ref object and piecewised towing_x_ref weightings
std::vector< std::pair< double, double > > ddx_bounds_
virtual void CalculateAffineConstraint(std::vector< c_float > *A_data, std::vector< c_int > *A_indices, std::vector< c_int > *A_indptr, std::vector< c_float > *lower_bounds, std::vector< c_float > *upper_bounds)
void set_weight_dddx(const double weight_dddx)
std::vector< std::pair< double, double > > dx_bounds_
void set_ddx_bounds(std::vector< std::pair< double, double > > ddx_bounds)
Planning module main class.
class register implement
Definition arena_queue.h:37