How to Install and Configure Visual Studio Code for ROS 2

In this tutorial, we will go over how to install and configure Visual Studio Code for your ROS 2 projects.

Visual Studio Code, often shortened to VS Code, is a free and open-source code editor developed by Microsoft. It’s a popular choice for programmers of all levels, from beginners to professionals, because it’s:

  • Lightweight and fast: It doesn’t take up much space on your computer and starts up quickly, so you can get coding right away.
  • Versatile: It supports a wide range of programming languages, including JavaScript, Python, Java, C++, and more. You can also install extensions to add support for even more languages.
  • Customizable: You can personalize VS Code with themes, extensions, and keyboard shortcuts to make it work the way you want.
  • Feature-rich: It comes with built-in features like syntax highlighting, code completion, and debugging, which can help you write better code faster.

Let’s get started!

Prerequisites

Directions

Go to the download page at Visual Studio Code.

Click the .deb button.

1-click-deb
2-download

Open a terminal window, and type:

cd ~/Downloads

Check the name of the file:

dir
3-check-name-of-file

Run the following command, replacing “filename.deb” with the actual name of your file:

sudo dpkg -i filename.deb

Enter your password when prompted.

Click Yes if you see this screen.

4-click-yes-if-you-see-this-code

The installation will start. Wait for it to complete.

Now, navigate to your workspace.

cd ~/ros2_ws/

Open VS Code by typing this command in a terminal window:

code .

Click the checkbox next to “Trust the authors of all files in the parent folder ‘ubuntu’”.

5-open-vs-code

Click “Yes, I trust the authors” to continue.

Choose your desired color theme by clicking the “Browse Color Themes” button.

6-choose-color-theme

I will select Solarized Dark because it reduces blue light emission from my screen, which can help minimize disruption to my natural sleep-wake cycle. This theme is particularly beneficial for low-light environments and late-night coding sessions, as it reduces eye strain and makes extended work more comfortable.

A similar option if you prefer lighter themes is Solarized Light.

Now you need to scroll down on the top menu, and select “Mark Done”.

7-select-mark-done

Double-click on the Extensions button on the left side of the window.

8-extensions-button

Search for the “C/C++” extension by Microsoft.

9-cpp-by-microsoft

Install it by clicking the Install button.

Search for the “ROS” extension.

Install the ROS extension from Microsoft by clicking the Install button. 

10-ros-extension-by-microsoft

Now let’s install the CMake extension. Search for “CMake”, and install the version made by twxs.

11-twxs

Install the CMake Tools extensions. Search for “CMake Tools”, and install the version made by Microsoft.

12-cmake-tools

Install the XML extensions. Search for “XML”, and install the version made by Red Hat.

13-xml-extension

Now let’s install the Python extension. Search for “python”, and install the version made by Microsoft. Note: You may have already installed it. 

14-python-extension

Install the autoDocstring extension. Search for “autoDocstring”, and install the version made by Nils Werner.

15-autodoctring

Let’s set our default indentation to 2 spaces.

Using the menu: Go to File > Preferences > Settings

Search for “Indentation” in the settings panel.

Change the following values:

“Editor: Tab Size”: Set this to 2 to control the width of a tab character.

16-set-to-2-indentation

“Editor: Insert Spaces”: Set this to true to ensure that pressing Tab inserts spaces instead of a literal tab character.

17-insert-spaces-to-true

The changes should take effect immediately. 

Now you can close VS Code by going to File -> Close Window.

You can reopen VS code inside your workspace like this:

cd ~/ros2_ws/src
code .

That’s it! I’ll see you in the next tutorial. Keep building!

How to Create a Workspace in ROS 2 Jazzy

In this tutorial, we will create a ROS 2 workspace.

In ROS 2, a workspace is a folder that is the central location for organizing and developing your robot software. It contains all the code, data files, and configuration scripts related to your specific robot project. 

The official instructions for creating a workspace are here, but I will walk you through the entire process, step by step.

Let’s get started!

Prerequisites

Directions

Open a terminal, and type the following command to create your workspace. We can name our workspace anything, but I will name it ros2_ws to stand for ROS 2 workspace.

mkdir -p ~/ros2_ws/src

The src folder that is inside your ROS 2 workspace is where all your code will go for your robot.

Move to the root of your new workspace using this command:

cd ~/ros2_ws/

We don’t have any code inside our workspace source folder yet, but when we do, we will need to run the command below to build the workspace. 

colcon build

This colcon build command needs to be run in the root of your workspace whenever you make additions or changes to the code for the robot.

‘colcon build’ takes all the different pieces of your robot’s software and puts them together. This makes the software ready for the robot to use.

Go ahead and run the colcon build command now.

colcon build

Now type this command:

dir

Here is what you should see:

1-colcon-build

