YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
multipleanalogsensors_nwc_ros2_test.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
7
8#include <yarp/os/Time.h>
11
12#ifndef _USE_MATH_DEFINES
13#define _USE_MATH_DEFINES
14#endif
15
16#include <math.h>
17#include <cmath>
18#include <chrono>
19#include <thread>
20#include <random>
21
22#include <catch2/catch_amalgamated.hpp>
23#include <harness.h>
24
25using namespace std::chrono_literals;
26
27
28TEST_CASE("dev::MultipleAnalogSensors_nwc_ros2_test", "[yarp::dev]")
29{
30 YARP_REQUIRE_PLUGIN("fakeIMU", "device");
31 YARP_REQUIRE_PLUGIN("imu_nws_ros2", "device");
32
33//#if defined(DISABLE_FAILING_TESTS)
34// YARP_SKIP_TEST("Skipping failing tests")
35//#endif
36
38
39 SECTION("Test the nwc alone")
40 {
42
44 pNwc.put("device", "imu_nwc_ros2");
45 pNwc.put("node_name", "imu_node");
46 pNwc.put("topic_name", "/imu_topic");
47 pNwc.put("sensor_name", "imu_sensor");
48 REQUIRE(nwc.open(pNwc)); // multipleanalogsensors nwc open reported successful
49
50 // Close devices
51 nwc.close();
52 }
53
54 SECTION("Test topic data reception")
55 {
59
61
63 std::string node_name = "imu_node";
64 std::string topic_name = "/imu_topic";
65 std::string sensor_name = "imu_sensor";
66 pNwc.put("device", "imu_nwc_ros2");
67 pNwc.put("node_name", node_name);
68 pNwc.put("topic_name", topic_name);
69 pNwc.put("sensor_name", sensor_name);
70 REQUIRE(nwc.open(pNwc)); // multipleanalogsensors nwc open reported successful
71
72 // Check Interfaces
73 REQUIRE(nwc.view(iTestGyro)); // IThreeAxisGyroscopes view reported successul
74 REQUIRE(iTestGyro != nullptr);
75 REQUIRE(nwc.view(iTestAccel)); // IThreeAxisLinearAccelerometers view reported successul
76 REQUIRE(iTestAccel != nullptr);
77 REQUIRE(nwc.view(iTestOrient)); // IOrientationSensors view reported successul
78 REQUIRE(iTestOrient != nullptr);
79
80 std::stringstream callStream;
81
82 // Calculate time stamp
83 uint64_t sec_part;
84 uint64_t nsec_part;
85 double yarpTime = yarp::os::Time::now();
86 sec_part = int(yarpTime);
87 nsec_part = (yarpTime - sec_part)*1000000000UL;
88
89 // Example values
90 std::string frame_name = "test_imu_device";
91 double lower_bound = 0.0;
92 double upper_bound = 45.0;
93 std::uniform_real_distribution<double> unif_orient(lower_bound,upper_bound);
94 std::default_random_engine re;
95 double roll = unif_orient(re);
96 double pitch = unif_orient(re);
97 double yaw = unif_orient(re);
98 double cr = cos((roll*M_PI/180.0) * 0.5);
99 double sr = sin((roll*M_PI/180.0) * 0.5);
100 double cp = cos((pitch*M_PI/180.0) * 0.5);
101 double sp = sin((pitch*M_PI/180.0) * 0.5);
102 double cy = cos((yaw*M_PI/180.0) * 0.5);
103 double sy = sin((yaw*M_PI/180.0) * 0.5);
104
105 double orientW = cr * cp * cy + sr * sp * sy;
106 double orientX = sr * cp * cy - cr * sp * sy;
107 double orientY = cr * sp * cy + sr * cp * sy;
108 double orientZ = cr * cp * sy - sr * sp * cy;
109
110 upper_bound = 5.0;
111 std::uniform_real_distribution<double> unif_angVel(lower_bound,upper_bound);
112 double angSpX = unif_angVel(re);
113 double angSpY = unif_angVel(re);
114 double angSpZ = unif_angVel(re);
115
116 upper_bound = 9.8;
117 std::uniform_real_distribution<double> unif_linAcc(lower_bound,upper_bound);
118 double linAccX = unif_linAcc(re);
119 double linAccY = unif_linAcc(re);
120 double linAccZ = unif_linAcc(re);
121
122 callStream << "ros2 topic pub --once " << topic_name << " sensor_msgs/msg/Imu \"{header: {stamp: {sec: ";
123 callStream << sec_part << ", nanosec: " << nsec_part << "}, frame_id: '" << frame_name << "'}, orientation: {x: ";
124 callStream << orientX << ", y: " << orientY << ", z: " << orientZ << ", w: " << orientW << "}, angular_velocity: ";
125 callStream << "{x: " << angSpX*M_PI/180.0 << ", y: " << angSpY*M_PI/180.0 << ", z: " << angSpZ*M_PI/180.0 << "}, linear_acceleration: ";
126 callStream << "{x: " << linAccX << ", y: " << linAccY << ", z: " << linAccZ << "}}\"";
127
128 system(callStream.str().c_str());
129
131 {
132 std::this_thread::sleep_for(250ms);
133 }
134
135 double timeStamp;
136 std::string gotSensName;
137 std::string gotFrameName;
138 size_t gotSensNum;
139
140 // Test IOrientationSensors
141 yarp::sig::Vector orient(3);
142 gotSensNum = iTestOrient->getNrOfOrientationSensors();
143 REQUIRE(gotSensNum==1);
144 REQUIRE(iTestOrient->getOrientationSensorName(0,gotSensName));
145 REQUIRE(gotSensName==sensor_name);
146 REQUIRE(iTestOrient->getOrientationSensorFrameName(0,gotFrameName));
147 REQUIRE(gotFrameName==frame_name);
148 REQUIRE(iTestOrient->getOrientationSensorMeasureAsRollPitchYaw(0,orient,timeStamp));
149 REQUIRE(roll == Catch::Approx(orient[0]));
150 REQUIRE(pitch == Catch::Approx(orient[1]));
151 REQUIRE(yaw == Catch::Approx(orient[2]));
152
153 // Test IThreeAxisGyroscopes
154 yarp::sig::Vector angSpeed(3);
155 gotSensNum = iTestGyro->getNrOfThreeAxisGyroscopes();
156 REQUIRE(gotSensNum==1);
157 REQUIRE(iTestGyro->getThreeAxisGyroscopeName(0,gotSensName));
158 REQUIRE(gotSensName==sensor_name);
159 REQUIRE(iTestGyro->getThreeAxisGyroscopeFrameName(0,gotFrameName));
160 REQUIRE(gotFrameName==frame_name);
161 REQUIRE(iTestGyro->getThreeAxisGyroscopeMeasure(0,angSpeed,timeStamp));
162 REQUIRE(angSpX == Catch::Approx(angSpeed[0]));
163 REQUIRE(angSpY == Catch::Approx(angSpeed[1]));
164 REQUIRE(angSpZ == Catch::Approx(angSpeed[2]));
165
166 // Test IThreeAxisLinearAccelerometers
167 yarp::sig::Vector linAccel(3);
168 gotSensNum = iTestAccel->getNrOfThreeAxisLinearAccelerometers();
169 REQUIRE(gotSensNum==1);
170 REQUIRE(iTestAccel->getThreeAxisLinearAccelerometerName(0,gotSensName));
171 REQUIRE(gotSensName==sensor_name);
172 REQUIRE(iTestAccel->getThreeAxisLinearAccelerometerFrameName(0,gotFrameName));
173 REQUIRE(gotFrameName==frame_name);
174 REQUIRE(iTestAccel->getThreeAxisLinearAccelerometerMeasure(0,linAccel,timeStamp));
175 REQUIRE(linAccX == Catch::Approx(linAccel[0]));
176 REQUIRE(linAccY == Catch::Approx(linAccel[1]));
177 REQUIRE(linAccZ == Catch::Approx(linAccel[2]));
178
179 // Close devices
180 nwc.close();
181 }
182
184}
#define M_PI
bool view(T *&x)
Get an interface to the device driver.
Device interface to one or multiple orientation sensors, such as IMUs with on board estimation algori...
virtual bool getOrientationSensorFrameName(size_t sens_index, std::string &frameName) const =0
Get the name of the frame of the specified sensor.
virtual bool getOrientationSensorName(size_t sens_index, std::string &name) const =0
Get the name of the specified sensor.
virtual size_t getNrOfOrientationSensors() const =0
Get the number of orientation sensors exposed by this device.
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.
Device interface to one or multiple three axis gyroscopes.
virtual yarp::dev::MAS_status getThreeAxisGyroscopeStatus(size_t sens_index) const =0
Get the status of the specified sensor.
virtual bool getThreeAxisGyroscopeName(size_t sens_index, std::string &name) const =0
Get the name of the specified sensor.
virtual size_t getNrOfThreeAxisGyroscopes() const =0
Get the number of three axis gyroscopes exposed by this sensor.
virtual bool getThreeAxisGyroscopeFrameName(size_t sens_index, std::string &frameName) const =0
Get the name of the frame of the specified sensor.
virtual bool getThreeAxisGyroscopeMeasure(size_t sens_index, yarp::sig::Vector &out, double &timestamp) const =0
Get the last reading of the gyroscope.
Device interface to one or multiple three axis linear accelerometers.
virtual bool getThreeAxisLinearAccelerometerName(size_t sens_index, std::string &name) const =0
Get the name of the specified sensor.
virtual size_t getNrOfThreeAxisLinearAccelerometers() const =0
Get the number of three axis linear accelerometers exposed by this device.
virtual bool getThreeAxisLinearAccelerometerMeasure(size_t sens_index, yarp::sig::Vector &out, double &timestamp) const =0
Get the last reading of the specified sensor.
virtual bool getThreeAxisLinearAccelerometerFrameName(size_t sens_index, std::string &frameName) const =0
Get the name of the frame of the specified sensor.
A container for a device driver.
Definition PolyDriver.h:23
bool close() override
Close the DeviceDriver.
bool open(const std::string &txt)
Construct and configure a device by its common name.
static bool setLocalMode(bool flag)
Chooses whether communication is process-local.
Definition Network.cpp:1049
A class for storing options and configuration information.
Definition Property.h:33
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition Property.cpp:987
TEST_CASE("dev::MultipleAnalogSensors_nwc_ros2_test", "[yarp::dev]")
@ MAS_OK
The sensor is working correctly.
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition Time.cpp:121