Building a line follower in Gazebo
Testing a line follower on a real floor is slow. Charge the battery, put the rover down, watch it wander off the line, pick it up, change a number, repeat.
Code: line_follower_ws on GitHub · Watch: The rover learning to follow a line
Testing a line follower on a real floor is slow. Charge the battery, put the rover down, watch it wander off the line, pick it up, change a number, repeat. A simulator turns that loop into seconds, the same way a local dev server beats deploying every change to production. This post walks through the Gazebo setup in my line_follower_ws repo.
The workspace
The repo is a ROS 2 workspace with one package per job:
| Package | What’s in it |
|---|---|
| line_follower_description | The robot model, in URDF and xacro |
| line_follower_models | The ground plane with the line drawn on it |
| line_follower_worlds | Gazebo world files |
| line_follower_bringup | Launch files that start everything |
| line_follower_controller | The Python node that does the actual line following |
Splitting it this way is the same instinct as separating components, assets and config in a web project. The controller doesn’t care whether its camera is real or simulated.
Describing the robot
URDF is an XML description of a robot: links (rigid parts) connected by joints. Xacro adds variables and includes on top, so it’s to URDF roughly what Sass is to CSS. My model is a 4-wheeled base with a camera on top. Each wheel hangs from a “clamp” link, and each clamp-to-wheel joint is what the simulator spins.
Making the wheels drive
In Gazebo Classic, plugins give the model behaviour. The diff-drive plugin listens on /cmd_vel and turns the wheels:
<plugin name="diff_drive_controller" filename="libgazebo_ros_diff_drive.so">
<update_rate>50</update_rate>
<num_wheel_pairs>2</num_wheel_pairs>
<left_joint>wheel_clamp_4_to_wheel_4</left_joint>
<right_joint>wheel_clamp_2_to_wheel_2</right_joint>
<left_joint>wheel_clamp_3_to_wheel_3</left_joint>
<right_joint>wheel_clamp_1_to_wheel_1</right_joint>
<wheel_separation>0.280</wheel_separation>
<wheel_separation>0.280</wheel_separation>
<wheel_diameter>0.100</wheel_diameter>
<wheel_diameter>0.100</wheel_diameter>
<command_topic>cmd_vel</command_topic>
<publish_odom>true</publish_odom>
<robot_base_frame>base_footprint</robot_base_frame>
</plugin>
One thing that looks like a copy-paste mistake but isn’t: with two wheel pairs, the plugin expects wheel_separation and wheel_diameter once per pair.
Giving it eyes
The camera is a sensor attached to a link, with the ROS camera plugin turning its frames into a ROS topic:
<gazebo reference="psudo_camera">
<sensor name="camera_sensor" type="camera">
<camera name="camera_sensor">
<image>
<width>1920</width>
<height>1080</height>
</image>
</camera>
<update_rate>30.0</update_rate>
<plugin name="camera_controller" filename="libgazebo_ros_camera.so">
<frame_name>primary_camera_frame</frame_name>
</plugin>
</sensor>
</gazebo>
That publishes /camera_sensor/image_raw at 30 frames per second, which is exactly what the controller node subscribes to. That said, 1920 by 1080 is far more than a line follower needs. A 640 by 480 camera renders faster and is closer to what a small robot can process in real time.
The track
The world uses a flat 40 by 40 m ground plane with a texture of the path drawn on it, and the launch file spawns the robot at a fixed spot on the line, facing along it. When the robot keeps losing the line in one particular corner, you can restart it in exactly the same spot every time, which never happens on a real floor.
A frame convention worth fixing early
My controller flips the sign of both speeds before publishing:
twist.linear.x = -linear_velocity
twist.angular.z = -angular_velocity
That’s a sign the model’s “forward” doesn’t match ROS’s convention, where x points forward, y left and z up, and a positive yaw turns left. Negating in the controller works, but every other tool (teleop, navigation, RViz) will still disagree with the model. The cleaner fix is to rotate the robot’s base frame in the URDF so its x axis points forward. Then the controller publishes plain positive numbers and everything else agrees.
What the simulator can’t tell you
Simulation is a staging environment, not production. The lighting is perfect, the line is perfectly grey, the camera has no noise or motion blur, and the wheels grip like glue (the ground’s friction coefficient in my world is 100). Thresholds tuned in Gazebo will need retuning on a real floor.
It’s still worth it. Getting the pipeline, the topics and the control logic right in simulation means the real-world session is about tuning numbers, not debugging plumbing.
The controller itself, the OpenCV part, gets its own post next.