Skip to content

Commit

Permalink
added test
Browse files Browse the repository at this point in the history
  • Loading branch information
randaz81 committed Aug 22, 2023
1 parent dbe762b commit 6f6df5f
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 23 deletions.
27 changes: 6 additions & 21 deletions src/devices/battery_nws_ros2/Battery_nws_ros2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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())));
Expand Down
1 change: 0 additions & 1 deletion src/devices/battery_nws_ros2/Battery_nws_ros2.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
*
*/

Expand Down
6 changes: 5 additions & 1 deletion src/devices/battery_nws_ros2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
4 changes: 4 additions & 0 deletions src/devices/battery_nws_ros2/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# SPDX-FileCopyrightText: 2023 Istituto Italiano di Tecnologia (IIT)
# SPDX-License-Identifier: BSD-3-Clause

create_device_test (battery_nws_ros2)
80 changes: 80 additions & 0 deletions src/devices/battery_nws_ros2/tests/battery_nws_ros2_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* SPDX-FileCopyrightText: 2023 Istituto Italiano di Tecnologia (IIT)
* SPDX-License-Identifier: BSD-3-Clause
*/

#include <yarp/os/Network.h>
#include <yarp/os/LogStream.h>
#include <yarp/dev/PolyDriver.h>
#include <yarp/dev/WrapperSingle.h>

#include <catch2/catch_amalgamated.hpp>
#include <harness.h>

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);
}

0 comments on commit 6f6df5f

Please sign in to comment.