There are three folders. These folders are automatically created when you run the colcon build command in your ROS 2 workspace:

  • build: This is where ROS 2 temporarily stores files while it’s putting your code together.
  • install: This is where the finished, ready-to-use parts of your robot’s software are placed.
  • log: This folder keeps track of what happened when you built your code, which is helpful for finding and fixing problems.

Now we need to run the following command to make sure our workspace can be found any time we open a new terminal window.

echo "source ~/ros2_ws/install/setup.bash" >> ~/.bashrc
source ~/.bashrc

Remember that ~/.bashrc file is a script that runs whenever a new terminal session is started.

You can see that we have a new entry in the bashrc file right at the bottom.

gedit ~/.bashrc
2-new-entry-bashrc

That’s it! You have now created a ROS 2 workspace. I’ll see you in the next tutorial. Keep building!

Understanding DDS and the ROS_DOMAIN_ID Variable

How DDS Works?

dds-overview

When you’re working with robots, you need a way for all the different parts of the robot to talk to each other. That’s where something called DDS comes in. DDS stands for Data Distribution Service, and it’s like a super-advanced messaging system for robots and computers.

Think of it like this: imagine you have a bunch of robots in a factory, and they all need to work together to build cars. Each robot has a specific job, like welding doors or painting bumpers. To do their jobs, they need to be able to send messages to each other, like “I’ve finished welding this door” or “I need more paint over here!”

DDS is what makes this communication possible. It is a set of rules that all the robots agree to follow so they can send messages quickly and reliably. It is kind of like a big chat room where robots can post and read messages, but instead of just being for fun, these messages contain important information that the robots need to do their jobs.

ROS 2 uses DDS under the hood to help all the different parts of a robot communicate. You can think of DDS as the glue that holds everything together in a ROS 2 system.

What is the ROS_DOMAIN_ID?

Now, here’s where something called the ROS_DOMAIN_ID comes in. In a DDS system, you can have multiple “chat rooms,” each with its own ID number. These “chat rooms” are called domains in DDS language. The ROS_DOMAIN_ID is just a number that tells ROS which domain to use.

By default, ROS uses 0 for the ROS_DOMAIN_ID. It is fine if we keep the default since we are working in simulation. However, in the real world, you might have a facility with multiple robots, all running ROS 2. If all these robots use the default settings, they’ll all be talking in the same “chat room” – domain 0. 

This is fine if you want all your robots to be able to communicate with each other. But let’s say you have two different teams of robots in your factory, and you don’t want them to get confused by hearing each other’s messages. You could put one team in domain 1 and the other team in domain 2. They’re all still using DDS to communicate, but the two teams can’t hear each other because they’re in different domains.

The easiest way to choose a ROS_DOMAIN_ID is to pick a number between 0 and 101, or between 215 and 232. These numbers are less likely to conflict with other programs that might be using DDS.

In my tutorials, we’ll be sticking with the default ROS_DOMAIN_ID of 0. This means all our robots will be able to communicate with each other. But if you ever need to work with multiple groups of robots that shouldn’t be talking to each other, remember that you can use different ROS_DOMAIN_IDs to keep their conversations separate.

If you did want to change the ROS_DOMAIN_ID, you can do it by typing this command:

export ROS_DOMAIN_ID=<your_chosen_number>

Replace <your_chosen_number> with the ROS_DOMAIN_ID you want to use. For example, if you want to use a ROS_DOMAIN_ID of 42, you would add in the terminal window before the commands you want to run:

export ROS_DOMAIN_ID=42

Remember, if you have multiple robots on the same WiFi network and you want them to be able to talk to each other, they should all have the same ROS_DOMAIN_ID. But if you have groups of robots that shouldn’t be communicating, give each group its own ROS_DOMAIN_ID.

Example

Let’s look at an example:

Open a new terminal, and type this:

ros2 run demo_nodes_cpp talker

This “talker” program publishes messages to the ROS 2 system. 

Open another terminal window, and type:

ros2 run demo_nodes_py listener

You can see both programs are able to talk to each other freely because they have the same ROS_DOMAIN_IDs.

1-talk-freely

Now close both nodes by pressing CTRL+C in both terminal windows.

Now in a the first terminal window, type this:

export ROS_DOMAIN_ID=8

In that same terminal window, type:

ros2 run demo_nodes_cpp talker

Now in your second terminal window, type:

ros2 run demo_nodes_py listener

You can see this node can’t receive messages from the other node.

2-node-cannot-receive-messages

You can now close this listener using CTRL + C.

Now set the ROS_DOMAIN_ID to match our publisher node.

export ROS_DOMAIN_ID=8
ros2 run demo_nodes_py listener

You can see now that, since these nodes have the same ROS_DOMAIN_ID, they can now communicate with each other. 

3-same-ros-domain-id

That’s it. I’ll see you in the next tutorial. Keep building!