-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapmatch.cpp
288 lines (201 loc) · 6.5 KB
/
mapmatch.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
255
256
257
258
#include "mapmatch.h"
#define _USE_MATH_DEFINES
#include <cmath>
#include <complex> //complex
#include "utils.h"
#include "world.h"
#include <iostream> //debugging
MapMatch::MapMatch(World& world){
this->world = world;
this-> seed = 0; //index of map to take a guess
this-> firstSearch = true;
this-> REQUIRED_DISTANCE = 10.0;
this-> MAX_BACKWARD_ITERS = 75;
this-> MAX_FORWARD_ITERS = 225;
}
MapMatch:: ~MapMatch(){
}
void MapMatch::localize(LocalState_T& localState, const GlobalState_T globalState){
MapMatchOutput_T out = this->convertToLocal(globalState.X, globalState.Y);
double e = out.e;
double s = out.s;
double psiDes = out.psiDes;
bool initStatus = out.converged;
if (!initStatus){
localState.e = 0.0;
localState.dPsi = 0.0;
localState.s = 0.0;
}
else{
//sanity check - s must be within map boundaries
double sEnd = *(this -> world.s.end()- 1);
if (s < 0){
s = sEnd + s; //cycle to end of path
}
else if (s > sEnd){
s = s - sEnd;
}
double dPsi = globalState.Psi - psiDes;
while (dPsi > M_PI){
dPsi = dPsi - 2 * M_PI;
}
while (dPsi < -M_PI){
dPsi = dPsi + 2 * M_PI;
}
localState.e = e;
localState.s = s;
localState.dPsi = dPsi;
}
}
MapMatchOutput_T MapMatch::convertToLocal(const double posE, const double posN){
//very crude map matching - works on small maps
World world = this->world;
size_t m = world.s.size();
std::complex<double> EN (posE, posN); //use complex type here to
//take advantage of built-in norm function.
//go forward
double lastPair = 9999999;
int forwardInd = this->seed;
bool stillDecreasing = true;
int numForwardIterations = 0;
double currentPair;
while (stillDecreasing && numForwardIterations < this->MAX_FORWARD_ITERS){
numForwardIterations++;
if (forwardInd <= m-2){
std::complex<double> EN0 (world.posE[forwardInd], world.posN[forwardInd]);
std::complex<double> EN1 (world.posE[forwardInd+1], world.posN[forwardInd+1]);
currentPair = std::norm(EN - EN0) + std::norm(EN-EN1);
}
else{
//allow searching at the beginning of the map if world is closed
if (world.isOpen){
currentPair = 9999999;
}
else{
std::complex<double> EN1 (world.posE[forwardInd], world.posN[forwardInd]);
std::complex<double> EN0 (world.posE[0], world.posN[0]);
currentPair = std::norm(EN - EN1) + std::norm(EN - EN0);
}
}
stillDecreasing = (currentPair < lastPair);
if (stillDecreasing){
lastPair = currentPair;
//allow searching at beginning of map if map is closed
if (forwardInd == m-1 && !world.isOpen){
forwardInd = 0;
}
else{
forwardInd ++;
}
}
}
forwardInd--; //needed because we increment by one even after
double smallestF = lastPair;
lastPair = 9999999; //inf
int backwardInd = this->seed;
stillDecreasing = true;
int numBackwardIterations = 0;
double smallestB;
int lowSind;
int highSind;
while (stillDecreasing && (numBackwardIterations < this->MAX_BACKWARD_ITERS)){
numBackwardIterations++;
if (backwardInd >=1){
std::complex<double> EN0 (world.posE[backwardInd], world.posN[backwardInd]);
std::complex<double> EN1 (world.posE[backwardInd-1], world.posN[backwardInd-1]);
currentPair = std::norm(EN - EN0) + std::norm(EN-EN1);
}
else{
//allow searching at end of map if map is closed
if (world.isOpen){
currentPair = 9999999;
}
else{
std::complex<double> EN0 (world.posE[backwardInd], world.posN[backwardInd]);
std::complex<double> EN1 (world.posE[m-1] , world.posN[m-1]);
currentPair = std::norm(EN - EN0) + std::norm(EN-EN1);
}
}
stillDecreasing = (currentPair < lastPair);
if (stillDecreasing){
lastPair = currentPair;
//allow searching from end of map if map is closed
if (backwardInd == 0 && !world.isOpen){
backwardInd = m-1;
}
else{
backwardInd--;
}
}
}
smallestB = lastPair;
if (smallestB < smallestF){
if (backwardInd > 0){
lowSind = backwardInd - 1;
}
else{
lowSind = m - 2;
//This should be m-1, but paths are defined so that
//the last point overlaps with the first point. This will
//mess up the cross product below, so we just go back one index
// when we cross to the next lap.
}
highSind = backwardInd;
}
else{
lowSind = forwardInd;
if (forwardInd < m-1){
highSind = forwardInd + 1;
}
else{
highSind = 1;
//This should be 0, but paths are defined so that the last
//point overlaps with the first point. This messes up the
//cross product, so just go up one index when we cross to the
//next laps
}
}
//Need to track this for initialization testing
double smallestNorm = std::fmin(smallestB, smallestF);
std::complex<double> ENlow (world.posE[lowSind], world.posN[lowSind]);
std::complex<double> ENhigh(world.posE[highSind], world.posN[highSind]);
double a = std::norm(EN - ENlow);
double b = std::norm(EN - ENhigh);
double c = std::norm(ENlow - ENhigh);
double deltaS = (pow(a,2) + pow(c,2) - pow(b,2)) / (2*c);
double abs_e = std::sqrt(std::abs(pow(a,2) - pow(deltaS,2)));
double s = world.s[lowSind] + deltaS;
double headingVector[3] = {-sin(world.roadPsi[lowSind]), cos(world.roadPsi[lowSind]), 0};
double pENaugmented[3] = {EN.real(), EN.imag(), 0};
double pathVector[3] = {world.posE[lowSind], world.posN[lowSind], 0};
double positionVector[3] = {pENaugmented[0] - pathVector[0], pENaugmented[1] - pathVector[1], pENaugmented[2] - pathVector[2]};
double crssSgn = crossSign(headingVector[0],headingVector[1], positionVector[0], positionVector[1]);
double e = abs_e * crssSgn;
//compute K and psi desired via interpolation
double SMALL_NUMBER = 0.000000000001; //needed in case of malformed maps
double psiDes = world.roadPsi[lowSind] + (world.roadPsi[highSind] - world.roadPsi[lowSind])/(world.s[highSind] - world.s[lowSind]+SMALL_NUMBER)*deltaS;
double K = world.curvature[lowSind] + (world.curvature[highSind] - world.curvature[lowSind])/(world.s[highSind] - world.s[lowSind]+SMALL_NUMBER)*deltaS;
bool converged;
if (smallestNorm < this->REQUIRED_DISTANCE){
converged = true;
this->seed = lowSind;
}
else{
converged = false;
this->seed = this->seed + this->MAX_BACKWARD_ITERS + this->MAX_FORWARD_ITERS;
//wrap around if necessary
if (this->seed > m - 1){
this->seed = 0;
}
}
int iterations = numForwardIterations + numBackwardIterations;
MapMatchOutput_T out;
out.e = e;
out.s = s;
out.K = K;
out.psiDes = psiDes;
out.converged = converged;
out.iterations = iterations;
out.smallestNorm = smallestNorm;
return out;
}