diff --git a/src/devices/battery_nws_ros2/Battery_nws_ros2.cpp b/src/devices/battery_nws_ros2/Battery_nws_ros2.cpp index b661fba..2b086ff 100644 --- a/src/devices/battery_nws_ros2/Battery_nws_ros2.cpp +++ b/src/devices/battery_nws_ros2/Battery_nws_ros2.cpp @@ -83,21 +83,6 @@ bool Battery_nws_ros2::open(yarp::os::Searchable &config) return false; } - if (config.check("subdevice")) { - yarp::os::Property p; - p.fromString(config.toString(), false); - p.put("device", config.find("subdevice").asString()); - - if (!m_driver.open(p) || !m_driver.isValid()) { - yCError(BATTERY_NWS_ROS2) << "failed to open subdevice.. check params"; - return false; - } - - if (!attach(&m_driver)) { - yCError(BATTERY_NWS_ROS2) << "failed to open subdevice.. check params"; - return false; - } - } rclcpp::NodeOptions node_options; node_options.allow_undeclared_parameters(true); node_options.automatically_declare_parameters_from_overrides(true); @@ -120,12 +105,12 @@ void Battery_nws_ros2::run() if (m_battery_interface==nullptr) { yCError(BATTERY_NWS_ROS2) << "the interface is not valid"; - } - else if (!m_ros2Publisher) - { + } + else if (!m_ros2Publisher) + { yCError(BATTERY_NWS_ROS2) << "the publisher is not ready"; - } - else + } + else { double voltage=0; double current=0; @@ -155,7 +140,7 @@ void Battery_nws_ros2::run() battMsg.present=true; battMsg.location=""; battMsg.serial_number=""; - + battMsg.header.frame_id = "" ; battMsg.header.stamp.sec = int(m_timeStamp.getTime()); battMsg.header.stamp.nanosec = int(1000000000UL * (m_timeStamp.getTime() - int(m_timeStamp.getTime()))); diff --git a/src/devices/battery_nws_ros2/Battery_nws_ros2.h b/src/devices/battery_nws_ros2/Battery_nws_ros2.h index 95f3f3d..3be17cd 100644 --- a/src/devices/battery_nws_ros2/Battery_nws_ros2.h +++ b/src/devices/battery_nws_ros2/Battery_nws_ros2.h @@ -30,7 +30,6 @@ * | period | - | double | s | 0.02 | No | refresh period of the broadcasted values in s | default 0.02s | * | node_name | - | string | - | - | Yes | name of the ros2 node | | * | topic_name | - | string | - | - | Yes | name of the topic where the device must publish the data| must begin with an initial '/' | - * | subdevice | - | string | - | - | alternative to 'attach' action | name of the subdevice to use as a data source | when used, parameters for the subdevice must be provided as well | * */ diff --git a/src/devices/battery_nws_ros2/CMakeLists.txt b/src/devices/battery_nws_ros2/CMakeLists.txt index 17c1c45..1cf83b3 100644 --- a/src/devices/battery_nws_ros2/CMakeLists.txt +++ b/src/devices/battery_nws_ros2/CMakeLists.txt @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT) +# SPDX-FileCopyrightText: 2023 Istituto Italiano di Tecnologia (IIT) # SPDX-License-Identifier: BSD-3-Clause yarp_prepare_plugin(battery_nws_ros2 @@ -49,4 +49,8 @@ if(NOT SKIP_battery_nws_ros2) set(YARP_${YARP_PLUGIN_MASTER}_PRIVATE_DEPS ${YARP_${YARP_PLUGIN_MASTER}_PRIVATE_DEPS} PARENT_SCOPE) set_property(TARGET yarp_battery_nws_ros2 PROPERTY FOLDER "Plugins/Device/NWS") + + if(YARP_COMPILE_TESTS) + add_subdirectory(tests) + endif() endif() diff --git a/src/devices/battery_nws_ros2/tests/CMakeLists.txt b/src/devices/battery_nws_ros2/tests/CMakeLists.txt new file mode 100644 index 0000000..4dcf806 --- /dev/null +++ b/src/devices/battery_nws_ros2/tests/CMakeLists.txt @@ -0,0 +1,4 @@ +# SPDX-FileCopyrightText: 2023 Istituto Italiano di Tecnologia (IIT) +# SPDX-License-Identifier: BSD-3-Clause + +create_device_test (battery_nws_ros2) diff --git a/src/devices/battery_nws_ros2/tests/battery_nws_ros2_test.cpp b/src/devices/battery_nws_ros2/tests/battery_nws_ros2_test.cpp new file mode 100644 index 0000000..7d62f39 --- /dev/null +++ b/src/devices/battery_nws_ros2/tests/battery_nws_ros2_test.cpp @@ -0,0 +1,80 @@ +/* + * SPDX-FileCopyrightText: 2023 Istituto Italiano di Tecnologia (IIT) + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include +#include +#include + +#include +#include + +using namespace yarp::dev; +using namespace yarp::os; + +TEST_CASE("dev::battery_nws_ros2_test", "[yarp::dev]") +{ + YARP_REQUIRE_PLUGIN("battery_nws_ros2", "device"); + YARP_REQUIRE_PLUGIN("fakeBattery", "device"); + + Network::setLocalMode(true); + + SECTION("Checking the nws alone") + { + PolyDriver ddnws; + + ////////"Checking opening nws" + { + Property pcfg; + pcfg.put("device", "battery_nws_ros2"); + pcfg.put("node_name", "battery_node"); + pcfg.put("topic_name","/robot_battery"); + REQUIRE(ddnws.open(pcfg)); + } + + //"Close all polydrivers and check" + { + CHECK(ddnws.close()); + } + } + + SECTION("Checking the nws attached to device") + { + PolyDriver ddnws; + PolyDriver ddfake; + yarp::dev::WrapperSingle* ww_nws = nullptr; + + ////////"Checking opening nws" + { + Property pcfg; + pcfg.put("device", "battery_nws_ros2"); + pcfg.put("node_name", "battery_node"); + pcfg.put("topic_name","/robot_battery"); + REQUIRE(ddnws.open(pcfg)); + } + + ////////"Checking opening device" + { + Property pcfg_fake; + pcfg_fake.put("device", "fakeBattery"); + REQUIRE(ddfake.open(pcfg_fake)); + } + + //attach the nws to the fake device + { + ddnws.view(ww_nws); + bool result_att = ww_nws->attach(&ddfake); + REQUIRE(result_att); + } + + //"Close all polydrivers and check" + { + CHECK(ddnws.close()); + CHECK(ddfake.close()); + } + } + + Network::setLocalMode(false); +}