YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Imu_nws_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_nws_ros2.h"
16#include <tf2/LinearMath/Quaternion.h>
17
18YARP_LOG_COMPONENT(GENERICSENSOR_NWS_ROS2, "yarp.device.Imu_nws_ros2")
19
20bool Imu_nws_ros2::viewInterfaces()
21{
22 // View all the interfaces
23 bool ok = true;
24 ok = m_poly->view(m_iThreeAxisGyroscopes);
25 if (!ok) {
26 yCError(GENERICSENSOR_NWS_ROS2) << "IThreeAxisGyroscopes interface is not available";
27 return false;
28 }
29 ok = m_poly->view(m_iThreeAxisLinearAccelerometers);
30 if (!ok) {
31 yCError(GENERICSENSOR_NWS_ROS2) << "IThreeAxisLinearAccelerometers interface is not available";
32 return false;
33 }
34 ok = m_poly->view(m_iThreeAxisMagnetometers);
35 if (!ok) {
36 yCError(GENERICSENSOR_NWS_ROS2) << "IThreeAxisMagnetometers interface is not available";
37 return false;
38 }
39 ok = m_poly->view(m_iOrientationSensors);
40 if (!ok) {
41 yCError(GENERICSENSOR_NWS_ROS2) << "IOrientationSensors interface is not available";
42 return false;
43 }
44
45 ok = m_iThreeAxisGyroscopes->getThreeAxisGyroscopeFrameName(m_sens_index, m_framename);
46 return ok;
47}
48
50{
51 if (m_publisher)
52 {
53 yarp::sig::Vector vecgyr(3);
54 yarp::sig::Vector vecacc(3);
55 yarp::sig::Vector vecrpy(3);
56 tf2::Quaternion quat;
57 sensor_msgs::msg::Imu imu_ros_data;
58
59 m_iThreeAxisGyroscopes->getThreeAxisGyroscopeMeasure(m_sens_index, vecgyr, m_timestamp);
60 m_iThreeAxisLinearAccelerometers->getThreeAxisLinearAccelerometerMeasure(m_sens_index, vecacc, m_timestamp);
62 quat.setRPY(vecrpy[0] * M_PI / 180.0, vecrpy[1] * M_PI / 180.0, vecrpy[2] * M_PI / 180.0);
63 quat.normalize();
64 imu_ros_data.header.frame_id = m_framename;
65 imu_ros_data.header.stamp = ros2TimeFromYarp(m_timestamp);
66 imu_ros_data.angular_velocity.x = vecgyr[0] * M_PI / 180.0;
67 imu_ros_data.angular_velocity.y = vecgyr[1] * M_PI / 180.0;
68 imu_ros_data.angular_velocity.z = vecgyr[2] * M_PI / 180.0;
69 imu_ros_data.linear_acceleration.x = vecacc[0];
70 imu_ros_data.linear_acceleration.y = vecacc[1];
71 imu_ros_data.linear_acceleration.z = vecacc[2];
72 imu_ros_data.orientation.x = quat.x();
73 imu_ros_data.orientation.y = quat.y();
74 imu_ros_data.orientation.z = quat.z();
75 imu_ros_data.orientation.w = quat.w();
76 m_publisher->publish(imu_ros_data);
77 }
78}
#define M_PI
const yarp::os::LogComponent & GENERICSENSOR_NWS_ROS2()
builtin_interfaces::msg::Time ros2TimeFromYarp(double yarpTime)
Definition Ros2Utils.cpp:28
rclcpp::Publisher< sensor_msgs::msg::Imu >::SharedPtr m_publisher
IMU_nws_ros2: This wrapper connects to a device and publishes a ROS topic of type sensor_msgs::Imu.
void run() override
Loop function.
bool view(T *&x)
Get an interface to the device driver.
virtual bool getOrientationSensorMeasureAsRollPitchYaw(size_t sens_index, yarp::sig::Vector &rpy, double &timestamp) const =0
Get the last reading of the orientation sensor as roll pitch yaw.
virtual bool getThreeAxisGyroscopeMeasure(size_t sens_index, yarp::sig::Vector &out, double &timestamp) const =0
Get the last reading of the gyroscope.
virtual bool getThreeAxisLinearAccelerometerMeasure(size_t sens_index, yarp::sig::Vector &out, double &timestamp) const =0
Get the last reading of the specified sensor.
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)