How can I call a ros2 executable with arguments from a non-ROS file
Hello,
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: pick.py, would call the pick_ros.py 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 pick_ros.py 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):
rclpy.init(args=None)
node = rclpy.create_node('pick')
node.declare_parameter('x')
node.declare_parameter('y')
node.declare_parameter('z')
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