g2o
odometry_measurement.cpp
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #include "odometry_measurement.h"
28 
29 #include <Eigen/Core>
30 #include <Eigen/Geometry>
31 
32 namespace g2o {
33 
35  _measurement(0., 0.), _dt(0.)
36  {
37  }
38 
40  _measurement(vl, vr), _dt(dt)
41  {
42  }
43 
45  _measurement(0., 0., 0.), _dt(0.)
46  {
47  }
48 
49  MotionMeasurement::MotionMeasurement(double x, double y, double theta, double dt) :
50  _measurement(x, y, theta), _dt(dt)
51  {
52  }
53 
55  _measurement(m), _dt(dt)
56  {
57  }
58 
60  {
61  Vector2D px2(0, 10);
62 
63  if (fabs(m.theta()) > 1e-7) {
64  Eigen::Rotation2Dd rot(m.theta());
65  Vector2D px3(m.x(), m.y());
66  Vector2D px4(rot * px2 + px3);
67 
68  const double& y2 = px2.y();
69  const double& x3 = px3.x();
70  const double& y3 = px3.y();
71  const double& x4 = px4.x();
72  const double& y4 = px4.y();
73 
74  double R = (y2 * (x3*y4 - y3*x4)) / (y2 * (x3 - x4));
75  double w;
76  if (fabs(m.dt()) > 1e-7)
77  w = m.theta() / m.dt();
78  else
79  w = 0.;
80 
81  double vl = (2.*R*w - w) / 2.;
82  double vr = w + vl;
83 
84  return VelocityMeasurement(vl, vr, m.dt());
85  } else {
86  double vl, vr;
87  if (fabs(m.dt()) > 1e-7)
88  vl = vr = hypot(m.x(), m.y()) / m.dt();
89  else
90  vl = vr = 0.;
91  return VelocityMeasurement(vl, vr, m.dt());
92  }
93  }
94 
96  {
97  double x, y, theta;
98  if (fabs(v.vr() - v.vl()) > 1e-7) {
99  double R = l * 0.5 * ((v.vl() + v.vr()) / (v.vr() - v.vl()));
100  double w = (v.vr() - v.vl()) / l;
101 
102  theta = w * v.dt();
103  Eigen::Rotation2Dd rot(theta);
104  Vector2D icc(0, R);
105  Vector2D motion = (rot * (Vector2D(-1.*icc))) + icc;
106  x = motion.x();
107  y = motion.y();
108  } else {
109  double tv = 0.5 * (v.vr() + v.vl());
110  theta = 0.;
111  x = tv * v.dt();
112  y = 0.;
113  }
114 
115  return MotionMeasurement(x, y, theta, v.dt());
116 
117  }
118 
119 } // end namespace
Eigen::Matrix< double, 2, 1, Eigen::ColMajor > Vector2D
Definition: eigen_types.h:45
Eigen::Matrix< double, 3, 1, Eigen::ColMajor > Vector3D
Definition: eigen_types.h:46
velocity measurement of a differential robot
static MotionMeasurement convertToMotion(const VelocityMeasurement &vi, double l=1.0)
A 2D odometry measurement expressed as a transformation.
static VelocityMeasurement convertToVelocity(const MotionMeasurement &m)
T hypot(T x, T y)
Definition: misc.h:61