首先,成功安装ros2环境,参考官方文档中的教程,能用运行出来此处的代码
Writing a simple publisher and subscriber (Python) --- ROS 2 Documentation: Iron documentation
下载vscode,进行远程开发,具体参考:Vscode进行远程开发-CSDN博客
在vscode中安装调试ros2所需要的插件,有Python、Pylance、C/C++、CMake Tools、ROS

这些插件安装完成之后,我们已经可以在Vscode中自由跳转Python代码和Ros环境中的rclpy包了

之所以能跳转ros环境的包,是因为我们项目文件夹下面的./vscode中settings.json中成功导入了ros环境的位置

除了.settings.json文件夹,安装Ros插件后,.vscode文件中会自动有一个c_cpp_properties.json文件,里面也是已经自动填充了内容

接下来在.vscode中创建tasks.json文件


会自动生成tasks.json文件,并且插入内容

然后继续在.vscode创建一个文件launch.json文件
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
// Example launch of a python file
{
"name": "Launch",
"type": "python",
"request": "launch",
"program": "/opt/demo_ws/src/py_pubsub/py_pubsub/publisher_member_function.py", #注意此处改成你自己的要调试的文件路径
"console": "integratedTerminal",
},
// Example gdb launch of a ros executable
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "/opt/demo_ws/py_pubsub/lib/py_pubsub/talker",#此处暂时不太清楚
"args": [],
"stopAtEntry": true,
"cwd": "/opt/demo_ws",
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
],
"inputs": [
{
"id": "package",
"type": "promptString",
"description": "Package name",
"default": "learning_ros2"
},
{
"id": "program",
"type": "promptString",
"description": "Program name",
"default": "ros2_talker"
}
]
}
我们目前只能调试python文件,无法调试 ros executable 。打上断点,即可调试了

还有一个问题是:
没有办法调试srv和msg,目前需要解决一下
