iSAM
anchorNodes.cpp
Go to the documentation of this file.
00001 
00034 #include <isam/isam.h>
00035 #include "isam/Anchor.h"
00036 
00037 using namespace std;
00038 using namespace isam;
00039 using namespace Eigen;
00040 
00041 
00042 int main() {
00043   // locally defined, as nodes and factors below are allocated on the
00044   // stack, and therefore will go out of scope after the end of this
00045   // function; note that you can (and typically have to ) dynamically
00046   // allocate, see demo.cpp, but I used local variables here to make
00047   // the example simpler.
00048   Slam slam;
00049 
00050   Noise noise = SqrtInformation(10. * eye(3));
00051 
00052   // first pose graph
00053   Pose2d prior_origin(0., 0., 0.);
00054   Pose2d_Node a0;
00055   slam.add_node(&a0);
00056   Pose2d_Factor p_a0(&a0, prior_origin, noise);
00057   slam.add_factor(&p_a0);
00058   Pose2d odo(1., 0., 0.);
00059   Pose2d_Node a1;
00060   slam.add_node(&a1);
00061   Pose2d_Pose2d_Factor o_a01(&a0, &a1, odo, noise);
00062   slam.add_factor(&o_a01);
00063 
00064   // second pose graph
00065   Pose2d_Node b0;
00066   slam.add_node(&b0);
00067   Pose2d_Factor p_b0(&b0, prior_origin, noise);
00068   slam.add_factor(&p_b0);
00069   Pose2d_Node b1;
00070   slam.add_node(&b1);
00071   Pose2d_Pose2d_Factor o_b01(&b0, &b1, odo, noise);
00072   slam.add_factor(&o_b01);
00073 
00074   cout << "two independent pose graphs:" << endl;
00075   cout << "batch optimization... (nothing changes)" << endl;
00076   slam.batch_optimization();
00077   cout << "...done" << endl;
00078   cout << "a0: " << a0.value() << endl;
00079   cout << "a1: " << a1.value() << endl;
00080   cout << "b0: " << b0.value() << endl;
00081   cout << "b1: " << b1.value() << endl;
00082 
00083   // constraint between two pose graphs:
00084 
00085   // first pose graph now also has a anchor node, requires an arbitrary
00086   // prior (here origin); also initializes the anchor node
00087   Anchor2d_Node anchor0(&slam);
00088   slam.add_node(&anchor0);
00089   //Pose2d_Factor p_anchor0(&anchor0, prior_origin, noise);
00090   //slam.add_factor(&p_anchor0);
00091 
00092   // anchor node for second trajectory (as before)
00093   Anchor2d_Node anchor1(&slam);
00094   slam.add_node(&anchor1);
00095 
00096   Pose2d measure0(0., 1., 0.);
00097   // now we need 2 optional parameters to refer to both anchor nodes -
00098   // the order determines which trajectory the measurement originates
00099   // from; also, the first one already needs to be initialized (done
00100   // using prior above), the second one gets initialized if needed
00101   Pose2d_Pose2d_Factor d_a1_b1(&a1, &b1, measure0, noise, &anchor0, &anchor1);
00102   slam.add_factor(&d_a1_b1);
00103 
00104   cout << "\nfirst constraint added:" << endl;
00105 
00106   cout << "batch optimization... (nothing changes)" << endl;
00107   slam.batch_optimization();
00108   cout << "...done" << endl;
00109 
00110   cout << "a0: " << a0.value() << endl;
00111   cout << "a1: " << a1.value() << endl;
00112   cout << "b0: " << b0.value() << endl;
00113   cout << "b1: " << b1.value() << endl;
00114   cout << "anchor0: " << anchor0.value() << endl;
00115   cout << "anchor1: " << anchor1.value() << endl;
00116 
00117   Pose2d measure1(0., 0.5, 0.); // conflicting measurement, want least squares
00118   Pose2d_Pose2d_Factor d_a1_b1_2(&a1, &b1, measure1, noise, &anchor0, &anchor1);
00119   slam.add_factor(&d_a1_b1_2);
00120 
00121   cout << "\nsecond conflicting constraint added (least squares is 0.75):" << endl;
00122 
00123   cout << "batch optimization... (least squares solution)" << endl;
00124   slam.batch_optimization();
00125   cout << "...done" << endl;
00126 
00127   cout.precision(2);
00128   cout << fixed;
00129   cout << "a0: " << a0.value() << endl;
00130   cout << "a1: " << a1.value() << endl;
00131   cout << "b0: " << b0.value() << endl;
00132   cout << "b1: " << b1.value() << endl;
00133   cout << "anchor0: " << anchor0.value() << endl;
00134   cout << "anchor1: " << anchor1.value() << endl;
00135 
00136   slam.save("anchorNodes.graph");
00137 
00138   return 0;
00139 }
 All Classes Files Functions Variables