一、问题描述
在进行ROS静态坐标变换学习时,导入tf2包运行报错。
出现这种问题一般是由于Ros目前只支持Python2,一些主要的功能包如tf2等都是在Python2的环境下运行的,在Python2中导入相关包不会出现问题,但在Python3中会出错。而目前大部分用户Ubuntu习惯配置和优先使用Python3,所以会导致这类问题出现。
个人感觉tf报错时,也可以用相同的处理方式,基本也是按照官方教程,链接如下
Traceback (most recent call last):
File "/home/bjh/catkin_ws/src/learning_tf2/nodes/static_turtle_tf2_broadcaster.py", line 8, in <module>
import tf
File "/home/bjh/catkin_ws/devel/lib/python2.7/dist-packages/tf/__init__.py", line 35, in <module>
exec(__fh.read())
File "<string>", line 30, in <module>
File "/home/bjh/catkin_ws/devel/lib/python2.7/dist-packages/tf2_ros/__init__.py", line 35, in <module>
exec(__fh.read())
File "<string>", line 38, in <module>
File "/home/bjh/catkin_ws/devel/lib/python2.7/dist-packages/tf2_py/__init__.py", line 35, in <module>
exec(__fh.read())
File "<string>", line 38, in <module>
ImportError: dynamic module does not define module export function (PyInit__tf2)
二、解决方法
ROS melodic 配置 python3:
1.安装 ROS python3 依赖
sudo apt update
sudo apt install python3-catkin-pkg-modules python3-rospkg-modules python3-empy
2.编译工作空间
在这里mkdir -p ~/catkin_ws/src不是必须要的,这是又新建了一个工作空间,直接cd到你需要rosrun的工作即可,然后依次终端输入即可。而且这里git https://github.com/ros/geometry2 -v 0.6.5是可以到的,因为自身是小白,所以还在担心没有科学上网,最后运行是成功的。
mkdir -p ~/catkin_ws/src; cd ~/catkin_ws
catkin_make
source devel/setup.bash
wstool init
wstool set -y src/geometry2 --git https://github.com/ros/geometry2 -v 0.6.5
wstool up
rosdep install --from-paths src --ignore-src -y -r
3.进行python3配置
catkin_make --cmake-args \
-DCMAKE_BUILD_TYPE=Release \
-DPYTHON_EXECUTABLE=/usr/bin/python3 \
-DPYTHON_INCLUDE_DIR=/usr/include/python3.6m \
-DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.so
4.source工作空间
source ./devel/setup.bash
5.其他情况解决
(1)wstool报错,No module named 'wstool',参考以下链接
(2)最后运行报错
Traceback (most recent call last):
File "/home/gln/catkin_ws/src/tf2_learning/scripts/demo01_static_pub_p.py", line 3, in <module>
import tf
File "/opt/ros/melodic/lib/python2.7/dist-packages/tf/__init__.py", line 31, in <module>
from .listener import Transformer, TransformListener, TransformerROS
File "/opt/ros/melodic/lib/python2.7/dist-packages/tf/listener.py", line 29, in <module>
import numpy
ModuleNotFoundError: No module named 'numpy'
此时
sudo apt-get install python3-numpy
即可。
当然中间还有其他的问题,比如说缺少ros_pkg包,编译bashrc的时候出现等常见问题,按照其他大神的流程操作即可,不再赘述。