message_filter (Python 3) not working in ROS Melodic
System information
- OS: Ubuntu 18.04
- ROS version: Melodic
- Conda environment: python=3.7 (Needed for a python3 processing module)
Problem explanation
I try to create a message filter that subscribes to two sensor_msg
topics after which a python3 module processes the Color and depth data in a callback function. To do this, I created the following script to achieve this:
#!/usr/bin/env python3
## Import standard library packages ##
import sys
## Import ROS python packages ##
import rospy
import sensor_msgs
from message_filters import ApproximateTimeSynchronizer, Subscriber
def gotimage(image, camerainfo):
assert image.header.stamp == camerainfo.header.stamp
print("got an Image and CameraInfo")
## Main script ##
# Only runs when this script is executed directly
if __name__ == "__main__":
## Initialize ros node ##
rospy.init_node('grasp_planner', anonymous=True)
image_sub = Subscriber("/kinect2/hd/image_color", sensor_msgs.msg.Image)
camera_sub = Subscriber("/kinect2/hd/camera_info", sensor_msgs.msg.CameraInfo)
## Create message_filter ##
ats = ApproximateTimeSynchronizer([image_sub, camera_sub], queue_size=5, slop=0.1)
ats.registerCallback(gotimage)
rospy.spin()
Unfortunately, after trying to run this script, I get the following error message:
/home/ricks/miniconda3/envs/panda_autograsp/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:526: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint8 = np.dtype([("qint8", np.int8, 1)])
/home/ricks/miniconda3/envs/panda_autograsp/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:527: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint8 = np.dtype([("quint8", np.uint8, 1)])
/home/ricks/miniconda3/envs/panda_autograsp/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:528: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint16 = np.dtype([("qint16", np.int16, 1)])
/home/ricks/miniconda3/envs/panda_autograsp/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:529: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint16 = np.dtype([("quint16", np.uint16, 1)])
/home/ricks/miniconda3/envs/panda_autograsp/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:530: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint32 = np.dtype([("qint32", np.int32, 1)])
/home/ricks/miniconda3/envs/panda_autograsp/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:535: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
np_resource = np.dtype([("resource", np.ubyte, 1)])
[ERROR] [1566834367.342856]: bad callback: <bound method Subscriber.callback of <message_filters.Subscriber object at 0x7fb849e44f50>>
Traceback (most recent call last):
File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/topics.py", line 750 ...
build
rospy
with the patch in your Catkin workspace (it wouldn't really be building, asrospy
is all Python, but you'll need to do it anyway for Catkin to properly overlay theapt
version with the version in your workspace)?Btw: you appear to be mixing Python 2.x and Python 3
PYTHONPATH
s. That is probably not a good idea.@gvdhoorn Thanks a lot for your answer. You mean that I have to build the
ros_comm
package (which includes therospy
module) from the source from within my catkin workspace as you explained in #252478 and #295012?My PYTHONPATH currently is
/opt/ros/melodic/lib/python2.7/dist-packages
since I sourced thedevel/setup.bash
of my workspace before running the python node. Do I need to remove this line when using a python3 ROS package?Ah, I now see what goes wrong. When I try to run the node in the python 3 virtual environment it gives me the
No module named 'rospy' not found
error.You don't need to build all of
ros_comm
most likely. Copying out just therospy
client library package should be sufficient (not very maintainable, but it should work).@gvdhoorn Thanks for your explanation. I tried building the
rospy
module from source but I now get a newattribute error
. The steps I performed while doing this can be found appended to the question above.The other strange thing is that the python3 package still ends up in the
/home/ricks/panda_autograsp_ws/devel/lib/python2.7/dist-packages
folder instead of the/home/ricks/panda_autograsp_ws/devel/lib/python3.7/dist-packages
.I don't believe that is strange: from what you show here it would appear you're still mixing Python 2 and Python 3 (your Conda environment). Without special care, Catkin will use Python 2.
source
ing a workspace from inside your Conda environment will not change that.Note that the patch only makes that particular part of
rospy
language-compatible with Python 3, if you're still using Python 2, Python 2 will still be used.You may try the procedure outlined here, but from within your Conda environment instead of a plain venv.
No guarantees, but it could work.
@gvdhoorn thanks a lot for the explanation. I will do some testing and report my solution back as an answer.
@gvdhoorn As I managed to use the package with python 2.7, after all, I didn't test the procedure you outlined in this topic. I, however, added an answer based on my current findings below so I can close this question. Although I know I saw a python 3 transitioning status REP for the ROS main packages (see the <reference> tag) somewhere I was unable to find it again. Do you maybe know where I can find such an overview? Feel free to correct me if I misunderstood something in my answer below.