基于python3的ros melodic源代码编译安装

1. 卸载已安装的ros版本

sudo apt-get remove ros-*
sudo apt-get remove ros-melodic-*
sudo apt-get autoremove

修改.bashrc内容, 注释掉 #source /opt/ros/melodic/setup.bash

vim ~/.bashrc
#source /opt/ros/melodic/setup.bash

删除source list文件

sudo rm -rf /etc/ros/rosdep/sources.list.d/20-default.list

2. 安装python3 ros的依赖

安装python3版本

sudo apt update
sudo apt install -y python3 python3-dev python3-pip build-essential

使用pip3安装ros工具包

sudo -H pip3 install rosdep rospkg rosinstall_generator rosinstall wstool vcstools catkin_tools catkin_pkg

初始化rosdep

sudo rosdep init
rosdep update

3. 下载并编译ros melodic

创建ros工作空间

mkdir ~/ros_catkin_ws
cd ~/ros_catkin_ws

这里下载编译ros-melodic-desktop-full版本,当然也可以下载ros-comm版本,见参考3链接.

rosinstall_generator desktop_full --rosdistro melodic --deps --tar > melodic-desktop-full.rosinstall
wstool init -j8 src melodic-desktop-full.rosinstall

如果下载失败,则运行, 可以继续下载

wstool update -j4 -t src

修改源代码对python的依赖,改为python3的依赖,这一步从参考5中而来

#install wxPython
pip install -U -f https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-18.04 wxPython

create install_skip file

printf '#/bin/bash\nif [ $(whoami) != root ]; then\n    echo You must be root or use sudo to install packages.\n    return\nfi\n\nfor pkg in "$@"\ndo\n    echo "Installing $pkg"\n    sudo apt-get -my install $pkg >> install.log\ndone' > install_skip

make file executable

chmod +x install_skip

install python 3 packages

sudo ./install_skip `rosdep check --from-paths src --ignore-src | grep python | sed -e "s/^apt\t//g" | sed -z "s/\n/ /g" | sed -e "s/\/python3/g"`

skip python 2 packages

rosdep install --from-paths src --ignore-src -y --skip-keys="`rosdep check --from-paths src --ignore-src | grep python | sed -e "s/^apt\t//g" | sed -z "s/\n/ /g"`"

rename all old python links to python3

find . -type f -exec sed -i 's/\/usr\/bin\/env[ ]*\/\/usr\/bin\/env python3/g' {} +

remove all depricated install-layout=deb arguments

find ./ -name 'python_distutils_install.sh' -exec sed -i 's/--install-layout=deb//g' {} \;

安装ros所需要的依赖库

rosdep install --from-paths src --ignore-src --rosdistro melodic -y

编译并安装ros

export ROS_PYTHON_VERSION=3
sudo ./src/catkin/bin/catkin_make_isolated --install --install-space /opt/ros/melodic -DCMAKE_BUILD_TYPE=Release

配置ros

echo “source /opt/ros/melodic/setup.bash” >> ~/.bashrc
source ~/.bashrc

现在已经成功安装python3版本的ros了.

4. 使用过程中的一些问题

在python代码中,使用ros的cv_bridge可能会出现下列问题:

Exception in thread Thread-6:
Traceback (most recent call last):
File “/usr/lib/python3.6/threading.py”, line 916, in _bootstrap_inner
self.run()
File “/usr/lib/python3.6/threading.py”, line 864, in run
self._target(*self._args, **self._kwargs)
File “/home/cloud/hfnet/hfnet_mapping_cpp/hfnet_loc_ros/src/hfnet_loc/scripts/main_loc.py”, line 108, in HfnetLoc
cv_image = bridge.imgmsg_to_cv2(img_msg, “MONO8”) #MONO8
File “/opt/ros/melodic/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/melodic/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/melodic/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)

这是由于在 cv_bridge在编译的时候用了python2.7了,而我们使用的python3运行就会失败.

解决方式如下:
需要重新使用 python3 编译安装 cv_bridge
首先安装一些工具

sudo apt-get install python-catkin-tools python3-dev python3-numpy

创建工作空间

mkdir ~/cv_bridge_ws && cd ~/cv_bridge_ws

设置编译环境

catkin config -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.6m -DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.so

catkin config --install

从github上下载官方源代码进行编译, 这里使用的是melodic分支

mkdir src && cd src
git clone -b melodic https://github.com/ros-perception/vision_opencv.git

编译代码

cd …
catkin build cv_bridge
source install/setup.bash --extend

Reference

  1. https://www.miguelalonsojr.com/blog/robotics/ros/python3/2019/08/20/ros-melodic-python-3-build.html
  2. http://wiki.ros.org/melodic/Installation/Source
  3. https://zhuanlan.zhihu.com/p/77682229
  4. https://medium.com/@beta_b0t/how-to-setup-ros-with-python-3-44a69ca36674
  5. https://gist.github.com/drmaj/20b365ddd3c4d69e37c79b01ca17587a

你可能感兴趣的:(基于python3的ros melodic源代码编译安装)