Skip to content

Commit

Permalink
added ParamParser to DeviceTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
randaz81 committed Jan 25, 2024
1 parent f0b8dbb commit 27dd882
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 7 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ include(FeatureSummary)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

find_package(YCM 0.15.1 REQUIRED)
find_package(YCM 0.15.3 REQUIRED)

include(GNUInstallDirs)

Expand All @@ -32,8 +32,8 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
set(YARP_FORCE_DYNAMIC_PLUGINS TRUE CACHE INTERNAL "yarp-devices-ros2 is always built with dynamic plugins")
option(BUILD_SHARED_LIBS "Build libraries as shared as opposed to static" ON)

find_package(YARP 3.8.0 COMPONENTS os sig dev rosmsg serversql OPTIONAL_COMPONENTS math REQUIRED)
find_package(YARP 3.8.0 COMPONENTS catch2 dev_tests QUIET)
find_package(YARP 3.10.0 COMPONENTS os sig dev rosmsg serversql OPTIONAL_COMPONENTS math REQUIRED)
find_package(YARP 3.10.0 COMPONENTS catch2 dev_tests QUIET)

if(YARP_catch2_FOUND AND YARP_dev_tests_FOUND)
option(YARP_COMPILE_TESTS "Enable YARP tests" OFF)
Expand Down
3 changes: 3 additions & 0 deletions src/devices/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# SPDX-FileCopyrightText: 2023 Istituto Italiano di Tecnologia (IIT)
# SPDX-License-Identifier: BSD-3-Clause

#message (Looking for CMakeFiles into this folder: ${YARP_MODULE_DIR})
include(${YARP_MODULE_DIR}/YarpDeviceParamsParserGenerator.cmake)

