ros2_control - rolling
imu_sensor.hpp
1 // Copyright 2021 PAL Robotics SL.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SEMANTIC_COMPONENTS__IMU_SENSOR_HPP_
16 #define SEMANTIC_COMPONENTS__IMU_SENSOR_HPP_
17 
18 #include <limits>
19 #include <string>
20 #include <vector>
21 
22 #include "semantic_components/semantic_component_interface.hpp"
23 #include "sensor_msgs/msg/imu.hpp"
24 
25 namespace semantic_components
26 {
27 class IMUSensor : public SemanticComponentInterface<sensor_msgs::msg::Imu>
28 {
29 public:
30  explicit IMUSensor(const std::string & name) : SemanticComponentInterface(name, 10)
31  {
32  interface_names_.emplace_back(name_ + "/" + "orientation.x");
33  interface_names_.emplace_back(name_ + "/" + "orientation.y");
34  interface_names_.emplace_back(name_ + "/" + "orientation.z");
35  interface_names_.emplace_back(name_ + "/" + "orientation.w");
36  interface_names_.emplace_back(name_ + "/" + "angular_velocity.x");
37  interface_names_.emplace_back(name_ + "/" + "angular_velocity.y");
38  interface_names_.emplace_back(name_ + "/" + "angular_velocity.z");
39  interface_names_.emplace_back(name_ + "/" + "linear_acceleration.x");
40  interface_names_.emplace_back(name_ + "/" + "linear_acceleration.y");
41  interface_names_.emplace_back(name_ + "/" + "linear_acceleration.z");
42 
43  // Set default values to NaN
44  orientation_.fill(std::numeric_limits<double>::quiet_NaN());
45  angular_velocity_.fill(std::numeric_limits<double>::quiet_NaN());
46  linear_acceleration_.fill(std::numeric_limits<double>::quiet_NaN());
47  }
48 
49  virtual ~IMUSensor() = default;
50 
52 
57  std::array<double, 4> get_orientation()
58  {
59  size_t interface_offset = 0;
60  for (size_t i = 0; i < orientation_.size(); ++i)
61  {
62  orientation_[i] = state_interfaces_[interface_offset + i].get().get_value();
63  }
64  return orientation_;
65  }
66 
68 
73  std::array<double, 3> get_angular_velocity()
74  {
75  size_t interface_offset = orientation_.size();
76  for (size_t i = 0; i < angular_velocity_.size(); ++i)
77  {
78  angular_velocity_[i] = state_interfaces_[interface_offset + i].get().get_value();
79  }
80  return angular_velocity_;
81  }
82 
84 
89  std::array<double, 3> get_linear_acceleration()
90  {
91  size_t interface_offset = orientation_.size() + angular_velocity_.size();
92  for (size_t i = 0; i < linear_acceleration_.size(); ++i)
93  {
94  linear_acceleration_[i] = state_interfaces_[interface_offset + i].get().get_value();
95  }
96  return linear_acceleration_;
97  }
98 
100 
104  bool get_values_as_message(sensor_msgs::msg::Imu & message)
105  {
106  // call get_orientation() and get_angular_velocity() get_linear_acceleration() to
107  // update with the latest values
108  get_orientation();
111 
112  // update the message values, covariances unknown
113  message.orientation.x = orientation_[0];
114  message.orientation.y = orientation_[1];
115  message.orientation.z = orientation_[2];
116  message.orientation.w = orientation_[3];
117 
118  message.angular_velocity.x = angular_velocity_[0];
119  message.angular_velocity.y = angular_velocity_[1];
120  message.angular_velocity.z = angular_velocity_[2];
121 
122  message.linear_acceleration.x = linear_acceleration_[0];
123  message.linear_acceleration.y = linear_acceleration_[1];
124  message.linear_acceleration.z = linear_acceleration_[2];
125 
126  return true;
127  }
128 
129 protected:
130  // Order is: orientation X,Y,Z,W angular velocity X,Y,Z and linear acceleration X,Y,Z
131  std::array<double, 4> orientation_;
132  std::array<double, 3> angular_velocity_;
133  std::array<double, 3> linear_acceleration_;
134 };
135 
136 } // namespace semantic_components
137 
138 #endif // SEMANTIC_COMPONENTS__IMU_SENSOR_HPP_
Definition: imu_sensor.hpp:28
std::array< double, 3 > get_linear_acceleration()
Return linear acceleration.
Definition: imu_sensor.hpp:89
std::array< double, 3 > get_angular_velocity()
Return angular velocity.
Definition: imu_sensor.hpp:73
std::array< double, 4 > get_orientation()
Return orientation.
Definition: imu_sensor.hpp:57
bool get_values_as_message(sensor_msgs::msg::Imu &message)
Return Imu message with orientation, angular velocity and linear acceleration.
Definition: imu_sensor.hpp:104
Definition: semantic_component_interface.hpp:28