imupublisher.h
Go to the documentation of this file.
1 
2 // Copyright (c) 2003-2021 Xsens Technologies B.V. or subsidiaries worldwide.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions, and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions, and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the names of the copyright holders nor the names of their contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
26 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
28 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
29 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
30 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
31 //
32 
33 #ifndef IMUPUBLISHER_H
34 #define IMUPUBLISHER_H
35 
36 #include "packetcallback.h"
37 #include <sensor_msgs/Imu.h>
39 
40 static void variance_from_stddev_param(std::string param, double *variance_out)
41 {
42  std::vector<double> stddev;
43  if (ros::param::get(param, stddev))
44  {
45  if (stddev.size() == 3)
46  {
47  auto squared = [](double x) { return x * x; };
48  std::transform(stddev.begin(), stddev.end(), variance_out, squared);
49  }
50  else
51  {
52  ROS_WARN("Wrong size of param: %s, must be of size 3", param.c_str());
53  }
54  }
55  else
56  {
57  memset(variance_out, 0, 3 * sizeof(double));
58  }
59 }
60 
62 {
66  double frequency_tolerance = 0.1;
68  std::string frame_id = DEFAULT_FRAME_ID;
69 
70 
74 
76  expected_frequency(ros::param::param("~expected_frequency", 100)),
77  inner_pub(node.advertise<sensor_msgs::Imu>("imu/data", ros::param::param("~publisher_queue_size", 5))),
78  updater(node),
80  {
81  updater.setHardwareID("none");
82  ros::param::get("~frame_id", frame_id);
83 
84  // REP 145: Conventions for IMU Sensor Drivers (http://www.ros.org/reps/rep-0145.html)
85  variance_from_stddev_param("~orientation_stddev", orientation_variance);
86  variance_from_stddev_param("~angular_velocity_stddev", angular_velocity_variance);
87  variance_from_stddev_param("~linear_acceleration_stddev", linear_acceleration_variance);
88  }
89 
90  void operator()(const XsDataPacket &packet, ros::Time timestamp)
91  {
92  bool quaternion_available = packet.containsOrientation();
93  bool gyro_available = packet.containsCalibratedGyroscopeData();
94  bool accel_available = packet.containsCalibratedAcceleration();
95 
96  geometry_msgs::Quaternion quaternion;
97  if (quaternion_available)
98  {
99  XsQuaternion q = packet.orientationQuaternion();
100 
101  quaternion.w = q.w();
102  quaternion.x = q.x();
103  quaternion.y = q.y();
104  quaternion.z = q.z();
105  }
106 
107  geometry_msgs::Vector3 gyro;
108  if (gyro_available)
109  {
110  XsVector g = packet.calibratedGyroscopeData();
111  gyro.x = g[0];
112  gyro.y = g[1];
113  gyro.z = g[2];
114  }
115 
116  geometry_msgs::Vector3 accel;
117  if (accel_available)
118  {
119  XsVector a = packet.calibratedAcceleration();
120  accel.x = a[0];
121  accel.y = a[1];
122  accel.z = a[2];
123  }
124 
125  // Imu message, publish if any of the fields is available
126  if (quaternion_available || accel_available || gyro_available)
127  {
128  sensor_msgs::Imu msg;
129 
130  msg.header.stamp = timestamp;
131  msg.header.frame_id = frame_id;
132 
133  msg.orientation = quaternion;
134  if (quaternion_available)
135  {
136  msg.orientation_covariance[0] = orientation_variance[0];
137  msg.orientation_covariance[4] = orientation_variance[1];
138  msg.orientation_covariance[8] = orientation_variance[2];
139  }
140  else
141  {
142  msg.orientation_covariance[0] = -1; // mark as not available
143  }
144 
145  msg.angular_velocity = gyro;
146  if (gyro_available)
147  {
148  msg.angular_velocity_covariance[0] = angular_velocity_variance[0];
149  msg.angular_velocity_covariance[4] = angular_velocity_variance[1];
150  msg.angular_velocity_covariance[8] = angular_velocity_variance[2];
151  }
152  else
153  {
154  msg.angular_velocity_covariance[0] = -1; // mark as not available
155  }
156 
157  msg.linear_acceleration = accel;
158  if (accel_available)
159  {
160  msg.linear_acceleration_covariance[0] = linear_acceleration_variance[0];
161  msg.linear_acceleration_covariance[4] = linear_acceleration_variance[1];
162  msg.linear_acceleration_covariance[8] = linear_acceleration_variance[2];
163  }
164  else
165  {
166  msg.linear_acceleration_covariance[0] = -1; // mark as not available
167  }
168 
169  pub.publish(msg);
170  updater.update();
171  }
172  }
173 };
174 
175 #endif
ImuPublisher::updater
diagnostic_updater::Updater updater
Definition: imupublisher.h:64
XsVector
A class that represents a vector of real numbers.
Definition: xsvector.h:113
msg
msg
ros::Publisher
ImuPublisher::pub
diagnostic_updater::DiagnosedPublisher< sensor_msgs::Imu > pub
Definition: imupublisher.h:67
DEFAULT_FRAME_ID
const char * DEFAULT_FRAME_ID
Definition: packetcallback.h:39
ros::param::get
ROSCPP_DECL bool get(const std::string &key, bool &b)
ros
packetcallback.h
ImuPublisher::expected_frequency
double expected_frequency
Definition: imupublisher.h:65
diagnostic_updater::Updater
XsDataPacket
Contains an interpreted data message. The class provides easy access to the contained data through it...
Definition: xsdatapacket.h:301
ImuPublisher::operator()
void operator()(const XsDataPacket &packet, ros::Time timestamp)
Definition: imupublisher.h:90
ImuPublisher::inner_pub
ros::Publisher inner_pub
Definition: imupublisher.h:63
XsQuaternion
A class that implements a quaternion.
Definition: xsquaternion.h:102
publisher.h
diagnostic_updater::DiagnosedPublisher::publish
virtual void publish(const boost::shared_ptr< T > &message)
ImuPublisher::ImuPublisher
ImuPublisher(ros::NodeHandle &node)
Definition: imupublisher.h:75
diagnostic_updater::DiagnosedPublisher< sensor_msgs::Imu >
ImuPublisher::orientation_variance
double orientation_variance[3]
Definition: imupublisher.h:71
ImuPublisher::frame_id
std::string frame_id
Definition: imupublisher.h:68
ROS_WARN
#define ROS_WARN(...)
ImuPublisher::linear_acceleration_variance
double linear_acceleration_variance[3]
Definition: imupublisher.h:72
variance_from_stddev_param
static void variance_from_stddev_param(std::string param, double *variance_out)
Definition: imupublisher.h:40
diagnostic_updater::Updater::setHardwareID
void setHardwareID(const std::string &hwid)
diagnostic_updater::Updater::update
void update()
ImuPublisher::angular_velocity_variance
double angular_velocity_variance[3]
Definition: imupublisher.h:73
ros::Time
PacketCallback
Definition: packetcallback.h:41
ImuPublisher
Definition: imupublisher.h:61
param
T param(const std::string &param_name, const T &default_val)
sensor_msgs
ImuPublisher::frequency_tolerance
double frequency_tolerance
Definition: imupublisher.h:66
ros::NodeHandle


xsens_mti_driver
Author(s):
autogenerated on Sun Sep 3 2023 02:43:20