From 26c558abdcbe9a5f27d8bee33fb03cc89e83a774 Mon Sep 17 00:00:00 2001 From: Kartik Agrawal Date: Sun, 4 May 2025 12:41:48 -0400 Subject: [PATCH 01/11] Create Configure_vscode_for_ros2.md Guidelines to configure vscode for ros2 functionalities and debugging --- .../Configure_vscode_for_ros2.md | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 wiki/common-platforms/Configure_vscode_for_ros2.md diff --git a/wiki/common-platforms/Configure_vscode_for_ros2.md b/wiki/common-platforms/Configure_vscode_for_ros2.md new file mode 100644 index 00000000..41bc280f --- /dev/null +++ b/wiki/common-platforms/Configure_vscode_for_ros2.md @@ -0,0 +1,106 @@ +--- +# Jekyll 'Front Matter' goes here. Most are set by default, and should NOT be +# overwritten except in special circumstances. +# You should set the date the article was last updated like this: +date: 2020-05-11 # YYYY-MM-DD +# This will be displayed at the bottom of the article +# You should set the article's title: +title: Title goes here +# The 'title' is automatically displayed at the top of the page +# and used in other parts of the site. +--- +This template acts as a tutorial on writing articles for the Robotics Knowledgebase. In it we will cover article structure, basic syntax, and other useful hints. Every tutorial and article should start with a proper introduction. + +This goes above the first subheading. The first 100 words are used as an excerpt on the Wiki's Index. No images, HTML, or special formating should be used in this section as it won't be displayed properly. + +If you're writing a tutorial, use this section to specify what the reader will be able to accomplish and the tools you will be using. If you're writing an article, this section should be used to encapsulate the topic covered. Use Wikipedia for inspiration on how to write a proper introduction to a topic. + +In both cases, tell them what you're going to say, use the sections below to say it, then summarize at the end (with suggestions for further study). + +## First subheading +Use this section to cover important terms and information useful to completing the tutorial or understanding the topic addressed. Don't be afraid to include to other wiki entries that would be useful for what you intend to cover. Notice that there are two \#'s used for subheadings; that's the minimum. Each additional sublevel will have an added \#. It's strongly recommended that you create and work from an outline. + +This section covers the basic syntax and some rules of thumb for writing. + +### Basic syntax +A line in between create a separate paragraph. *This is italicized.* **This is bold.** Here is [a link](/). If you want to display the URL, you can do it like this . + +> This is a note. Use it to reinforce important points, especially potential show stoppers for your readers. It is also appropriate to use for long quotes from other texts. + + +#### Bullet points and numbered lists +Here are some hints on writing (in no particular order): +- Focus on application knowledge. + - Write tutorials to achieve a specific outcome. + - Relay theory in an intuitive way (especially if you initially struggled). + - It is likely that others are confused in the same way you were. They will benefit from your perspective. + - You do not need to be an expert to produce useful content. + - Document procedures as you learn them. You or others may refine them later. +- Use a professional tone. + - Be non-partisan. + - Characterize technology and practices in a way that assists the reader to make intelligent decisions. + - When in doubt, use the SVOR (Strengths, Vulnerabilities, Opportunities, and Risks) framework. + - Personal opinions have no place in the Wiki. Do not use "I." Only use "we" when referring to the contributors and editors of the Robotics Knowledgebase. You may "you" when giving instructions in tutorials. +- Use American English (for now). + - We made add support for other languages in the future. +- The Robotics Knowledgebase is still evolving. We are using Jekyll and GitHub Pages in and a novel way and are always looking for contributors' input. + +Entries in the Wiki should follow this format: +1. Excerpt introducing the entry's contents. + - Be sure to specify if it is a tutorial or an article. + - Remember that the first 100 words get used else where. A well written excerpt ensures that your entry gets read. +2. The content of your entry. +3. Summary. +4. See Also Links (relevant articles in the Wiki). +5. Further Reading (relevant articles on other sites). +6. References. + +#### Code snippets +There's also a lot of support for displaying code. You can do it inline like `this`. You should also use the inline code syntax for `filenames` and `ROS_node_names`. + +Larger chunks of code should use this format: +``` +def recover_msg(msg): + + // Good coders comment their code for others. + + pw = ProtocolWrapper() + + // Explanation. + + if rec_crc != calc_crc: + return None +``` +This would be a good spot further explain you code snippet. Break it down for the user so they understand what is going on. + +#### LaTex Math Support +Here is an example MathJax inline rendering $ \phi(x\|y) $ (note the additional escape for using \|), and here is a block rendering: +$$ \frac{1}{n^{2}} $$ + +#### Images and Video +Images and embedded video are supported. + +![Put a relevant caption here](assets/images/Hk47portrait-298x300.jpg) + +{% include video id="8P9geWwi9e0" provider="youtube" %} + +{% include video id="148982525" provider="vimeo" %} + +The video id can be found at the end of the URL. In this case, the URLs were +`https://www.youtube.com/watch?v=8P9geWwi9e0` +& `https://vimeo.com/148982525`. + +## Summary +Use this space to reinforce key points and to suggest next steps for your readers. + +## See Also: +- Links to relevant material within the Robotics Knowledgebase go here. + +## Further Reading +- Links to articles of interest outside the Wiki (that are not references) go here. + +## References +- Links to References go here. +- References should be in alphabetical order. +- References should follow IEEE format. +- If you are referencing experimental results, include it in your published report and link to it here. From 40e97dcdc84434c26c3485008bece10c993a8df6 Mon Sep 17 00:00:00 2001 From: Kartik Agrawal Date: Sun, 4 May 2025 12:42:25 -0400 Subject: [PATCH 02/11] Rename Configure_vscode_for_ros2.md to configure_vscode_for_ros2.md --- ...{Configure_vscode_for_ros2.md => configure_vscode_for_ros2.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename wiki/common-platforms/{Configure_vscode_for_ros2.md => configure_vscode_for_ros2.md} (100%) diff --git a/wiki/common-platforms/Configure_vscode_for_ros2.md b/wiki/common-platforms/configure_vscode_for_ros2.md similarity index 100% rename from wiki/common-platforms/Configure_vscode_for_ros2.md rename to wiki/common-platforms/configure_vscode_for_ros2.md From aa0face3d9a446a94ffb28679fb969c82bb6512d Mon Sep 17 00:00:00 2001 From: Kartik Agrawal Date: Sun, 4 May 2025 12:56:27 -0400 Subject: [PATCH 03/11] Update configure_vscode_for_ros2.md --- .../configure_vscode_for_ros2.md | 175 ++++++++++-------- 1 file changed, 98 insertions(+), 77 deletions(-) diff --git a/wiki/common-platforms/configure_vscode_for_ros2.md b/wiki/common-platforms/configure_vscode_for_ros2.md index 41bc280f..ddec9b8f 100644 --- a/wiki/common-platforms/configure_vscode_for_ros2.md +++ b/wiki/common-platforms/configure_vscode_for_ros2.md @@ -1,106 +1,127 @@ +# Debugging and Compiling ROS 2 Packages in Visual Studio Code + +This tutorial explains how to set up **Visual Studio Code** for building and debugging ROS 2 packages effectively. + --- -# Jekyll 'Front Matter' goes here. Most are set by default, and should NOT be -# overwritten except in special circumstances. -# You should set the date the article was last updated like this: -date: 2020-05-11 # YYYY-MM-DD -# This will be displayed at the bottom of the article -# You should set the article's title: -title: Title goes here -# The 'title' is automatically displayed at the top of the page -# and used in other parts of the site. ---- -This template acts as a tutorial on writing articles for the Robotics Knowledgebase. In it we will cover article structure, basic syntax, and other useful hints. Every tutorial and article should start with a proper introduction. -This goes above the first subheading. The first 100 words are used as an excerpt on the Wiki's Index. No images, HTML, or special formating should be used in this section as it won't be displayed properly. +## 🧱 1. Preparing for Debugging + +Imagine this scenario: You comment out a line that creates a publisher in your ROS 2 C++ node. The code compiles fine, but running the node gives a **segmentation fault**, with no helpful error message. + +To debug such issues, we'll configure **GDB debugging** inside VS Code. -If you're writing a tutorial, use this section to specify what the reader will be able to accomplish and the tools you will be using. If you're writing an article, this section should be used to encapsulate the topic covered. Use Wikipedia for inspiration on how to write a proper introduction to a topic. +--- -In both cases, tell them what you're going to say, use the sections below to say it, then summarize at the end (with suggestions for further study). +## 🧰 2. Install GDB Server (If Needed) -## First subheading -Use this section to cover important terms and information useful to completing the tutorial or understanding the topic addressed. Don't be afraid to include to other wiki entries that would be useful for what you intend to cover. Notice that there are two \#'s used for subheadings; that's the minimum. Each additional sublevel will have an added \#. It's strongly recommended that you create and work from an outline. +Ensure `gdbserver` is installed: -This section covers the basic syntax and some rules of thumb for writing. +```bash +sudo apt update +sudo apt install gdbserver +``` -### Basic syntax -A line in between create a separate paragraph. *This is italicized.* **This is bold.** Here is [a link](/). If you want to display the URL, you can do it like this . +--- -> This is a note. Use it to reinforce important points, especially potential show stoppers for your readers. It is also appropriate to use for long quotes from other texts. +## ⚙️ 3. Running a Node with GDB Server +Use this command to run a ROS 2 node with GDB server: -#### Bullet points and numbered lists -Here are some hints on writing (in no particular order): -- Focus on application knowledge. - - Write tutorials to achieve a specific outcome. - - Relay theory in an intuitive way (especially if you initially struggled). - - It is likely that others are confused in the same way you were. They will benefit from your perspective. - - You do not need to be an expert to produce useful content. - - Document procedures as you learn them. You or others may refine them later. -- Use a professional tone. - - Be non-partisan. - - Characterize technology and practices in a way that assists the reader to make intelligent decisions. - - When in doubt, use the SVOR (Strengths, Vulnerabilities, Opportunities, and Risks) framework. - - Personal opinions have no place in the Wiki. Do not use "I." Only use "we" when referring to the contributors and editors of the Robotics Knowledgebase. You may "you" when giving instructions in tutorials. -- Use American English (for now). - - We made add support for other languages in the future. -- The Robotics Knowledgebase is still evolving. We are using Jekyll and GitHub Pages in and a novel way and are always looking for contributors' input. +```bash +ros2 run --prefix 'gdbserver localhost:3000' +``` -Entries in the Wiki should follow this format: -1. Excerpt introducing the entry's contents. - - Be sure to specify if it is a tutorial or an article. - - Remember that the first 100 words get used else where. A well written excerpt ensures that your entry gets read. -2. The content of your entry. -3. Summary. -4. See Also Links (relevant articles in the Wiki). -5. Further Reading (relevant articles on other sites). -6. References. +Replace `` and `` with your specific values. -#### Code snippets -There's also a lot of support for displaying code. You can do it inline like `this`. You should also use the inline code syntax for `filenames` and `ROS_node_names`. +--- -Larger chunks of code should use this format: +## 🐞 4. Configure the Debugger in VS Code + +Create or edit the file `.vscode/launch.json`: + +```json +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C++ Debugger", + "request": "launch", + "type": "cppdbg", + "miDebuggerServerAddress": "localhost:3000", + "cwd": "/", + "program": "/home/$USER/Workspaces/ros2_cpp_ws/install/udemy_ros2_pkg/lib/udemy_ros2_pkg/service_client" + } + ] +} ``` -def recover_msg(msg): - // Good coders comment their code for others. +> 📝 Replace the `program` path with your actual executable path inside `install/`. - pw = ProtocolWrapper() +Now press `F5` to launch the debugger and catch segmentation faults right where they happen. - // Explanation. +--- + +## 🔄 5. Use `--symlink-install` for Faster Builds + +By default, `colcon build` copies files from `build/` to `install/`. You can make builds faster by creating symbolic links: - if rec_crc != calc_crc: - return None +```bash +colcon build --symlink-install ``` -This would be a good spot further explain you code snippet. Break it down for the user so they understand what is going on. -#### LaTex Math Support -Here is an example MathJax inline rendering $ \phi(x\|y) $ (note the additional escape for using \|), and here is a block rendering: -$$ \frac{1}{n^{2}} $$ +To switch to this method, first clean your workspace: -#### Images and Video -Images and embedded video are supported. +```bash +rm -rf build/ install/ log/ +colcon build --symlink-install +``` -![Put a relevant caption here](assets/images/Hk47portrait-298x300.jpg) +This is especially helpful when you're making small changes to files like `package.xml`, launch files, or interface definitions. -{% include video id="8P9geWwi9e0" provider="youtube" %} +--- -{% include video id="148982525" provider="vimeo" %} +## 🔧 6. Automate Build and Debug Tasks with `tasks.json` + +Add this to `.vscode/tasks.json`: + +```json +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "type": "shell", + "command": "source /opt/ros/humble/setup.bash && colcon build --symlink-install" + }, + { + "label": "debug", + "type": "shell", + "command": "echo -e '\n\nRun the node using the following prefix: \n ros2 run --prefix 'gdbserver localhost:3000' \n\nAnd modify the executable path in .vscode/launch.json file \n' && source /opt/ros/humble/setup.bash && colcon build --symlink-install --cmake-args -DCMAKE_BUILD_TYPE=RelWithDebInfo" + }, + { + "label": "test", + "type": "shell", + "command": "colcon test && colcon test-result" + } + ] +} +``` + +Then in VS Code: +- Press `Ctrl+Shift+B` to build. +- Open the command palette (F1) → "Run Task" → choose `debug` or `test`. +- Use the printed instructions to launch your node under GDB. -The video id can be found at the end of the URL. In this case, the URLs were -`https://www.youtube.com/watch?v=8P9geWwi9e0` -& `https://vimeo.com/148982525`. +--- -## Summary -Use this space to reinforce key points and to suggest next steps for your readers. +## 📌 Final Tips -## See Also: -- Links to relevant material within the Robotics Knowledgebase go here. +- Open your ROS workspace at the root level (`~/ros2_ws`) in VS Code. +- The ROS VS Code extension adds tools like: + - Sourced terminals + - Node runners + - ROS topic graph visualizers -## Further Reading -- Links to articles of interest outside the Wiki (that are not references) go here. +--- -## References -- Links to References go here. -- References should be in alphabetical order. -- References should follow IEEE format. -- If you are referencing experimental results, include it in your published report and link to it here. +Happy debugging! 🛠️🐢 From 7d7ec3654a57dbb97416e969a6816ee7e68b7490 Mon Sep 17 00:00:00 2001 From: Kartik Agrawal Date: Sun, 4 May 2025 13:01:11 -0400 Subject: [PATCH 04/11] Update configure_vscode_for_ros2.md --- .../configure_vscode_for_ros2.md | 204 +++++++++++------- 1 file changed, 127 insertions(+), 77 deletions(-) diff --git a/wiki/common-platforms/configure_vscode_for_ros2.md b/wiki/common-platforms/configure_vscode_for_ros2.md index ddec9b8f..d44976eb 100644 --- a/wiki/common-platforms/configure_vscode_for_ros2.md +++ b/wiki/common-platforms/configure_vscode_for_ros2.md @@ -1,127 +1,177 @@ -# Debugging and Compiling ROS 2 Packages in Visual Studio Code +# Debugging and Building ROS 2 Packages in Visual Studio Code -This tutorial explains how to set up **Visual Studio Code** for building and debugging ROS 2 packages effectively. +This tutorial covers how to configure Visual Studio Code (VS Code) for building and debugging ROS 2 packages using C++. It assumes that you already have both ROS 2 (e.g., Humble) and VS Code installed and set up properly. --- -## 🧱 1. Preparing for Debugging +## 1. Background: Why Debugging Matters -Imagine this scenario: You comment out a line that creates a publisher in your ROS 2 C++ node. The code compiles fine, but running the node gives a **segmentation fault**, with no helpful error message. +Sometimes your C++ ROS 2 code compiles successfully, but fails during runtime—perhaps with a segmentation fault and little information to guide you. To address such issues effectively, it's essential to: -To debug such issues, we'll configure **GDB debugging** inside VS Code. +* Enable debug symbol generation during compilation. +* Use a debugger like GDB. +* Connect VS Code to GDB for a visual debugging experience. --- -## 🧰 2. Install GDB Server (If Needed) - -Ensure `gdbserver` is installed: - -```bash -sudo apt update -sudo apt install gdbserver +## 2. Example ROS 2 Publisher Code + +We'll use a basic publisher node in C++ as our example. + +```cpp +#include "rclcpp/rclcpp.hpp" +#include "std_msgs/msg/string.hpp" + +class MinimalPublisher : public rclcpp::Node { +public: + MinimalPublisher() : Node("minimal_publisher") { + publisher_ = this->create_publisher("topic", 10); + timer_ = this->create_wall_timer( + std::chrono::milliseconds(500), + [this]() { + auto message = std_msgs::msg::String(); + message.data = "Hello, world!"; + publisher_->publish(message); + } + ); + } + +private: + rclcpp::Publisher::SharedPtr publisher_; + rclcpp::TimerBase::SharedPtr timer_; +}; + +int main(int argc, char *argv[]) { + rclcpp::init(argc, argv); + rclcpp::spin(std::make_shared()); + rclcpp::shutdown(); + return 0; +} ``` --- -## ⚙️ 3. Running a Node with GDB Server +## 3. Compiling with Debug Symbols -Use this command to run a ROS 2 node with GDB server: +To generate debugging symbols, compile your workspace using the following command: ```bash -ros2 run --prefix 'gdbserver localhost:3000' +colcon build --symlink-install --cmake-args -DCMAKE_BUILD_TYPE=RelWithDebInfo ``` -Replace `` and `` with your specific values. +You can automate this with VS Code using the `tasks.json` setup below. --- -## 🐞 4. Configure the Debugger in VS Code +## 4. Setting Up `tasks.json` -Create or edit the file `.vscode/launch.json`: +Create a file named `.vscode/tasks.json` in the root of your workspace: ```json { - "version": "0.2.0", - "configurations": [ - { - "name": "C++ Debugger", - "request": "launch", - "type": "cppdbg", - "miDebuggerServerAddress": "localhost:3000", - "cwd": "/", - "program": "/home/$USER/Workspaces/ros2_cpp_ws/install/udemy_ros2_pkg/lib/udemy_ros2_pkg/service_client" - } - ] + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "type": "shell", + "command": "source /opt/ros/humble/setup.bash && colcon build --symlink-install" + }, + { + "label": "debug", + "type": "shell", + "command": "echo -e '\n\nRun the node using the following prefix: \n ros2 run --prefix 'gdbserver localhost:3000' \n\nAnd modify the executable path in .vscode/launch.json file \n' && source /opt/ros/humble/setup.bash && colcon build --symlink-install --cmake-args -DCMAKE_BUILD_TYPE=RelWithDebInfo" + }, + { + "label": "test", + "type": "shell", + "command": "colcon test && colcon test-result" + } + ] } ``` -> 📝 Replace the `program` path with your actual executable path inside `install/`. +--- + +## 5. Setting Up `launch.json` -Now press `F5` to launch the debugger and catch segmentation faults right where they happen. +To attach VS Code's debugger to a ROS 2 node, configure `.vscode/launch.json` like this: + +```json +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C++ Debugger", + "request": "launch", + "type": "cppdbg", + "miDebuggerServerAddress": "localhost:3000", + "cwd": "/", + "program": "/home/$USER/Workspaces/ros2_cpp_ws/install/udemy_ros2_pkg/lib/udemy_ros2_pkg/service_client" + } + ] +} +``` + +Replace the `program` path with the absolute path to your ROS 2 node executable. --- -## 🔄 5. Use `--symlink-install` for Faster Builds +## 6. Running the Debugger -By default, `colcon build` copies files from `build/` to `install/`. You can make builds faster by creating symbolic links: +### Step-by-step: -```bash -colcon build --symlink-install -``` +1. Build your workspace using: -To switch to this method, first clean your workspace: + ```bash + colcon build --symlink-install --cmake-args -DCMAKE_BUILD_TYPE=RelWithDebInfo + ``` +2. Run your node under GDB server: -```bash -rm -rf build/ install/ log/ -colcon build --symlink-install -``` + ```bash + ros2 run --prefix 'gdbserver localhost:3000' + ``` +3. In VS Code, press `F5` or go to `Run > Start Debugging`. +4. VS Code will connect to the GDB server and break at the error location if there is a crash. + +--- + +## 7. Using the VS Code ROS Extension + +VS Code’s ROS extension adds convenience: -This is especially helpful when you're making small changes to files like `package.xml`, launch files, or interface definitions. +* `ROS: Show Status` – View running nodes and topics. +* `ROS: Create Terminal` – Opens a sourced terminal. +* `ROS: Run ROS Command` – Run nodes from a graphical menu. + +Access these via the command palette (`F1`) and typing `ROS`. --- -## 🔧 6. Automate Build and Debug Tasks with `tasks.json` +## 8. Tips for Debugging -Add this to `.vscode/tasks.json`: +* Use the left panel during debugging to inspect variables and the call stack. +* Use `readlink` to confirm symlink targets (for `--symlink-install` builds): -```json -{ - "version": "2.0.0", - "tasks": [ - { - "label": "build", - "type": "shell", - "command": "source /opt/ros/humble/setup.bash && colcon build --symlink-install" - }, - { - "label": "debug", - "type": "shell", - "command": "echo -e '\n\nRun the node using the following prefix: \n ros2 run --prefix 'gdbserver localhost:3000' \n\nAnd modify the executable path in .vscode/launch.json file \n' && source /opt/ros/humble/setup.bash && colcon build --symlink-install --cmake-args -DCMAKE_BUILD_TYPE=RelWithDebInfo" - }, - { - "label": "test", - "type": "shell", - "command": "colcon test && colcon test-result" - } - ] -} -``` + ```bash + readlink install/your_pkg/lib/your_pkg/your_executable + ``` +* To fix symbol issues, clean your build: -Then in VS Code: -- Press `Ctrl+Shift+B` to build. -- Open the command palette (F1) → "Run Task" → choose `debug` or `test`. -- Use the printed instructions to launch your node under GDB. + ```bash + rm -rf build/ install/ log/ + ``` --- -## 📌 Final Tips +## 9. Summary + +With the above setup: -- Open your ROS workspace at the root level (`~/ros2_ws`) in VS Code. -- The ROS VS Code extension adds tools like: - - Sourced terminals - - Node runners - - ROS topic graph visualizers +* You can compile and debug C++ ROS 2 nodes directly inside VS Code. +* The `tasks.json` makes builds reproducible. +* The `launch.json` connects VS Code's debugger to your running ROS node. +* You can track variables, memory, and exceptions visually during runtime. --- -Happy debugging! 🛠️🐢 +Now you're ready to debug efficiently in ROS 2 using Visual Studio Code! From 7f7e99b816447299a00470ac4f202ac5e79c867c Mon Sep 17 00:00:00 2001 From: Kartik Agrawal Date: Sun, 4 May 2025 13:14:25 -0400 Subject: [PATCH 05/11] Update configure_vscode_for_ros2.md --- .../configure_vscode_for_ros2.md | 286 ++++++++++-------- 1 file changed, 157 insertions(+), 129 deletions(-) diff --git a/wiki/common-platforms/configure_vscode_for_ros2.md b/wiki/common-platforms/configure_vscode_for_ros2.md index d44976eb..14a61e14 100644 --- a/wiki/common-platforms/configure_vscode_for_ros2.md +++ b/wiki/common-platforms/configure_vscode_for_ros2.md @@ -1,177 +1,205 @@ -# Debugging and Building ROS 2 Packages in Visual Studio Code +@@ -1,106 +1,127 @@ +# Debugging and Compiling ROS 2 Packages in Visual Studio Code -This tutorial covers how to configure Visual Studio Code (VS Code) for building and debugging ROS 2 packages using C++. It assumes that you already have both ROS 2 (e.g., Humble) and VS Code installed and set up properly. +This tutorial explains how to set up **Visual Studio Code** for building and debugging ROS 2 packages effectively. --- +# Jekyll 'Front Matter' goes here. Most are set by default, and should NOT be +# overwritten except in special circumstances. +# You should set the date the article was last updated like this: +date: 2020-05-11 # YYYY-MM-DD +# This will be displayed at the bottom of the article +# You should set the article's title: +title: Title goes here +# The 'title' is automatically displayed at the top of the page +# and used in other parts of the site. +--- +This template acts as a tutorial on writing articles for the Robotics Knowledgebase. In it we will cover article structure, basic syntax, and other useful hints. Every tutorial and article should start with a proper introduction. -## 1. Background: Why Debugging Matters +This goes above the first subheading. The first 100 words are used as an excerpt on the Wiki's Index. No images, HTML, or special formating should be used in this section as it won't be displayed properly. +## 🧱 1. Preparing for Debugging -Sometimes your C++ ROS 2 code compiles successfully, but fails during runtime—perhaps with a segmentation fault and little information to guide you. To address such issues effectively, it's essential to: +Imagine this scenario: You comment out a line that creates a publisher in your ROS 2 C++ node. The code compiles fine, but running the node gives a **segmentation fault**, with no helpful error message. -* Enable debug symbol generation during compilation. -* Use a debugger like GDB. -* Connect VS Code to GDB for a visual debugging experience. +To debug such issues, we'll configure **GDB debugging** inside VS Code. +If you're writing a tutorial, use this section to specify what the reader will be able to accomplish and the tools you will be using. If you're writing an article, this section should be used to encapsulate the topic covered. Use Wikipedia for inspiration on how to write a proper introduction to a topic. --- -## 2. Example ROS 2 Publisher Code - -We'll use a basic publisher node in C++ as our example. - -```cpp -#include "rclcpp/rclcpp.hpp" -#include "std_msgs/msg/string.hpp" - -class MinimalPublisher : public rclcpp::Node { -public: - MinimalPublisher() : Node("minimal_publisher") { - publisher_ = this->create_publisher("topic", 10); - timer_ = this->create_wall_timer( - std::chrono::milliseconds(500), - [this]() { - auto message = std_msgs::msg::String(); - message.data = "Hello, world!"; - publisher_->publish(message); - } - ); - } - -private: - rclcpp::Publisher::SharedPtr publisher_; - rclcpp::TimerBase::SharedPtr timer_; -}; - -int main(int argc, char *argv[]) { - rclcpp::init(argc, argv); - rclcpp::spin(std::make_shared()); - rclcpp::shutdown(); - return 0; -} -``` +In both cases, tell them what you're going to say, use the sections below to say it, then summarize at the end (with suggestions for further study). +## 🧰 2. Install GDB Server (If Needed) ---- - -## 3. Compiling with Debug Symbols - -To generate debugging symbols, compile your workspace using the following command: +## First subheading +Use this section to cover important terms and information useful to completing the tutorial or understanding the topic addressed. Don't be afraid to include to other wiki entries that would be useful for what you intend to cover. Notice that there are two \#'s used for subheadings; that's the minimum. Each additional sublevel will have an added \#. It's strongly recommended that you create and work from an outline. +Ensure `gdbserver` is installed: +This section covers the basic syntax and some rules of thumb for writing. ```bash -colcon build --symlink-install --cmake-args -DCMAKE_BUILD_TYPE=RelWithDebInfo +sudo apt update +sudo apt install gdbserver ``` -You can automate this with VS Code using the `tasks.json` setup below. - +### Basic syntax +A line in between create a separate paragraph. *This is italicized.* **This is bold.** Here is [a link](/). If you want to display the URL, you can do it like this . --- -## 4. Setting Up `tasks.json` - -Create a file named `.vscode/tasks.json` in the root of your workspace: - -```json -{ - "version": "2.0.0", - "tasks": [ - { - "label": "build", - "type": "shell", - "command": "source /opt/ros/humble/setup.bash && colcon build --symlink-install" - }, - { - "label": "debug", - "type": "shell", - "command": "echo -e '\n\nRun the node using the following prefix: \n ros2 run --prefix 'gdbserver localhost:3000' \n\nAnd modify the executable path in .vscode/launch.json file \n' && source /opt/ros/humble/setup.bash && colcon build --symlink-install --cmake-args -DCMAKE_BUILD_TYPE=RelWithDebInfo" - }, - { - "label": "test", - "type": "shell", - "command": "colcon test && colcon test-result" - } - ] -} +> This is a note. Use it to reinforce important points, especially potential show stoppers for your readers. It is also appropriate to use for long quotes from other texts. +## ⚙️ 3. Running a Node with GDB Server + +Use this command to run a ROS 2 node with GDB server: + +#### Bullet points and numbered lists +Here are some hints on writing (in no particular order): +- Focus on application knowledge. + - Write tutorials to achieve a specific outcome. + - Relay theory in an intuitive way (especially if you initially struggled). + - It is likely that others are confused in the same way you were. They will benefit from your perspective. + - You do not need to be an expert to produce useful content. + - Document procedures as you learn them. You or others may refine them later. +- Use a professional tone. + - Be non-partisan. + - Characterize technology and practices in a way that assists the reader to make intelligent decisions. + - When in doubt, use the SVOR (Strengths, Vulnerabilities, Opportunities, and Risks) framework. + - Personal opinions have no place in the Wiki. Do not use "I." Only use "we" when referring to the contributors and editors of the Robotics Knowledgebase. You may "you" when giving instructions in tutorials. +- Use American English (for now). + - We made add support for other languages in the future. +- The Robotics Knowledgebase is still evolving. We are using Jekyll and GitHub Pages in and a novel way and are always looking for contributors' input. +```bash +ros2 run --prefix 'gdbserver localhost:3000' ``` +Entries in the Wiki should follow this format: +1. Excerpt introducing the entry's contents. + - Be sure to specify if it is a tutorial or an article. + - Remember that the first 100 words get used else where. A well written excerpt ensures that your entry gets read. +2. The content of your entry. +3. Summary. +4. See Also Links (relevant articles in the Wiki). +5. Further Reading (relevant articles on other sites). +6. References. +Replace `` and `` with your specific values. + +#### Code snippets +There's also a lot of support for displaying code. You can do it inline like `this`. You should also use the inline code syntax for `filenames` and `ROS_node_names`. --- -## 5. Setting Up `launch.json` +Larger chunks of code should use this format: +## 🐞 4. Configure the Debugger in VS Code -To attach VS Code's debugger to a ROS 2 node, configure `.vscode/launch.json` like this: +Create or edit the file `.vscode/launch.json`: ```json { - "version": "0.2.0", - "configurations": [ - { - "name": "C++ Debugger", - "request": "launch", - "type": "cppdbg", - "miDebuggerServerAddress": "localhost:3000", - "cwd": "/", - "program": "/home/$USER/Workspaces/ros2_cpp_ws/install/udemy_ros2_pkg/lib/udemy_ros2_pkg/service_client" - } - ] + "version": "0.2.0", + "configurations": [ + { + "name": "C++ Debugger", + "request": "launch", + "type": "cppdbg", + "miDebuggerServerAddress": "localhost:3000", + "cwd": "/", + "program": "/home/$USER/Workspaces/ros2_cpp_ws/install/udemy_ros2_pkg/lib/udemy_ros2_pkg/service_client" + } + ] } ``` +def recover_msg(msg): -Replace the `program` path with the absolute path to your ROS 2 node executable. - ---- - -## 6. Running the Debugger + // Good coders comment their code for others. +> 📝 Replace the `program` path with your actual executable path inside `install/`. -### Step-by-step: + pw = ProtocolWrapper() +Now press `F5` to launch the debugger and catch segmentation faults right where they happen. -1. Build your workspace using: + // Explanation. +--- - ```bash - colcon build --symlink-install --cmake-args -DCMAKE_BUILD_TYPE=RelWithDebInfo - ``` -2. Run your node under GDB server: +## 🔄 5. Use `--symlink-install` for Faster Builds - ```bash - ros2 run --prefix 'gdbserver localhost:3000' - ``` -3. In VS Code, press `F5` or go to `Run > Start Debugging`. -4. VS Code will connect to the GDB server and break at the error location if there is a crash. +By default, `colcon build` copies files from `build/` to `install/`. You can make builds faster by creating symbolic links: ---- - -## 7. Using the VS Code ROS Extension + if rec_crc != calc_crc: + return None +```bash +colcon build --symlink-install +``` +This would be a good spot further explain you code snippet. Break it down for the user so they understand what is going on. -VS Code’s ROS extension adds convenience: +#### LaTex Math Support +Here is an example MathJax inline rendering $ \phi(x\|y) $ (note the additional escape for using \|), and here is a block rendering: +$$ \frac{1}{n^{2}} $$ +To switch to this method, first clean your workspace: -* `ROS: Show Status` – View running nodes and topics. -* `ROS: Create Terminal` – Opens a sourced terminal. -* `ROS: Run ROS Command` – Run nodes from a graphical menu. +#### Images and Video +Images and embedded video are supported. +```bash +rm -rf build/ install/ log/ +colcon build --symlink-install +``` -Access these via the command palette (`F1`) and typing `ROS`. +![Put a relevant caption here](assets/images/Hk47portrait-298x300.jpg) +This is especially helpful when you're making small changes to files like `package.xml`, launch files, or interface definitions. +{% include video id="8P9geWwi9e0" provider="youtube" %} --- -## 8. Tips for Debugging +{% include video id="148982525" provider="vimeo" %} +## 🔧 6. Automate Build and Debug Tasks with `tasks.json` -* Use the left panel during debugging to inspect variables and the call stack. -* Use `readlink` to confirm symlink targets (for `--symlink-install` builds): +Add this to `.vscode/tasks.json`: - ```bash - readlink install/your_pkg/lib/your_pkg/your_executable - ``` -* To fix symbol issues, clean your build: +```json +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "type": "shell", + "command": "source /opt/ros/humble/setup.bash && colcon build --symlink-install" + }, + { + "label": "debug", + "type": "shell", + "command": "echo -e '\n\nRun the node using the following prefix: \n ros2 run --prefix 'gdbserver localhost:3000' \n\nAnd modify the executable path in .vscode/launch.json file \n' && source /opt/ros/humble/setup.bash && colcon build --symlink-install --cmake-args -DCMAKE_BUILD_TYPE=RelWithDebInfo" + }, + { + "label": "test", + "type": "shell", + "command": "colcon test && colcon test-result" + } + ] +} +``` - ```bash - rm -rf build/ install/ log/ - ``` +Then in VS Code: +- Press `Ctrl+Shift+B` to build. +- Open the command palette (F1) → "Run Task" → choose `debug` or `test`. +- Use the printed instructions to launch your node under GDB. +The video id can be found at the end of the URL. In this case, the URLs were +`https://www.youtube.com/watch?v=8P9geWwi9e0` +& `https://vimeo.com/148982525`. --- -## 9. Summary - -With the above setup: +## Summary +Use this space to reinforce key points and to suggest next steps for your readers. +## 📌 Final Tips -* You can compile and debug C++ ROS 2 nodes directly inside VS Code. -* The `tasks.json` makes builds reproducible. -* The `launch.json` connects VS Code's debugger to your running ROS node. -* You can track variables, memory, and exceptions visually during runtime. +## See Also: +- Links to relevant material within the Robotics Knowledgebase go here. +- Open your ROS workspace at the root level (`~/ros2_ws`) in VS Code. +- The ROS VS Code extension adds tools like: + - Sourced terminals + - Node runners + - ROS topic graph visualizers +## Further Reading +- Links to articles of interest outside the Wiki (that are not references) go here. --- -Now you're ready to debug efficiently in ROS 2 using Visual Studio Code! +## References +- Links to References go here. +- References should be in alphabetical order. +- References should follow IEEE format. +- If you are referencing experimental results, include it in your published report and link to it here. +Happy debugging! 🛠️🐢 From 104fe2b1009d18caade64e7576a9ca1ccfe1092f Mon Sep 17 00:00:00 2001 From: Kartik Agrawal Date: Sun, 4 May 2025 15:15:02 -0400 Subject: [PATCH 06/11] Update configure_vscode_for_ros2.md --- .../configure_vscode_for_ros2.md | 268 ++++++++---------- 1 file changed, 116 insertions(+), 152 deletions(-) diff --git a/wiki/common-platforms/configure_vscode_for_ros2.md b/wiki/common-platforms/configure_vscode_for_ros2.md index 14a61e14..7d68caa3 100644 --- a/wiki/common-platforms/configure_vscode_for_ros2.md +++ b/wiki/common-platforms/configure_vscode_for_ros2.md @@ -1,205 +1,169 @@ -@@ -1,106 +1,127 @@ -# Debugging and Compiling ROS 2 Packages in Visual Studio Code -This tutorial explains how to set up **Visual Studio Code** for building and debugging ROS 2 packages effectively. +> This tutorial assumes you already have ROS 2 and Visual Studio Code installed and properly set up on your system. ---- -# Jekyll 'Front Matter' goes here. Most are set by default, and should NOT be -# overwritten except in special circumstances. -# You should set the date the article was last updated like this: -date: 2020-05-11 # YYYY-MM-DD -# This will be displayed at the bottom of the article -# You should set the article's title: -title: Title goes here -# The 'title' is automatically displayed at the top of the page -# and used in other parts of the site. ---- -This template acts as a tutorial on writing articles for the Robotics Knowledgebase. In it we will cover article structure, basic syntax, and other useful hints. Every tutorial and article should start with a proper introduction. +In this tutorial, we'll walk through setting up a development environment in Visual Studio Code (VS Code) to debug and compile ROS 2 packages effectively. We'll start with configuring debugging tools to help identify runtime errors and then explore how to streamline your build process using VS Code's task configurations. -This goes above the first subheading. The first 100 words are used as an excerpt on the Wiki's Index. No images, HTML, or special formating should be used in this section as it won't be displayed properly. -## 🧱 1. Preparing for Debugging +## Debugging in VS Code -Imagine this scenario: You comment out a line that creates a publisher in your ROS 2 C++ node. The code compiles fine, but running the node gives a **segmentation fault**, with no helpful error message. +Let's begin with debugging. -To debug such issues, we'll configure **GDB debugging** inside VS Code. +Open VS Code and load a ROS 2 package. For this example, we'll be using a simple publisher node written in C++. We'll start by intentionally commenting out the line that creates the publisher to simulate a bug. Even though the code appears fine to C++ linting tools, compiling it with `colcon build` will complete successfully. -If you're writing a tutorial, use this section to specify what the reader will be able to accomplish and the tools you will be using. If you're writing an article, this section should be used to encapsulate the topic covered. Use Wikipedia for inspiration on how to write a proper introduction to a topic. ---- +![image](https://github.com/user-attachments/assets/6eadbdbd-1b8e-4a15-99f7-0a303aa4e57d) -In both cases, tell them what you're going to say, use the sections below to say it, then summarize at the end (with suggestions for further study). -## 🧰 2. Install GDB Server (If Needed) -## First subheading -Use this section to cover important terms and information useful to completing the tutorial or understanding the topic addressed. Don't be afraid to include to other wiki entries that would be useful for what you intend to cover. Notice that there are two \#'s used for subheadings; that's the minimum. Each additional sublevel will have an added \#. It's strongly recommended that you create and work from an outline. -Ensure `gdbserver` is installed: +However, when you try to run the node, you’ll get a segmentation fault. This error isn't informative, so we'll set up proper debugging using `gdb` and VS Code. + +![image](https://github.com/user-attachments/assets/d09892fb-50d6-4d78-b8c9-ea76a9dfe1b4) + + +### Using GDB + +First, build your ROS 2 workspace with debugging symbols: -This section covers the basic syntax and some rules of thumb for writing. ```bash -sudo apt update -sudo apt install gdbserver +colcon build --cmake-args -DCMAKE_BUILD_TYPE=Debug ``` -### Basic syntax -A line in between create a separate paragraph. *This is italicized.* **This is bold.** Here is [a link](/). If you want to display the URL, you can do it like this . ---- - -> This is a note. Use it to reinforce important points, especially potential show stoppers for your readers. It is also appropriate to use for long quotes from other texts. -## ⚙️ 3. Running a Node with GDB Server - -Use this command to run a ROS 2 node with GDB server: - -#### Bullet points and numbered lists -Here are some hints on writing (in no particular order): -- Focus on application knowledge. - - Write tutorials to achieve a specific outcome. - - Relay theory in an intuitive way (especially if you initially struggled). - - It is likely that others are confused in the same way you were. They will benefit from your perspective. - - You do not need to be an expert to produce useful content. - - Document procedures as you learn them. You or others may refine them later. -- Use a professional tone. - - Be non-partisan. - - Characterize technology and practices in a way that assists the reader to make intelligent decisions. - - When in doubt, use the SVOR (Strengths, Vulnerabilities, Opportunities, and Risks) framework. - - Personal opinions have no place in the Wiki. Do not use "I." Only use "we" when referring to the contributors and editors of the Robotics Knowledgebase. You may "you" when giving instructions in tutorials. -- Use American English (for now). - - We made add support for other languages in the future. -- The Robotics Knowledgebase is still evolving. We are using Jekyll and GitHub Pages in and a novel way and are always looking for contributors' input. +Then, run the node using `gdbserver`: + ```bash -ros2 run --prefix 'gdbserver localhost:3000' +ros2 run --prefix 'gdbserver localhost:3000' ``` -Entries in the Wiki should follow this format: -1. Excerpt introducing the entry's contents. - - Be sure to specify if it is a tutorial or an article. - - Remember that the first 100 words get used else where. A well written excerpt ensures that your entry gets read. -2. The content of your entry. -3. Summary. -4. See Also Links (relevant articles in the Wiki). -5. Further Reading (relevant articles on other sites). -6. References. -Replace `` and `` with your specific values. +If `gdbserver` is not installed, you can install it with: -#### Code snippets -There's also a lot of support for displaying code. You can do it inline like `this`. You should also use the inline code syntax for `filenames` and `ROS_node_names`. ---- +```bash +sudo apt update +sudo apt install gdbserver +``` + +So here we see processing our node listening on Port 3000, but now we need to configure our Vs code + +to communicate with the debugger. -Larger chunks of code should use this format: -## 🐞 4. Configure the Debugger in VS Code +![image](https://github.com/user-attachments/assets/f9d6c269-eda2-4839-96e0-628bd8b95b70) -Create or edit the file `.vscode/launch.json`: + +Now, configure VS Code to connect to the `gdbserver`. Create a `.vscode/launch.json` file: ```json { - "version": "0.2.0", - "configurations": [ - { - "name": "C++ Debugger", - "request": "launch", - "type": "cppdbg", - "miDebuggerServerAddress": "localhost:3000", - "cwd": "/", - "program": "/home/$USER/Workspaces/ros2_cpp_ws/install/udemy_ros2_pkg/lib/udemy_ros2_pkg/service_client" - } - ] + "version": "0.2.0", + "configurations": [ + { + "name": "Attach to gdbserver", + "type": "cppdbg", + "request": "launch", + "miDebuggerServerAddress": "localhost:3000", + "program": "/path/to/install//lib//", + "cwd": "${workspaceFolder}", + "MIMode": "gdb", + "externalConsole": false, + "stopAtEntry": false + } + ] } ``` -def recover_msg(msg): - // Good coders comment their code for others. -> 📝 Replace the `program` path with your actual executable path inside `install/`. - pw = ProtocolWrapper() -Now press `F5` to launch the debugger and catch segmentation faults right where they happen. - // Explanation. ---- +Once saved, go to the Debug tab in VS Code and click **Start Debugging**. You'll see the line where the segmentation fault occurred highlighted. The debug pane shows variables, their values, and the call stack—helping identify issues such as an uninitialized publisher. + +![image](https://github.com/user-attachments/assets/2b411708-6e57-4882-a9c7-480f60ea1c68) -## 🔄 5. Use `--symlink-install` for Faster Builds -By default, `colcon build` copies files from `build/` to `install/`. You can make builds faster by creating symbolic links: - if rec_crc != calc_crc: - return None +## Simlink Install with colcon + +To improve build efficiency, use the `--symlink-install` flag with colcon. This creates symbolic links instead of copying files into the `install/` directory: + ```bash colcon build --symlink-install ``` -This would be a good spot further explain you code snippet. Break it down for the user so they understand what is going on. -#### LaTex Math Support -Here is an example MathJax inline rendering $ \phi(x\|y) $ (note the additional escape for using \|), and here is a block rendering: -$$ \frac{1}{n^{2}} $$ -To switch to this method, first clean your workspace: +If you've already built the workspace, delete `build/`, `install/`, and `log/` directories first: -#### Images and Video -Images and embedded video are supported. ```bash rm -rf build/ install/ log/ -colcon build --symlink-install ``` -![Put a relevant caption here](assets/images/Hk47portrait-298x300.jpg) -This is especially helpful when you're making small changes to files like `package.xml`, launch files, or interface definitions. - -{% include video id="8P9geWwi9e0" provider="youtube" %} ---- +Then rerun the build command with the symlink flag. -{% include video id="148982525" provider="vimeo" %} -## 🔧 6. Automate Build and Debug Tasks with `tasks.json` +## Automating Tasks with tasks.json -Add this to `.vscode/tasks.json`: +You can simplify your build process in VS Code using `tasks.json` under `.vscode/`. Here's an example configuration: ```json { - "version": "2.0.0", - "tasks": [ - { - "label": "build", - "type": "shell", - "command": "source /opt/ros/humble/setup.bash && colcon build --symlink-install" - }, - { - "label": "debug", - "type": "shell", - "command": "echo -e '\n\nRun the node using the following prefix: \n ros2 run --prefix 'gdbserver localhost:3000' \n\nAnd modify the executable path in .vscode/launch.json file \n' && source /opt/ros/humble/setup.bash && colcon build --symlink-install --cmake-args -DCMAKE_BUILD_TYPE=RelWithDebInfo" - }, - { - "label": "test", - "type": "shell", - "command": "colcon test && colcon test-result" - } - ] + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "type": "shell", + "command": "source /opt/ros/humble/setup.bash && colcon build --symlink-install", + "group": { + "kind": "build", + "isDefault": true + }, + "problemMatcher": [] + }, + { + "label": "debug", + "type": "shell", + "command": "echo \"\nRun your node with:\nros2 run --prefix 'gdbserver localhost:3000' \n\nThen update launch.json to point to the right executable.\"", + "group": "build", + "problemMatcher": [] + } + ] } ``` -Then in VS Code: -- Press `Ctrl+Shift+B` to build. -- Open the command palette (F1) → "Run Task" → choose `debug` or `test`. -- Use the printed instructions to launch your node under GDB. +You can now trigger builds via **Terminal > Run Build Task** or `Ctrl+Shift+B`. + +## Useful VS Code ROS 2 Extension Commands + +![image](https://github.com/user-attachments/assets/a6fcc84a-beca-4650-876f-2b4d9d7f4318) + +Click on the gear icon and by default we could set the Ros2 distribution to Humble. + +So this is cool because once you do this via code will automatically know how to implement certain functionality + +based on the ROS distro you are using. + +![image](https://github.com/user-attachments/assets/54980fdd-5871-414c-b97e-4c330adb1e4b) + + + + +VS Code’s ROS 2 extension provides useful shortcuts: + +* `ROS: Show Status` – shows active nodes and topics. +* `ROS: Create ROS Terminal` – opens a terminal with the ROS environment sourced. +* `ROS: Run a ROS Executable` – select a package and executable to run. +* `ROS: Update C++ Properties` – updates `c_cpp_properties.json` for intellisense. + +Access these with `F1` → search `ROS`. + -The video id can be found at the end of the URL. In this case, the URLs were -`https://www.youtube.com/watch?v=8P9geWwi9e0` -& `https://vimeo.com/148982525`. ---- ## Summary -Use this space to reinforce key points and to suggest next steps for your readers. -## 📌 Final Tips + +In this tutorial, we: + +* Learned to build ROS 2 workspaces with debug symbols. +* Set up `gdbserver` with VS Code. +* Used symlink installs to streamline builds. +* Automated common tasks with `tasks.json`. +* Explored ROS-related commands in the VS Code extension. ## See Also: -- Links to relevant material within the Robotics Knowledgebase go here. -- Open your ROS workspace at the root level (`~/ros2_ws`) in VS Code. -- The ROS VS Code extension adds tools like: - - Sourced terminals - - Node runners - - ROS topic graph visualizers + +* [Debugging ROS 2](https://docs.ros.org/en/rolling/Tutorials/Debugging-ROS-2.html) +* [colcon documentation](https://colcon.readthedocs.io/en/released/index.html) ## Further Reading -- Links to articles of interest outside the Wiki (that are not references) go here. ---- - -## References -- Links to References go here. -- References should be in alphabetical order. -- References should follow IEEE format. -- If you are referencing experimental results, include it in your published report and link to it here. -Happy debugging! 🛠️🐢 + +* [VS Code launch.json reference](https://code.visualstudio.com/docs/editor/debugging) +* [ROS 2 Tools Extension on Marketplace](https://marketplace.visualstudio.com/items?itemName=ms-iot.vscode-ros) + From fcd47d19af04cb2313f51a0a367347fa0b9b19b8 Mon Sep 17 00:00:00 2001 From: Kartik Agrawal Date: Sun, 4 May 2025 15:53:29 -0400 Subject: [PATCH 07/11] Update configure_vscode_for_ros2.md --- wiki/common-platforms/configure_vscode_for_ros2.md | 1 + 1 file changed, 1 insertion(+) diff --git a/wiki/common-platforms/configure_vscode_for_ros2.md b/wiki/common-platforms/configure_vscode_for_ros2.md index 7d68caa3..37c0ec5d 100644 --- a/wiki/common-platforms/configure_vscode_for_ros2.md +++ b/wiki/common-platforms/configure_vscode_for_ros2.md @@ -72,6 +72,7 @@ Once saved, go to the Debug tab in VS Code and click **Start Debugging**. You'll ![image](https://github.com/user-attachments/assets/2b411708-6e57-4882-a9c7-480f60ea1c68) +![image](https://github.com/user-attachments/assets/77b4a867-58d8-4dbb-bfc1-1102aed37ec9) ## Simlink Install with colcon From f807b351628b420e475805ee9f9199e534c86713 Mon Sep 17 00:00:00 2001 From: Kartik Agrawal Date: Sun, 4 May 2025 15:55:56 -0400 Subject: [PATCH 08/11] Update configure_vscode_for_ros2.md --- wiki/common-platforms/configure_vscode_for_ros2.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wiki/common-platforms/configure_vscode_for_ros2.md b/wiki/common-platforms/configure_vscode_for_ros2.md index 37c0ec5d..88e65b43 100644 --- a/wiki/common-platforms/configure_vscode_for_ros2.md +++ b/wiki/common-platforms/configure_vscode_for_ros2.md @@ -76,7 +76,7 @@ Once saved, go to the Debug tab in VS Code and click **Start Debugging**. You'll ## Simlink Install with colcon - +By default, when you run `colcon build`, the build system **copies** the compiled binaries, libraries, and resources into the `install/` directory. While this works fine, it can be **inefficient and slow**—especially during iterative development—because every time you rebuild, files are duplicated into the install space. To improve build efficiency, use the `--symlink-install` flag with colcon. This creates symbolic links instead of copying files into the `install/` directory: ```bash From 22d0ef7f63e8c8d36e16e67452a9407d985b28f2 Mon Sep 17 00:00:00 2001 From: Kartik Agrawal Date: Sun, 4 May 2025 16:02:16 -0400 Subject: [PATCH 09/11] Update configure_vscode_for_ros2.md --- .../configure_vscode_for_ros2.md | 91 ++++++++++++++----- 1 file changed, 67 insertions(+), 24 deletions(-) diff --git a/wiki/common-platforms/configure_vscode_for_ros2.md b/wiki/common-platforms/configure_vscode_for_ros2.md index 88e65b43..9d194b27 100644 --- a/wiki/common-platforms/configure_vscode_for_ros2.md +++ b/wiki/common-platforms/configure_vscode_for_ros2.md @@ -91,9 +91,15 @@ rm -rf build/ install/ log/ Then rerun the build command with the symlink flag. -## Automating Tasks with tasks.json +## Automating Builds and Debugging with `tasks.json` -You can simplify your build process in VS Code using `tasks.json` under `.vscode/`. Here's an example configuration: +Now, it can be inconvenient to remember and re-type long commands like `colcon build --symlink-install`, especially if you often switch between workspaces. VS Code allows us to automate such tasks using a `tasks.json` file. + +### Creating a Build Task + +Open your workspace in VS Code. In the file explorer, navigate to the `.vscode/` directory. If it doesn’t exist, create it. Inside that directory, create a file named `tasks.json`. + +Here’s an example configuration: ```json { @@ -110,9 +116,16 @@ You can simplify your build process in VS Code using `tasks.json` under `.vscode "problemMatcher": [] }, { - "label": "debug", + "label": "build (debug)", + "type": "shell", + "command": "source /opt/ros/humble/setup.bash && colcon build --cmake-args -DCMAKE_BUILD_TYPE=Debug", + "group": "build", + "problemMatcher": [] + }, + { + "label": "debug-helper", "type": "shell", - "command": "echo \"\nRun your node with:\nros2 run --prefix 'gdbserver localhost:3000' \n\nThen update launch.json to point to the right executable.\"", + "command": "echo "\nRun your node with:\nros2 run --prefix 'gdbserver localhost:3000' \n\nThen update launch.json to point to the right executable."", "group": "build", "problemMatcher": [] } @@ -120,44 +133,74 @@ You can simplify your build process in VS Code using `tasks.json` under `.vscode } ``` -You can now trigger builds via **Terminal > Run Build Task** or `Ctrl+Shift+B`. +> 🔧 **Tip:** If you want to source your full `.bashrc` instead of just ROS setup, modify the `command` line: +> +> ```bash +> source ~/.bashrc && colcon build --symlink-install +> ``` -## Useful VS Code ROS 2 Extension Commands +To run these tasks: +- Go to **Terminal → Run Build Task** or press **Ctrl+Shift+B**. +- Choose `build`, `build (debug)`, or `debug-helper`. -![image](https://github.com/user-attachments/assets/a6fcc84a-beca-4650-876f-2b4d9d7f4318) +This automates builds and reminds you of necessary debug setup steps. -Click on the gear icon and by default we could set the Ros2 distribution to Humble. +> ⚠️ **Important**: Make sure VS Code is opened in your **ROS 2 workspace root**. If you're in the wrong directory, `colcon build` may fail or build the wrong packages. -So this is cool because once you do this via code will automatically know how to implement certain functionality +--- -based on the ROS distro you are using. +## Using the ROS 2 VS Code Extension -![image](https://github.com/user-attachments/assets/54980fdd-5871-414c-b97e-4c330adb1e4b) +The [ROS Tools extension](https://marketplace.visualstudio.com/items?itemName=ms-iot.vscode-ros) in VS Code offers convenient tools for interacting with ROS 2. +### Common ROS Commands in VS Code +Press **F1** and type `ROS` to view available commands: +- **ROS: Show Status** – Displays current ROS nodes, topics, and status. +- **ROS: Create ROS Terminal** – Opens a terminal with the correct ROS environment sourced. +- **ROS: Run a ROS Executable** – Lets you select a package and node to run from a dropdown. +- **ROS: Update C++ Properties** – Updates your `c_cpp_properties.json` file to enable IntelliSense in C++ ROS projects. +- **ROS: Install ROS Dependencies (rosdep)** – Installs missing dependencies in your workspace using `rosdep install`. -VS Code’s ROS 2 extension provides useful shortcuts: +> 🧠 Even if you’re using ROS 2 (e.g., Humble), some commands still reference `rosrun` from ROS 1. These labels are legacy but still work for launching executables. -* `ROS: Show Status` – shows active nodes and topics. -* `ROS: Create ROS Terminal` – opens a terminal with the ROS environment sourced. -* `ROS: Run a ROS Executable` – select a package and executable to run. -* `ROS: Update C++ Properties` – updates `c_cpp_properties.json` for intellisense. +### Running a Node via Extension -Access these with `F1` → search `ROS`. +1. Press **F1** → `ROS: Run a ROS Executable`. +2. Choose your package. +3. Select the executable (e.g., `minimal_publisher`). +4. If needed, enter arguments. +5. A terminal will open and run the node with the correct environment. +### Creating a ROS-Sourced Terminal + +Select **ROS: Create ROS Terminal** from the F1 menu. This opens a new terminal tab with ROS environment variables sourced, allowing you to run commands like: + +```bash +ros2 topic echo /your_topic +``` + +### Updating C++ IntelliSense + +If your C++ symbols or headers aren’t resolving correctly, use: + +```text +ROS: Update C++ Properties +``` +This command re-generates `c_cpp_properties.json` with include paths specific to your workspace and ROS installation. -## Summary +--- -In this tutorial, we: +## Troubleshooting and Best Practices -* Learned to build ROS 2 workspaces with debug symbols. -* Set up `gdbserver` with VS Code. -* Used symlink installs to streamline builds. -* Automated common tasks with `tasks.json`. -* Explored ROS-related commands in the VS Code extension. +- Always open VS Code in your **workspace root** directory. +- Use `--symlink-install` to reduce rebuild times. +- Use `launch.json` to attach VS Code’s debugger to ROS nodes. +- Keep reusable scripts or environment helpers in your workspace for consistency. +--- ## See Also: * [Debugging ROS 2](https://docs.ros.org/en/rolling/Tutorials/Debugging-ROS-2.html) From 11acd0e63734014d95e853a4526f872f3482eaf1 Mon Sep 17 00:00:00 2001 From: Kartik Agrawal Date: Sun, 4 May 2025 16:04:19 -0400 Subject: [PATCH 10/11] Update configure_vscode_for_ros2.md --- wiki/common-platforms/configure_vscode_for_ros2.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/wiki/common-platforms/configure_vscode_for_ros2.md b/wiki/common-platforms/configure_vscode_for_ros2.md index 9d194b27..b0390cb8 100644 --- a/wiki/common-platforms/configure_vscode_for_ros2.md +++ b/wiki/common-platforms/configure_vscode_for_ros2.md @@ -133,7 +133,7 @@ Here’s an example configuration: } ``` -> 🔧 **Tip:** If you want to source your full `.bashrc` instead of just ROS setup, modify the `command` line: +> **Tip:** If you want to source your full `.bashrc` instead of just ROS setup, modify the `command` line: > > ```bash > source ~/.bashrc && colcon build --symlink-install @@ -145,7 +145,7 @@ To run these tasks: This automates builds and reminds you of necessary debug setup steps. -> ⚠️ **Important**: Make sure VS Code is opened in your **ROS 2 workspace root**. If you're in the wrong directory, `colcon build` may fail or build the wrong packages. +> **Important**: Make sure VS Code is opened in your **ROS 2 workspace root**. If you're in the wrong directory, `colcon build` may fail or build the wrong packages. --- @@ -163,7 +163,7 @@ Press **F1** and type `ROS` to view available commands: - **ROS: Update C++ Properties** – Updates your `c_cpp_properties.json` file to enable IntelliSense in C++ ROS projects. - **ROS: Install ROS Dependencies (rosdep)** – Installs missing dependencies in your workspace using `rosdep install`. -> 🧠 Even if you’re using ROS 2 (e.g., Humble), some commands still reference `rosrun` from ROS 1. These labels are legacy but still work for launching executables. +> Even if you’re using ROS 2 (e.g., Humble), some commands still reference `rosrun` from ROS 1. These labels are legacy but still work for launching executables. ### Running a Node via Extension From 01be3fe1a6dbd2f572368c1208a8d6bf642d6019 Mon Sep 17 00:00:00 2001 From: Kartik Agrawal Date: Sun, 4 May 2025 16:21:24 -0400 Subject: [PATCH 11/11] Update navigation.yml --- _data/navigation.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/_data/navigation.yml b/_data/navigation.yml index 6600d2a4..dda5662a 100644 --- a/_data/navigation.yml +++ b/_data/navigation.yml @@ -98,6 +98,8 @@ wiki: url: /wiki/common-platforms/ros2-navigation-for-clearpath-husky/ - title: Hello Robot Stretch RE1 url: /wiki/common-platforms/hello-robot + - title: Configure VScode for ros2 + url: /wiki/common-platforms/configure_vscode_for_ros2/ - title: Sensing url: /wiki/sensing/ children: pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy