Ask Your Question
0

Importing ROS Messages in Python

asked 2014-06-30 16:25:35 -0500

nckswt gravatar image

I'm using ROS as middleware to connect my model (in Python) and my view (in JS), so I want to keep my Python code as separate as possible from ROS-related code. My directory structure looks like:

.
|____CMakeLists.txt
|____cops_and_robots
| |______init__.py
| |____Cop.py
| |____Map.py
| |____MapObj.py
| |____Robot.py
| |____test
| | |____unit
| | | |______init__.py
| | | |____moveTest.py
|____msg
| |____battery.msg
| |____cmd.msg
|____package.xml
|____README.txt
|____scripts
| |____status_talker.py
| |____websocket_interface.py
|____setup.py

I'm trying to import battery.msg from my status_talker.py script, but Python doesn't work too well with relative imports.

I have two questions:

  1. Do I need to add an __init__.py to my msg folder to be able to import battery.msg? I don't entirely know pythonic standards, but it seems odd that I'd need to designate msg as a module.
  2. Is this an acceptable way to structure my package? I'm trying to be standards-compliant, but I don't know if I am!
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2014-06-30 20:58:53 -0500

nckswt gravatar image

Turns out the problem was with calling setup.py myself. The documentation explains issues with calling setup.py manually screws things up.

Instead, just uncomment catkin_python_setup() in CMakeLists.txt and follow the instructions in this tutorial.

edit flag offensive delete link more
2

answered 2014-06-30 17:07:38 -0500

.msg files are not importable by python. Python modules will be generated when you build your package (with catkin or rosbuild), and placed in your package's src/PKG_NAME/msg directory. They will be added to your pythonpath (either automatically with catkin, or with the load_manifest(PKG_NAME) command with rosbuild)

edit flag offensive delete link more

Comments

Hmm - it doesn't look like I'm getting cops_and_robots.msg added as a submodule when I examine it using pkgutil even though I think I've configured my CMakeLists and my package.xml properly (see http://goo.gl/2lKTjk ). rosmsg show cops_and_robots/battery works, though.

nckswt gravatar imagenckswt ( 2014-06-30 20:25:38 -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

1 follower

Stats

Asked: 2014-06-30 16:25:35 -0500

Seen: 747 times

Last updated: Jun 30 '14