Ask Your Question
0

Input on launch file

asked 2020-06-03 03:57:20 -0500

anonymous user

Anonymous

updated 2020-06-03 03:57:33 -0500

Hey all,

I am using ROS Noetic and I have a a python 3 script that is being launched with a launch file. To further automatize my project, I wanted to know if it's possible to include input on the launch file to be used within the python script? And if so, How do I do it?

Thank you

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-06-03 04:08:52 -0500

Mackou gravatar image

updated 2020-06-03 04:15:02 -0500

The best would be to create parameters in your python script. Examples here (Make them private with ~)

For example in my node I have :

self.target_frame = rospy.get_param("~target_frame", "map")

And I can start it like this in in my launchfile :

<node pkg="robot_platform" type="test.py" name="test" output="screen">
    <param name="target_frame" value="test" />
</node>

This method is very good because it allows you to use the parameter server to set your parameters also and load them from a yaml file for example.

You can read more about the parameter server here

edit flag offensive delete link more

Comments

Hey! I tried your approach but on the terminal I keep getting a KeyError. MY launch file is below:

<?xml version="1.0"?>
<launch>

  <node pkg="gspn_framework_package" ns="robot_1" type="gspn_execution_node.py" name="executor_1">
    <param name="user_robot_id" value="1" />
    <param name="user_current_place" value="p1" />
  </node>

  <node pkg="gspn_framework_package" ns="robot_2" type="gspn_execution_node.py" name="executor_2">
    <param name="user_robot_id" value="2" />
    <param name="user_current_place" value="p3" />
  </node>

</launch>

And on my Python I have:

namespace = rospy.get_namespace()
user_robot_id = rospy.get_param(namespace + "user_robot_id")
user_current_place = rospy.get_param(namespace + "user_current_place")

What am I doing wrong? I also tried without the namespace but I had the same error.

anonymous userAnonymous ( 2020-06-03 04:40:00 -0500 )edit

I didnt use rospy.get_namespace.

If you put ~ at the beginning of your param name to set it private you shouldn't need it,

Like so :

user_robot_id = rospy.get_param("~user_robot_id")
user_current_place = rospy.get_param("~user_current_place")

Does it solve your problem ?

Mackou gravatar image Mackou  ( 2020-06-03 04:44:54 -0500 )edit

Sadly, I still get the KeyError. I did exactly as you told me: without using the namespace and using the "~". Maybe there's something wrong in my launch file.

anonymous userAnonymous ( 2020-06-03 05:54:13 -0500 )edit
1

Do you have rospy.init_node() in your node ?

Mackou gravatar image Mackou  ( 2020-06-03 05:57:48 -0500 )edit
1

Okay it worked! I was doing rospy.init_node() afterwards. Thank you :)

anonymous userAnonymous ( 2020-06-03 06:06:46 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2020-06-03 03:57:20 -0500

Seen: 287 times

Last updated: Jun 03 '20