Bird
Raised Fist0
ROSframework~10 mins

Keyboard teleoperation (teleop_twist_keyboard) in ROS - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Keyboard teleoperation (teleop_twist_keyboard)
Start Program
Wait for Key Press
Read Key
Match Key to Command
If Key is Movement
Create Twist Message
Publish Twist
Wait for Next Key
If Key is Quit
Exit Program
Loop Back to Wait for Key Press
The program waits for keyboard input, converts keys to movement commands, publishes them, and loops until quit.
Execution Sample
ROS
while True:
    key = get_key()
    if key in move_bindings:
        twist = create_twist(move_bindings[key])
        pub.publish(twist)
    elif key == 'q':
        break
Continuously reads keys, publishes movement commands for recognized keys, and exits on 'q'.
Execution Table
StepKey PressedConditionActionOutput
1'w'key in move_bindings is TrueCreate Twist for forwardPublish Twist (linear.x=1)
2'a'key in move_bindings is TrueCreate Twist for left turnPublish Twist (angular.z=1)
3'x'key in move_bindings is TrueCreate Twist for backwardPublish Twist (linear.x=-1)
4's'key in move_bindings is TrueCreate Twist for stopPublish Twist (all zero)
5'q'key == 'q' is TrueExit loopProgram terminates
💡 Program stops when 'q' key is pressed to quit.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
keyNone'w''a''x''s''q'
twist.linear.x010-100
twist.angular.z001000
Key Moments - 3 Insights
Why does pressing 's' stop the robot instead of moving it?
Because in the execution_table at Step 4, 's' maps to a Twist message with all zero velocities, which stops movement.
What happens if a key not in move_bindings is pressed?
The program ignores it and waits for the next key, as shown by no action rows for unknown keys in the execution_table.
Why does the program stop when 'q' is pressed?
At Step 5 in the execution_table, 'q' triggers the exit condition, breaking the loop and ending the program.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of twist.linear.x after Step 3?
A0
B1
C-1
DUndefined
💡 Hint
Check the 'twist.linear.x' value in variable_tracker after Step 3.
At which step does the program stop running?
AStep 4
BStep 5
CStep 3
DNever stops
💡 Hint
Look at the exit_note and Step 5 in execution_table.
If the key 'd' is pressed but not in move_bindings, what happens?
AIgnores the key and waits for next input
BExits the program
CPublishes a Twist with zero velocities
DCauses an error
💡 Hint
Refer to key_moments about unknown keys and execution_table rows.
Concept Snapshot
Keyboard teleoperation listens for keys,
Maps keys to movement commands,
Publishes Twist messages to control robot,
Stops on 'q' key press,
Ignores unknown keys,
Loops continuously until quit.
Full Transcript
This concept shows how the teleop_twist_keyboard program works in ROS. It waits for keyboard input, reads the pressed key, and checks if it matches known movement commands. If it does, it creates a Twist message with linear and angular velocities and publishes it to move the robot. If the key is 'q', the program exits. Unknown keys are ignored. The program loops continuously, allowing real-time robot control from the keyboard.

Practice

(1/5)
1. What is the main purpose of the teleop_twist_keyboard node in ROS?
easy
A. To control a robot using keyboard keys
B. To visualize robot sensor data
C. To simulate robot movements automatically
D. To compile ROS packages

Solution

  1. Step 1: Understand the node's function

    The teleop_twist_keyboard node allows manual control of a robot using keyboard inputs.
  2. Step 2: Compare options with the node's purpose

    Only To control a robot using keyboard keys describes controlling a robot with keyboard keys, which matches the node's purpose.
  3. Final Answer:

    To control a robot using keyboard keys -> Option A
  4. Quick Check:

    Keyboard control = A [OK]
Hint: Remember: teleop means tele-operation via keyboard [OK]
Common Mistakes:
  • Confusing teleop with simulation or visualization
  • Thinking it compiles code
  • Assuming it runs robot automatically