add_subdirectory(deviceTemplate)
3 changes: 3 additions & 0 deletions src/devices/deviceTemplate/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ yarp_prepare_plugin(deviceTemplate

if(NOT SKIP_deviceTemplate)
yarp_add_plugin(yarp_deviceTemplate)
generateDeviceParamsParser(DeviceTemplate)

target_sources(yarp_deviceTemplate
PRIVATE
DeviceTemplate.cpp
DeviceTemplate.h
DeviceTemplate_ParamsParser.cpp
DeviceTemplate_ParamsParser.h
)

target_link_libraries(yarp_deviceTemplate
Expand Down
8 changes: 5 additions & 3 deletions src/devices/deviceTemplate/DeviceTemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ using namespace yarp::os;
using namespace yarp::dev;


YARP_LOG_COMPONENT(DEVICETEMPLATE, "yarp.deviceTemaplate", yarp::os::Log::TraceType);
YARP_LOG_COMPONENT(DEVICETEMPLATE, "yarp.deviceTemplate", yarp::os::Log::TraceType);


DeviceTemplate::DeviceTemplate()
Expand All @@ -28,12 +28,14 @@ DeviceTemplate::DeviceTemplate()

bool DeviceTemplate::open(yarp::os::Searchable &config)
{
yCError(DEVICETEMPLATE) << "Open";
if (!parseParams(config)) { return false; }

yCInfo(DEVICETEMPLATE) << "Open";
return true;
}

bool DeviceTemplate::close()
{
yCError(DEVICETEMPLATE) << "Close";
yCInfo(DEVICETEMPLATE) << "Close";
return true;
}
4 changes: 3 additions & 1 deletion src/devices/deviceTemplate/DeviceTemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
#define YARP_DEVICETEMPLATE_H

#include <yarp/dev/DeviceDriver.h>
#include "DeviceTemplate_ParamsParser.h"

class DeviceTemplate :
public yarp::dev::DeviceDriver
public yarp::dev::DeviceDriver,
public DeviceTemplate_ParamsParser
{
public:
DeviceTemplate();
Expand Down
110 changes: 110 additions & 0 deletions src/devices/deviceTemplate/DeviceTemplate_ParamsParser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* SPDX-FileCopyrightText: 2023-2023 Istituto Italiano di Tecnologia (IIT)
* SPDX-License-Identifier: LGPL-2.1-or-later
*/


// Generated by yarpDeviceParamParserGenerator (1.0)
//
// This is an automatically generated file. Please do not edit it.
// It will be re-generated if the cmake flag ALLOW_DEVICE_PARAM_PARSER_GERNERATION is ON.

// Generated on: Thu Jan 25 14:38:34 2024


#include "DeviceTemplate_ParamsParser.h"
#include <yarp/os/LogStream.h>
#include <yarp/os/Value.h>

namespace {
YARP_LOG_COMPONENT(DeviceTemplateParamsCOMPONENT, "yarp.device.DeviceTemplate")
}


std::vector<std::string> DeviceTemplate_ParamsParser::getListOfParams() const
{
std::vector<std::string> params;
params.push_back("dummy_group::dummy_param1");
params.push_back("dummy_param2");
return params;
}


bool DeviceTemplate_ParamsParser::parseParams(const yarp::os::Searchable & config)
{
std::string config_string = config.toString();
yarp::os::Property prop_check(config_string.c_str());
//Parser of parameter dummy_group::dummy_param1
{
yarp::os::Bottle sectionp;
sectionp = config.findGroup("dummy_group");
if (sectionp.check("dummy_param1"))
{
m_dummy_group_dummy_param1 = sectionp.find("dummy_param1").asString();
yCInfo(DeviceTemplateParamsCOMPONENT) << "Parameter 'dummy_group::dummy_param1' using value:" << m_dummy_group_dummy_param1;
}
else
{
yCError(DeviceTemplateParamsCOMPONENT) << "Mandatory parameter 'dummy_group::dummy_param1' not found!";
yCError(DeviceTemplateParamsCOMPONENT) << "Description of the parameter: This is the description of dummy_param1";
return false;
}
prop_check.unput("dummy_group::dummy_param1");
}

//Parser of parameter dummy_param2
{
if (config.check("dummy_param2"))
{
m_dummy_param2 = config.find("dummy_param2").asFloat64();
yCInfo(DeviceTemplateParamsCOMPONENT) << "Parameter 'dummy_param2' using value:" << m_dummy_param2;
}
else
{
yCInfo(DeviceTemplateParamsCOMPONENT) << "Parameter 'dummy_param2' using DEFAULT value:" << m_dummy_param2;
}
prop_check.unput("dummy_param2");
}

/*
//This code check if the user set some parameter which are not check by the parser
//If the parser is set in strict mode, this will generate an error
if (prop_check.size() > 0)
{
bool extra_params_found = false;
for (auto it=prop_check.begin(); it!=prop_check.end(); it++)
{
if (m_parser_is_strict)
{
yCError(DeviceTemplateParamsCOMPONENT) << "User asking for parameter: "<<it->name <<" which is unknown to this parser!";
extra_params_found = true;
}
else
{
yCWarning(DeviceTemplateParamsCOMPONENT) << "User asking for parameter: "<< it->name <<" which is unknown to this parser!";
}
}
if (m_parser_is_strict && extra_params_found)
{
return false;
}
}
*/
return true;
}


std::string DeviceTemplate_ParamsParser::getDocumentationOfDeviceParams() const
{
std::string doc;
doc = doc + std::string("'dummy_group::dummy_param1': This is the description of dummy_param1\n");
doc = doc + std::string("'dummy_param2': This is the description of dummy_param2\n");
doc = doc + std::string("\n");
doc = doc + std::string("Here are some examples of invocation command with yarpdev, with all params:\n");
doc = doc + " yarpdev --device DeviceTemplate --dummy_group::dummy_param1 dummy_default_value --dummy_param2 9.81\n";
doc = doc + std::string("Using only mandatory params:\n");
doc = doc + " yarpdev --device DeviceTemplate --dummy_group::dummy_param1 dummy_default_value\n";
doc = doc + std::string("\n");
return doc;
}
59 changes: 59 additions & 0 deletions src/devices/deviceTemplate/DeviceTemplate_ParamsParser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* SPDX-FileCopyrightText: 2023-2023 Istituto Italiano di Tecnologia (IIT)
* SPDX-License-Identifier: LGPL-2.1-or-later
*/


// Generated by yarpDeviceParamParserGenerator (1.0)
//
// This is an automatically generated file. Please do not edit it.
// It will be re-generated if the cmake flag ALLOW_DEVICE_PARAM_PARSER_GERNERATION is ON.

// Generated on: Thu Jan 25 14:38:34 2024


#ifndef DEVICETEMPLATE_PARAMSPARSER_H
#define DEVICETEMPLATE_PARAMSPARSER_H

#include <yarp/os/Searchable.h>
#include <yarp/dev/IDeviceDriverParams.h>
#include <string>

/**
* This class is the parameters parser for class DeviceTemplate.
*
* These are the used parameters:
* | Group name | Parameter name | Type | Units | Default Value | Required | Description | Notes |
* |:-----------:|:--------------:|:------:|:-----:|:-------------------:|:--------:|:---------------------------------------:|:-----------------------------------------:|
* | dummy_group | dummy_param1 | string | - | dummy_default_value | 1 | This is the description of dummy_param1 | Here are additional notes |
* | - | dummy_param2 | double | m/s^2 | 9.81 | 0 | This is the description of dummy_param2 | The default value is the gravity constant |
*
* The device can be launched by yarpdev using one of the following examples:
* \code{.unparsed}
* yarpdev --device DeviceTemplate --dummy_group::dummy_param1 dummy_default_value --dummy_param2 9.81
* \endcode
*
* \code{.unparsed}
* yarpdev --device DeviceTemplate --dummy_group::dummy_param1 dummy_default_value
* \endcode
*
*/

class DeviceTemplate_ParamsParser : public yarp::dev::IDeviceDriverParams
{
public:
~DeviceTemplate_ParamsParser() override = default;

public:
const std::string m_device_type = {"DeviceTemplate"};
bool m_parser_is_strict = false;
std::string m_dummy_group_dummy_param1 = {"dummy_default_value"};
double m_dummy_param2 = {9.81};

bool parseParams(const yarp::os::Searchable & config) override;
std::string getDeviceType() const override { return m_device_type; }
std::string getDocumentationOfDeviceParams() const override;
std::vector<std::string> getListOfParams() const override;
};

#endif
2 changes: 2 additions & 0 deletions src/devices/deviceTemplate/DeviceTemplate_params.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
| dummy_group | dummy_param1 | string | - | dummy_default_value | Yes | This is the description of dummy_param1 | Here are additional notes |
| - | dummy_param2 | double | m/s^2 | 9.81 | No | This is the description of dummy_param2 | The default value is the gravity constant |

0 comments on commit 27dd882

Please sign in to comment.