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)
32  name, {{name + "/" + "orientation.x"},
33  {name + "/" + "orientation.y"},
34  {name + "/" + "orientation.z"},
35  {name + "/" + "orientation.w"},
36  {name + "/" + "angular_velocity.x"},
37  {name + "/" + "angular_velocity.y"},
38  {name + "/" + "angular_velocity.z"},
39  {name + "/" + "linear_acceleration.x"},
40  {name + "/" + "linear_acceleration.y"},
41  {name + "/" + "linear_acceleration.z"}})
42  {
43  }
45 
50  std::array<double, 4> get_orientation() const
51  {
52  std::array<double, 4> orientation;
53  for (auto i = 0u; i < orientation.size(); ++i)
54  {
55  orientation[i] = state_interfaces_[i].get().get_value();
56  }
57  return orientation;
58  }
59 
61 
66  std::array<double, 3> get_angular_velocity() const
67  {
68  std::array<double, 3> angular_velocity;
69  const std::size_t interface_offset{4};
70  for (auto i = 0u; i < angular_velocity.size(); ++i)
71  {
72  angular_velocity[i] = state_interfaces_[interface_offset + i].get().get_value();
73  }
74  return angular_velocity;
75  }
76 
78 
83  std::array<double, 3> get_linear_acceleration() const
84  {
85  std::array<double, 3> linear_acceleration;
86  const std::size_t interface_offset{7};
87  for (auto i = 0u; i < linear_acceleration.size(); ++i)
88  {
89  linear_acceleration[i] = state_interfaces_[interface_offset + i].get().get_value();
90  }
91  return linear_acceleration;
92  }
93 
95 
99  bool get_values_as_message(sensor_msgs::msg::Imu & message) const
100  {
101  const auto [orientation_x, orientation_y, orientation_z, orientation_w] = get_orientation();
102  const auto [angular_velocity_x, angular_velocity_y, angular_velocity_z] =
104  const auto [linear_acceleration_x, linear_acceleration_y, linear_acceleration_z] =
106 
107  message.orientation.x = orientation_x;
108  message.orientation.y = orientation_y;
109  message.orientation.z = orientation_z;
110  message.orientation.w = orientation_w;
111 
112  message.angular_velocity.x = angular_velocity_x;
113  message.angular_velocity.y = angular_velocity_y;
114  message.angular_velocity.z = angular_velocity_z;
115 
116  message.linear_acceleration.x = linear_acceleration_x;
117  message.linear_acceleration.y = linear_acceleration_y;
118  message.linear_acceleration.z = linear_acceleration_z;
119 
120  return true;
121  }
122 };
123 
124 } // namespace semantic_components
125 
126 #endif // SEMANTIC_COMPONENTS__IMU_SENSOR_HPP_
Definition: imu_sensor.hpp:28
std::array< double, 3 > get_angular_velocity() const
Return angular velocity.
Definition: imu_sensor.hpp:66
bool get_values_as_message(sensor_msgs::msg::Imu &message) const
Return Imu message with orientation, angular velocity and linear acceleration.
Definition: imu_sensor.hpp:99
std::array< double, 3 > get_linear_acceleration() const
Return linear acceleration.
Definition: imu_sensor.hpp:83
std::array< double, 4 > get_orientation() const
Return orientation.
Definition: imu_sensor.hpp:50
Definition: semantic_component_interface.hpp:28