2. Which command correctly runs the teleop_twist_keyboard node in ROS 2?
easy
A. ros run teleop_twist_keyboard teleop_twist_keyboard
B. ros2 launch teleop_twist_keyboard teleop_twist_keyboard.launch
C. ros2 run teleop_twist_keyboard teleop_twist_keyboard
D. ros2 start teleop_twist_keyboard

Solution

  1. Step 1: Identify the correct ROS 2 run syntax

    In ROS 2, the command to run a node is ros2 run <package> <executable>.
  2. Step 2: Match the command to teleop_twist_keyboard

    ros2 run teleop_twist_keyboard teleop_twist_keyboard uses the correct syntax and package/executable names.
  3. Final Answer:

    ros2 run teleop_twist_keyboard teleop_twist_keyboard -> Option C
  4. Quick Check:

    ros2 run + package + executable = B [OK]
Hint: Use 'ros2 run' for running nodes in ROS 2 [OK]
Common Mistakes:
  • Using 'ros' instead of 'ros2'
  • Using 'launch' instead of 'run' for this node
  • Typing 'start' which is not a ROS command
3. Given the following keys pressed in teleop_twist_keyboard: i, j, k, what is the expected robot movement sequence?
medium
A. Turn left, move forward, stop
B. Move backward, turn right, stop
C. Stop, move forward, turn right
D. Move forward, turn left, move backward

Solution

  1. Step 1: Recall key functions in teleop_twist_keyboard

    Key 'i' moves robot forward, 'j' turns left, 'k' moves backward.
  2. Step 2: Match keys to movements in sequence

    Pressing 'i' then 'j' then 'k' results in moving forward, turning left, then moving backward.
  3. Final Answer:

    Move forward, turn left, move backward -> Option D
  4. Quick Check:

    i = forward, j = left, k = backward [OK]
Hint: Remember i=forward, j=left, k=backward keys [OK]
Common Mistakes:
  • Mixing up 'j' and 'l' keys for turning
  • Assuming 'k' stops movement
  • Confusing forward and backward keys
4. You run ros2 run teleop_twist_keyboard teleop_twist_keyboard but pressing keys does not move the robot. What is the most likely cause?
medium
A. The robot is not connected or not receiving velocity commands
B. The keyboard keys are incorrect; use arrow keys instead
C. The teleop_twist_keyboard node must be launched with a launch file
D. ROS 2 is not installed properly

Solution

  1. Step 1: Check common reasons for no robot movement

    If keys are pressed but robot doesn't move, often the robot is not connected or not subscribed to velocity commands.
  2. Step 2: Evaluate other options

    Keyboard keys are correct as per documentation; launch file is not required; ROS 2 installation issues would prevent node running.
  3. Final Answer:

    The robot is not connected or not receiving velocity commands -> Option A
  4. Quick Check:

    No movement = no connection [OK]
Hint: Check robot connection before blaming keys or launch files [OK]
Common Mistakes:
  • Assuming arrow keys must be used
  • Thinking launch file is mandatory
  • Blaming ROS 2 install without checking connection
5. You want to increase the robot's forward speed using teleop_twist_keyboard. Which method correctly achieves this?
hard
A. Press 'i' key twice quickly to double speed
B. Press 'w' key to increase speed while running teleop_twist_keyboard
C. Restart teleop_twist_keyboard with a higher speed parameter
D. Edit the robot's URDF file to increase max speed

Solution

  1. Step 1: Understand teleop_twist_keyboard speed control

    The node allows speed adjustment during runtime using 'w' and 'x' keys to increase or decrease linear speed.
  2. Step 2: Evaluate other options

    Editing URDF changes robot model, not teleop speed; restarting with parameters is possible but less direct; pressing 'i' twice does not increase speed.
  3. Final Answer:

    Press 'w' key to increase speed while running teleop_twist_keyboard -> Option B
  4. Quick Check:

    Use 'w' to increase speed live [OK]
Hint: Use 'w' and 'x' keys to adjust speed live [OK]
Common Mistakes:
  • Trying to speed up by pressing movement keys repeatedly
  • Editing URDF for speed instead of teleop parameters
  • Restarting node instead of adjusting speed live