Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for forcetorque plugin #30

Merged
merged 17 commits into from
Aug 11, 2023
Merged

Add test for forcetorque plugin #30

merged 17 commits into from
Aug 11, 2023

Conversation

lucapa17
Copy link
Contributor

@lucapa17 lucapa17 commented Aug 9, 2023

Fix #20

@traversaro
Copy link
Member

Thanks @lucapa17, some preliminary comments:

Run cd build
Test project /home/runner/work/gz-yarp-plugins/gz-yarp-plugins/build
No tests were found!!!"

so something is not working as expected.

Comment on lines 1 to 5
project(GTestSetup)

# Find Gazebo
find_package(gz-sim7 REQUIRED)
set(GZ_SIM_VER ${gz-sim7_VERSION_MAJOR})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines are not requested, and can be removed to reduce the noise in the code. project is only required in the root CMakeLists.txt of the project, and find_package needs only to be invoked once.

Suggested change
project(GTestSetup)
# Find Gazebo
find_package(gz-sim7 REQUIRED)
set(GZ_SIM_VER ${gz-sim7_VERSION_MAJOR})

set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

enable_testing()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only works when called in the root CMakeLists.txt, so I suggest to remove it here and add it there.

Suggested change
enable_testing()

Comment on lines 1 to 8
## ForceTorque plugin Test

~~~
cd build
export GZ_SIM_SYSTEM_PLUGIN_PATH=`pwd`
cd tests/forcetorque
./ForceTorqueTest
~~~
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the comment to the PR as an whole, I suggest to remove this and just document in the README how to run tests.

Suggested change
## ForceTorque plugin Test
~~~
cd build
export GZ_SIM_SYSTEM_PLUGIN_PATH=`pwd`
cd tests/forcetorque
./ForceTorqueTest
~~~

CMakeLists.txt Outdated
@@ -15,4 +15,5 @@ set(GZ_SIM_VER ${gz-sim7_VERSION_MAJOR})

add_subdirectory(libraries)
add_subdirectory(plugins)
add_subdirectory(tests)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest the following modification, based on the docs in https://cmake.org/cmake/help/book/mastering-cmake/chapter/Testing%20With%20CMake%20and%20CTest.html :

Suggested change
add_subdirectory(tests)
option(BUILD_TESTING "Create tests using CMake" OFF)
enable(CTest)
if(BUILD_TESTING)
add_subdirectory(tests)
endif()

Explanation:

  • Instead of calling enable_testing directly, the CMake docs suggest to just include the CTest module
  • The CTest module defines the BUILD_TESTING variable, and if that variable is set to OFF, no tests are enabled. For this reason, if BUILD_TESTING is OFF, we do not add the tests subdirectory. Furthermore, tipically in our project we set the default value of BUILD_TESTING to OFF, so that users that do not need to compile the tests do not spend time compiling them.

@lucapa17 lucapa17 changed the title Ft tests Add test for forcetorque plugin Aug 10, 2023
@traversaro
Copy link
Member

Can you check out the conflicts with the main branch?

@traversaro
Copy link
Member

traversaro commented Aug 10, 2023

The test is failing, there are two errors:

First Error

unning main() from /home/runner/work/gz-yarp-plugins/gz-yarp-plugins/build/_deps/googletest-src/googletest/src/gtest_main.cc
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from ForceTorqueTest
[ RUN      ] ForceTorqueTest.PluginTest
[Err] [SystemLoader.cc:118] Failed to load system plugin: (Reason: Could not find shared library)
- Requested plugin name: [GazeboYarpForceTorque]
- Requested library name: [plugins/forcetorque/libGazeboYarpForceTorque.so]
- Library search paths:
  - /home/runner/.gz/sim/plugins/
  - /usr/lib/x86_64-linux-gnu/gz-sim-7/plugins/
  - /home/runner/.ignition/gazebo/plugins/
[Err] [SystemLoader.cc:118] Failed to load system plugin: (Reason: Could not find shared library)
- Requested plugin name: [GazeboYarpRobotInterface]
- Requested library name: [plugins/robotinterface/libGazeboYarpRobotInterface.so]
- Library search paths:
  - /home/runner/.gz/sim/plugins/
  - /usr/lib/x86_64-linux-gnu/gz-sim-7/plugins/
  - /home/runner/.ignition/gazebo/plugins/

