Ubuntu16.04安装ROS kinetic踩坑全记录(apt-get出现依赖问题、python3与kinetic不兼容、安装ROS后不能导入cv2等)
- 使用ROS官网的安装命令sudo apt-get install ros-kinetic-desktop-full出现依赖问题
- 完成官网给出的安装过程后,在python环境出现不能import cv2的问题
最近需要在实验室的服务器上安装ROS调试机械手,因为服务器的ubuntu版本是16.04,所以只能装kinetic版的ROS,过程中出现了很多坑,记录一下,有出现类似问题的可以参考进行解决。
使用ROS官网的安装命令sudo apt-get install ros-kinetic-desktop-full出现依赖问题
这个问题是处理完链接key等前面的命令后出现的,正常来说apt-get install不应该出现依赖问题,后面搜索相关博客说是因为没有使用国内的镜像源,但是我在更改为国内镜像源后仍然出现了依赖问题,尝试一一安装依赖然而并不能成功,后面选择使用aptitude进行安装:
sudo aptitude install ros-kinetic-desktop-full
注意:不要选择一开始aptitude给你的解决方案,第一次选择n,之后再选择Y
使用aptitude成功安装ros之后,后面就可以正常使用apt进行ROS的安装和卸载。
完成官网给出的安装过程后,在python环境出现不能import cv2的问题
在最后执行完
echo "source /opt/ros/kinetic/setup.bash" >> ~/.bashrc
后,在对应的python3 conda环境中不能正常导入cv2,报错显示是/opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so有问题,Stack Overflow上给出的方法是注释掉.bashrc中的source /opt/ros/kinetic/setup.bash,然而这样做之后ROS不能正常使用,也就是说我们只能用cv2或者ros或者在python2中使用cv2和ros,然而现在很多项目需要python3。在尝试删除/opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so文件后虽然能正常导入cv2了,但是仍然不能正常使用CvBridge,报错如下:
[ERROR] [1520780674.845066]: bad callback: <bound method ViewsBuffer.update of <__main__.ViewsBuffer object at 0x7f5f45a07f28>>
Traceback (most recent call last):
File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/topics.py", line 750, in _invoke_callback
cb(msg)
File "test.py", line 48, in update
im = self.bridge.imgmsg_to_cv2(im, "bgr8")
File "/opt/ros/kinetic/lib/python2.7/dist-packages/cv_bridge/core.py", line 163, in imgmsg_to_cv2
dtype, n_channels = self.encoding_to_dtype_with_channels(img_msg.encoding)
File "/opt/ros/kinetic/lib/python2.7/dist-packages/cv_bridge/core.py", line 99, in encoding_to_dtype_with_channels
return self.cvtype2_to_dtype_with_channels(self.encoding_to_cvtype2(encoding))
File "/opt/ros/kinetic/lib/python2.7/dist-packages/cv_bridge/core.py", line 91, in encoding_to_cvtype2
from cv_bridge.boost.cv_bridge_boost import getCvType
ImportError: dynamic module does not define module export function (PyInit_cv_bridge_boost)
出现这种请款的原因是我们使用apt或者aptitude安装ros时默认会搜索python2进行安装和编译,然而我们使用的conda环境却是python3的,这与ros编译之后的部分内容冲突,需要我们基于python3重新编译这一部分。主要就是cv_bridge。相关连接可以参考:stackoverflow 重新编译cv_bridge的全部命令如下:
# `python-catkin-tools` is needed for catkin tool
# `python3-dev` and `python3-catkin-pkg-modules` is needed to build cv_bridge
# `python3-numpy` and `python3-yaml` is cv_bridge dependencies
# `ros-kinetic-cv-bridge` is needed to install a lot of cv_bridge deps. Probaply you already have it installed.
sudo apt-get install python-catkin-tools python3-dev python3-catkin-pkg-modules python3-numpy python3-yaml ros-kinetic-cv-bridge
# Create catkin workspace
mkdir catkin_workspace
cd catkin_workspace
catkin init
# Instruct catkin to set cmake variables
# 下面这个命令的三个python路径可以使用这些命令查询:
# $python3
# import sys
# print(sys.executable)
# print(sys.path)
# $python3-config --includes
catkin config -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.5m -DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so
# Instruct catkin to install built packages into install place. It is $CATKIN_WORKSPACE/install folder
catkin config --install
# Clone cv_bridge src
git clone https://github.com/ros-perception/vision_opencv.git src/vision_opencv
# Find version of cv_bridge in your repository
apt-cache show ros-kinetic-cv-bridge | grep Version
Version: 1.12.8-0xenial-20180416-143935-0800
# Checkout right version in git repo. In our case it is 1.12.8
cd src/vision_opencv/
git checkout 1.12.8
cd ../../
# Build
catkin build cv_bridge
# Extend environment with new package
source install/setup.bash --extend
在编译cv_bridge时将相应的python解释器路径设置为自己的conda环境下的路径,之后进行编译。编译过程可能会出现不能import numpy等情况,使用conda安装即可。
之后可能编译会不成功,出现如下报错:
Warnings << cv_bridge:make /home/wj/catkin_workspace/logs/cv_bridge/build.make.000.log
In file included from /home/wj/catkin_workspace/src/vision_opencv/cv_bridge/src/module_opencv3.cpp:3:0:
/home/wj/catkin_workspace/src/vision_opencv/cv_bridge/src/module.hpp: In function ‘int do_numpy_import()’:
/home/wj/catkin_workspace/src/vision_opencv/cv_bridge/src/module.hpp:39:5: warning: converting to non-pointer type ‘int’ from NULL [-Wconversion-null]
import_array( );
^
In file included from /home/wj/catkin_workspace/src/vision_opencv/cv_bridge/src/module.cpp:35:0:
/home/wj/catkin_workspace/src/vision_opencv/cv_bridge/src/module.hpp: In function ‘int do_numpy_import()’:
/home/wj/catkin_workspace/src/vision_opencv/cv_bridge/src/module.hpp:39:5: warning: converting to non-pointer type ‘int’ from NULL [-Wconversion-null]
import_array( );
^
cd /home/wj/catkin_workspace/build/cv_bridge; catkin build --get-env cv_bridge | catkin env -si /usr/bin/make --jobserver-fds=6,7 -j; cd -
........................................................................................................................................................................................................
________________________________________________________________________________________________________________________________________________________________________________________________________
Errors << cv_bridge:install /home/wj/catkin_workspace/logs/cv_bridge/build.install.000.log
usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
or: setup.py --help [cmd1 cmd2 ...]
or: setup.py --help-commands
or: setup.py cmd --help
error: option --install-layout not recognized
CMake Error at catkin_generated/safe_execute_install.cmake:4 (message):
execute_process(/home/wj/catkin_workspace/build/cv_bridge/catkin_generated/python_distutils_install.sh)
returned error code
Call Stack (most recent call first):
cmake_install.cmake:146 (include)
make: *** [install] Error 1
cd /home/wj/catkin_workspace/build/cv_bridge; catkin build --get-env cv_bridge | catkin env -si /usr/bin/make install; cd -
........................................................................................................................................................................................................
Failed << cv_bridge:install [ Exited with code 2 ]
Failed <<< cv_bridge [ 7.5 seconds ]
[build] Summary: 1 of 2 packages succeeded.
[build] Ignored: 3 packages were skipped or are blacklisted.
[build] Warnings: 1 packages succeeded with warnings.
[build] Abandoned: None.
[build] Failed: 1 packages failed.
[build] Runtime: 9.1 seconds total.
查看问题日志,出现的主要问题就是
error: option --install-layout not recognized
解决方法很简单在catkin build cv_bridge 后面加上-DCMAKE_BUILD_TYPE=Release -DSETUPTOOLS_DEB_LAYOUT=OFF就可以正常编译了,即
catkin build cv_bridge -DCMAKE_BUILD_TYPE=Release -DSETUPTOOLS_DEB_LAYOUT=OFF
正确的重新编译cv_bridge后会如下图所示:
之后进入conda的python3环境:
完全正常!现在你可以使用conda创建的python3环境在ROS上跑相关的python3代码了