Ask Your Question
1

Importing custom messages from other packages in Python

asked 2017-09-09 09:20:04 -0600

StevenCoral gravatar image

updated 2017-09-09 09:26:18 -0600

Hi all, I've been on this for hours now and I can't figure it out. It seems as if not this forum or the tutorials have a solution for this simple question. I have downloaded the Vesc stack here, without the vesc_ackerman package. It all builds nicely with the addition of the serial library, and even connects to the device and publishes topics stated in vesc_msgs. You can rostopic echo them, you can rosmsg list them (VescState and VescStateStamped). Now, what I am trying to do is to use these messages in another package, so that I can manipulate it.

I have a standard python node code, but it starts with: #import rospy, then #import VescState.msg (or from VescState import XXX) , and continues on (If any1 can point me on how to paste the code here without having only the first line as code format and everything else outside, plus hash-tags making the font huge, i'de love that...). I could go on but it doesnt really matter, because all I get when I try to run the code is:

Traceback (most recent call last): File "/home/tim/catkin_ws/src/vesc_comm/src/vesc_comm.py", line 5, in <module> from VescState.msg import * ImportError: No module named VescState.msg

I have tried every conceivable way of doing this. i have tried to rename the message files, adding them as native messages to my package, including all the package.xml and cmakelists.txt additions just to make a sanity check. tried catkin_make install. none of it worked. I actually do not want to make these messages native anyway. what I do want is to get my vesc_comm.py script to recognize the VescState.msg file from the other package (again - the messages work with the vesc_driver node and are listed in rosmsg!).

Why can I use every single message offered by ROS (std, geometry, pointcloud2...) but get the error stated above when I try and use a custom message?? my ROS is just not recognizing it. what am I doing wrong?

Thanks in advance, Steve

edit retag flag offensive close merge delete

Comments

If any1 can point me on how to paste the code here

copy-paste code, select lines, press ctrl+k or click the Preformatted Code button (the one with 101010 on it).

gvdhoorn gravatar imagegvdhoorn ( 2017-09-09 15:30:30 -0600 )edit

Thank you! i was clicking the icon first and then tried to paste code instead of "enter code here". wasnt working.

StevenCoral gravatar imageStevenCoral ( 2017-09-10 11:25:50 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2017-09-09 15:38:49 -0600

gvdhoorn gravatar image

updated 2017-09-10 11:36:45 -0600

import rospy
import VescState.msg   # (or from VescState import XXX)

In rospy, the ROS package name is the module name, and the message filename is the classname. As a package can include both messages and services, either a msg or srv submodule needs to be affixed.

So the .msg is not the .msg extension of the filename, but a module.

For the package you link (vesc_msgs), the import statement should probably be:

from vesc_msgs.msg import VescState

or if you just want to import all messages:

from vesc_msgs.msg import *

It seems as if not this forum or the tutorials have a solution for this simple question

This is always difficult, but I have the impression (but I've obviously been doing this for so long that it's easy for me to find this) that most tutorials and example code shows examples of this.

See wiki/ROS/tutorials/Defining Custom Messages - Including or Importing Messages - Python fi.

edit flag offensive delete link more

Comments

Yeah, thas exactly what I thought! knowing python, it felt weird to use the .msg for the filename while periods indicate modules. therefore i completely understood the error message, just didnt know why it worked for std_msgs.msg (that file exists). thanks a lot! ill try this.

StevenCoral gravatar imageStevenCoral ( 2017-09-10 11:29:25 -0600 )edit

btw, also tried 'from vesc_msgs import VescState', didnt work. in my own corrupt way i worked around this issue by copying "_VescState.py" into my src folder and using 'from _vescState import VescState', that did the trick. Thanks again for the explanation as well!

StevenCoral gravatar imageStevenCoral ( 2017-09-10 11:32:28 -0600 )edit

I'm a bit unsure what to make of your last comments. Just make sure to use the from package_name.msg import X approach. That should work. If it doesn't, something is wrong, but not with the import statement.

gvdhoorn gravatar imagegvdhoorn ( 2017-09-10 11:36:23 -0600 )edit

update: it works, thanks a lot. I was just mentioning the bad way I got it to work (using the actual python build files from the build directory), but I rather do it the right way (especially for future work).

StevenCoral gravatar imageStevenCoral ( 2017-09-10 12:00:07 -0600 )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: 2017-09-09 09:20:04 -0600

Seen: 6,317 times

Last updated: Sep 10 '17