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 #include <tf/transform_datatypes.h>
45 
46 #include <boost/bind.hpp>
47 
49 {
50 
51 namespace bacc = boost::accumulators;
52 
54 Odometry::Odometry(size_t velocity_rolling_window_size)
55 : timestamp_(0.0)
56 , x_(0.0)
57 , y_(0.0)
58 , heading_(0.0)
59 , linearX_(0.0)
60 , linearY_(0.0)
61 , angular_(0.0)
62 , wheels_k_(0.0)
63 , wheels_radius_(0.0)
64 , velocity_rolling_window_size_(velocity_rolling_window_size)
65 , linearX_acc_(RollingWindow::window_size = velocity_rolling_window_size)
66 , linearY_acc_(RollingWindow::window_size = velocity_rolling_window_size)
67 , angular_acc_(RollingWindow::window_size = velocity_rolling_window_size)
68 , integrate_fun_(boost::bind(&Odometry::integrateExact, this, _1, _2, _3))
69 {
70 }
71 
73 void Odometry::init(const ros::Time& time)
74 {
75  // Reset accumulators:
76  linearX_acc_ = RollingMeanAcc(RollingWindow::window_size = velocity_rolling_window_size_);
77  linearY_acc_ = RollingMeanAcc(RollingWindow::window_size = velocity_rolling_window_size_);
78  angular_acc_ = RollingMeanAcc(RollingWindow::window_size = velocity_rolling_window_size_);
79 
80  // Reset timestamp:
81  timestamp_ = time;
82 }
83 
85 bool Odometry::update(double wheel0_vel, double wheel1_vel, double wheel2_vel, double wheel3_vel, const ros::Time &time)
86 {
88  const double dt = (time - timestamp_).toSec();
89  if (dt < 0.0001)
90  return false; // Interval too small to integrate with
91 
92  timestamp_ = time;
93 
99  linearX_ = 0.25 * wheels_radius_ * ( wheel0_vel + wheel1_vel + wheel2_vel + wheel3_vel);
100  linearY_ = 0.25 * wheels_radius_ * (-wheel0_vel + wheel1_vel - wheel2_vel + wheel3_vel);
101  angular_ = 0.25 * wheels_radius_ / wheels_k_ * (-wheel0_vel - wheel1_vel + wheel2_vel + wheel3_vel);
102 
104  integrate_fun_(linearX_ * dt, linearY_ * dt, angular_ * dt);
105 
106  return true;
107 }
108 
110 void Odometry::updateOpenLoop(double linearX, double linearY, double angular, const ros::Time &time)
111 {
113  linearX_ = linearX;
114  linearY_ = linearY;
115  angular_ = angular;
116 
118  const double dt = (time - timestamp_).toSec();
119  timestamp_ = time;
120  integrate_fun_(linearX * dt, linearY * dt, angular * dt);
121 }
122 
124 void Odometry::setWheelsParams(double wheels_k, double wheels_radius)
125 {
126  wheels_k_ = wheels_k;
127 
128  wheels_radius_ = wheels_radius;
129 }
130 
132 void Odometry::integrateExact(double linearX, double linearY, double angular)
133 {
135  heading_ += angular;
136 
140  tf::Vector3 vel_inOdom = R_m_odom * tf::Vector3(linearX, linearY, 0.0);
141 
143  x_ += vel_inOdom.x();
144  y_ += vel_inOdom.y();
145 }
146 
147 } // namespace mecanum_drive_controller
void setWheelsParams(double wheels_k, double wheels_radius)
Sets the wheels parameters: mecanum geometric param and radius.
Definition: odometry.cpp:124
void integrateExact(double linearX, double linearY, double angular)
Integrates the velocities (linear and angular) using exact method.
Definition: odometry.cpp:132
IntegrationFunction integrate_fun_
Integration funcion, used to integrate the odometry:
Definition: odometry.h:199
size_t velocity_rolling_window_size_
Rolling mean accumulators for the linar and angular velocities:
Definition: odometry.h:193
double linearX_
Current velocity:
Definition: odometry.h:184
TFSIMD_FORCE_INLINE const tfScalar & x() const
double x_
Current pose:
Definition: odometry.h:179
static Quaternion createQuaternionFromYaw(double yaw)
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:54
TFSIMD_FORCE_INLINE const tfScalar & y() const
void updateOpenLoop(double linearX, double linearY, double angular, const ros::Time &time)
Updates the odometry class with latest velocity command.
Definition: odometry.cpp:110
void init(const ros::Time &time)
Initialize the odometry.
Definition: odometry.cpp:73
bacc::tag::rolling_window RollingWindow
Definition: odometry.h:165
double wheels_k_
Wheels kinematic parameters [m]:
Definition: odometry.h:189
bool update(double wheel0_vel, double wheel1_vel, double wheel2_vel, double wheel3_vel, const ros::Time &time)
Updates the odometry class with latest wheels position.
Definition: odometry.cpp:85
ros::Time timestamp_
Current timestamp:
Definition: odometry.h:176
The Odometry class handles odometry readings (2D pose and velocity with related timestamp) ...
Definition: odometry.h:60
bacc::accumulator_set< double, bacc::stats< bacc::tag::rolling_mean > > RollingMeanAcc
Rolling mean accumulator and window:
Definition: odometry.h:164


ridgeback_control
Author(s): Mike Purvis
autogenerated on Thu Mar 5 2020 03:31:54