12#ifndef _USE_MATH_DEFINES
13#define _USE_MATH_DEFINES
22#include <catch2/catch_amalgamated.hpp>
25using namespace std::chrono_literals;
28TEST_CASE(
"dev::MultipleAnalogSensors_nwc_ros2_test",
"[yarp::dev]")
30 YARP_REQUIRE_PLUGIN(
"fakeIMU",
"device");
31 YARP_REQUIRE_PLUGIN(
"imu_nws_ros2",
"device");
39 SECTION(
"Test the nwc alone")
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));
54 SECTION(
"Test topic data reception")
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));
73 REQUIRE(nwc.
view(iTestGyro));
74 REQUIRE(iTestGyro !=
nullptr);
75 REQUIRE(nwc.
view(iTestAccel));
76 REQUIRE(iTestAccel !=
nullptr);
77 REQUIRE(nwc.
view(iTestOrient));
78 REQUIRE(iTestOrient !=
nullptr);
80 std::stringstream callStream;
86 sec_part = int(yarpTime);
87 nsec_part = (yarpTime - sec_part)*1000000000UL;
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);
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;
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);
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);
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 <<
"}}\"";
128 system(callStream.str().c_str());
132 std::this_thread::sleep_for(250ms);
136 std::string gotSensName;
137 std::string gotFrameName;
143 REQUIRE(gotSensNum==1);
145 REQUIRE(gotSensName==sensor_name);
147 REQUIRE(gotFrameName==frame_name);
149 REQUIRE(roll == Catch::Approx(orient[0]));
150 REQUIRE(pitch == Catch::Approx(orient[1]));
151 REQUIRE(yaw == Catch::Approx(orient[2]));
156 REQUIRE(gotSensNum==1);
158 REQUIRE(gotSensName==sensor_name);
160 REQUIRE(gotFrameName==frame_name);
162 REQUIRE(angSpX == Catch::Approx(angSpeed[0]));
163 REQUIRE(angSpY == Catch::Approx(angSpeed[1]));
164 REQUIRE(angSpZ == Catch::Approx(angSpeed[2]));
169 REQUIRE(gotSensNum==1);
171 REQUIRE(gotSensName==sensor_name);
173 REQUIRE(gotFrameName==frame_name);
175 REQUIRE(linAccX == Catch::Approx(linAccel[0]));
176 REQUIRE(linAccY == Catch::Approx(linAccel[1]));
177 REQUIRE(linAccZ == Catch::Approx(linAccel[2]));
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 ×tamp) 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 ×tamp) 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 ×tamp) 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.
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.
A class for storing options and configuration information.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
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.