ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
2

ros2 how to get non-ros parameter argument count

asked 2020-04-08 11:01:47 -0600

cmorgan gravatar image

I have code like:

int main(...)
{
   rclcpp::init(argc, argv);

   if(argc != 3) {
      printf("Error, missing arguments for a, b\n");
 }
...

The problem with this is that when using launch_testing.launch_test, or any launch file and specifying arguments for my application, these tools/libraries are adding extra parameters like:

mcu_sim_test 20 -50 --ros-args -r __node:=test -r __ns:=/mcu_sim_namespace

These additional --ros-args are throwing off the argument counts even though they aren't being used by my package but rather used by ros2.

I did see that this is an intentional change from ros1 where the argc/argv were being altered and that makes a ton of sense. Where I'm stuck is how to address it.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-02-10 22:19:49 -0600

guru_florida gravatar image

For rclcpp, in #include <utilities.hpp> you have the following two functions that return a std::vector<std::string> with the argv trimmed down of any ROS arguments:

std::vector<std::string>
init_and_remove_ros_arguments(
  int argc,
  char const * const * argv,
  const InitOptions & init_options = InitOptions());

and:

std::vector<std::string>
remove_ros_arguments(int argc, char const * const * argv);

In python rclpy, you have args = rclpy.utilities.remove_ros_args(sys.argv) which does the same thing.

edit flag offensive delete link more

Comments

I know this is a reply to a 2yo question, but it's worthy of an answer. :)

guru_florida gravatar image guru_florida  ( 2022-02-10 22:20:17 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-04-08 11:01:47 -0600

Seen: 316 times

Last updated: Feb 10 '22