欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ROS笔记——在conda的python3.7中使用ROS Kinetic

程序员文章站 2021-12-08 19:22:13
环境:ubuntu 16.04, ROS Kinetic, conda 4.8.3, python3.7思路在python3环境下,直接使用tf会有如下报错(rospy可正常使用):>>> import tfTraceback (most recent call last): File "", line 1, in File "/opt/ros/kinetic/lib/python2.7/dist-pack....

环境:ubuntu 16.04, ROS Kinetic, conda 4.8.3, python3.7

思路

在python3环境下,直接使用tf会有如下报错(rospy可正常使用):

>>> import tf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/tf/__init__.py", line 28, in <module>
    from tf2_ros import TransformException as Exception, ConnectivityException, LookupException, ExtrapolationException
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/tf2_ros/__init__.py", line 38, in <module>
    from tf2_py import *
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/tf2_py/__init__.py", line 38, in <module>
    from ._tf2 import *
ImportError: dynamic module does not define module export function (PyInit__tf2)

解决:用conda环境下的python重新编译tf相关的catkin包。

步骤1:安装miniconda并配置

下载地址:https://conda.io/en/latest/miniconda.html

# 安装
sh Miniconda3-latest-Linux-x86_64.sh -b

# 初始化命令行
~/miniconda3/bin/conda init

# 创建新环境并配置
conda create --name ros_demo -y
conda activate ros_demo

conda install python=3.7 pip -y
conda install -c anaconda make
pip install numpy
pip install empy
pip install pyyaml

步骤2:安装ROS相关包(注意要将non-catkin包与catkin包分别放置)

安装non-catkin package

cd ~/miniconda3/envs/ros_demo/
mkdir pycode && cd pycode

# 安装rospkg
git clone https://github.com/ros-infrastructure/rospkg.git
cd rospkg/
python setup.py install

# 安装sip
cd ..
# 下载地址 https://riverbankcomputing.com/software/sip/download
cd sip-4.19.3
python configure.py 
make
sudo make install

安装catkin package

cd ~/miniconda3/envs/ros_demo/
mkdir -p catkin_ws/src && cd catkin_ws/src

#下载tf与tf2
git clone -b indigo-devel https://github.com/ros/geometry.git
git clone -b indigo-devel https://github.com/ros/geometry2.git

#下载tf所需orocos_kinematics_dynamics,直接下载kinetic对应的1.3.1版本
https://github.com/orocos/orocos_kinematics_dynamics/releases/tag/1.3.1

然后,将geometry2/test_tf2/CmakeLists.txt中的

if(NOT CATKIN_ENABLE_TESTING)
  return()
endif()

注释掉,原因见这里

步骤3:catkin_make编译

cd ~/miniconda3/envs/ros_demo/catkin_ws
catkin_make_isolated --cmake-args \
                     -DCMAKE_BUILD_TYPE=Release \
                     -DPYTHON_EXECUTABLE=~/miniconda3/envs/ros_demo/bin/python \
                     -DPYTHON_INCLUDE_DIR=~/miniconda3/envs/ros_demo/include/python3.7m \
                     -DPYTHON_LIBRARY=~/miniconda3/envs/ros_demo/lib/libpython3.7m.so
source devel_isolated/setup.bash

测试

Python 3.7.8 | packaged by conda-forge | (default, Jul 23 2020, 03:54:19) 
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import rospy
>>> rospy
<module 'rospy' from '/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/__init__.py'>
>>> import tf
>>> tf
<module 'tf' from '/home/f/miniconda3/envs/ros_demo/catkin_ws/devel_isolated/tf/lib/python3/dist-packages/tf/__init__.py'>
>>> 

 

本文地址:https://blog.csdn.net/weixin_42315410/article/details/107653495