Skip to content

Commit

Permalink
Enable setting prefix as param prefix.
Browse files Browse the repository at this point in the history
  • Loading branch information
destogl committed Feb 14, 2023
1 parent 1be2ca3 commit 6c90a20
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
22 changes: 16 additions & 6 deletions include/control_toolbox/pid_ros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,19 @@ class CONTROL_TOOLBOX_PUBLIC PidROS
* to add a prefix to the pid parameters
*
* \param node ROS node
* \param topic_prefix prefix to add to the pid parameters.
* \param prefix prefix to add to the pid parameters.
* Per default is prefix interpreted as prefix for topics.
* \param prefix_is_for_params provided prefix should be interpreted as prefix for parameters.
* See `initialize` for details.
*/
template<class NodeT>
explicit PidROS(std::shared_ptr<NodeT> node_ptr, std::string topic_prefix = std::string(""))
explicit PidROS(std::shared_ptr<NodeT> node_ptr, std::string prefix = std::string(""), bool prefix_is_for_params = false)
: PidROS(
node_ptr->get_node_base_interface(),
node_ptr->get_node_logging_interface(),
node_ptr->get_node_parameters_interface(),
node_ptr->get_node_topics_interface(),
topic_prefix)
prefix, prefix_is_for_params)
{
}

Expand All @@ -79,13 +82,13 @@ class CONTROL_TOOLBOX_PUBLIC PidROS
rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging,
rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_params,
rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr topics_interface,
std::string topic_prefix = std::string(""))
std::string prefix = std::string(""), bool prefix_is_for_params = false)
: node_base_(node_base),
node_logging_(node_logging),
node_params_(node_params),
topics_interface_(topics_interface)
{
initialize(topic_prefix);
initialize(prefix, prefix_is_for_params);
}

/*!
Expand Down Expand Up @@ -211,7 +214,14 @@ class CONTROL_TOOLBOX_PUBLIC PidROS

bool getBooleanParam(const std::string & param_name, bool & value);

void initialize(std::string topic_prefix);
/*!
* \param prefix prefix to add to the pid parameters.
* Per default is prefix interpreted as prefix for topics.
* \param prefix_is_for_params provided prefix should be interpreted as prefix for parameters.
* If the parameter is `true` then "/" will not be replaced with "." for parameters prefix,
* and "/" will be added to topic prefix, if prefix is not starting with "/" or "~".
*/
void initialize(std::string prefix, bool prefix_is_for_params = false);

rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr parameter_callback_;

Expand Down
22 changes: 17 additions & 5 deletions src/pid_ros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
namespace control_toolbox
{

void PidROS::initialize(std::string topic_prefix)
void PidROS::initialize(std::string prefix, bool prefix_is_for_params)
{
param_prefix_ = topic_prefix;
param_prefix_ = prefix;
// If it starts with a "~", remove it
if (param_prefix_.compare(0, 1, "~") == 0) {
param_prefix_.erase(0, 1);
Expand All @@ -54,14 +54,26 @@ void PidROS::initialize(std::string topic_prefix)
if (param_prefix_.compare(0, 1, "/") == 0) {
param_prefix_.erase(0, 1);
}
// Replace namespacing separator from "/" to "." in parameters
std::replace(param_prefix_.begin(), param_prefix_.end(), '/', '.');
if (!prefix_is_for_params)
{
// Replace namespacing separator from "/" to "." in parameters
std::replace(param_prefix_.begin(), param_prefix_.end(), '/', '.');
}
// Add a trailing "."
if (!param_prefix_.empty() && param_prefix_.back() != '.') {
param_prefix_.append(".");
}

topic_prefix_ = topic_prefix;
topic_prefix_ = prefix;
if (prefix_is_for_params)
{
// Add local namespace is global is not defined
if (param_prefix_.compare(0, 1, "~") != 0 && param_prefix_.compare(0, 1, "/") != 0)
{
topic_prefix_ = "/" + topic_prefix_;
}
}

// Replace parameter separator from "." to "/" in topics
std::replace(topic_prefix_.begin(), topic_prefix_.end(), '.', '/');
// Add a trailing "/"
Expand Down

0 comments on commit 6c90a20

Please sign in to comment.