imu_sensor_controller.cpp
Go to the documentation of this file.
1 // Copyright (C) 2012, hiDOF INC.
3 // Copyright (C) 2013, PAL Robotics S.L.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright notice,
8 // this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of PAL Robotics S.L. nor the names of its
13 // contributors may be used to endorse or promote products derived from
14 // this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 // POSSIBILITY OF SUCH DAMAGE.
28 
30 
31 
33 
34 
35 
36 namespace imu_sensor_controller
37 {
38 
40  {
41  // get all joint states from the hardware interface
42  const std::vector<std::string>& sensor_names = hw->getNames();
43  for (unsigned i=0; i<sensor_names.size(); i++)
44  ROS_DEBUG("Got sensor %s", sensor_names[i].c_str());
45 
46  // get publishing period
47  if (!controller_nh.getParam("publish_rate", publish_rate_)){
48  ROS_ERROR("Parameter 'publish_rate' not set");
49  return false;
50  }
51 
52  for (unsigned i=0; i<sensor_names.size(); i++){
53  // sensor handle
54  sensors_.push_back(hw->getHandle(sensor_names[i]));
55 
56  // realtime publisher
57  RtPublisherPtr rt_pub(new realtime_tools::RealtimePublisher<sensor_msgs::Imu>(root_nh, sensor_names[i], 4));
58  realtime_pubs_.push_back(rt_pub);
59  }
60 
61  // Last published times
62  last_publish_times_.resize(sensor_names.size());
63  return true;
64  }
65 
67  {
68  // initialize time
69  for (unsigned i=0; i<last_publish_times_.size(); i++){
70  last_publish_times_[i] = time;
71  }
72  }
73 
74  void ImuSensorController::update(const ros::Time& time, const ros::Duration& /*period*/)
75  {
76  using namespace hardware_interface;
77 
78  // limit rate of publishing
79  for (unsigned i=0; i<realtime_pubs_.size(); i++){
80  if (publish_rate_ > 0.0 && last_publish_times_[i] + ros::Duration(1.0/publish_rate_) < time){
81  // try to publish
82  if (realtime_pubs_[i]->trylock()){
83  // we're actually publishing, so increment time
85 
86  // populate message
87  realtime_pubs_[i]->msg_.header.stamp = time;
88  realtime_pubs_[i]->msg_.header.frame_id = sensors_[i].getFrameId();
89 
90  // Orientation
91  if (sensors_[i].getOrientation())
92  {
93  realtime_pubs_[i]->msg_.orientation.x = sensors_[i].getOrientation()[0];
94  realtime_pubs_[i]->msg_.orientation.y = sensors_[i].getOrientation()[1];
95  realtime_pubs_[i]->msg_.orientation.z = sensors_[i].getOrientation()[2];
96  realtime_pubs_[i]->msg_.orientation.w = sensors_[i].getOrientation()[3];
97  }
98 
99  // Orientation covariance
100  if (sensors_[i].getOrientationCovariance())
101  {
102  for (unsigned j=0; j<realtime_pubs_[i]->msg_.orientation_covariance.size(); ++j){
103  realtime_pubs_[i]->msg_.orientation_covariance[j] = sensors_[i].getOrientationCovariance()[j];
104  }
105  }
106  else
107  {
108  if (sensors_[i].getOrientation())
109  {
110  // Orientation available
111  for (unsigned j=0; j<realtime_pubs_[i]->msg_.orientation_covariance.size(); ++j){
112  realtime_pubs_[i]->msg_.orientation_covariance[j] = 0.0;
113  }
114  }
115  else
116  {
117  // No orientation available
118  realtime_pubs_[i]->msg_.orientation_covariance[0] = -1.0;
119  }
120  }
121 
122  // Angular velocity
123  if (sensors_[i].getAngularVelocity())
124  {
125  realtime_pubs_[i]->msg_.angular_velocity.x = sensors_[i].getAngularVelocity()[0];
126  realtime_pubs_[i]->msg_.angular_velocity.y = sensors_[i].getAngularVelocity()[1];
127  realtime_pubs_[i]->msg_.angular_velocity.z = sensors_[i].getAngularVelocity()[2];
128  }
129 
130  // Angular velocity covariance
131  if (sensors_[i].getAngularVelocityCovariance())
132  {
133  for (unsigned j=0; j<realtime_pubs_[i]->msg_.angular_velocity_covariance.size(); ++j){
134  realtime_pubs_[i]->msg_.angular_velocity_covariance[j] = sensors_[i].getAngularVelocityCovariance()[j];
135  }
136  }
137  else
138  {
139  if (sensors_[i].getAngularVelocity())
140  {
141  // Angular velocity available
142  for (unsigned j=0; j<realtime_pubs_[i]->msg_.angular_velocity_covariance.size(); ++j){
143  realtime_pubs_[i]->msg_.angular_velocity_covariance[j] = 0.0;
144  }
145  }
146  else
147  {
148  // No angular velocity available
149  realtime_pubs_[i]->msg_.angular_velocity_covariance[0] = -1.0;
150  }
151  }
152 
153  // Linear acceleration
154  if (sensors_[i].getLinearAcceleration())
155  {
156  realtime_pubs_[i]->msg_.linear_acceleration.x = sensors_[i].getLinearAcceleration()[0];
157  realtime_pubs_[i]->msg_.linear_acceleration.y = sensors_[i].getLinearAcceleration()[1];
158  realtime_pubs_[i]->msg_.linear_acceleration.z = sensors_[i].getLinearAcceleration()[2];
159  }
160 
161  // Linear acceleration covariance
162  if (sensors_[i].getLinearAccelerationCovariance())
163  {
164  for (unsigned j=0; j<realtime_pubs_[i]->msg_.linear_acceleration_covariance.size(); ++j){
165  realtime_pubs_[i]->msg_.linear_acceleration_covariance[j] = sensors_[i].getLinearAccelerationCovariance()[j];
166  }
167  }
168  else
169  {
170  if (sensors_[i].getLinearAcceleration())
171  {
172  // Linear acceleration available
173  for (unsigned j=0; j<realtime_pubs_[i]->msg_.linear_acceleration_covariance.size(); ++j){
174  realtime_pubs_[i]->msg_.linear_acceleration_covariance[j] = 0.0;
175  }
176  }
177  else
178  {
179  // No linear acceleration available
180  realtime_pubs_[i]->msg_.linear_acceleration_covariance[0] = -1.0;
181  }
182  }
183 
184  realtime_pubs_[i]->unlockAndPublish();
185  }
186  }
187  }
188  }
189 
191  {}
192 
193 }
194 
virtual void update(const ros::Time &time, const ros::Duration &)
virtual bool init(hardware_interface::ImuSensorInterface *hw, ros::NodeHandle &root_nh, ros::NodeHandle &controller_nh)
std::vector< RtPublisherPtr > realtime_pubs_
std::vector< hardware_interface::ImuSensorHandle > sensors_
virtual void starting(const ros::Time &time)
ImuSensorHandle getHandle(const std::string &name)
bool getParam(const std::string &key, std::string &s) const
std::vector< std::string > getNames() const
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
#define ROS_ERROR(...)
#define ROS_DEBUG(...)


imu_sensor_controller
Author(s): Adolfo Rodriguez Tsouroukdissian
autogenerated on Thu Apr 11 2019 03:08:32