odometry.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2013, PAL Robotics, S.L.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the PAL Robotics nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /*
36  * Author: Luca Marchionni
37  * Author: Bence Magyar
38  * Author: Enrique Fernández
39  * Author: Paul Mathieu
40  */
41 
43 
44 namespace diff_drive_controller
45 {
46  namespace bacc = boost::accumulators;
47 
48  Odometry::Odometry(size_t velocity_rolling_window_size)
49  : timestamp_(0.0)
50  , x_(0.0)
51  , y_(0.0)
52  , heading_(0.0)
53  , linear_(0.0)
54  , angular_(0.0)
55  , wheel_separation_(0.0)
56  , left_wheel_radius_(0.0)
57  , right_wheel_radius_(0.0)
58  , left_wheel_old_pos_(0.0)
59  , right_wheel_old_pos_(0.0)
60  , velocity_rolling_window_size_(velocity_rolling_window_size)
61  , linear_acc_(RollingWindow::window_size = velocity_rolling_window_size)
62  , angular_acc_(RollingWindow::window_size = velocity_rolling_window_size)
63  , integrate_fun_(std::bind(&Odometry::integrateExact, this, std::placeholders::_1, std::placeholders::_2))
64  {
65  }
66 
67  void Odometry::init(const ros::Time& time)
68  {
69  // Reset accumulators and timestamp:
70  resetAccumulators();
71  timestamp_ = time;
72  }
73 
74  bool Odometry::update(double left_pos, double right_pos, const ros::Time &time)
75  {
77  const double left_wheel_cur_pos = left_pos * left_wheel_radius_;
78  const double right_wheel_cur_pos = right_pos * right_wheel_radius_;
79 
81  const double left_wheel_est_vel = left_wheel_cur_pos - left_wheel_old_pos_;
82  const double right_wheel_est_vel = right_wheel_cur_pos - right_wheel_old_pos_;
83 
85  left_wheel_old_pos_ = left_wheel_cur_pos;
86  right_wheel_old_pos_ = right_wheel_cur_pos;
87 
89  const double linear = (right_wheel_est_vel + left_wheel_est_vel) * 0.5 ;
90  const double angular = (right_wheel_est_vel - left_wheel_est_vel) / wheel_separation_;
91 
93  integrate_fun_(linear, angular);
94 
96  const double dt = (time - timestamp_).toSec();
97  if (dt < 0.0001)
98  return false; // Interval too small to integrate with
99 
100  timestamp_ = time;
101 
103  linear_acc_(linear/dt);
104  angular_acc_(angular/dt);
105 
106  linear_ = bacc::rolling_mean(linear_acc_);
107  angular_ = bacc::rolling_mean(angular_acc_);
108 
109  return true;
110  }
111 
112  void Odometry::updateOpenLoop(double linear, double angular, const ros::Time &time)
113  {
115  linear_ = linear;
116  angular_ = angular;
117 
119  const double dt = (time - timestamp_).toSec();
120  timestamp_ = time;
121  integrate_fun_(linear * dt, angular * dt);
122  }
123 
124  void Odometry::setWheelParams(double wheel_separation, double left_wheel_radius, double right_wheel_radius)
125  {
126  wheel_separation_ = wheel_separation;
127  left_wheel_radius_ = left_wheel_radius;
128  right_wheel_radius_ = right_wheel_radius;
129  }
130 
131  void Odometry::setVelocityRollingWindowSize(size_t velocity_rolling_window_size)
132  {
133  velocity_rolling_window_size_ = velocity_rolling_window_size;
134 
135  resetAccumulators();
136  }
137 
138  void Odometry::integrateRungeKutta2(double linear, double angular)
139  {
140  const double direction = heading_ + angular * 0.5;
141 
143  x_ += linear * cos(direction);
144  y_ += linear * sin(direction);
145  heading_ += angular;
146  }
147 
153  void Odometry::integrateExact(double linear, double angular)
154  {
155  if (fabs(angular) < 1e-6)
156  integrateRungeKutta2(linear, angular);
157  else
158  {
160  const double heading_old = heading_;
161  const double r = linear/angular;
162  heading_ += angular;
163  x_ += r * (sin(heading_) - sin(heading_old));
164  y_ += -r * (cos(heading_) - cos(heading_old));
165  }
166  }
167 
168  void Odometry::resetAccumulators()
169  {
170  linear_acc_ = RollingMeanAcc(RollingWindow::window_size = velocity_rolling_window_size_);
171  angular_acc_ = RollingMeanAcc(RollingWindow::window_size = velocity_rolling_window_size_);
172  }
173 
174 } // namespace diff_drive_controller
odometry.h
diff_drive_controller
Definition: diff_drive_controller.h:56
diff_drive_controller::Odometry::RollingMeanAcc
bacc::accumulator_set< double, bacc::stats< bacc::tag::rolling_mean > > RollingMeanAcc
Rolling mean accumulator and window:
Definition: odometry.h:223
diff_drive_controller::Odometry::Odometry
Odometry(size_t velocity_rolling_window_size=10)
Constructor Timestamp will get the current time value Value will be set to zero.
Definition: odometry.cpp:80
ros::Time
std


diff_drive_controller
Author(s): Bence Magyar
autogenerated on Fri May 24 2024 02:41:05