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

How can I call a ros2 executable with arguments from a non-ROS file

asked 2022-07-27 12:02:52 -0600

AlexandrosNic gravatar image

updated 2022-10-04 09:11:17 -0600


I have two questions: I would like to create a ROS API to start ROS nodes from a non-ROS python script. For example the non-ROS python script:, would call the to start a node (assuming that ROS is already running). Even though this is not elegant, the only way I found to do that, is by writing in the Python script the terminal command: os.system("ros2 run package pick") My first question, is there another way to run ros2 run within Python?

Second, the pick method of the pick_ros executable ideally should take three arguments: x, y, z. Is there a way to call the ros node with arguments, without declaring these as ros parameters? (the reason is, that the calls smach (state machine), which is not a node. And declaring parameters can be done only in nodes) So I would like something like: ros2 run package pick x=0.2, y=0.2, z=0.2

Edit: Declaring parameters however raises a type error:

I get x, y, z values from an action message (as floats - double type is not valid), whether ROS parameter types only accepts double type (no float) which for a reason identifies it as a type error.

In particular, my action msg is:

# Request
float64 x
float64 y
float64 z

and the pick node is:

def pick_sm(x=0.2, y=0.3, z=0.2):

    node = rclpy.create_node('pick')


    x_par = Parameter('x', Parameter.Type.DOUBLE, x)
    y_par = Parameter('y', Parameter.Type.DOUBLE, y)
    z_par = Parameter('z', Parameter.Type.DOUBLE, z)

When I call the pick_sm node through a non-ROS python script, I get this error:

AssertionError: The 'x' field must be of type 'float'

which prevents me from using these x, y, z fields as ROS parameters

Thank you in advance

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2022-07-28 00:58:39 -0600

I put here how you could do that via python package ros2run.

from ros2run.api import get_executable_path, run_executable

executable_path = get_executable_path(executable_name="my_executable", package_name="my_package")
my_arg_values = ["myarg1", "myarg2", "myarg3"]

run_executable(path=executable_path,argv= my_arg_values)
edit flag offensive delete link more


That's exactly what I was looking for. Thanks Pablo! But then I would like to know what is the best way to use these arguments in the ros node executable.

It could be somethine like:

import sys
    x = float(sys.argv[1])
    y = float(sys.argv[2])
    z = float(sys.argv[3])

or maybe it has to do with:



In ROS1 it would be:


what about the ROS2 equivalent?

AlexandrosNic gravatar image AlexandrosNic  ( 2022-07-28 04:36:29 -0600 )edit

I would use ROS CLI: to setup the node parameters. You have a nice tutorial with YAML parameters file there:

ljaniec gravatar image ljaniec  ( 2022-07-28 08:21:08 -0600 )edit

thank you @ljaniec for your answer, however I wouldn't like to use yaml parameters since these are dynamically changing.

I could however use them as ROS parameters but oddly, I encountered some errors since x, y, z get their values from an action message (as floats - double type is not valid), whether ROS parameter types only accepts double type (no float) which for a reason identifies it as a type error.

And for that reason I was checking the possibility for sys.argv

AlexandrosNic gravatar image AlexandrosNic  ( 2022-07-28 08:30:19 -0600 )edit

Ok, dynamically changing parameters aren't currently possible AFAIK from documentation. This error with float/double seems a bit weird to be honest, maybe it is worth a try to debug? Could you add these error messages in the edit of the question?

ljaniec gravatar image ljaniec  ( 2022-07-28 08:38:15 -0600 )edit

I just edited the question to add these errors. Thank you for your help @ljaniec

AlexandrosNic gravatar image AlexandrosNic  ( 2022-07-28 09:26:36 -0600 )edit

Question Tools

1 follower


Asked: 2022-07-27 12:02:52 -0600

Seen: 553 times

Last updated: Oct 04 '22