Skip to content

Commit

Permalink
lot of bugs fixed from debugging the files individually. THere's stil…
Browse files Browse the repository at this point in the history
…l a bug in data_assoc and get_obs. looking into it.
  • Loading branch information
yglee committed Aug 23, 2012
1 parent 2070534 commit fcccc59
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 90 deletions.
2 changes: 1 addition & 1 deletion cpp/compute_jacobians.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void compute_jacobians(Particle particle,
HfMat<< dx/d, dy/d, HfMat<< dx/d, dy/d,
-dy/d2, dx/d2; -dy/d2, dx/d2;


Hv->push_back(HvMat); Hv->push_back(HvMat);
Hf->push_back(HfMat); Hf->push_back(HfMat);


//innovation covariance of 'feature observation given the vehicle' //innovation covariance of 'feature observation given the vehicle'
Expand Down
19 changes: 13 additions & 6 deletions cpp/get_observations.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ void get_visible_landmarks(VectorXf x, MatrixXf &lm, vector<int> &idf, float rma
} }
} }
lm = MatrixXf(lm_new); lm = MatrixXf(lm_new);
idf = vector<int>(ii); //idf = vector<int>(ii);
vector<int>idf_backup(idf);
idf.clear();
for(int i=0; i<ii.size(); i++) {
idf.push_back(idf_backup[ii[i]]);
}
} }


MatrixXf compute_range_bearing(VectorXf x, MatrixXf lm) MatrixXf compute_range_bearing(VectorXf x, MatrixXf lm)
Expand All @@ -71,12 +76,14 @@ vector<int> find2(MatrixXf dx, MatrixXf dy, float phi, float rmax)
{ {
vector<int> index; vector<int> index;
//incremental tests for bounding semi-circle //incremental tests for bounding semi-circle
for (int i =0; i<dx.cols(); i++) { for (int j=0; j<dx.rows(); j++) {
if ((abs(dx(0,i)) < rmax) && (abs(dy(0,i)) < rmax) for (int i =0; i<dx.cols(); i++) {
&& ((dx(0,i)* cos(phi) + dy(0,i)* sin(phi)) > 0) if ((abs(dx(j,i)) < rmax) && (abs(dy(j,i)) < rmax)
&& ((pow(dx(0,i),2) + pow(dy(0,i),2)) < pow(rmax,2))) { && ((dx(j,i)* cos(phi) + dy(j,i)* sin(phi)) > 0)
&& ((pow(dx(j,i),2) + pow(dy(j,i),2)) < pow(rmax,2))) {
index.push_back(i); index.push_back(i);
} }
} }
}
return index; return index;
} }
8 changes: 5 additions & 3 deletions cpp/multivariate_gauss.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ VectorXf multivariate_gauss(VectorXf x, MatrixXf P, int n)
int len = x.size(); int len = x.size();
//choleksy decomposition //choleksy decomposition
MatrixXf S = P.llt().matrixL(); MatrixXf S = P.llt().matrixL();
//TODO: why is random doesn't change values? cout<<"S"<<endl;
//MatrixXf X = MatrixXf::Random(len,n); cout<<S<<endl;
MatrixXf X = MatrixXf::Ones(len,n); cout<<endl;
MatrixXf X(len,n); //TODO this needs to be randomized
X<<-0.8809,0.8571,0.2638;
VectorXf ones = VectorXf::Ones(n).transpose(); VectorXf ones = VectorXf::Ones(n).transpose();
return S*X + x*ones; return S*X + x*ones;
} }
Expand Down
64 changes: 29 additions & 35 deletions cpp/pi_to_pi.cpp
Original file line number Original file line Diff line number Diff line change
@@ -1,49 +1,42 @@
#include "pi_to_pi.h" #include "pi_to_pi.h"
#include <iostream>


void pi_to_pi(VectorXf &angle) void pi_to_pi(VectorXf &angle)
{ {
vector<int> index; int n;
index = find1(angle); for (int i=0; i<angle.size(); i++) {
vector<int>::iterator iter; if ((angle[i] < (-2*pi)) || (angle[i] > (2*pi))) {

n=floor(angle[i]/(2*pi));
if (!index.empty()){ angle[i] = angle[i]-n*(2*pi);
for (iter = index.begin(); iter != index.end(); iter++) {
//remember that iterators are pointers if (angle[i] > pi) {
int n = floor(angle[*iter] / 2*pi); angle[i] = angle[i] - (2*pi);
angle[*iter] = angle[*iter] - n*(2*pi);//angle[*iter] % (2*pi); }
} if (angle[i] < -pi) {
} angle[i] = angle[i] + (2*pi);

}
for (int i = 0; i< angle.size(); i++) {
if (angle[i] > pi) {
angle[i] = angle[i]-2*pi;
}
}

for (int j=0; j< angle.size(); j++) {
if(angle[j] < -1*pi){
angle[j] = angle[j]+2*pi;
} }
} }
} }