The problem is that the test is not finding the plugin, as no one is adding the folder in which the plugin can be found to the GZ_SIM_SYSTEM_PLUGIN_PATH environment variable. Indeed, it is non trivial to do so as now the plugins are spread in many different directories. To solve this, I suggest to:

  1. Add in the root CMakeLists.txt the following lines (taken from https://github.com/robotology/how-to-export-cpp-library/blob/afb21efb655e7b1cc11636c3d42aad9e02fb626f/CMakeLists.txt#L41 and https://github.com/robotology/gazebo-yarp-plugins/blob/9ce261e8c0f3433d48de8032820afabc0b33afcb/CMakeLists.txt#L22):
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")

this will ensure that all plugins are placed in build/lib on Linux/macOS and build/bin on Windows. That means that for using the plugins from the build directory will boil down to just add build/lib or or build/bin to GZ_SIM_SYSTEM_PLUGIN_PATH

  1. As after the change in point 1. the plugins will not be found in a nested directory, change all occurences in sdf files of filename="plugins/forcetorque/libGazeboYarpForceTorque.so" or similar to filename="GazeboYarpForceTorque" . This would be also helpful to achieve Add install step #28, as when the plugins are installed they are not installed in a plugins/forcetorque or similar directory, and to support Windows and macOS (where the plugin do not end with .so).

  2. Once 1. and 2. are done, we will then just need to tell to the test to add the directory with the plugins to the correct env variable, this can be done by adding (see https://github.com/gazebosim/gz-sim/blob/579013fe2841de68c0901192780ab8ee86cc9bb1/src/CMakeLists.txt#L274):

  set(_env_vars)
  list(APPEND _env_vars "GZ_SIM_SYSTEM_PLUGIN_PATH=$<TARGET_FILE_DIR:GazeboYarpForceTorque>")

  set_tests_properties(ForceTorqueTest PROPERTIES
    ENVIRONMENT "${_env_vars}")

Second Error

Error:  |yarp.os.Port| YARP not initialized; create a yarp::os::Network object before using ports

Whenever you use yarp ports, you need to have somewhere a yarp::os::Network object defined, just adding yarp::os::Network yarp should solve the problem.

gz::sim::TestFixture fixture("../../../tests/forcetorque/model.sdf");

int iterations = 1000;
fixture.Server()->Run(true, iterations, false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit difficult to read, see https://abseil.io/tips/94 for a description way.

Suggested change
fixture.Server()->Run(true, iterations, false);
fixture.Server()->Run(/*_blocking=*/true, iterations, /*_paused=*/false);

p.open("/read");
yarp::os::Network::connect("/forcetorque/measures:o","/read");
yarp::os::Bottle b;
p.read(b);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

macOS and Windows tests are failing. Perhaps we should add a check that the read is successful, checking the return value of this function (see https://www.yarp.it/latest/classyarp_1_1os_1_1Port.html#a7b4eb018faba3837d50fffee0ea722ef). If it fails, it is possible that it is not able to read in time anything, as nothing is published between the connect and the read. Perhaps we can try to migrate this code to multipleanalogsensorsclient and then look again at this code? Tryng to fix it now before migrating to multipleanalogsensorsclient may be a waste of time.

double timestamp;

isixaxis->getSixAxisForceTorqueSensorName(0, sensorName);
isixaxis->getSixAxisForceTorqueSensorMeasure(0, measure, timestamp);
Copy link
Member

@traversaro traversaro Aug 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest:

Suggested change
isixaxis->getSixAxisForceTorqueSensorMeasure(0, measure, timestamp);
size_t maxNrOfReadingAttempts = 20;
bool readSuccessful = false;
for (size_t i=0; (i < maxNrOfReadingAttempts) && !readSuccessful ; i++)
{
readSuccessful = isixaxis->getSixAxisForceTorqueSensorMeasure(0, measure, timestamp);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
ASSERT_TRUE(readSuccessful);

if the failure continue, you can inspect also the value returned by getSixAxisForceTorqueSensorStatus to understand why the output is not the expected one.

@traversaro
Copy link
Member

In 6697372 I added a fix for actually use the cache, this should speedup the execution of apt-based workflow.

For what regards the failures, there are the following two failures:

conda-windows

It still fails with error:

 [INFO] |yarp.dev.PolyDriver|multipleanalogsensorsclient| Created device <multipleanalogsensorsclient>. See C++ class MultipleAnalogSensorsClient for documentation.
D:\a\gz-yarp-plugins\gz-yarp-plugins\tests\forcetorque\ForceTorqueTest.cc(44): error: The difference between measure(2) and -9.8*10 is 98, which exceeds 1e-2, where
measure(2) evaluates to 0,
-9.8*10 evaluates to -98, and
1e-2 evaluates to 0.01.
[INFO] |yarp.os.impl.PortCoreInputUnit|/ForceTorqueTest

It seems that you tried to fix this by increasting the delay. This is not ideal as it also slows down the tests that do not need to wait more. What I suggest instead is to check the return values of the getSixAxisForceTorqueSensorMeasure method, and only consider the output once the value is successful, see https://github.com/robotology/gz-yarp-plugins/pull/30/files#r1291112238 .

apt

In here probably we just need to set the YARP_DATA_DIRS when we install YARP, let me try to do that.

@traversaro
Copy link
Member

apt

In here probably we just need to set the YARP_DATA_DIRS when we install YARP, let me try to do that.

Done in 2864978, inspired by https://github.com/robotology/yarp-devices-ros2/blob/ecc88be70dac7fa3f44076b0f85bfca471754642/.github/workflows/conda-ci.yml#L103 .

@traversaro
Copy link
Member

apt

In here probably we just need to set the YARP_DATA_DIRS when we install YARP, let me try to do that.

Done in 2864978, inspired by https://github.com/robotology/yarp-devices-ros2/blob/ecc88be70dac7fa3f44076b0f85bfca471754642/.github/workflows/conda-ci.yml#L103 .

It worked fine, probably we can just remove the attemps to set YARP_DATA_DIRS in the code.

@traversaro
Copy link
Member

conda-windows

It still fails with error:

 [INFO] |yarp.dev.PolyDriver|multipleanalogsensorsclient| Created device <multipleanalogsensorsclient>. See C++ class MultipleAnalogSensorsClient for documentation.
D:\a\gz-yarp-plugins\gz-yarp-plugins\tests\forcetorque\ForceTorqueTest.cc(44): error: The difference between measure(2) and -9.8*10 is 98, which exceeds 1e-2, where
measure(2) evaluates to 0,
-9.8*10 evaluates to -98, and
1e-2 evaluates to 0.01.
[INFO] |yarp.os.impl.PortCoreInputUnit|/ForceTorqueTest

It seems that you tried to fix this by increasting the delay. This is not ideal as it also slows down the tests that do not need to wait more. What I suggest instead is to check the return values of the getSixAxisForceTorqueSensorMeasure method, and only consider the output once the value is successful, see https://github.com/robotology/gz-yarp-plugins/pull/30/files#r1291112238 .

Apparently also macOS sometimes fails. We can try to implement the logic in https://github.com/robotology/gz-yarp-plugins/pull/30/files#r1291112238 and see if that improves.

@@ -36,7 +36,15 @@ TEST(ForceTorqueTest, PluginTest)
double timestamp;

isixaxis->getSixAxisForceTorqueSensorName(0, sensorName);
isixaxis->getSixAxisForceTorqueSensorMeasure(0, measure, timestamp);
//isixaxis->getSixAxisForceTorqueSensorMeasure(0, measure, timestamp);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//isixaxis->getSixAxisForceTorqueSensorMeasure(0, measure, timestamp);

If we are not using it, let's remove it.

readSuccessful = isixaxis->getSixAxisForceTorqueSensorMeasure(0, measure, timestamp);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
ASSERT_TRUE(readSuccessful);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid mixing tabs and spaces, as it may result in inconsistent rendring (the width of tabs is not constant).

Suggested change
ASSERT_TRUE(readSuccessful);
ASSERT_TRUE(readSuccessful);

@traversaro
Copy link
Member

Ok, it seems fine. If Windows is the only missing platform, for now probably we can skip the test and open an issue to track the test failures on Windows.

@GiacomoBisio
Copy link
Contributor

Ok, it seems fine. If Windows is the only missing platform, for now probably we can skip the test and open an issue to track the test failures on Windows.

Ok, we added a new issue #31. So, can we merge?

@traversaro
Copy link
Member

Ok, it seems fine. If Windows is the only missing platform, for now probably we can skip the test and open an issue to track the test failures on Windows.

Ok, we added a new issue #31. So, can we merge?

There are a few things we probably still need to fix before merging, to ensure that the main branch is a good step, basically:

  • We can't just have a test on Windows fail, as then it would hide the failure could hide other failures that could emerge in future PRs. If tests on Windows are know to fail, we should suppress running tests on Windows. We can do that by adding the if: contains(matrix.os, 'windows') after the line https://github.com/robotology/gz-yarp-plugins/blob/FT-tests/.github/workflows/conda-forge.yml#L69.
  • As we changed the location of the plugins in the build directory, we should change all the sdf files in tutorials from <plugin name="GazeboYarpForceTorque" filename="plugins/forcetorque/libGazeboYarpForceTorque.so"> to <plugin name="GazeboYarpForceTorque" filename="GazeboYarpForceTorque"> (also for other non-FT sensors plugins), otherwise all the tutorial would be broken by this PR.

After that, I think we are ready to Squash and merge (again, squash to avoid having a messy history).

@lucapa17 lucapa17 merged commit f498935 into main Aug 11, 2023
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Write tests for forcetorque plugin
3 participants