Visual Studio有极其强大的调试功能,所以在windows上开发运行ROS程序时,可以尝试用VS2019的工具来进行调试。
(1)安装调试版ROS2
要开发调试版程序,必须使用调试版本的ROS2,我使用的是galactic版本,因此到这里去下载,
https://github.com/ros2/ros2/releases/download/release-galactic-20210716/ros2-galactic-20210616-windows-debug-amd64.ziphttps://github.com/ros2/ros2/releases/download/release-galactic-20210716/ros2-galactic-20210616-windows-debug-amd64.zip下载完了后解压 ,整个安装过程和官网的指导完全一致,、
Installing ROS 2 on Windows — ROS 2 Documentation: Galactic documentationhttps://docs.ros.org/en/galactic/Installation/Windows-Install-Binary.html唯一不同的是解压地址,原来的dev被十替换成了dbg,表示这是debug版本。其余不变,
C:\dev\ros2_galactic ----> 替换成 ---> C:\dbg\ros2_galactic
当然,相应的启动文件是: d:\>dbg.bat,内容如下,
chcp 65001
call C:\dbg\ros2_galactic\local_setup.bat
(2)安装支持调试的python_d.exe
如果你的python已经安装了调试库,这一步可以忽略。
如果你在前面的安装中没有enable debug,那么你会碰到这个问题
https://github.com/ros2/ros2/issues/525https://github.com/ros2/ros2/issues/525也就是运行ros2时会输出“failed to create process”。
解决办法是修改你的python安装,比如我用的python3.8.3,下载地址是
Python Release Python 3.8.3 | Python.orghttps://www.python.org/downloads/release/python-383/下载的python安装包是python-3.8.3-amd64.exe、
然后勾选调试选项再安装就OK了,
另外,还要安装一下和调试版python_d配合的numpy和lxml,这个在官网的Extra stuff for Debug Mode中有提到,
Building ROS 2 on Windows — ROS 2 Documentation: Galactic documentationhttps://docs.ros.org/en/galactic/Installation/Windows-Development-Setup.html#extra-stuff-for-debug-mode如果不安装的话,你可能会碰到下面的报错,
D:\ros2dbg\examples_ws>ros2 interface --help
Failed to load entry point 'list':
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
We have compiled some common reasons and troubleshooting tips at:
https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
* The Python version is: Python3.8 from "c:\python38\python_d.exe"
* The NumPy version is: "1.22.3"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: No module named 'numpy.core._multiarray_umath'
Failed to load entry point 'package':
安装指令如下,
python_d -m pip install --force-reinstall https://github.com/ros2/ros2/releases/download/numpy-archives/numpy-1.18.4-cp38-cp38d-win_amd64.whl python_d -m pip install --force-reinstall https://github.com/ros2/ros2/releases/download/lxml-archives/lxml-4.5.1-cp38-cp38d-win_amd64.whl
你可以用下面的指令来检查一下,
python_d -c "from lxml import etree ; import numpy"
当你指望再次使用和编译release版二进制文件时,你必须重新安装这些库,
python -m pip uninstall numpy lxml python -m pip install numpy lxml
或者,
pip uninstall -y lxml pip uninstall -y numpy #pip uninstall -y setuptools #pip install setuptools --force-reinstall pip install numpy --force-reinstall pip install lxml --force-reinstall
或者
pip install numpy --upgrade
总体上看起来,貌似和重新编译ros2-galactic的过程很类似,不过要省掉很多编译工作。我自己在windows上试了好几回,都不能完整编译成功。所以还是选择了官方的调试版库二进制库文件进行调试先。
另外,到现在也有一些不明白的地方,比如都是同样的调试环境,调用colcon的时候默认居然是python.exe,而不是python_d.exe,即使使用下面的指令,也会时不时有报错,
python_d C:\Python38\Scripts\colcon.exe build
但是ros2指令用的却是python_d.exe。所以不同情况下使用,会有不同的要求。后续如果弄清楚了,我会在这里补充。
当然,如果你完全不在debug模式下,就不用担心这些问题。
(3)编译examples
首先,把examples从github上拉下来,
cd examples_ws/src
git clone --recursive -b galactic https://github.com/ros2/examples.git
然后切换到examples_ws目录下,
colcon build
通常不会有太多的问题,就能顺利编译成功。
这里我们以publisher, subscriber为例进行讲解。官方的讲解在这里,
Writing a simple publisher and subscriber (C++) — ROS 2 Documentation: Galactic documentationhttps://docs.ros.org/en/galactic/Tutorials/Writing-A-Simple-Cpp-Publisher-And-Subscriber.html但官方的编译路径和我们的有所不同,我没有单独拉出来处理,而是直接在examples目录下一次性编译,需要注意。
(4)初步调试运行
关于如何查找package并枚举出package中的所有节点,请参考这个
ROS2进阶:如何查找特定的包(package)并列出包中所有节点(node)_高精度计算机视觉的博客-CSDN博客https://spacevision.blog.csdn.net/article/details/123778532先启动publisher,
D:\ros2prj\examples_ws> call install/setup.bat
D:\ros2prj\examples_ws>ros2 run examples_rclcpp_minimal_publisher publisher_member_function
1648376964.791433 [0] 20188: using network interface 本地连接 (udp/10.88.0.6) selected arbitrarily from: 本地连接, 以太网 5
[INFO] [1648376965.313165200] [minimal_publisher]: Publishing: 'Hello, world! 0'
[INFO] [1648376965.808296400] [minimal_publisher]: Publishing: 'Hello, world! 1'
[INFO] [1648376966.304059200] [minimal_publisher]: Publishing: 'Hello, world! 2'
[INFO] [1648376966.814580500] [minimal_publisher]: Publishing: 'Hello, world! 3'
然后打开另一个控制窗口,启动subscriber,
D:\ros2prj\examples_ws>call install/setup.bat
"[rti_connext_dds_cmake_module][warning] RTI Connext DDS environment script not found (\resource\scripts\rtisetenv_x64Win64VS2017.bat). RTI Connext DDS will not be available at runtime, unless you already configured PATH manually."
"[rti_connext_dds_cmake_module][warning] RTI Connext DDS environment script not found (\resource\scripts\rtisetenv_x64Win64VS2017.bat). RTI Connext DDS will not be available at runtime, unless you already configured PATH manually."
D:\ros2prj\examples_ws>ros2 run examples_rclcpp_minimal_subscriber subscriber_member_function
1648381040.630496 [0] 15480: using network interface 本地连接 (udp/10.88.0.6) selected arbitrarily from: 本地连接, 以太网 5
[INFO] [1648381040.781102900] [minimal_subscriber]: I heard: 'Hello, world! 149'
[INFO] [1648381041.289512100] [minimal_subscriber]: I heard: 'Hello, world! 150'
[INFO] [1648381041.784118000] [minimal_subscriber]: I heard: 'Hello, world! 151'
[INFO] [1648381042.280680300] [minimal_subscriber]: I heard: 'Hello, world! 152'
[INFO] [1648381042.790295100] [minimal_subscriber]: I heard: 'Hello, world! 153'
[INFO] [1648381043.287359800] [minimal_subscriber]: I heard: 'Hello, world! 154'
这样,publisher和subcriber通迅就完成了。
(5)VS2019单步调试
通过前面的编译,这时候我们已经得到可以运行的VS2019项目了。但是,要独立运行这些项目,必须让系统能找到ROS的库文件,一个办法是把使用的库路径添加到系统环境变量中,例如,
C:\dbg\ros2_galactic\bin
同样,如果发现其他找不到的库,也需要添加相应的环境变量。
需要注意的是,如果你同时在系统中安装有多个ROS库,那么在使用其他版本时就必须把这个路径从环境变量中删除,否则会出现库文件冲突的现象。
调试结果,
如果调试publisher,那就保持subscriber在cmd窗口运行;反过来,如果调试subscriber,就保持publisher在cmd窗口运行,这样,就可以清楚看到各个程序的运行状态了。
本文结束。
附:编译过程中碰到的问题
CMake Error in CMakeLists.txt:
Imported target "rclcpp::rclcpp" includes non-existent path
"C:/ci/ws/install/include"
in its INTERFACE_INCLUDE_DIRECTORIES. Possible reasons include:
* The path was deleted, renamed, or moved to another location.
* An install or uninstall procedure did not complete successfully.
* The installation package was faulty and references files it does not
provide.
解决办法,
https://github.com/ros2/rclcpp/issues/1688
这里,这个C:\ci\ws\install\include是多余的,明显是忘记清除了。你可以找到那些CMakeLists.txt中相应的行,并将他们删除,另外一个简单的办法直接在以盘创建这么一个目录
C:\ci\ws\install\include
然后重新编译就没有问题了。