Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TransformToGlobal* added and fast_slam2_sim changed.
- Loading branch information
Showing
11 changed files
with
259 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include "TransformToGlobal.h" | ||
#include "pi_to_pi.h" | ||
|
||
void TransformToGlobal(MatrixXf &p, VectorXf b) | ||
{ | ||
//rotate | ||
MatrixXf rot(2,2); | ||
rot<<cos(b(2)), -sin(b(2)), sin(b(2)), cos(b(2)); | ||
|
||
MatrixXf p_resized; | ||
p_resized = MatrixXf(p); | ||
p_resized.conservativeResize(2,p_resized.cols()); | ||
p_resized = rot*p_resized; | ||
|
||
//translate | ||
int c; | ||
for (c=0;c<p_resized.cols();c++) { | ||
p(0,c) = p_resized(0,c)+b(0); | ||
p(1,c) = p_resized(1,c)+b(1); | ||
} | ||
|
||
float input; | ||
//if p is a pose and not a point | ||
if (p.rows() ==3){ | ||
for (int k=0; k<p_resized.cols();k++) { | ||
input = p(2,k) +b(2); | ||
pi_to_pi(input); | ||
p(2,k) = input; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#ifndef TRANSFORMGLOBAL_H | ||
#define TRANSFORMGLOBAL_H | ||
|
||
#include <Eigen/Dense> | ||
#include <iostream> | ||
|
||
using namespace Eigen; | ||
|
||
void TransformToGlobal(MatrixXf &p, VectorXf b); | ||
|
||
#endif //TRANSFORMGLOBAL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,130 @@ | ||
#include "get_observations.h" | ||
#include <iostream> | ||
#include <math.h> | ||
|
||
MatrixXf get_observations(VectorXf x, MatrixXf lm, vector<int> &idf, float rmax) | ||
{ | ||
get_visible_landmarks(x,lm,idf,rmax); | ||
return compute_range_bearing(x,lm); | ||
} | ||
#include "fastslam2_sim.h" | ||
#include "particle.h" | ||
#include "add_control_noise.h" | ||
#include "predict.h" | ||
#include "observe_heading.h" | ||
#include "get_observations.h" | ||
#include "add_observation_noise.h" | ||
|
||
using namespace config; | ||
|
||
void get_visible_landmarks(VectorXf x, MatrixXf &lm, vector<int> &idf, float rmax) | ||
void fastslam2_sim(MatrixXf lm, MatrixXf wp) | ||
{ | ||
//select set of landmarks that are visible within vehicle's | ||
//semi-circular field of view | ||
MatrixXf dx(1,lm.cols()); | ||
MatrixXf dy(1,lm.cols()); | ||
for (int c=0; c<lm.cols(); c++) { | ||
dx(0,c) = lm(0,c) - x(0); | ||
dy(1,c) = lm(1,c) - x(1); | ||
} | ||
float phi = x(2); | ||
vector<int> ii = find2(dx,dy,phi,rmax); | ||
if (SWITCH_PREDICT_NOISE) { | ||
printf("Sampling from predict noise usually OFF for FastSLAM 2.0\n"); | ||
} | ||
if (SWITCH_SAMPLE_PROPOSAL == 0) { | ||
printf("Sampling from optimal proposal is usually ON for FastSLAM 2.0\n"); | ||
} | ||
|
||
//lm(:[2 3]) return matrix of column 2 and colum 3 of lm | ||
MatrixXf lm_new (lm.rows(), ii.size()); | ||
vector<int>::iterator iter; | ||
for (int j=0; j<lm.rows(); j++){ | ||
for(iter = ii.begin(); iter != ii.end(); iter++){ | ||
lm_new(j,*iter) = lm(j,*iter); | ||
} | ||
Q << pow(sigmaV,2), 0, | ||
0 , pow(sigmaG,2); | ||
|
||
R << sigmaR*sigmaR, 0, | ||
0, sigmaB*sigmaB; | ||
|
||
float veh[2][3] = {{0,-WHEELBASE,-WHEELBASE},{0,-1,1}}; | ||
|
||
Particle *particles = new Particle[NPARTICLES]; | ||
float uniformw = 1.0/(float)NPARTICLES; | ||
for (unsigned int p = 0; p < NPARTICLES; p++) { | ||
particles[p].setW(uniformw); | ||
} | ||
VectorXf xtrue(3); | ||
xtrue.setZero(); | ||
|
||
float dt = DT_CONTROLS; //change in time btw predicts | ||
float dtsum = 0; //change in time since last observation | ||
vector<int> ftag; | ||
|
||
for (int i=0; i< lm.cols(); i++) { | ||
ftag.push_back(i); //ftag items are indexed from 1 | ||
} | ||
|
||
VectorXf da_table(lm.cols()); | ||
da_table.setZero(); | ||
|
||
lm = lm_new; //TODO: does this properly copy it over? I need a copy constructor | ||
idf = ii; //check this too | ||
} | ||
int iwp = 1; //index to first waypoint | ||
float G = 0; //initial steer angle | ||
MatrixXf plines; //will later change to list of points | ||
|
||
MatrixXf compute_range_bearing(VectorXf x, MatrixXf lm) | ||
{ | ||
MatrixXf dx(1,lm.cols()); | ||
MatrixXf dy(1,lm.cols()); | ||
for (int c=0; c<lm.cols(); c++) { | ||
dx(0,c) = lm(0,c) - x(0); | ||
dy(1,c) = lm(1,c) - x(1); | ||
} | ||
float phi = x(2); | ||
if (SWITCH_SEED_RANDOM !=0) { | ||
srand(SWITCH_SEED_RANDOM); | ||
} | ||
|
||
MatrixXf z(2,dx.size()); | ||
for (int i =0; i<dx.size(); i++) { | ||
z(1,i) = pow(dx[i],2) + pow(dy[i],2); | ||
z(2,i) = atan2(dy[i],dx[i]) - phi; | ||
Matrix2f Qe = Matrix2f(Q); | ||
Matrix2f Re = Matrix2f(R); | ||
|
||
if (SWITCH_INFLATE_NOISE ==1) { | ||
Qe = 2*Q; | ||
Re = 2*R; | ||
} | ||
|
||
return z; | ||
if (SWITCH_PROFILE) { | ||
//TODO: | ||
} | ||
|
||
vector<int> ftag_visible; | ||
MatrixXf z; | ||
while (iwp !=0) { | ||
//compute true data | ||
compute_steering(xtrue, wp, iwp, AT_WAYPOINT, G, RATEG, MAXG, dt); | ||
if (iwp ==0 && NUMBER_LOOPS > 1) { | ||
iwp = 1; | ||
NUMBER_LOOPS = NUMBER_LOOPS-1; | ||
} | ||
predict_true(xtrue,V,G,WHEELBASE,dt); | ||
|
||
//add process noise noise | ||
//TODO: need to truly randomize function in multivariate_gauss | ||
float* VnGn = new float[2]; | ||
add_control_noise(V,G,Q,SWITCH_CONTROL_NOISE,VnGn); | ||
float Vn = VnGn[0]; | ||
float Gn = VnGn[1]; | ||
|
||
//predict step | ||
for (unsigned int i=0; i< NPARTICLES; i++) { | ||
predict(particles[i],Vn,Gn,Qe,WHEELBASE,dt,SWITCH_PREDICT_NOISE); | ||
observe_heading(particles[i], xtrue(2), SWITCH_HEADING_KNOWN); //if heading known, observe heading | ||
} | ||
|
||
//observe step | ||
dtsum = dtsum+dt; | ||
if (dtsum >= DT_OBSERVE) { | ||
dtsum=0; | ||
//compute true data, then add noise | ||
ftag_visible = vector<int>(ftag); //modify the copy, not the ftag | ||
z = get_observations(xtrue,lm,ftag_visible,MAX_RANGE); | ||
add_observation_noise(z,R,SWITCH_SENSOR_NOISE); | ||
if(!z.isZero()){ | ||
plines = make_laser_lines(z,xtrue); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
vector<int> find2(VectorXf dx, VectorXf dy, float phi, float rmax) | ||
MatrixXf make_laser_lines(MatrixXf rb, VectorXf xv) | ||
{ | ||
vector<int> index; | ||
//incremental tests for bounding semi-circle | ||
for (int i =0; i<input.size(); i++) { | ||
if ((abs(dx[i]) < rmax) && (abs(dy[i]) < rmax) | ||
&& ((dx[i]* cos(phi) + dy[i]* sin(phi)) > 0) | ||
&& ((pow(dx[i],2) + pow(dy[i],2)) < pow(rmax,2))) { | ||
index.push_back(i); | ||
} | ||
} | ||
return index; | ||
if (rb.isZero()) { | ||
return MatrixXf(0,0); | ||
} | ||
|
||
int len = rb.cols(); | ||
MatrixXf lnes(4,2); | ||
|
||
MatrixXf globalMat(2,rb.cols()); | ||
int i,j; | ||
for (j=0; j<globalMat.cols();j++) { | ||
globalMat(0,j) = rb(0,j)*cos(rb(1,j)); | ||
} | ||
|
||
|
||
for (int c=0; c<lnes.cols();c++) { | ||
lnes(0,c) = xv(0); | ||
lnes(1,c) = xv(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.