CodeTheRobot

ROS 2 Lyrical Luth · Lesson 21 of 86

joint_state_publisher

Fake joint values with the GUI, the /joint_states message and its fields.

Estimated time · 30–45 min

Learning goals

What you will understand

  • Explain joint_state_publisher in your own words
  • Use it in a small robotics example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

sensor_msgs/msg/JointState holds parallel arrays of names, positions, velocities and efforts. On a real robot the hardware driver publishes it; during development joint_state_publisher_gui gives you sliders for every non-fixed joint of the URDF.

It reads the description from robot_description and publishes /joint_states. Use it to check joint limits and motion before any simulation or hardware exists. Do not run it together with a real driver or simulator that publishes joint states, because they would conflict.

Guided lesson

Work through these steps

  1. 1.

    Source the ROS 2 Lyrical environment in a fresh terminal.

  2. 2.

    Read the joint_state_publisher example and identify what each line does.

  3. 3.

    Build only the affected package, source the workspace overlay, and run the example.

  4. 4.

    Inspect the result from a second terminal with the ros2 CLI or RViz2 and compare it with the expected result.

Worked example

Read it before you run it

ros2 run joint_state_publisher_gui joint_state_publisher_gui
# Open sliders for every movable joint of the loaded description.
ros2 topic echo /joint_states --once
# Print one JointState message to see names and positions.
ros2 topic hz /joint_states
# Check the publishing rate.
ros2 interface show sensor_msgs/msg/JointState
# See the fields of the message.
ros2 launch urdf_tutorial display.launch.py model:=mini_arm.urdf
# A ready-made launch file that starts the GUI, state publisher and RViz2 together.

What the example is doing

  1. 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
  2. 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
  3. 3.Use a second sourced terminal to inspect nodes and interfaces while the example runs.

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.

tip x110 y58ROBOT ARM

Demo for joint_state_publisher. Expected result: Moving the slider moves the robot in RViz2

Practice code

Now type it yourself

Copy the starter into your ROS 2 package, replace every TODO, then build with colcon build and run it from a sourced terminal.

# launch/display.launch.py: add the slider GUI next to robot_state_publisher
from launch import LaunchDescription
from launch_ros.actions import Node


def generate_launch_description():
    # TODO: start the joint_state_publisher_gui executable from package joint_state_publisher_gui
    return LaunchDescription([gui])

Expected output

Moving the slider moves the robot in RViz2
Stuck? Show the full solution (try it yourself first)
# launch/display.launch.py: add the slider GUI next to robot_state_publisher
from launch import LaunchDescription
from launch_ros.actions import Node


def generate_launch_description():
    gui = Node(package='joint_state_publisher_gui', executable='joint_state_publisher_gui')
    return LaunchDescription([gui])

Expected result

How you know it worked

  • ROS_DISTRO is lyrical in every terminal used for the exercise.
  • The complete package from the exercise builds and the expected graph entity appears in a second terminal.

Practical exercise

Apply the lesson

Display your URDF with the GUI sliders, move each joint to its limits, and write down which limit values RViz2 respects. Then stop the GUI and publish the same joint from a C++ node.

Before you continue

  • ROS_DISTRO reports lyrical in every terminal you used
  • The example builds and runs without errors
  • You checked the result with a ros2 command, RViz2 or a test, not just by reading the code

If something goes wrong

ros2 cannot find a package or executable.

Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.

Nodes run but cannot discover each other.

Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.