float pi_to_pi(float ang) float pi_to_pi(float ang)
{ {
if ((ang<-2*pi) || (ang>2*pi)) { int n;
int n = floor(ang / 2*pi); if ((ang < (-2*pi)) || (ang > (2*pi))) {
ang = ang-n*2*pi; n=floor(ang/(2*pi));
} ang = ang-n*(2*pi);


if(ang > pi) { if (ang > pi) {
ang = ang-2*pi; ang = ang - (2*pi);
} }
if (ang < -pi) {
if (ang < -pi) { ang = ang + (2*pi);
ang = ang+2*pi; }
} }
return ang; return ang;
} }


#if 0
vector<int> find1(VectorXf input) vector<int> find1(VectorXf input)
{ {
vector<int> index; vector<int> index;
Expand All @@ -54,3 +47,4 @@ vector<int> find1(VectorXf input)
} }
return index; return index;
} }
#endif
16 changes: 15 additions & 1 deletion cpp/predict.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <math.h> #include <math.h>
#include <iostream> #include <iostream>



#define pi 3.1416
//checked all values. everything works //checked all values. everything works


using namespace std; using namespace std;
Expand Down Expand Up @@ -55,6 +57,18 @@ void predict(Particle &particle,float V,float G,Matrix2f Q, float WB,float dt, i
VectorXf xv_temp(3); VectorXf xv_temp(3);
xv_temp << xv(0) + V*dt*cos(G+xv(2)), xv_temp << xv(0) + V*dt*cos(G+xv(2)),
xv(1) + V*dt*sin(G+xv(2)), xv(1) + V*dt*sin(G+xv(2)),
pi_to_pi(xv(2) + V*dt*sin(G/WB)); pi_to_pi2(xv(2) + V*dt*sin(G/WB));
particle.setXv(xv_temp); particle.setXv(xv_temp);
} }

float pi_to_pi2(float ang)
{
if (ang > pi) {
ang = ang - (2*pi);
}
if (ang < -pi) {
ang = ang + (2*pi);
}
return ang;
}

3 changes: 2 additions & 1 deletion cpp/predict.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
#include <Eigen/Dense> #include <Eigen/Dense>
#include "particle.h" #include "particle.h"
#include "multivariate_gauss.h" #include "multivariate_gauss.h"
#include "pi_to_pi.h"


using namespace Eigen; using namespace Eigen;


void predict(Particle &particle,float V,float G,Matrix2f Q, float WB,float dt, int addrandom); void predict(Particle &particle,float V,float G,Matrix2f Q, float WB,float dt, int addrandom);


float pi_to_pi2(float ang);

#endif //PREDICT_H #endif //PREDICT_H
10 changes: 5 additions & 5 deletions cpp/resample_particles.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,21 +12,19 @@ void resample_particles(vector<Particle> &particles, int Nmin, int doresample)
VectorXf w(N); VectorXf w(N);
w.setZero(); w.setZero();


unsigned i; int i;
for (i=0; i<N; i++) { for (i=0; i<N; i++) {
w(i) = particles[i].w(); w(i) = particles[i].w();
//cout<<"w at "<<i<<" "<<w(i)<<endl; //cout<<"w at "<<i<<" "<<w(i)<<endl;
} }


float ws = w.sum(); float ws = w.sum();


//TODO: ws is zero!
cout<<"ws is "<<ws<<endl;
for (i=0; i<N; i++) { for (i=0; i<N; i++) {
w(i) = w(i)/ws; w(i) = w(i)/ws;
} }


float Neff; float Neff=0;
vector<int> keep; vector<int> keep;
stratified_resample(w,keep,Neff); stratified_resample(w,keep,Neff);


Expand All @@ -38,7 +36,9 @@ void resample_particles(vector<Particle> &particles, int Nmin, int doresample)
particles[i] = old_particles[keep[i]]; particles[i] = old_particles[keep[i]];
} }
for (i=0; i<N; i++) { for (i=0; i<N; i++) {
particles[i].setW(1/N); cout<<"N"<<endl;
cout<<N<<endl;
particles[i].setW(1.0/(float)N);
} }
} }
} }
42 changes: 8 additions & 34 deletions cpp/sample_proposal.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void sample_proposal(Particle &particle, MatrixXf z, vector<int> idf, MatrixXf R
//cout<<"vi["<<r<<"] "<<vi[r]<<endl; //cout<<"vi["<<r<<"] "<<vi[r]<<endl;
} }


