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 65001call 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、
https://www.python.org/ftp/python/3.8.3/python-3.8.3-amd64.exehttps://www.python.org/ftp/python/3.8.3/python-3.8.3-amd64.exe启动修改安装
然后勾选调试选项再安装就OK了,
(3)编译examples
首先,把examples从github上拉下来,
cd examples_ws/srcgit 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)_高精度计算机视觉的博客先启动publisher,
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
然后重新编译就没有问题了。