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

How to run ROS command from a python script?

asked 2017-05-29 04:03:04 -0500

Radar gravatar image

Hello! I need some help as I was trying to write a python script which could run some ROS commands, such as $rostopic list and $rostopic echo(while some nodes are running). I tried something like the following, but none of them works. when I tried this:

os.system("rostopic list")

I got this error when I ran the above: sh: 1: rostopic: not found

Or this:

subprocess.call(["rostopic", "list"])

It was not working and a error message was given like this "FileNotFoundError: [Errno 2] No such file or directory: 'rostopic'"

I was thinking about execute a shell script from my python script, but I don't know how to write one for running ROS commands.

I appreciate any help!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2017-05-29 05:45:57 -0500

NEngelhard gravatar image

updated 2017-05-29 05:46:17 -0500

You can get the topics much easier using the rospy client functions rospy client module

import rospy
l = rospy.get_published_topics()
edit flag offensive delete link more
1

answered 2017-05-29 06:26:20 -0500

gvdhoorn gravatar image

I would do what @NEngelhard suggests, but just to answer your question: you need to run your Python script in an environment that has been setup with the ROS environment variables.

Two ways to do that:

  • start your script from a terminal in which you've sourced the right setup.bash
  • setup the environment which is created by os.system(..) with the needed environment variables

If you do want to continue starting ROS command line utilities, I'd recommend you use subprocess or popen instead of os.system(..).

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2017-05-29 04:03:04 -0500

Seen: 4,381 times

Last updated: May 29 '17