ROS (Robot Operating System) is a set of libraries and tools designed for robot applications. It mainly targets C++ and Python development and uses catkin build system, which is based on CMake with Python scripts. ROS software distributions are available for Linux, and also for macOS in experimental mode.
You can use CLion as an IDE for your ROS projects. This tutorial describes how to set up the workflow and gives an example of creating a ROS package and working with it in CLion. Note that this setup procedure has been tested on Ubuntu.
Launch CLion in the sourced environment
CLion needs to be informed of the ROS-specific environment variables. These variables are retrieved in the current shell when you source the workspace by running
source ./devel/setup.bash
The simplest way to provide CLion with ROS environment variables is to launch the IDE from the same shell. After the workspace is sourced, you can type in the same terminal:
sh PATH_TO_CLION/bin/clion.sh
To avoid launching CLion manually from the terminal, you can modify the desktop entry so that CLion is launched from bash. The desktop entry file is located in ~/.local/share/applications and named jetbrains-clion-*.desktop. You can also generate if from CLion's main menu Tools | Create Desktop Entry. In this file, change the line that starts with Exec= to be the following:
After you have launched CLion in the sourced environment, do one of the following to open your ROS project:
Click File | Open and select CMakeLists.txt in the src directory of your ROS workspace, then choose Open as Project.
Click File | New CMake Project from Sources and select the src directory of the workspace to import the project from.
The CMake tool window will show the status of the project loading:
Set build paths to the catkin workspace
By default, CLion places build output in cmake-build-debug or cmake-build-release directory that is created automatically. For ROS development, it means that you will have two different builds in CLion and in the console where you run catkin_make.
To have a single build across the IDE and console, you need to set CLion build paths to the catkin workspace directories. For this, go to File | Settings (Ctrl+Alt+S) | Build, Execution, Deployment | CMake and change two fields:
In Generation path, set workspace_folder/build.
In CMake options, add -DCATKIN_DEVEL_PREFIX:PATH=workspace_folder/devel.
Work with launch files
You can run and debug ROS nodes as regular applications in CLion. Launch files cannot be executed directly, but you can edit them with XML syntax highlighting and completion, and attach the debugger to a running node.
Register launch files as XML
Go to File | Settings (Ctrl+Alt+S) | Editor | File Types and select XML from the list of Recognized File Types.
Add *.launch extension to the list of Registered Patterns.
Attach the debugger to a running node
Run your .launch file from the command line. For example
roslaunch roscpp_tutorials talker_listener.launch
You can check the list of currently running nodes by the rosnode list command. In our example, the list will contain talker and listener:
Call Run | Attach to Process from the main menu or press Ctrl+Alt+F5.
Connect to the desired node by its PID or name:
Example: create a basic ROS node, edit and run it in CLion
In this example, we will create a simple ROS package, edit the source code, and run the node from CLion. We will use ROS Melodic on Ubuntu 18.04 and the basic publisher node from the Writing a Simple Publisher and Subscriber (C++) tutorial available on the ROS wiki.
Create a basic ROS package
Create and build a ROS workspace:
mkdir -p ros_workspace/src
cd ros_workspace
catkin_make
In the workspace, create a package called my_package:
cd src
catkin_create_pkg my_package roscpp rospy std_msgs
Launch CLion
Source the workspace:
cd ../../../
source ./devel/setup.bash
And launch CLion in the same terminal:
sh /opt/clion-2018.2/bin/clion.sh
Open the package as a project in CLion
In CLion, go to File | Open and select the CMakeLists.txt file located inside the package folder, and choose to open it as a project:
Add a source file and edit CMakeLists.txt
Add a new source file to the project: right-click on src in the Project tree, select New | C/C++ Source File and call it my_package.cpp.
After that, reload the CMake project, and notice my_package in the list of Run/Debug configurations:
Run a ROS node
Before running the node from CLion, open the ROS master in a new terminal:
roscore
In CLion, Run the my_package configuration. Run tool window will show the node output:
In a separate terminal, check the currently active ROS topics. If we print the messages from chatter, the output will be similar to what we can see in CLion: Now if we stop the application in CLion, the rostopic echo /chatter command will have no output.