Ubuntu Linux for Robotics · Lesson 57 of 96
robot_state_publisher
Running it from a launch file with a xacro-generated description.
Estimated time · 45–60 min
Learning goals
What you will understand
- Explain robot_state_publisher in your own words
- Use it in a small workstation example
- Verify the result instead of assuming it worked
Understand the idea first
The mental model
robot_state_publisher reads the robot_description parameter and the /joint_states topic and publishes the transform of every link on /tf. Give it the URDF text through a launch file that runs the xacro command, and set use_sim_time when running with a simulator.
If the robot looks broken in RViz2, check the log for URDF parse errors, confirm /joint_states contains the movable joint names, and look at the tree with view_frames. Fixed joints appear on /tf_static.
Guided lesson
Work through these steps
- 1.
Open a terminal and record the current state before changing anything.
- 2.
Read the robot_state_publisher commands and predict what each one does.
- 3.
Run the commands one at a time and compare each output with your prediction.
- 4.
Write the commands that worked in a setup note you can repeat later.
Worked example
Read it before you run it
ros2 run robot_state_publisher robot_state_publisher --ros-args -p robot_description:="$(xacro robot.urdf.xacro)"
# Start it by hand with a xacro-generated description.
ros2 topic echo /tf_static --once
# See the fixed transforms it published.
ros2 topic pub -r 10 /joint_states sensor_msgs/msg/JointState "{name: ['shoulder'], position: [0.5]}"
# Publish a joint value so the moving link appears.
ros2 topic echo /tf --once
# See the moving transform.What the example is doing
- 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
- 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
- 3.Comments beginning with # explain safety checks and are not executed by the shell.
Watch it run · Robot arm
See what this code does
The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.
Demo for robot_state_publisher. Expected result: The robot appears in RViz2
Practice code
Now type it yourself
Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.
# TODO: start robot_state_publisher with robot_description taken from the output of xacro robot.urdf.xacroExpected output
The robot appears in RViz2Stuck? Show the full solution (try it yourself first)
ros2 run robot_state_publisher robot_state_publisher --ros-args -p robot_description:="$(xacro robot.urdf.xacro)"Expected result
How you know it worked
- Each command completes without an unexplained error.
- Your notes contain the command, the important output, and what it proves.
Practical exercise
Apply the lesson
Start robot_state_publisher with your URDF, publish a joint value from the terminal, and watch the link move in RViz2. Then misspell a joint name and describe what changes.
Before you continue
- You recorded the starting state
- Every command and output is understood
- The change can be repeated from your notes
If something goes wrong
Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.
Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.