Building an Autonomous Control Node for TurtleBot3: A ROS 2 Guide
- Karan Bhakuni
- Dec 8, 2024
- 3 min read
Updated: Jan 12
References
1 Introduction
If you’re exploring robotics, understanding how to control a mobile robot to move towards a target
is a fundamental skill. In this tutorial, we’ll dive into the creation of a custom control node for the
popular TurtleBot3 using ROS 2, focusing on the high-level logic and mathematical foundations
that guide the robot from its current location to a user-defined goal. By the end of this guide, you’ll
grasp how to work with topics in ROS 2, leverage sensor data for odometry, calculate goal-directed
movement, and publish commands to the robot.
2 Overview: Setting Up Your Control Node
The control node we’ll develop allows you to set a goal position (x, y coordinates) for the TurtleBot3.
The robot will continuously update its position based on odometry data and adjust its motion until
it reaches the target.
We’ll achieve this by following these key steps:
1. Receive Real-Time Odometry Data: Gather the robot’s current position and orientation.
2. Calculate Distance and Angular Error to Goal: Use mathematical formulas to compute the
distance and angle between the robot’s position and the goal.
3. Publish Control Commands: Send velocity commands to drive the robot forward and adjust
its heading toward the goal.
3 Key Concepts and Mathematics
3.1 Odometry: Getting the Robot’s Current Position
Odometry data from the /odom topic provides the current position and orientation of the robot,
which we store in a Pose message. This message contains:
• Position (x, y): The robot’s coordinates in the map.
• Orientation (quaternion): The robot’s orientation in quaternion form, which we convert
to a usable angle.
3.2 Calculating the Euclidean Distance to the Goal
The Euclidean distance gives the shortest path between two points in a plane. For a goal located
at (goalx, goaly), and the current robot position (x, y), the formula is:
This distance serves as our error term, guiding the robot’s forward speed.
3.3 Calculating the Angular Error to Goal
To move toward the target, the robot must adjust its heading to point directly at the goal. We
calculate the angular error using the arctangent function:
The difference between the robot’s current heading and the desired angle is the angular error.
In practice, we use the quaternion data from the odometry message to calculate the robot’s heading
and compare it to desired angle to determine how much to rotate.
3.3.1 Challenges with Euler Angles
Interpolating directly between Euler angles can create problems:
• Gimbal Lock: When two rotational axes align, you lose a degree of freedom, making it
impossible to represent all rotations correctly.
• Angle Wrapping: Multiple sets of Euler angles can represent the same orientation due to
the wrapping of angles (e.g., 0◦ and 360◦).
3.3.2 Using Rotation Matrices
3.3.3 Using Quaternions
3.4 Control Law: Adjusting Linear and Angular Velocity
A proportional controller adjusts both linear and angular velocities:
linear velocity = kd × distance
angular velocity = kθ × angular error
Here, kd and kθ are tuning parameters that adjust the sensitivity of the control response. You
can see in the code that depending on the how close we are to the goal i have varied the parameters
because otherwise it will always overshoot the desired position.
4 Building the Control Node in ROS 2
Node Overview
Our control node:
• Publishes Twist messages to /cmd vel to control movement.
• Subscribes to /odom for position feedback.
• Calculates control commands using the distance and angular error, derived from real-time
odometry.
5 Challenges in Mobile Robot Control
• Tuning Parameters: Proper selection of kd and kθ is essential for smooth motion.
• Coordinate Transformations: Converting quaternions to angles is nontrivial.
• Thread Management: Ensuring the control loop and input handling work concurrently.
• Settling Time: In the video you can see that settling time to the goal is high, so in future
we can try PID control where we are able to minimize the settling time without damping.
6 Conclusion
Creating a control node in ROS 2 for TurtleBot3 provides foundational knowledge in mobile robot
control and ROS-based programming. By grasping the concepts of odometry, goal setting, and
error correction with control laws, you’re well on your way to building more complex autonomous
systems in ROS 2.
7 References
1. Turtlebot3
4. Robotics and Control by RK Mittal and IJ Nagrath