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
修改/src/catkin/bin文件夾下的所有文件,將第一行修改成python3,例如:
#!/usr/bin/env python3
安裝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
- https://www.miguelalonsojr.com/blog/robotics/ros/python3/2019/08/20/ros-melodic-python-3-build.html
- http://wiki.ros.org/melodic/Installation/Source
- https://zhuanlan.zhihu.com/p/77682229
- https://medium.com/@beta_b0t/how-to-setup-ros-with-python-3-44a69ca36674