YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Imu_nwc_ros2.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2023 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6// We define and include this first to ensure
7// that no transitivetely-included header already
8// includes cmath withut defining _USE_MATH_DEFINES
9#ifndef _USE_MATH_DEFINES
10#define _USE_MATH_DEFINES
11#endif
12
13#include <cmath>
14
15#include "Imu_nwc_ros2.h"
16#include <tf2/LinearMath/Quaternion.h>
17
18YARP_LOG_COMPONENT(GENERICSENSOR_NWC_ROS2, "yarp.device.imu_nwc_ros2")
19
20void Imu_nwc_ros2::subscription_callback(const std::shared_ptr<sensor_msgs::msg::Imu> msg)
21{
22 std::lock_guard<std::mutex> dataGuard(m_dataMutex);
23 if(m_internalStatus == yarp::dev::MAS_status::MAS_WAITING_FOR_FIRST_READ) { m_internalStatus = yarp::dev::MAS_status::MAS_OK; }
24 m_currentData = *msg;
25}
26
27// IThreeAxisLinearAccelerometers ------------------------------------------------------------------------------------------------- START //
28bool Imu_nwc_ros2::getThreeAxisLinearAccelerometerMeasure(size_t sens_index, yarp::sig::Vector& out, double& timestamp) const
29{
30 std::lock_guard<std::mutex> dataGuard(m_dataMutex);
31
32 YARP_UNUSED(sens_index);
34 {
35 yCError(GENERICSENSOR_NWC_ROS2) << "No data received yet";
36 return false;
37 }
38 yarp::sig::Vector accelerations(3);
39 timestamp = yarpTimeFromRos2(m_currentData.header.stamp);
40
41 accelerations[0] = m_currentData.linear_acceleration.x;
42 accelerations[1] = m_currentData.linear_acceleration.y;
43 accelerations[2] = m_currentData.linear_acceleration.z;
44
45 out = accelerations;
46
47 return true;
48}
49
50bool Imu_nwc_ros2::getThreeAxisLinearAccelerometerName(size_t sens_index, std::string &name) const
51{
52 YARP_UNUSED(sens_index);
53 name = m_sensor_name;
54
55 return true;
56}
57
58bool Imu_nwc_ros2::getThreeAxisLinearAccelerometerFrameName(size_t sens_index, std::string &frameName) const
59{
60 std::lock_guard<std::mutex> dataGuard(m_dataMutex);
61
62 YARP_UNUSED(sens_index);
64 {
65 yCError(GENERICSENSOR_NWC_ROS2) << "No data received yet";
66 return false;
67 }
68 frameName = m_currentData.header.frame_id;
69
70 return true;
71}
72
74{
75 return 1;
76}
77
82// IThreeAxisLinearAccelerometers --------------------------------------------------------------------------------------------------- END //
83
84// IThreeAxisGyroscopes ----------------------------------------------------------------------------------------------------------- START //
85bool Imu_nwc_ros2::getThreeAxisGyroscopeMeasure(size_t sens_index, yarp::sig::Vector& out, double& timestamp) const
86{
87 std::lock_guard<std::mutex> dataGuard(m_dataMutex);
88
89 YARP_UNUSED(sens_index);
91 {
92 yCError(GENERICSENSOR_NWC_ROS2) << "No data received yet";
93 return false;
94 }
95 yarp::sig::Vector angularVelocities(3);
96 timestamp = yarpTimeFromRos2(m_currentData.header.stamp);
97
98 angularVelocities[0] = m_currentData.angular_velocity.x * 180.0 / M_PI;
99 angularVelocities[1] = m_currentData.angular_velocity.y * 180.0 / M_PI;
100 angularVelocities[2] = m_currentData.angular_velocity.z * 180.0 / M_PI;
101
102 out = angularVelocities;
103
104 return true;
105}
106
107bool Imu_nwc_ros2::getThreeAxisGyroscopeName(size_t sens_index, std::string &name) const
108{
109 YARP_UNUSED(sens_index);
110 name = m_sensor_name;
111
112 return true;
113}
114
115bool Imu_nwc_ros2::getThreeAxisGyroscopeFrameName(size_t sens_index, std::string &frameName) const
116{
117 std::lock_guard<std::mutex> dataGuard(m_dataMutex);
118
119 YARP_UNUSED(sens_index);
121 {
122 yCError(GENERICSENSOR_NWC_ROS2) << "No data received yet";
123 return false;
124 }
125 frameName = m_currentData.header.frame_id;
126
127 return true;
128}
129
131{
132 return 1;
133}
134
139// IThreeAxisGyroscopes ------------------------------------------------------------------------------------------------------------- END //
140
141// IOrientationSensors ------------------------------------------------------------------------------------------------------------ START //
142bool Imu_nwc_ros2::getOrientationSensorMeasureAsRollPitchYaw(size_t sens_index, yarp::sig::Vector& rpy, double& timestamp) const
143{
144 std::lock_guard<std::mutex> dataGuard(m_dataMutex);
145
146 YARP_UNUSED(sens_index);
148 {
149 yCError(GENERICSENSOR_NWC_ROS2) << "No data received yet";
150 return false;
151 }
152 yarp::sig::Vector orient(3);
153 tf2Scalar roll, pitch, yaw;
154 tf2::Quaternion tempQuat;
155 tempQuat.setX(m_currentData.orientation.x);
156 tempQuat.setY(m_currentData.orientation.y);
157 tempQuat.setZ(m_currentData.orientation.z);
158 tempQuat.setW(m_currentData.orientation.w);
159 tf2::Matrix3x3 tempMat(tempQuat);
160 tempMat.getRPY(roll, pitch, yaw);
161
162 timestamp = yarpTimeFromRos2(m_currentData.header.stamp);
163
164 orient[0] = roll * 180.0 / M_PI;
165 orient[1] = pitch * 180.0 / M_PI;
166 orient[2] = yaw * 180.0 / M_PI;
167
168 rpy = orient;
169
170 return true;
171}
172
173bool Imu_nwc_ros2::getOrientationSensorName(size_t sens_index, std::string &name) const
174{
175 YARP_UNUSED(sens_index);
176 name = m_sensor_name;
177
178 return true;
179}
180
181bool Imu_nwc_ros2::getOrientationSensorFrameName(size_t sens_index, std::string &frameName) const
182{
183 std::lock_guard<std::mutex> dataGuard(m_dataMutex);
184
185 YARP_UNUSED(sens_index);
187 {
188 yCError(GENERICSENSOR_NWC_ROS2) << "No data received yet";
189 return false;
190 }
191 frameName = m_currentData.header.frame_id;
192
193 return true;
194}
195
197{
198 return 1;
199}
200
205// IOrientationSensors -------------------------------------------------------------------------------------------------------------- END //
#define M_PI
const yarp::os::LogComponent & GENERICSENSOR_NWC_ROS2()
const yarp::os::LogComponent & GENERICSENSOR_NWC_ROS2()
double yarpTimeFromRos2(builtin_interfaces::msg::Time ros2Time)
Definition Ros2Utils.cpp:40
IMU_nwc_ros2: This wrapper connects to a device and publishes a ROS topic of type sensor_msgs::Imu.
yarp::dev::MAS_status getOrientationSensorStatus(size_t sens_index) const override
Get the status of the specified sensor.
bool getThreeAxisGyroscopeName(size_t sens_index, std::string &name) const override
Get the name of the specified sensor.
yarp::dev::MAS_status getThreeAxisGyroscopeStatus(size_t sens_index) const override
Get the status of the specified sensor.
bool getOrientationSensorMeasureAsRollPitchYaw(size_t sens_index, yarp::sig::Vector &rpy, double &timestamp) const override
Get the last reading of the orientation sensor as roll pitch yaw.
bool getThreeAxisLinearAccelerometerMeasure(size_t sens_index, yarp::sig::Vector &out, double &timestamp) const override
Get the last reading of the specified sensor.
size_t getNrOfOrientationSensors() const override
Get the number of orientation sensors exposed by this device.
bool getOrientationSensorName(size_t sens_index, std::string &name) const override
Get the name of the specified sensor.
bool getThreeAxisLinearAccelerometerName(size_t sens_index, std::string &name) const override
Get the name of the specified sensor.
bool getThreeAxisGyroscopeFrameName(size_t sens_index, std::string &frameName) const override
Get the name of the frame of the specified sensor.
size_t getNrOfThreeAxisGyroscopes() const override
Get the number of three axis gyroscopes exposed by this sensor.
yarp::dev::MAS_status getThreeAxisLinearAccelerometerStatus(size_t sens_index) const override
Get the status of the specified sensor.
bool getThreeAxisLinearAccelerometerFrameName(size_t sens_index, std::string &frameName) const override
Get the name of the frame of the specified sensor.
bool getOrientationSensorFrameName(size_t sens_index, std::string &frameName) const override
Get the name of the frame of the specified sensor.
size_t getNrOfThreeAxisLinearAccelerometers() const override
Get the number of three axis linear accelerometers exposed by this device.
bool getThreeAxisGyroscopeMeasure(size_t sens_index, yarp::sig::Vector &out, double &timestamp) const override
Get the last reading of the gyroscope.
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)
STL namespace.
MAS_status
Status of a given analog sensor exposed by a multiple analog sensors interface.
@ MAS_WAITING_FOR_FIRST_READ
The sensor is read through the network, and the device is waiting to receive the first measurement.
@ MAS_OK
The sensor is working correctly.
#define YARP_UNUSED(var)
Definition api.h:162