IT编程 > 脚本编程 > Python

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

146355人参与2020-07-30

环境: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

您对本文有任何疑问!!点此进行留言回复

推荐阅读

猜你喜欢

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

07-30

剑指Offer-4二维数组中的查找(2020.7.28)

07-30

荐 Python+Mysql爬虫之爬取Json格式数据

07-30

Pyhton缺失值处理

07-30

基于id3算法根据房价数据进行画图预测python

07-30

python学习之路 --(列表推导式的应用)

07-30

拓展阅读

大家都在看

Numpy用于数据存储和读取

12-06

python 异常处理 StopIteration 用来作为迭代器的输出停止/next()

07-12

OpenCV 画多边形 — cv.polylines()函数使用

10-06

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

07-30

使用Keras加载含有自定义层或函数的模型操作

06-18

python 实现rolling和apply函数的向下取值操作

06-23

pycharm 2020 Error: failed to send plot to http://127.0.0.1:63342

07-30

使用Python来开发Markdown脚本扩展的实例分享

05-30

热门评论