imu_filter_ros.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010, CCNY Robotics Lab
3  * Ivan Dryanovski <ivan.dryanovski@gmail.com>
4  *
5  * http://robotics.ccny.cuny.edu
6  *
7  * Based on implementation of Madgwick's IMU and AHRS algorithms.
8  * http://www.x-io.co.uk/node/8#open_source_ahrs_and_imu_algorithms
9  *
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 
27 #include "geometry_msgs/TransformStamped.h"
30 
32  nh_(nh),
33  nh_private_(nh_private),
34  initialized_(false)
35 {
36  ROS_INFO ("Starting ImuFilter");
37 
38  // **** get paramters
39  if (!nh_private_.getParam ("stateless", stateless_))
40  stateless_ = false;
41  if (!nh_private_.getParam ("use_mag", use_mag_))
42  use_mag_ = true;
43  if (!nh_private_.getParam ("publish_tf", publish_tf_))
44  publish_tf_ = true;
45  if (!nh_private_.getParam ("reverse_tf", reverse_tf_))
46  reverse_tf_ = false;
47  if (!nh_private_.getParam ("fixed_frame", fixed_frame_))
48  fixed_frame_ = "odom";
49  if (!nh_private_.getParam ("constant_dt", constant_dt_))
50  constant_dt_ = 0.0;
51  if (!nh_private_.getParam ("publish_debug_topics", publish_debug_topics_))
52  publish_debug_topics_= false;
53 
54  std::string world_frame;
55  if (!nh_private_.getParam ("world_frame", world_frame))
56  world_frame = "enu";
57 
58  if (world_frame == "ned") {
60  } else if (world_frame == "nwu"){
62  } else if (world_frame == "enu"){
64  } else {
65  ROS_ERROR("The parameter world_frame was set to invalid value '%s'.", world_frame.c_str());
66  ROS_ERROR("Valid values are 'enu', 'ned' and 'nwu'. Setting to 'enu'.");
68  }
70 
71  // check for illegal constant_dt values
72  if (constant_dt_ < 0.0)
73  {
74  ROS_FATAL("constant_dt parameter is %f, must be >= 0.0. Setting to 0.0", constant_dt_);
75  constant_dt_ = 0.0;
76  }
77 
78  // if constant_dt_ is 0.0 (default), use IMU timestamp to determine dt
79  // otherwise, it will be constant
80  if (constant_dt_ == 0.0)
81  ROS_INFO("Using dt computed from message headers");
82  else
83  ROS_INFO("Using constant dt of %f sec", constant_dt_);
84 
85  // **** register dynamic reconfigure
87  FilterConfigServer::CallbackType f = boost::bind(&ImuFilterRos::reconfigCallback, this, _1, _2);
88  config_server_->setCallback(f);
89 
90  // **** register publishers
91  imu_publisher_ = nh_.advertise<sensor_msgs::Imu>(
92  ros::names::resolve("imu") + "/data", 5);
93 
95  {
96  rpy_filtered_debug_publisher_ = nh_.advertise<geometry_msgs::Vector3Stamped>(
97  ros::names::resolve("imu") + "/rpy/filtered", 5);
98 
99  rpy_raw_debug_publisher_ = nh_.advertise<geometry_msgs::Vector3Stamped>(
100  ros::names::resolve("imu") + "/rpy/raw", 5);
101  }
102 
103  // **** register subscribers
104  // Synchronize inputs. Topic subscriptions happen on demand in the connection callback.
105  int queue_size = 5;
106 
107  imu_subscriber_.reset(new ImuSubscriber(
108  nh_, ros::names::resolve("imu") + "/data_raw", queue_size));
109 
110  if (use_mag_)
111  {
112  mag_subscriber_.reset(new MagSubscriber(
113  nh_, ros::names::resolve("imu") + "/mag", queue_size));
114 
115  sync_.reset(new Synchronizer(
116  SyncPolicy(queue_size), *imu_subscriber_, *mag_subscriber_));
117  sync_->registerCallback(boost::bind(&ImuFilterRos::imuMagCallback, this, _1, _2));
118  }
119  else
120  {
121  imu_subscriber_->registerCallback(&ImuFilterRos::imuCallback, this);
122  }
123 
125 }
126 
128 {
129  ROS_INFO ("Destroying ImuFilter");
130 
131  // Explicitly stop callbacks; they could execute after we're destroyed
133 }
134 
135 void ImuFilterRos::imuCallback(const ImuMsg::ConstPtr& imu_msg_raw)
136 {
137  boost::mutex::scoped_lock lock(mutex_);
138 
139  const geometry_msgs::Vector3& ang_vel = imu_msg_raw->angular_velocity;
140  const geometry_msgs::Vector3& lin_acc = imu_msg_raw->linear_acceleration;
141 
142  ros::Time time = imu_msg_raw->header.stamp;
143  imu_frame_ = imu_msg_raw->header.frame_id;
144 
145  if (!initialized_ || stateless_)
146  {
147  geometry_msgs::Quaternion init_q;
149  {
150  ROS_WARN_THROTTLE(5.0, "The IMU seems to be in free fall, cannot determine gravity direction!");
151  return;
152  }
153  filter_.setOrientation(init_q.w, init_q.x, init_q.y, init_q.z);
154  }
155 
156  if (!initialized_)
157  {
158  ROS_INFO("First IMU message received.");
160 
161  // initialize time
162  last_time_ = time;
163  initialized_ = true;
164  }
165 
166  // determine dt: either constant, or from IMU timestamp
167  float dt;
168  if (constant_dt_ > 0.0)
169  dt = constant_dt_;
170  else
171  {
172  dt = (time - last_time_).toSec();
173  if (time.isZero())
174  ROS_WARN_STREAM_THROTTLE(5.0, "The IMU message time stamp is zero, and the parameter constant_dt is not set!" <<
175  " The filter will not update the orientation.");
176  }
177 
178  last_time_ = time;
179 
180  if (!stateless_)
182  ang_vel.x, ang_vel.y, ang_vel.z,
183  lin_acc.x, lin_acc.y, lin_acc.z,
184  dt);
185 
186  publishFilteredMsg(imu_msg_raw);
187  if (publish_tf_)
188  publishTransform(imu_msg_raw);
189 }
190 
192  const ImuMsg::ConstPtr& imu_msg_raw,
193  const MagMsg::ConstPtr& mag_msg)
194 {
195  boost::mutex::scoped_lock lock(mutex_);
196 
197  const geometry_msgs::Vector3& ang_vel = imu_msg_raw->angular_velocity;
198  const geometry_msgs::Vector3& lin_acc = imu_msg_raw->linear_acceleration;
199  const geometry_msgs::Vector3& mag_fld = mag_msg->magnetic_field;
200 
201  ros::Time time = imu_msg_raw->header.stamp;
202  imu_frame_ = imu_msg_raw->header.frame_id;
203 
204  /*** Compensate for hard iron ***/
205  geometry_msgs::Vector3 mag_compensated;
206  mag_compensated.x = mag_fld.x - mag_bias_.x;
207  mag_compensated.y = mag_fld.y - mag_bias_.y;
208  mag_compensated.z = mag_fld.z - mag_bias_.z;
209 
210  double roll = 0.0;
211  double pitch = 0.0;
212  double yaw = 0.0;
213 
214  if (!initialized_ || stateless_)
215  {
216  // wait for mag message without NaN / inf
217  if(!std::isfinite(mag_fld.x) || !std::isfinite(mag_fld.y) || !std::isfinite(mag_fld.z))
218  {
219  return;
220  }
221 
222  geometry_msgs::Quaternion init_q;
223  if (!StatelessOrientation::computeOrientation(world_frame_, lin_acc, mag_compensated, init_q))
224  {
225  ROS_WARN_THROTTLE(5.0, "The IMU seems to be in free fall or close to magnetic north pole, cannot determine gravity direction!");
226  return;
227  }
228  filter_.setOrientation(init_q.w, init_q.x, init_q.y, init_q.z);
229  }
230 
231  if (!initialized_)
232  {
233  ROS_INFO("First pair of IMU and magnetometer messages received.");
235 
236  // initialize time
237  last_time_ = time;
238  initialized_ = true;
239  }
240 
241  // determine dt: either constant, or from IMU timestamp
242  float dt;
243  if (constant_dt_ > 0.0)
244  dt = constant_dt_;
245  else
246  {
247  dt = (time - last_time_).toSec();
248  if (time.isZero())
249  ROS_WARN_STREAM_THROTTLE(5.0, "The IMU message time stamp is zero, and the parameter constant_dt is not set!" <<
250  " The filter will not update the orientation.");
251  }
252 
253  last_time_ = time;
254 
255  if (!stateless_)
257  ang_vel.x, ang_vel.y, ang_vel.z,
258  lin_acc.x, lin_acc.y, lin_acc.z,
259  mag_compensated.x, mag_compensated.y, mag_compensated.z,
260  dt);
261 
262  publishFilteredMsg(imu_msg_raw);
263  if (publish_tf_)
264  publishTransform(imu_msg_raw);
265 
267  {
268  geometry_msgs::Quaternion orientation;
269  if (StatelessOrientation::computeOrientation(world_frame_, lin_acc, mag_compensated, orientation))
270  {
271  tf2::Matrix3x3(tf2::Quaternion(orientation.x, orientation.y, orientation.z, orientation.w)).getRPY(roll, pitch, yaw, 0);
272  publishRawMsg(time, roll, pitch, yaw);
273  }
274  }
275 }
276 
277 void ImuFilterRos::publishTransform(const ImuMsg::ConstPtr& imu_msg_raw)
278 {
279  double q0,q1,q2,q3;
280  filter_.getOrientation(q0,q1,q2,q3);
281  geometry_msgs::TransformStamped transform;
282  transform.header.stamp = imu_msg_raw->header.stamp;
283  if (reverse_tf_)
284  {
285  transform.header.frame_id = imu_frame_;
286  transform.child_frame_id = fixed_frame_;
287  transform.transform.rotation.w = q0;
288  transform.transform.rotation.x = -q1;
289  transform.transform.rotation.y = -q2;
290  transform.transform.rotation.z = -q3;
291  }
292  else {
293  transform.header.frame_id = fixed_frame_;
294  transform.child_frame_id = imu_frame_;
295  transform.transform.rotation.w = q0;
296  transform.transform.rotation.x = q1;
297  transform.transform.rotation.y = q2;
298  transform.transform.rotation.z = q3;
299  }
300  tf_broadcaster_.sendTransform(transform);
301 
302 }
303 
304 void ImuFilterRos::publishFilteredMsg(const ImuMsg::ConstPtr& imu_msg_raw)
305 {
306  double q0,q1,q2,q3;
307  filter_.getOrientation(q0,q1,q2,q3);
308 
309  // create and publish filtered IMU message
310  boost::shared_ptr<ImuMsg> imu_msg =
311  boost::make_shared<ImuMsg>(*imu_msg_raw);
312 
313  imu_msg->orientation.w = q0;
314  imu_msg->orientation.x = q1;
315  imu_msg->orientation.y = q2;
316  imu_msg->orientation.z = q3;
317 
318  imu_msg->orientation_covariance[0] = orientation_variance_;
319  imu_msg->orientation_covariance[1] = 0.0;
320  imu_msg->orientation_covariance[2] = 0.0;
321  imu_msg->orientation_covariance[3] = 0.0;
322  imu_msg->orientation_covariance[4] = orientation_variance_;
323  imu_msg->orientation_covariance[5] = 0.0;
324  imu_msg->orientation_covariance[6] = 0.0;
325  imu_msg->orientation_covariance[7] = 0.0;
326  imu_msg->orientation_covariance[8] = orientation_variance_;
327 
328  imu_publisher_.publish(imu_msg);
329 
331  {
332  geometry_msgs::Vector3Stamped rpy;
333  tf2::Matrix3x3(tf2::Quaternion(q1,q2,q3,q0)).getRPY(rpy.vector.x, rpy.vector.y, rpy.vector.z);
334 
335  rpy.header = imu_msg_raw->header;
337  }
338 }
339 
341  float roll, float pitch, float yaw)
342 {
343  geometry_msgs::Vector3Stamped rpy;
344  rpy.vector.x = roll;
345  rpy.vector.y = pitch;
346  rpy.vector.z = yaw ;
347  rpy.header.stamp = t;
348  rpy.header.frame_id = imu_frame_;
350 }
351 
352 
353 void ImuFilterRos::reconfigCallback(FilterConfig& config, uint32_t level)
354 {
355  double gain, zeta;
356  boost::mutex::scoped_lock lock(mutex_);
357  gain = config.gain;
358  zeta = config.zeta;
361  ROS_INFO("Imu filter gain set to %f", gain);
362  ROS_INFO("Gyro drift bias set to %f", zeta);
363  mag_bias_.x = config.mag_bias_x;
364  mag_bias_.y = config.mag_bias_y;
365  mag_bias_.z = config.mag_bias_z;
366  orientation_variance_ = config.orientation_stddev * config.orientation_stddev;
367  ROS_INFO("Magnetometer bias values: %f %f %f", mag_bias_.x, mag_bias_.y, mag_bias_.z);
368 }
369 
371 {
372  if (use_mag_)
373  ROS_WARN_STREAM("Still waiting for data on topics " << ros::names::resolve("imu") << "/data_raw"
374  << " and " << ros::names::resolve("imu") << "/mag" << "...");
375  else
376  ROS_WARN_STREAM("Still waiting for data on topic " << ros::names::resolve("imu") << "/data_raw" << "...");
377 }
void getRPY(tf2Scalar &roll, tf2Scalar &pitch, tf2Scalar &yaw, unsigned int solution_number=1) const
#define ROS_FATAL(...)
#define ROS_WARN_STREAM_THROTTLE(period, args)
message_filters::Subscriber< ImuMsg > ImuSubscriber
void publish(const boost::shared_ptr< M > &message) const
message_filters::sync_policies::ApproximateTime< ImuMsg, MagMsg > SyncPolicy
f
bool publish_debug_topics_
boost::shared_ptr< Synchronizer > sync_
double orientation_variance_
void publishRawMsg(const ros::Time &t, float roll, float pitch, float yaw)
void imuMagCallback(const ImuMsg::ConstPtr &imu_msg_raw, const MagMsg::ConstPtr &mav_msg)
void setOrientation(double q0, double q1, double q2, double q3)
Definition: imu_filter.h:82
void stop()
ros::Publisher rpy_raw_debug_publisher_
void madgwickAHRSupdate(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz, float dt)
Definition: imu_filter.cpp:175
imu_filter_madgwick::ImuFilterMadgwickConfig FilterConfig
double constant_dt_
message_filters::Synchronizer< SyncPolicy > Synchronizer
ROSCPP_DECL std::string resolve(const std::string &name, bool remap=true)
std::string imu_frame_
message_filters::Subscriber< MagMsg > MagSubscriber
ros::Time last_time_
void checkTopicsTimerCallback(const ros::TimerEvent &)
void getOrientation(double &q0, double &q1, double &q2, double &q3)
Definition: imu_filter.h:65
boost::shared_ptr< FilterConfigServer > config_server_
std::string fixed_frame_
ImuFilterRos(ros::NodeHandle nh, ros::NodeHandle nh_private)
void setAlgorithmGain(double gain)
Definition: imu_filter.h:50
ros::NodeHandle nh_private_
boost::shared_ptr< ImuSubscriber > imu_subscriber_
#define ROS_WARN_THROTTLE(period,...)
void madgwickAHRSupdateIMU(float gx, float gy, float gz, float ax, float ay, float az, float dt)
Definition: imu_filter.cpp:258
ros::Publisher imu_publisher_
#define ROS_INFO(...)
dynamic_reconfigure::Server< FilterConfig > FilterConfigServer
void setWorldFrame(WorldFrame::WorldFrame frame)
Definition: imu_filter.h:60
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
boost::mutex mutex_
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
virtual ~ImuFilterRos()
void publishTransform(const ImuMsg::ConstPtr &imu_msg_raw)
#define ROS_WARN_STREAM(args)
void sendTransform(const geometry_msgs::TransformStamped &transform)
ros::NodeHandle nh_
ImuFilter filter_
ros::Timer check_topics_timer_
static bool computeOrientation(WorldFrame::WorldFrame frame, geometry_msgs::Vector3 acceleration, geometry_msgs::Vector3 magneticField, geometry_msgs::Quaternion &orientation)
void setDriftBiasGain(double zeta)
Definition: imu_filter.h:55
void reconfigCallback(FilterConfig &config, uint32_t level)
bool getParam(const std::string &key, std::string &s) const
void publishFilteredMsg(const ImuMsg::ConstPtr &imu_msg_raw)
WorldFrame::WorldFrame world_frame_
tf2_ros::TransformBroadcaster tf_broadcaster_
void imuCallback(const ImuMsg::ConstPtr &imu_msg_raw)
boost::shared_ptr< MagSubscriber > mag_subscriber_
ros::Publisher rpy_filtered_debug_publisher_
#define ROS_ERROR(...)
geometry_msgs::Vector3 mag_bias_


imu_filter_madgwick
Author(s): Ivan Dryanovski
autogenerated on Tue May 7 2019 03:16:55