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

rtt_ros2_params: add declareParameter operation #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions rtt_ros2_params/include/orocos/rtt_ros2_params/rosparam.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ class RosParam : public RTT::ServiceRequester
: RTT::ServiceRequester("rosparam", owner),
getParameter("getParameter"),
setParameter("setParameter"),
declareParameter("declareParameter"),
setOrDeclareParameter("setOrDeclareParameter"),
loadProperty("loadProperty"),
storeProperty("storeProperty")
{
this->addOperationCaller(getParameter);
this->addOperationCaller(setParameter);
this->addOperationCaller(declareParameter);
this->addOperationCaller(setOrDeclareParameter);

// Operations loadProperty and storeProperty are only available if the requester is connected
Expand All @@ -61,6 +63,9 @@ class RosParam : public RTT::ServiceRequester
RTT::OperationCaller<bool(
const std::string & name,
const rclcpp::ParameterValue & value)> setParameter;
RTT::OperationCaller<bool(
const std::string & name,
const rclcpp::ParameterValue & default_value)> declareParameter;
RTT::OperationCaller<bool(
const std::string & name,
const rclcpp::ParameterValue & value)> setOrDeclareParameter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class RosParamService : public RTT::Service
bool setParameter(
const std::string & name,
const rclcpp::ParameterValue & value);
bool declareParameter(
const std::string & name,
const rclcpp::ParameterValue & default_value);
bool setOrDeclareParameter(
const std::string & name,
const rclcpp::ParameterValue & value);
Expand Down
41 changes: 41 additions & 0 deletions rtt_ros2_params/src/rtt_ros2_params_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ RosParamService::RosParamService(RTT::TaskContext * owner)
.arg("name", "Name of the parameter to set")
.arg("value", "The value of the parameter to set");

addOperation(
"declareParameter", &RosParamService::declareParameter, this, RTT::ClientThread)
.doc("Try to declare a parameter to the node parameter server and set its value.")
.arg("name", "Name of the parameter to set")
.arg("default_value", "The default value of the parameter to declare");

addOperation(
"setOrDeclareParameter", &RosParamService::setOrDeclareParameter, this, RTT::ClientThread)
.doc("Sets a parameter to the node parameter server and eventually declares it")
Expand Down Expand Up @@ -147,6 +153,41 @@ bool RosParamService::setParameter(
return true;
}

bool RosParamService::declareParameter(
const std::string & name,
const rclcpp::ParameterValue & default_value=rclcpp::ParameterValue())
{
RTT::Logger::In in(getName());

RTT::log(RTT::Debug) <<
"Declaring the parameter \"" << name << "\"" <<
RTT::endlog();

auto rosnode = rtt_ros2_node::getNode(getOwner());
if (nullptr == rosnode) {
RTT::log(RTT::Error) <<
"No ROS node service from package rtt_ros2_node loaded into this "
"component or as a global service." <<
RTT::endlog();
return false;
}

if (!rosnode->has_parameter(name)) {
// Create new parameter
RTT::log(RTT::Info) <<
"The parameter did not exist" <<
RTT::endlog();
rosnode->declare_parameter(name, default_value);
return true;
}
else{
RTT::log(RTT::Error) <<
"Failed to declare ROS parameter " << name << ": " <<
"has already been declared" << RTT::endlog();
return false;
}
}

bool RosParamService::setOrDeclareParameter(
const std::string & name,
const rclcpp::ParameterValue & value)
Expand Down