当前位置: 萬仟网 > IT编程>脚本编程>Python > ROS笔记——在conda的python3.7中使用ROS Kinetic

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

2020年07月30日  | 萬仟网IT编程  | 我要评论
环境:ubuntu 16.04, ROS Kinetic, conda 4.8.3, python3.7思路在python3环境下,直接使用tf会有如下报错(rospy可正常使用):>>> import tfTraceback (most recent call last): File "<stdin>", line 1, in <module> 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

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

相关文章:

验证码:
Copyright © 2017-2021  萬仟网 保留所有权利. 粤ICP备17035492号-1
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com