ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
I think I have the same problem right now. I am building a quadruped robot and I want to have the code independend from the actual names of the motors (they should only be mapped via the yaml and urdf).
I made a pretty hacky solution. My yaml looks like this:
param_tester:
ros__parameters:
my_names: ["one", "two", "three"]
my_nodes:
one:
id : 0
init : 1500
two:
id : 1
init : 1234
three:
id : 2
init : 4321
And my code would then looks like this:
this->declare_parameter("my_names")
std::vector<std::string> n = this->get_parameter("my_names").as_string_array();
for(std::string &name : n) {
this->declare_parameter("my_nodes." + name + ".id");
this->declare_parameter("my_nodes." + name + ".init");
int64_t id = this->get_parameter("my_nodes." + name + ".id").as_int();
int64_t init = this->get_parameter("my_nodes." + name + ".init").as_int();
RCLCPP_INFO(this->get_logger(), "Name: '%s'", name.c_str());
RCLCPP_INFO(this->get_logger(), "id: %d", id);
RCLCPP_INFO(this->get_logger(), "init: %d", init);
}
Is there any simpler/easier way to do this? I don't really like the repeating name thing, but it does work. This actually should have been an answer to the comment from Christian Rauch, but the comment was too long.
Edit: fixed the code.