-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPC.cpp
220 lines (191 loc) · 7.45 KB
/
MPC.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
#include "MPC.h"
Point2d MPC(int m, int predict_length, vector<Point2d> H, double gammaX, double gammaY, double shift_head2yolk,
vector<Point2d> command_history, vector<Point2d> position_history,
vector<Point2d> fish_tr_history, vector<Point2d> fish_direction_history)
{
vector<Point2d> predicted_fish_tr; //predicted fish trajectory
/*predict fish trajectory*/
predict_fish_tr(predicted_fish_tr,
predict_length, shift_head2yolk,
fish_tr_history, fish_direction_history);
vector<Point2d> predicted_stage_positions; //predicted stage positions (considering only previous commands)
/*predict stage positions*/
predict_stage_positions(predicted_stage_positions,
command_history, position_history.back(), //position_history.back() is the present stage position
H, m, predict_length);
Mat A1(predict_length, predict_length, CV_64FC1); //Ax=b (x direction)
Mat x1(predict_length, 1, CV_64FC1); //predicted optimal commands
Mat b1(predict_length, 1, CV_64FC1);
Mat A2(predict_length, predict_length, CV_64FC1); //Ax=b (y direction)
Mat x2(predict_length, 1, CV_64FC1); //predicted optimal commands
Mat b2(predict_length, 1, CV_64FC1);
/*calculate A*/
double a1, a2;
for (int i = 1; i <= predict_length; i++)
{
for (int j = 1; j <= predict_length; j++)
{
a1 = 0;
a2 = 0;
for (int k = 1; k <= predict_length; k++)
{
a1 += calcHt(H, k - i + 1).x * calcHt(H, k - j + 1).x;
a2 += calcHt(H, k - i + 1).y * calcHt(H, k - j + 1).y;
}
//L2 penalty to the planned future command vector
if (i == j)
{
a1 += gammaX;
a2 += gammaY;
}
A1.at<double>(i - 1, j - 1) = a1;
A2.at<double>(i - 1, j - 1) = a2;
}
}
/*calculate b*/
double bb1, bb2, bbb1, bbb2;
for (int i = 1; i <= predict_length; i++)
{
bb1 = 0;
bb2 = 0;
for (int k = 1; k <= predict_length; k++)
{
bb1 -= calcHt(H, k - i + 1).x*(predicted_stage_positions[k].x + predicted_fish_tr[k].x);
bb2 -= calcHt(H, k - i + 1).y*(predicted_stage_positions[k].y + predicted_fish_tr[k].y);
}
b1.at<double>(i - 1, 0) = bb1;
b2.at<double>(i - 1, 0) = bb2;
}
/*solve for x*/
x1 = A1.inv(DECOMP_EIG)*b1;
x2 = A2.inv(DECOMP_EIG)*b2;
Point2d c1;
c1.x = x1.at<double>(0, 0);
c1.y = x2.at<double>(0, 0);
return c1;
}
Point2d calcHt(vector<Point2d> &H, int t)
{
if (t <= 0)
return Point2d(0, 0);
else
{
if (t >= H.size())
return *(H.end() - 1);
else
return H[t];
}
}
void predict_stage_positions(vector<Point2d> &predicted_stage_positions,
vector<Point2d> &command_history, Point2d position,
vector<Point2d> &H, int m, int predict_length)
{
predicted_stage_positions.clear();
predicted_stage_positions.push_back(position); //predicted_stage_positions[0] is the present stage position
for (int t = 1; t <= predict_length; t++)
{
Point2d predicted_stage_position_temp = predicted_stage_positions.back();
for (int tao = t; tao < m; tao++)
{
predicted_stage_position_temp.x += command_history[tao].x * (calcHt(H, m + t - tao).x - calcHt(H, m + t - 1 - tao).x);
predicted_stage_position_temp.y += command_history[tao].y * (calcHt(H, m + t - tao).y - calcHt(H, m + t - 1 - tao).y);
}
predicted_stage_positions.push_back(predicted_stage_position_temp);
}
}
void predict_fish_tr(vector<Point2d> &predicted_fish_tr,
int predict_length, double shift_head2yolk,
vector<Point2d> fish_tr_history, vector<Point2d> &fish_direction_history)
{
/*shift head to yolk*/
vector<Point2d>::iterator it, it2;
it2 = fish_direction_history.begin();
for (it = fish_tr_history.begin(); it != fish_tr_history.end(); it++)
{
it->x -= it2->x*shift_head2yolk;
it->y -= it2->y*shift_head2yolk;
it2++;
}
predicted_fish_tr.clear();
predicted_fish_tr.push_back(fish_tr_history.back()); //fish_tr_history.back() and predicted_fish_tr[0] are both the present fish position
Point2d fish_direction_average(0, 0); //average fish heading direction
double fish_direction_average_length;
for (it = fish_direction_history.begin(); it != fish_direction_history.end(); it++)
{
fish_direction_average.x += it->x;
fish_direction_average.y += it->y;
}
fish_direction_average_length = sqrt(inner_prod(fish_direction_average, fish_direction_average));
if (fish_direction_average_length < 1) //the fish is taking a U-turn. 可以根据实际的鱼的情况确定该条件
{
for (int t = 1; t <= predict_length; t++) //if the fish is taking a U-turn, we give up predicting the fish trajectory, and just assume that the fish stays static.
{
predicted_fish_tr.push_back(fish_tr_history.back());
}
/*shift yolk to head*/
it2 = fish_direction_history.end() - 1;
for (it = predicted_fish_tr.begin(); it != predicted_fish_tr.end(); it++)
{
it->x += it2->x*shift_head2yolk;
it->y += it2->y*shift_head2yolk;
}
return;
}
else
{
fish_direction_average.x = fish_direction_average.x / fish_direction_average_length;
fish_direction_average.y = fish_direction_average.y / fish_direction_average_length;
}
Point2d fish_position_average(0, 0); //average fish position
for (it = fish_tr_history.begin(); it != fish_tr_history.end(); it++)
{
fish_position_average.x += it->x;
fish_position_average.y += it->y;
}
fish_position_average.x = fish_position_average.x / fish_tr_history.size();
fish_position_average.y = fish_position_average.y / fish_tr_history.size();
double fish_velocity_average_proj; //average fish velocity projected to average fish heading direction(assuming that the fish move along the average heading direction)
Point2d fish_velocity_average(0, 0); //average fish velocity
for (it = fish_tr_history.begin(); it != fish_tr_history.end() - 1; it++)
{
fish_velocity_average.x += (it + 1)->x - it->x;
fish_velocity_average.y += (it + 1)->y - it->y;
}
fish_velocity_average.x = fish_velocity_average.x / (fish_tr_history.size() - 1);
fish_velocity_average.y = fish_velocity_average.y / (fish_tr_history.size() - 1);
fish_velocity_average_proj = inner_prod(fish_velocity_average, fish_direction_average);
if (fish_velocity_average_proj < 0) fish_velocity_average_proj = 0;
Point2d fish_position_proj; //present fish position projected to the line with average fish heading direction and through the average fish position
fish_position_proj.x = fish_tr_history.back().x - fish_position_average.x;
fish_position_proj.y = fish_tr_history.back().y - fish_position_average.y;
double fish_position_proj_length = inner_prod(fish_position_proj, fish_direction_average);
fish_position_proj.x = fish_direction_average.x*fish_position_proj_length;
fish_position_proj.y = fish_direction_average.y*fish_position_proj_length;
fish_position_proj = fish_position_proj + fish_position_average;
for (int t = 1; t <= predict_length; t++)
{
Point2d predicted_fish_pos_t;
predicted_fish_pos_t.x = fish_position_proj.x + t * fish_velocity_average_proj*fish_direction_average.x;
predicted_fish_pos_t.y = fish_position_proj.y + t * fish_velocity_average_proj*fish_direction_average.y;
predicted_fish_tr.push_back(predicted_fish_pos_t);
}
/*shift yolk to head*/
it2 = fish_direction_history.end() - 1;
for (it = predicted_fish_tr.begin(); it != predicted_fish_tr.end(); it++)
{
it->x += it2->x*shift_head2yolk;
it->y += it2->y*shift_head2yolk;
}
}
double inner_prod(Point2d v1, Point2d v2)
{
return v1.x*v2.x + v1.y*v2.y;
}
Point2d trans_c2s(double scale_x, double scale_y, double theta, Point2d p, Point2d stage_position)
{
double x = -sin(theta)*p.y + cos(theta)*p.x;
double y = sin(theta)*p.x + cos(theta)*p.y;
x = scale_x * x - stage_position.x;
y = scale_y * y - stage_position.y;
return Point2d(x, y);
}