-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfollow_car.cpp
254 lines (230 loc) · 7.03 KB
/
follow_car.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
* @Author: wpbit
* @Date: 2023-11-27 14:07:12
* @LastEditors: wpbit
* @LastEditTime: 2023-12-05 14:33:39
* @Description:
*/
#include "./include/following.h"
class solver
{
private:
int horizon_ = 0;
int num_variables_ = 0;
int num_constraints_ = 0;
public:
solver(int horizon);
bool solve(std::vector<std::vector<double>> input, Eigen::VectorXd &out);
~solver();
};
solver::solver(int horizon)
{
horizon_ = horizon;
num_variables_ = 3 * horizon - 1;
num_constraints_ = 5 * horizon - 3;
}
solver::~solver()
{
}
bool solver::solve(std::vector<std::vector<double>> input, Eigen::VectorXd &out)
{
// for (int i = 0; i < horizon_; ++i)
// {
// // input[i][0] -= 2.0;
// input[i][1] -= 20.0;
// }
// QP参数矩阵
// [v_r, d_r, a] * (horizon-1)
// [v_r - 0, d_r - TARGET_D, a - 0]^T * [v_r - 0, d_r - TARGET_D, a - 0]的二次系数为H,一次为G
Eigen::MatrixXd H = Eigen::MatrixXd::Identity(num_variables_, num_variables_);
// H(0, 0) = 0;
// H(1 ,1) = 0;
// 相对速度
for (int i = 0; i < horizon_; ++i)
{
H(i * 3, i * 3) = 1;
}
// 相对距离
for (int i = 0; i < horizon_; ++i)
{
H(i * 3 + 1, i * 3 + 1) = 1.8;
}
Eigen::VectorXd G = Eigen::VectorXd::Zero(num_variables_);
for (int i = 0; i < horizon_; ++i)
{
G(3 * i + 1) = -2 * TARGET_D;
G(3 * i) = 2 * (Speed_f - TARGET_SPEED);
}
// std::cout << H << std::endl;
// std::cout << G << std::endl;
// 约束矩阵
Eigen::MatrixXd C = Eigen::MatrixXd::Zero(num_constraints_, num_variables_);
// 状态量和控制量不等式约束 3*horizon-1个
for (int i = 0; i < num_variables_; ++i)
{
C(i, i) = 1;
}
// 状态方程等式约束 horizon-1个
int start_index = num_variables_;
for (int i = 0; i < horizon_ - 1; ++i)
{
C(start_index + 2 * i, 3 * (i + 1)) = 1;
C(start_index + 2 * i, 3 * i) = -1;
C(start_index + 2 * i, 3 * i + 2) = -DT;
C(start_index + 2 * i + 1, 3 * (i + 1) + 1) = 1;
C(start_index + 2 * i + 1, 3 * i + 1) = -1;
C(start_index + 2 * i + 1, 3 * i) = DT;
C(start_index + 2 * i + 1, 3 * i + 2) = 0.5 * DT * DT;
}
// std::cout << C << std::endl;
// 约束汇总
Eigen::VectorXd lowerBound(num_constraints_);
for (int i = 0; i < horizon_; ++i)
{
if (i == 0)
{
lowerBound(i) = input[0][0];
lowerBound(i + 1) = input[0][1];
// lowerBound(i) = MIN_SPEED - Speed_f;
// lowerBound(i + 1) = MIN_D;
}
else
{
lowerBound(3 * i - 1) = MIN_ACC;
lowerBound(3 * i) = MIN_SPEED - Speed_f;
lowerBound(3 * i + 1) = MIN_D;
}
}
for (int i = 0; i < horizon_ - 1; ++i)
{
lowerBound(start_index + 2 * i) = 0;
lowerBound(start_index + 2 * i + 1) = 0;
}
Eigen::VectorXd upperBound(num_constraints_);
for (int i = 0; i < horizon_; ++i)
{
if (i == 0)
{
upperBound(i) = input[0][0];
upperBound(i + 1) = input[0][1];
// upperBound(i) = MAX_SPEED - Speed_f;
// upperBound(i + 1) = MAX_D;
}
else
{
upperBound(3 * i - 1) = MAX_ACC;
upperBound(3 * i) = MAX_SPEED - Speed_f;
upperBound(3 * i + 1) = MAX_D;
}
}
for (int i = 0; i < horizon_ - 1; ++i)
{
upperBound(start_index + 2 * i) = 0;
upperBound(start_index + 2 * i + 1) = 0;
}
// std::cout << lowerBound << std::endl;
// std::cout << upperBound << std::endl;
// osqp求解
Eigen::SparseMatrix<double> hessian;
Eigen::VectorXd gradient;
Eigen::SparseMatrix<double> linearMatrix;
hessian = H.sparseView(); // 转换为稀疏矩阵
gradient = G;
linearMatrix = C.sparseView();
OsqpEigen::Solver solver;
solver.settings()->setVerbosity(false);
solver.settings()->setWarmStart(true);
solver.data()->setNumberOfVariables(hessian.cols());
solver.data()->setNumberOfConstraints(linearMatrix.rows());
if (!solver.data()->setHessianMatrix(hessian))
return false;
if (!solver.data()->setLinearConstraintsMatrix(linearMatrix))
return false;
if (!solver.data()->setGradient(gradient))
return false; // 注意,一次项系数set必须为一维数组,不能为矩阵
if (!solver.data()->setLowerBound(lowerBound))
return false;
if (!solver.data()->setUpperBound(upperBound))
return false;
if (!solver.initSolver())
return false;
if (static_cast<int>(solver.solveProblem()) != 0)
return false;
out = solver.getSolution();
// cout << out << endl;
return true;
}
int main()
{
// 预测时刻
int horizon = 10;
// 仿真参数初始化
float S_f = S0_f;
float S_r = S0_r;
float Speed_r = Speed0_r;
std::vector<float> Vec_Vr{-2};
std::vector<float> Vec_t{0};
std::vector<float> Vec_D{S0_f - S0_r};
std::vector<float> Vec_Acc{0};
// 输入 {a, b}, a->相对车速,自车-目标车; b->相对距离,目标车-自车 > 0
std::vector<std::vector<double>> input;
input.resize(horizon);
input[0] = {-Speed_f, S0_f};
for(int i = 1; i < horizon; ++i){
input[i] = {-Speed_f, S0_f+Speed_f*DT*i};
}
for (int sim = 0; sim < 200; sim++)
{
Eigen::VectorXd out;
solver test(horizon);
test.solve(input, out);
// 状态更新
// 前车状态更新
S_f = S_f + Speed_f * DT;
// 后车状态更新
float AccNew = out(2);
float Vel = input[0][0] + Speed_f;
if(Vel < 0) Vel = 0;
if(Vel < 1e-5 && AccNew < 0) AccNew = 0;
input[0][0] += AccNew * DT;
input[0][1] = input[0][1] - Vel * DT - 0.5 * AccNew * DT * DT + Speed_f * DT;
for(int i = 1; i < horizon; ++i){
input[i][0] = out(3 * i);
input[i][1] = out(3 * i + 1);
}
std::cout << "加速度:" << AccNew << std::endl;
std::cout << "相对速度:" << input[0][0] << std::endl;
std::cout << "相对车距:" << input[0][1] << std::endl;
Vec_D.push_back(input[0][1]);
Vec_Vr.push_back(input[0][0]);
Vec_t.push_back((sim + 1) * DT);
Vec_Acc.push_back(AccNew);
}
plt::figure_size(1500, 500);
plt::subplot(1, 3, 1);
plt::plot(Vec_t, Vec_Vr, "b-");
plt::xlim(0, int(Vec_t.back()));
plt::ylim(-5, 5);
plt::xlabel("t (s)");
plt::ylabel("Vel (km/h)");
plt::title("Vel-t");
plt::grid(1);
plt::subplot(1, 3, 2);
plt::plot(Vec_t, Vec_Acc, "b-");
plt::xlim(0, int(Vec_t.back()));
plt::ylim(-5, 5);
plt::xlabel("t (s)");
plt::ylabel("Acc (m/s2)");
plt::title("Acc-t");
plt::grid(1);
plt::subplot(1, 3, 3);
plt::plot(Vec_t, Vec_D, "r-");
plt::xlim(0, int(Vec_t.back()));
plt::ylim(0, 50);
plt::xlabel("t (s)");
plt::ylabel("D (m)");
plt::title("D-t");
plt::grid(1);
plt::show();
return 0;
}