-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtools.cpp
72 lines (57 loc) · 1.75 KB
/
tools.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
#include <iostream>
#include <cmath>
#include "tools.h"
using namespace std;
using Eigen::VectorXd;
using Eigen::MatrixXd;
using std::vector;
Tools::Tools() {}
Tools::~Tools() {}
VectorXd Tools::CalculateRMSE(const vector<VectorXd> &estimations,
const vector<VectorXd> &ground_truth) {
VectorXd rmse(4);
rmse << 0,0,0,0;
//Validate the estimations vector
if(estimations.size() == 0 || estimations.size() != ground_truth.size()){
cout<<"Error in size of Estimations vector or size mismatch with Ground Truth vector";
return rmse;
}
//Accumulate the residual
for(int i = 0; i < estimations.size(); ++i){
VectorXd residual = estimations[i] - ground_truth[i];
rmse = rmse + (residual.array() * residual.array()).matrix();
}
//Mean and Sqrt the error
rmse = rmse / estimations.size();
rmse = rmse.array().sqrt();
return rmse;
}
MatrixXd Tools::CalculateJacobian(const VectorXd& x_state) {
//Initalize the Jacobian
MatrixXd Hj(3,4);
//Retrive the state values from the vector
float px = x_state(0);
float py = x_state(1);
float vx = x_state(2);
float vy = x_state(3);
//Set the Jacobian to zero
Hj << 0,0,0,0,
0,0,0,0,
0,0,0,0;
//Check for small values of position magnitude to avoid division by zero
float rho = pow((pow(px,2) + pow(py,2)), 0.5);
if( rho < 0.0001){
cout << "Value of rho too small - possible div by 0. Reassigning rho = 0.0005";
rho = 0.0001;
}
float inv_rho = pow(rho, -1);
Hj(0,0) = px * inv_rho;
Hj(1,0) = -py * pow(inv_rho,2);
Hj(2,0) = py * (vx*py - vy*px) * pow(inv_rho, 3);
Hj(0,1) = py * inv_rho;
Hj(1,1) = px * pow(inv_rho, 2);
Hj(2,1) = px * (vy*px - vx*py) * pow(inv_rho,3);
Hj(2,2) = Hj(0,0);
Hj(2,3) = Hj(0,1);
return Hj;
}