ros2_control - rolling
Loading...
Searching...
No Matches
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 <algorithm>
19#include <limits>
20#include <string>
21#include <vector>
22
23#include "semantic_components/semantic_component_interface.hpp"
24#include "sensor_msgs/msg/imu.hpp"
25
26namespace semantic_components
27{
28class IMUSensor : public SemanticComponentInterface<sensor_msgs::msg::Imu>
29{
30public:
31 explicit IMUSensor(const std::string & name)
33 name, {{name + "/" + "orientation.x"},
34 {name + "/" + "orientation.y"},
35 {name + "/" + "orientation.z"},
36 {name + "/" + "orientation.w"},
37 {name + "/" + "angular_velocity.x"},
38 {name + "/" + "angular_velocity.y"},
39 {name + "/" + "angular_velocity.z"},
40 {name + "/" + "linear_acceleration.x"},
41 {name + "/" + "linear_acceleration.y"},
42 {name + "/" + "linear_acceleration.z"}})
43 {
44 }
52 std::array<double, 4> get_orientation() const
53 {
54 update_data_from_interfaces();
55 std::array<double, 4> orientation;
56 std::copy(data_.begin(), data_.begin() + 4, orientation.begin());
57 return orientation;
58 }
59
67 std::array<double, 3> get_angular_velocity() const
68 {
69 update_data_from_interfaces();
70 std::array<double, 3> angular_velocity;
71 std::copy(data_.begin() + 4, data_.begin() + 7, angular_velocity.begin());
72 return angular_velocity;
73 }
74
82 std::array<double, 3> get_linear_acceleration() const
83 {
84 update_data_from_interfaces();
85 std::array<double, 3> linear_acceleration;
86 std::copy(data_.begin() + 7, data_.end(), linear_acceleration.begin());
87 return linear_acceleration;
88 }
89
97 bool get_values_as_message(sensor_msgs::msg::Imu & message) const
98 {
99 update_data_from_interfaces();
100 message.orientation.x = data_[0];
101 message.orientation.y = data_[1];
102 message.orientation.z = data_[2];
103 message.orientation.w = data_[3];
104
105 message.angular_velocity.x = data_[4];
106 message.angular_velocity.y = data_[5];
107 message.angular_velocity.z = data_[6];
108
109 message.linear_acceleration.x = data_[7];
110 message.linear_acceleration.y = data_[8];
111 message.linear_acceleration.z = data_[9];
112
113 return true;
114 }
115
116private:
123 void update_data_from_interfaces() const
124 {
125 for (auto i = 0u; i < data_.size(); ++i)
126 {
127 const auto data = state_interfaces_[i].get().get_optional();
128 if (data.has_value())
129 {
130 data_[i] = data.value();
131 }
132 }
133 }
134
135 // Array to store the data of the IMU sensor
136 mutable std::array<double, 10> data_{{0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}};
137};
138
139} // namespace semantic_components
140
141#endif // SEMANTIC_COMPONENTS__IMU_SENSOR_HPP_
Definition imu_sensor.hpp:29
std::array< double, 3 > get_linear_acceleration() const
Return linear acceleration.
Definition imu_sensor.hpp:82
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:97
std::array< double, 4 > get_orientation() const
Return orientation.
Definition imu_sensor.hpp:52
std::array< double, 3 > get_angular_velocity() const
Return angular velocity.
Definition imu_sensor.hpp:67
Definition semantic_component_interface.hpp:28