Skip to content

Commit

Permalink
Several improvements:
Browse files Browse the repository at this point in the history
- Update DeviceRegistry::getDevicesAsPolyDriverList method
- Add pragma once direcgtive to DeviceRegistry
- Remove check to BaseStateDriver raising exeption with new data management
- Move initialization of Camera buffers after Driver has been opened
- Add test-helpers library and TestHelpers class
- Fix Imu test
- Update ControlBoardPositionDirectTest (still failing)
  • Loading branch information
xela-95 committed Apr 17, 2024
1 parent c345e4c commit a901e0f
Show file tree
Hide file tree
Showing 18 changed files with 326 additions and 147 deletions.
5 changes: 2 additions & 3 deletions libraries/device-registry/DeviceRegistry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <sstream>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#include <yarp/dev/PolyDriver.h>
Expand Down Expand Up @@ -51,8 +52,7 @@ bool DeviceRegistry::getDevicesAsPolyDriverList(

// If the deviceDatabaseKey starts with the modelScopedName (device spawned by model
// plugins), then it is eligible for insertion in the returned list
if ((deviceDatabaseKey.rfind(modelScopedName, 0)
== 0) /*|| (deviceDatabaseKey.rfind(worldName + "/" + modelScopedName, 0) == 0)*/)
if ((deviceDatabaseKey.find(modelScopedName, 0) != std::string::npos))
{
// Extract yarpDeviceName from deviceDatabaseKey
yarpDeviceName = deviceDatabaseKey.substr(deviceDatabaseKey.find_last_of("/") + 1);
Expand Down Expand Up @@ -153,7 +153,6 @@ DeviceRegistry::getDevicesKeys(const gz::sim::EntityComponentManager& ecm) const

{
std::lock_guard<std::mutex> lock(mutex());
std::cerr << "==================== ecm address: " << EcmPtrSs.str() << std::endl;
std::vector<std::string> keys;
for (auto&& [key, value] : m_devicesMap)
{
Expand Down
2 changes: 2 additions & 0 deletions libraries/device-registry/DeviceRegistry.hh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

#include <map>
#include <mutex>
#include <string>
Expand Down
1 change: 1 addition & 0 deletions plugins/basestate/BaseState.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <BaseStateShared.hh>
#include <ConfigurationHelpers.hh>
#include <DeviceIdGenerator.hh>
#include <DeviceRegistry.hh>

#include <memory>
#include <mutex>
Expand Down
6 changes: 0 additions & 6 deletions plugins/basestate/BaseStateDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ class yarp::dev::gzyarp::BaseStateDriver : public yarp::dev::DeviceDriver,
m_baseLinkName = config.find("baseLink").asString().substr(pos + separator.size() - 1);
}

if (!m_baseLinkData)
{
yError() << "Error, BaseState sensor was not found";
return false;
}

return true;
}

Expand Down
1 change: 1 addition & 0 deletions plugins/camera/Camera.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <CameraShared.hh>
#include <ConfigurationHelpers.hh>
#include <DeviceIdGenerator.hh>
#include <DeviceRegistry.hh>

#include <cstddef>
#include <cstring>
Expand Down
19 changes: 13 additions & 6 deletions plugins/camera/CameraDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <cstdio>
#include <cstring>
#include <iostream>
#include <mutex>

#include <string>
Expand Down Expand Up @@ -62,12 +63,6 @@ class yarp::dev::gzyarp::CameraDriver : public yarp::dev::DeviceDriver,
{
std::string sensorScopedName(config.find(YarpCameraScopedName.c_str()).asString().c_str());

{
std::lock_guard<std::mutex> lock(m_sensorData->m_mutex);
m_sensorData->m_imageBuffer = new unsigned char[getRawBufferSize()];
memset(m_sensorData->m_imageBuffer, 0x00, getRawBufferSize());
}

if (config.check("vertical_flip"))
m_vertical_flip = true;
if (config.check("horizontal_flip"))
Expand Down Expand Up @@ -275,6 +270,18 @@ class yarp::dev::gzyarp::CameraDriver : public yarp::dev::DeviceDriver,
void setCameraData(::gzyarp::CameraData* dataPtr) override
{
m_sensorData = dataPtr;

// Now that we have a pointer to CameraData we can initialize the camera buffers
initializeCamera();
}

void initializeCamera()
{
{
std::lock_guard<std::mutex> lock(m_sensorData->m_mutex);
m_sensorData->m_imageBuffer = new unsigned char[getRawBufferSize()];
memset(m_sensorData->m_imageBuffer, 0x00, getRawBufferSize());
}
}

private:
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ add_subdirectory(camera)
add_subdirectory(clock)
add_subdirectory(controlboard)
add_subdirectory(commons)
add_subdirectory(test-helpers)
44 changes: 37 additions & 7 deletions tests/commons/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,24 +1,54 @@
add_executable(ConfigurationParsingTest ConfigurationParsingTest.cc)
add_executable(ConfigurationParsingFromFileTest ConfigurationParsingFromFileTest.cc)
add_executable(ConfigurationParsingFromStringTest ConfigurationParsingFromStringTest.cc)

target_link_libraries(ConfigurationParsingTest
target_link_libraries(ConfigurationParsingFromFileTest
PRIVATE
GTest::gtest_main
test-helpers
gz-sim${GZ_SIM_VER}::gz-sim${GZ_SIM_VER}
gz-plugin${GZ_PLUGIN_VER}::gz-plugin${GZ_PLUGIN_VER}
YARP::YARP_dev
YARP::YARP_os
YARP::YARP_init
gz-sim-yarp-device-registry
gz-sim-yarp-basestate-system
gz-sim-yarp-camera-system
gz-sim-yarp-controlboard-system
gz-sim-yarp-forcetorque-system
gz-sim-yarp-imu-system
gz-sim-yarp-laser-system
)

target_link_libraries(ConfigurationParsingFromStringTest
PRIVATE
GTest::gtest_main
test-helpers
gz-sim${GZ_SIM_VER}::gz-sim${GZ_SIM_VER}
gz-plugin${GZ_PLUGIN_VER}::gz-plugin${GZ_PLUGIN_VER}
YARP::YARP_dev
YARP::YARP_os
YARP::YARP_init
gz-sim-yarp-device-registry
gz-sim-yarp-basestate-system
gz-sim-yarp-camera-system
gz-sim-yarp-controlboard-system
gz-sim-yarp-forcetorque-system
gz-sim-yarp-imu-system
gz-sim-yarp-laser-system
)

add_test(NAME ConfigurationParsingTest COMMAND ConfigurationParsingTest)

# add_test(NAME ConfigurationParsingFromFileTest COMMAND ConfigurationParsingFromFileTest)
add_test(NAME ConfigurationParsingFromStringTest COMMAND ConfigurationParsingFromStringTest)

set(_env_vars)
# list(APPEND _env_vars "GZ_SIM_SYSTEM_PLUGIN_PATH=$<TARGET_FILE_DIR:gz-sim-yarp-camera-system>")
list(APPEND _env_vars "LIBGL_ALWAYS_SOFTWARE=1"
"GZ_SIM_SYSTEM_PLUGIN_PATH=$<TARGET_FILE_DIR:gz-sim-yarp-device-registry>"
"GZ_SIM_RESOURCE_PATH=${CMAKE_CURRENT_SOURCE_DIR}/.."
)
"GZ_SIM_RESOURCE_PATH=${GZ_SIM_RESOURCE_PATH}:${CMAKE_CURRENT_SOURCE_DIR}/.."
)

# set_tests_properties(ConfigurationParsingFromFileTest PROPERTIES
# ENVIRONMENT "${_env_vars}")

set_tests_properties(ConfigurationParsingTest PROPERTIES
set_tests_properties(ConfigurationParsingFromStringTest PROPERTIES
ENVIRONMENT "${_env_vars}")
84 changes: 84 additions & 0 deletions tests/commons/ConfigurationParsingFromFileTest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include <gtest/gtest.h>

#include <BaseStateDriver.cpp>
#include <CameraDriver.cpp>
#include <ControlBoardDriver.hh>
#include <DeviceRegistry.hh>
#include <ForceTorqueDriver.cpp>
#include <ImuDriver.cpp>
#include <LaserDriver.cpp>
#include <TestHelpers.hh>

#include <gz/common/Console.hh>
#include <gz/sim/Entity.hh>
#include <gz/sim/EntityComponentManager.hh>
#include <gz/sim/EventManager.hh>
#include <gz/sim/TestFixture.hh>
#include <sdf/Element.hh>

#include <iostream>
#include <memory>
#include <string>

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

TEST(ConfigurationParsingTest, LoadPluginsWithYarpConfigurationFile)
{
std::string modelSdfName = "model_configuration_file.sdf";
std::string sdfPath = std::string("../../../tests/commons/") + modelSdfName;
yarp::os::NetworkBase::setLocalMode(true);
gz::common::Console::SetVerbosity(4);

gz::sim::TestFixture testFixture{sdfPath};

testFixture.OnConfigure([&](const gz::sim::Entity& _worldEntity,
const std::shared_ptr<const sdf::Element>& /*_sdf*/,
gz::sim::EntityComponentManager& _ecm,
gz::sim::EventManager& /*_eventMgr*/) {
// Test Camera
// std::cerr << "Testing Camera configuration" << std::endl;
// auto cameraDrivers
// = gzyarp::testing::TestHelpers::getDevicesOfType<yarp::dev::gzyarp::CameraDriver>();
// EXPECT_EQ(cameraDrivers.size(), 1);
// EXPECT_TRUE(cameraDrivers[0] != nullptr);

// Test ForceTorque
std::cerr << "Testing FT configuration" << std::endl;
auto ftDrivers
= gzyarp::testing::TestHelpers::getDevicesOfType<yarp::dev::gzyarp::ForceTorqueDriver>();
EXPECT_EQ(ftDrivers.size(), 1);
EXPECT_TRUE(ftDrivers[0] != nullptr);

// Test Imu
std::cerr << "Testing Imu configuration" << std::endl;
auto imuDrivers
= gzyarp::testing::TestHelpers::getDevicesOfType<yarp::dev::gzyarp::ImuDriver>();
EXPECT_EQ(imuDrivers.size(), 1);
EXPECT_TRUE(imuDrivers[0] != nullptr);

// Test Laser
std::cerr << "Testing Laser configuration" << std::endl;
auto laserDrivers
= gzyarp::testing::TestHelpers::getDevicesOfType<yarp::dev::gzyarp::LaserDriver>();
EXPECT_EQ(laserDrivers.size(), 1);
EXPECT_TRUE(laserDrivers[0] != nullptr);

// Test basestate
std::cerr << "Testing BaseState configuration" << std::endl;
auto bsDrivers
= gzyarp::testing::TestHelpers::getDevicesOfType<yarp::dev::gzyarp::BaseStateDriver>();
EXPECT_EQ(bsDrivers.size(), 1);
EXPECT_TRUE(bsDrivers[0] != nullptr);

// Test controlboard
std::cerr << "Testing ControlBoard configuration" << std::endl;
auto cbDrivers
= gzyarp::testing::TestHelpers::getDevicesOfType<yarp::dev::gzyarp::ControlBoardDriver>();
EXPECT_EQ(cbDrivers.size(), 1);
EXPECT_TRUE(cbDrivers[0] != nullptr);
});

testFixture.Finalize();
}
79 changes: 79 additions & 0 deletions tests/commons/ConfigurationParsingFromStringTest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <gtest/gtest.h>

#include <BaseStateDriver.cpp>
#include <CameraDriver.cpp>
#include <ControlBoardDriver.hh>
#include <DeviceRegistry.hh>
#include <ForceTorqueDriver.cpp>
#include <ImuDriver.cpp>
#include <LaserDriver.cpp>
#include <TestHelpers.hh>

#include <gz/common/Console.hh>
#include <gz/sim/Entity.hh>
#include <gz/sim/EntityComponentManager.hh>
#include <gz/sim/EventManager.hh>
#include <gz/sim/TestFixture.hh>
#include <sdf/Element.hh>

#include <iostream>
#include <memory>
#include <string>

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

TEST(ConfigurationParsingTest, LoadPluginsWithYarpConfigurationString)
{
std::string modelSdfName = "model_configuration_string.sdf";
std::string sdfPath = std::string("../../../tests/commons/") + modelSdfName;
yarp::os::NetworkBase::setLocalMode(true);

gz::sim::TestFixture testFixture{sdfPath};
gz::common::Console::SetVerbosity(4);

testFixture.OnConfigure([&](const gz::sim::Entity& _worldEntity,
const std::shared_ptr<const sdf::Element>& /*_sdf*/,
gz::sim::EntityComponentManager& _ecm,
gz::sim::EventManager& /*_eventMgr*/) {
// Test Camera
std::cerr << "Testing Camera configuration" << std::endl;
auto cameraDrivers
= gzyarp::testing::TestHelpers::getDevicesOfType<yarp::dev::gzyarp::CameraDriver>();
EXPECT_EQ(cameraDrivers.size(), 1);
EXPECT_TRUE(cameraDrivers[0] != nullptr);

// Test ForceTorque
std::cerr << "Testing FT configuration" << std::endl;
auto ftDrivers
= gzyarp::testing::TestHelpers::getDevicesOfType<yarp::dev::gzyarp::ForceTorqueDriver>();
EXPECT_EQ(ftDrivers.size(), 1);
EXPECT_TRUE(ftDrivers[0] != nullptr);

// Test Imu
std::cerr << "Testing Imu configuration" << std::endl;
auto imuDrivers
= gzyarp::testing::TestHelpers::getDevicesOfType<yarp::dev::gzyarp::ImuDriver>();
EXPECT_EQ(imuDrivers.size(), 1);
EXPECT_TRUE(imuDrivers[0] != nullptr);

// Test Laser
std::cerr << "Testing Laser configuration" << std::endl;
auto laserDrivers
= gzyarp::testing::TestHelpers::getDevicesOfType<yarp::dev::gzyarp::LaserDriver>();
EXPECT_EQ(laserDrivers.size(), 1);
EXPECT_TRUE(laserDrivers[0] != nullptr);

// Test basestate
std::cerr << "Testing BaseState configuration" << std::endl;
auto bsDrivers
= gzyarp::testing::TestHelpers::getDevicesOfType<yarp::dev::gzyarp::BaseStateDriver>();
EXPECT_EQ(bsDrivers.size(), 1);
EXPECT_TRUE(bsDrivers[0] != nullptr);

// Controlboard test skipped since configuration too complex to pass as string
});

testFixture.Finalize();
}
Loading

0 comments on commit a901e0f

Please sign in to comment.