#if 1 #if 0
cout<<"in sample proposal"<<endl; cout<<"in sample proposal"<<endl;
cout<<"z"<<endl; cout<<"z"<<endl;
cout<<z<<endl; cout<<z<<endl;
Expand Down Expand Up @@ -134,36 +134,6 @@ float likelihood_given_xv(Particle particle, MatrixXf z, vector<int>idf, MatrixX
MatrixXf Sfi; MatrixXf Sfi;
VectorXf v(z.rows()); VectorXf v(z.rows());


cout<<"xv"<<endl;
cout<<particle.xv()<<endl;
cout<<endl;
cout<<"Pv"<<endl;
cout<<particle.Pv()<<endl;
cout<<endl;
cout<<"xf"<<endl;
cout<<particle.xf()<<endl;
cout<<endl;
cout<<"Pf"<<endl;
cout<<particle.Pf()[0]<<endl;
cout<<endl;
cout<<particle.Pf()[1]<<endl;
cout<<endl;
cout<<"w"<<endl;
cout<<particle.w()<<endl;
cout<<endl;
cout<<"z"<<endl;
cout<<z<<endl;
cout<<endl;

cout<<"idf"<<endl;
for (int i=0; i< idf.size(); i++) {
cout<<idf[i]<<" ";
}
cout<<endl;

cout<<"R"<<endl;
cout<<R<<endl;

unsigned i,k; unsigned i,k;
//cout<<"idf.size() "<<idf.size()<<endl; //cout<<"idf.size() "<<idf.size()<<endl;
for (i=0; i<idf.size(); i++){ for (i=0; i<idf.size(); i++){
Expand All @@ -179,19 +149,23 @@ float likelihood_given_xv(Particle particle, MatrixXf z, vector<int>idf, MatrixX
cout<<endl; cout<<endl;


for (k=0; k<z.rows(); k++) { for (k=0; k<z.rows(); k++) {
v(k) = z(k,i)-zp(k,0); //TODO: this returns wrong values v(k) = z(k,i)-zp(k,i); //TODO: this returns wrong values
} }
v(1) = pi_to_pi(v(1)); v(1) = pi_to_pi(v(1));


cout<<"GAUSS EVAL INPUTS"<<endl;
cout<<"v"<<endl; cout<<"v"<<endl;
cout<<v<<endl; cout<<v<<endl;
cout<<endl; cout<<endl;


cout<<"sf"<<endl;
cout<<Sf[0]<<endl;


cout<<"gauss_eval "<<gauss_evaluate(v,Sf[0],0)<<endl;
w = w*gauss_evaluate(v,Sf[0],0); w = w*gauss_evaluate(v,Sf[0],0);
cout<<"new w: "<<w<<endl; //cout<<"new w: "<<w<<endl;
} }
cout<<"w returned "<<w<<endl; //cout<<"w returned "<<w<<endl;
return w; return w;
} }


Expand Down
14 changes: 10 additions & 4 deletions cpp/stratified_resample.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@ void stratified_resample(VectorXf w, vector<int> &keep, float &Neff)
wsqrd(i) = pow(w(i),2); wsqrd(i) = pow(w(i),2);
} }
Neff = 1/wsqrd.sum(); Neff = 1/wsqrd.sum();

int len = w.size(); int len = w.size();
keep.resize(len);
for (int i=0; i<len; i++) {
keep[i] = -1;
}

vector<float> select; vector<float> select;
cout<<"len in stratified resample "<<len<<endl;
stratified_random(len,select); stratified_random(len,select);
cumsum(w); cumsum(w);


int ctr=0; int ctr=0;
for (int i=0; i<len; i++) { for (int i=0; i<len; i++) {
while ((ctr<len) && (select[ctr] < w(i))) { while ((ctr<len) && (select[ctr]<w(i))) {
keep.push_back(i); keep[ctr] = i;
ctr++;
} }
} }
} }
Expand All @@ -34,7 +40,7 @@ void cumsum(VectorXf &w)
VectorXf csumVec(w.size()); VectorXf csumVec(w.size());
for (int i=0; i< w.size(); i++) { for (int i=0; i< w.size(); i++) {
float sum =0; float sum =0;
for (int j=0; j<i; j++) { for (int j=0; j<=i; j++) {
sum+=w(j); sum+=w(j);
} }
csumVec(i) = sum; csumVec(i) = sum;
Expand Down

0 comments on commit fcccc59

Please sign in to comment.