In this Robot Operating System 2 (ROS2) Jazzy tutorial, we explain how to write a Python program and a ROS2 package from scratch that demonstrate how to implement all the components of a controller for controlling the position of a mobile robot in 2D space.

In particular, we explain how to implement a Python class that embeds:
- A ROS2 subscriber object for receiving sensor information, and a ROS2 publisher object for sending the control commands to the robot.
- A callback function for reading sensor information. The sensor information consists of the current position, orientation, and velocity of the robot. This callback function is activated every time there is sensor data on a ROS2 sensor topic. This callback function adds the feedback loop to the system.
- A timer object for ensuring that a controller function is called at fixed time steps. This function is important for enforcing the desired control frequency.
- The controller function which implements the control method. In this tutorial, we implement a proportional position controller. However, everything explained in this tutorial can easily be generalized to the case of implementing feedback control algorithms, such as PID, LQR, and other control algorithms.
As a proof of principle and as a model of a 2D mobile robot we use a Turtlesim simulation. The Turtlesim simulation simulates a 2D mobile robot. It enables us to quickly test and prototype control algorithms before proceeding with the next step and testing algorithms in a Gazebo simulation. The figure below shows a Turtlesim simulation and the trajectory that the robot describes. The turtle is located in the desired position (final position), and the trajectory starts from the initial position.
The YouTube tutorial is given below.