ROS 2 Lyrical Luth · Lesson 34 of 86
Teleoperation
teleop_twist_keyboard, joystick teleop, scaling and safety limits.
Estimated time · 30–45 min
Learning goals
What you will understand
- Explain teleoperation 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
Teleoperation lets a person drive the robot: teleop_twist_keyboard publishes Twist messages from key presses, and teleop_twist_joy does the same for a gamepad. It is the fastest way to test the simulated drive, sensors and mapping.
Limit speeds to safe values, publish at a steady rate, and add a timeout so the robot stops if commands stop arriving. In a real system a velocity multiplexer (twist_mux) chooses between teleop, navigation and emergency commands by priority.
Guided lesson
Work through these steps
- 1.
Source the ROS 2 Lyrical environment in a fresh terminal.
- 2.
Read the teleoperation example and identify what each line does.
- 3.
Build only the affected package, source the workspace overlay, and run the example.
- 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 teleop_twist_keyboard teleop_twist_keyboard
# Drive with the i, j, l and k keys; publishes Twist on /cmd_vel.
ros2 run teleop_twist_keyboard teleop_twist_keyboard --ros-args -r cmd_vel:=/diff_drive_controller/cmd_vel
# Remap the output to a controller's topic.
ros2 topic echo /cmd_vel
# See exactly what is being commanded.
ros2 launch teleop_twist_joy teleop-launch.py
# Drive with a gamepad (needs a joystick).
ros2 run twist_mux twist_mux --ros-args --params-file config/twist_mux.yaml
# Choose between command sources by priority.What the example is doing
- 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
- 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
- 3.Use a second sourced terminal to inspect nodes and interfaces while the example runs.
Watch it run · Mobile robot
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 Teleoperation. Expected result: The robot follows the keyboard
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.
# TODO: run teleop_twist_keyboard and remap its cmd_vel topic to /diff_drive_controller/cmd_velExpected output
The robot follows the keyboardStuck? Show the full solution (try it yourself first)
ros2 run teleop_twist_keyboard teleop_twist_keyboard --ros-args -r cmd_vel:=/diff_drive_controller/cmd_velExpected 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
Drive your simulated robot around a box with the keyboard while recording a bag of /scan and /odom. Then add a 0.5 s watchdog that publishes zero velocity when commands stop.
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
Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.
Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.