# ros2_example **Repository Path**: SAquarius/ros2_example ## Basic Information - **Project Name**: ros2_example - **Description**: ros2的示例程序 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-08-05 - **Last Updated**: 2023-02-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 把ros的脚本加入到.bashrc中 执行一次即可,这样每次启动终端都会识别到ros2命令 ```shell # 添加 echo "source /opt/ros/humble/setup.sh" >> ~/.bashrc # source一遍 source ~/.bashrc ``` # 编译指令 在工作空间目录下执行以下命令: ```shell # 编译 colcon build --symlink-install # 传入cmake编译类型参数用于调试 colcon build --symlink-install --cmake-args -DCMAKE_BUILD_TYPE=Debug ``` # source install/setup.bash 在工作空间目录下执行source命令: ```shell source install/setup.bash ``` # 运行cpp编写的可执行程序 ```shell ros2 run cpp_pubsub publisher ``` # 运行python编写的可执行程序 ```shell ros2 run py_pubsub talker ``` # 调试配置文件和方法 在工作空间目录下创建`.vscode`文件夹,然后创建以下三个文件用于配置工作区和调试的启动文件: ## c_cpp_properties.json 该文件主要用于描述c和cpp文件的相关配置项,比如使用的编译器,头文件搜索路径等。其中的配置不会影响到整个工作目录的正常使用,仅仅服务于编码时候的智能提示: ```json { "configurations": [ { "name": "ROS", "includePath": [ "${workspaceFolder}/**", "/opt/ros/humble/include/**" ], "defines": [], "compilerPath": "/usr/bin/g++", "cStandard": "gnu17", "cppStandard": "c++17", "intelliSenseMode": "linux-gcc-x64", "configurationProvider": "ms-vscode.cmake-tools" } ], "version": 4 } ``` ## tasks.json 该文件用于定义运行的自动化脚本任务,比如执行编译命令等。这里使用以下配置项定义一个用于编译所有功能包的任务: ```json { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "colcon build", "command": "colcon build --symlink-install --cmake-args -DCMAKE_BUILD_TYPE=Debug", "args": [], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher":"$msCompile" } ] } ``` ## launch.json 该文件用于启动一项调试任务,每项任务可以在侧边栏的调试选项卡中自由选择,这里配置了两项调试任务,一个是具有复用性的Python程序调试项,另一个是C++可执行程序的调试项,其中`preLaunchTask`选项可以在启动调试前自动调试`tasks.json`中定义的编译指令进行编译: ```json { "configurations": [ { "name": "Python: ${fileBasenameNoExtension}", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal", "justMyCode": true, "preLaunchTask": "colcon build" }, { "name": "调试ros节点-publisher(cpp)", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/build/cpp_pubsub/publisher", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "/usr/bin/gdb", "preLaunchTask": "colcon build" } ] } ``` ## 启动调试的方法 Python文件打开需要调试的源文件,然后调试中选择label为`Python: ${fileBasenameNoExtension}`的调试选项,打断点后启动对当前Python文件的调试;cpp可执行程序,在源文件中打断点然后启动对应文件的调试选项`调试ros节点-publisher(cpp)`,可以创建多个cpp的调试配置项以调试多个可执行程序。