Skip to content

Latest commit

 

History

History
210 lines (151 loc) · 8.59 KB

README.md

File metadata and controls

210 lines (151 loc) · 8.59 KB

ROS and IDEs

How to get full autocompletion for ROS in your IDE.

Contents

ros-ide-sync script

Creates necessary configuration for VSCode and CLion (PyCharm) to get autocompletion working with ROS 2 (both C++ and Python packages).

Usage

In order for the C++ autocompletion to work correctly, you need to enable Compilation database generation for every build:

colcon build --cmake-args -DCMAKE_EXPORT_COMPILE_COMMANDS=1

Tip: colcon compile-commands mixin

Instead of having to write (and remember) --cmake-args -DCMAKE_EXPORT_COMPILE_COMMANDS=1 arguments over and over, you can use a colcon mixin from the default colcon mixin repository.

If you've never used colcon mixins before, you'll have to do an initial set up:

# 1. install colcon mixin plugin via apt
sudo apt install python3-colcon-mixin
# or via pip
python3 -m pip install -U colcon-mixin
# 2. add default mixins repository and download the mixins from it
#    see https://github.com/colcon/colcon-mixin-repository/
colcon mixin add default https://raw.githubusercontent.com/colcon/colcon-mixin-repository/master/index.yaml
colcon mixin update default

Then, you can use the compile-commands mixin like this:

colcon build --mixin compile-commands

Installation

It's a standalone Bash script. You can download it, place it anywhere, and run it (after you make it executable).

  1. Download ros-ide-sync.sh to ~/bin/ros-ide-sync and make it executable.
    mkdir ~/bin
    cd ~/bin
    wget https://raw.githubusercontent.com/pokusew/ros-setup/master/ide/ros-ide-sync.sh
    mv ros-ide-sync.sh ros-ide-sync
    chmod +x ros-ide-sync
  2. Restart your terminal. If you are running Ubuntu 20, you should have ~/bin dir (and thus ros-ide-sync executable) on your PATH by default. You can verify it by attempting to run ros-ide-sync in an arbitrary dir:
    ros-ide-sync
    If that's not the case, you can easily fix it by adding the following at the end of the ~/.bashrc:
    export PATH="$HOME/bin:$PATH"

JetBrains IDEs (CLion, PyCharm) and ROS

What's great on JetBrains IDEs:

  • Autocompletion, linting, IntelliSense, Git integration, coding assistance and overall features are superior to other editors/IDEs.

What's not so good on JetBrains IDEs:

  • The remote mode is very limited and does not work well.

Common

There are two different situations to consider:

  1. ROS and JetBrains IDE are both running in the same system (OS).
  2. ROS is running in the remote/virtual machine (or in Docker container).

Further, ROS 2 setup is different from ROS 1 setup.

The best JetBrains IDE to use is CLion as it has also Python support (so it will work as well as PyCharm). However, CLion does NOT support remote Python interpreter and Jupyter notebooks (you can always use PyCharm for that).

CLion + ROS 2 (same system)

C/C++: The best way (and practically the only way) is to use the CLion's support for Compilation database.

Python: The best way is to set Python interpreter to python.local.sh (a wrapper executable script generated by ros-ide-sync script that fixes PYTHONPATH).

Project setup:

  1. Install and read how to use ros-ide-sync
  2. Run ros-ide-sync in your project root.
  3. Open your project root in CLion.
    1. Make sure the project is detected as a Compilation database project.
    2. Create a new System Interpreter. Set location to project's python.local.sh (a wrapper executable script generated by ros-ide-sync script that fixes PYTHONPATH). Set this newly created interpreter as the project's Python interpreter.

Other scenarios

All combinations of ROS 1 / ROS 2, local/remote are possible to use. I tried them all, but I haven't got time to document the setup (yet). In case you need help, feel free to contact me. Nevertheless, the remote mode in JetBrains IDEs is not good and for remote work I recommend VSCode.

Related links

VSCode and ROS

What's great on VSCode:

  • It can be used in Remote mode over SSH.
    • The Remote mode is a first-class feature and everything works really great.
    • Compared to that, CLion's Remote mode is a very bad.

What's not so good on VSCode:

  • Autocomplete, linting, IntelliSense, coding assistance, Git integration and overall features are way worse than what CLion offers.

Setup ROS 2 and VSCode

That's currently the setup I use.

VSCode extensions:

  • C/C++ ms-vscode.cpptools
  • Python ms-python.python

Project setup:

  1. Install and read how to use ros-ide-sync
  2. Run ros-ide-sync in your project root.
    • If there is no .vscode/c_cpp_properties.json in your project, the script will create a new one for Linux + ROS If there is already one, it won't get updated.
    • Note the compileCommands option. It tells VSCode to use the Compilation Database produced by colcon build.
    • Feel free to modify it. Especially includePath, as sometimes compileCommands option is not enough.
  3. Open your project root in VSCode (or add it to your workspace using File > Add Folder to Workspace...).

Files in the workspace

  • ${workspaceFolder}/.vscode/settings.json
    • python.autoComplete.extraPaths
      • used by Python autocompletion (IntelliSense)
      • however, ignored by linting tools (such as pylint) (even when they are invoked automatically under the hood by VSCode), see below for solution (.env file)
      • auto-managed by the ROS extension it turned out that my solution is better, see below
        • the ROS extension will automatically detect the devel/setup* file (for both ROS 1 and ROS 2),
        • note! When PYTHONPATH changes, the whole VSCode must be restarted in order for run it and extract the PYTHONPATH from it (and update python.autoComplete.extraPaths) the ROS extension to detect the change. The setup devel/setup* file is evaluated only on the ROS extension's activation and then the PYTHONPATH is cached (even when using the action ROS: Update Python Path).
      • now it is automatically updated while running ros-vsc-sync.sh script (workspaces not directly in workspaceFolder are also supported)
  • ${workspaceFolder}/.env (can be changed via setting python.envFile)

Related links