🤖
Introduction to ROS2 and Robotics
  • Introduction
  • ROS2
    • Index
    • IDE and CMake Setup
      • How to add additional include search path
    • ROS2 Building Blocks
      • ROS Workspace and Package Layout
      • Launch File
      • tf2
      • Quality of Service
      • Configurations
        • Rviz Configuration
      • Built-in Types
        • Built-in Message Type
    • ROS Architecture
      • Intra-process Communication
    • Navigation and Planning
      • Navigation Stack and Concepts
      • Navigation2 Implementation Overview
        • 🏗️Cost Map
        • Obstacle Avoidance and DWB Controller
      • DWB Controller
      • Page 5
    • How to launch the Nav2 stack
    • ROS2 Control
      • Online Resources
      • Overview of Codebase
    • 🍳Cookbook
      • Useful Commands
      • How to specify parameters
      • How to build the workspace
      • 🏗️How to publish message to a topic from command line?
      • How to inspect service and make a service call
      • How to properly terminate ROS and Gazebo
      • How to add and remove models in Gazebo simulation dynamically
      • 🚧How to spin nodes
    • 🛒Tutorials
      • Services and Communication between ROS2 and Gazebo
      • Subscription and Message Filters Demo
      • Executor and Spin Explained
      • Lifecycle Node Demo
      • Robotic Arm Demo
      • ⚒️Multiple Robotic Arms Simulation Demo
      • 🚧Introduction to xacro
    • Page
    • 🍺Tech Blog
      • Difference between URDF and SDF and how to convert
  • Gazebo
    • Index
    • Terminology
    • GUI
    • World Frame and Axis
    • Cookbook
    • Page 1
  • Programming in Robotics
    • C++
      • CMake
    • Python
    • Rust
  • Mathematics in Robotics
    • Linear Algebra
    • Matrix Properties
    • Probability
      • Expectation-Maximization Algorithm
    • Multivariable Function and Derivatives
  • Physics in Robotics
  • Control of Dynamic Systems
    • Dynamic Response and Transfer Function
    • Block Diagram
    • PID Controller
  • Robot Modeling and Control
    • Rotation and Homogeneous Transformation
  • Probabilistic Robotics
    • Bayes Filter
    • Kalman Filter
    • Particle Filter
    • Discrete Bayes Filter
    • Motion Model
    • Perception Model
    • Localization
    • SLAM
  • Miscellany
  • Concept Index
    • Quaternions
Powered by GitBook
On this page
  • CMake Project Setup
  • Resolve Symbols
  1. ROS2

IDE and CMake Setup

ROS2 has its own build system and tools. For example, ROS2 humble uses ament cmake. It's convenient to use the build tools in terminal but this can cause some issues with IDE. In this article, we address some common issues.

OS: Ubuntu 22.04, ROS: Humble, IDE: CLion

CMake Project Setup

Let's first talk about the issues. In CLion, it only support single-project configuration. Here, project roughly means ROS packages. Recall that ROS2 development is centered around workspace and each workspace contains multiple packages. The structure below is a typical layout of a ROS2 workspace

user:~/workspace/ros-projects/ros2_ws_tutorial_creating_ws$ tree -L 2
.
├── build
├── cmake-build-debug
├── install
├── log
├── src
    ├── cpp_pubsub
    ├── cpp_srvcli
    └── tutorial_interfaces

In the workspace ros2_ws_tutorial_creating_ws, there are three pakcages and each of them has a CMakeLists.txt file:

user:~/workspace/ros-projects/ros2_ws_tutorial_creating_ws/src$ tree -L 2
.
├── cpp_pubsub
│   ├── cmake-build-debug
│   ├── CMakeLists.txt
│   ├── include
│   ├── package.xml
│   └── src
├── cpp_srvcli
│   ├── cmake-build-debug
│   ├── CMakeLists.txt
│   ├── include
│   ├── package.xml
│   └── src
└── tutorial_interfaces
    ├── CMakeLists.txt
    ├── include
    ├── msg
    ├── package.xml
    ├── src
    └── srv

Here comes our first problem. We cannot just open the ros2_ws_tutorial_creating_ws as the root directory because CLion does not support multiple projects. To make it work, we need to combine all ROS2 packages/projects as one single CMake project. This means we need to have a top-level CMakeLists.txt file. We cannot have one directly in the ros2_ws_tutorial_creating_ws directory because it will interfere withe ROS2 build tools. What we can do is to create a tmp directory under ros2_ws_tutorial_creating_ws and put a CMakeLists.txt in it. For instance:

# Location of this file is ~/workspace/ros-projects/ros2_ws_tutorial_creating_ws/tmp/CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
project(ros2_ws_tutorial_creating_ws)

add_subdirectory("../src/cpp_pubsub" "../src/cpp_pubsub")
add_subdirectory("../src/cpp_srvcli" "../src/cpp_srvcli")
add_subdirectory("../src/tutorial_interfaces" "../src/tutorial_interfaces")

Remember to reload the CMakeLists.txt file after the change.

Resolve Symbols

You may notice that jump to definition does not work for ROS2 defined symbols. The problem is that CLion does not know where to search for the symbols. In fact, when you reloade the CMake project in CLion, it may complain that it cannot find the ament_cmake.

The solution to this problem is to add environment variables to the cmake configuration in CLion.

How do we know which variables to add? Recall that when we launch a new terminal, we need to source the ROS2 setup.bash file (i.e. /opt/ros/humble/setup.bash). To determine the environment variables, you could following the steps below:

  • Update ~/.bashrc file and make sure the terminal does not automatically source the ROS2 setup file.

  • Launch a new terminal and save the current environment variables using the command printenv

  • Source the ROS2 setup file and save the environment variables again to a different file

  • Compare the two files using the command diff --color -u old-env-vars-file new-env-vars-file

After the change, don't forget to reload the CMakeLists.txt files.

TODO: Set up IDE for python files:

/opt/ros/humble/lib/python3.10

PreviousIndexNextHow to add additional include search path

Last updated 1 year ago