Use a Bluetooth Gamepad for Teleoperation
Install the ROS2 joy and teleop packages, create a custom package and launch file to read a Bluetooth gamepad, build the workspace and verify that velocity commands are published.
This module guides you through pairing a Bluetooth gamepad with your Raspberry Pi, installing the necessary ROS2 packages to interface with it, creating a new workspace and package to store your launch and configuration files, building the workspace and launching the teleoperation node.
Pair the Bluetooth Gamepad
Before installing ROS packages, pair your Bluetooth controller with the Raspberry Pi. You can use the command-line tool bluetoothctl:
bluetoothctl
# Inside bluetoothctl:
power on
agent on
default-agent
scan on
# Wait for your controller to appear in the list, note its MAC address
pair <MAC_ADDRESS>
connect <MAC_ADDRESS>
trust <MAC_ADDRESS>
exit
Alternatively, use the graphical Bluetooth manager available in the Raspberry Pi desktop environment. Once paired, the controller should automatically reconnect on subsequent boots.
sudo apt update
sudo apt install ros-${ROS_DISTRO}-joy ros-${ROS_DISTRO}-teleop-twist-joy
mkdir -p ~/ros2_ws/src
cd ~/ros2_ws/src
ros2 pkg create --build-type ament_cmake common_robot
cd common_robot
mkdir launch
mkdir config
Create a YAML configuration file under config/joystick.yaml with the mapping for your controller. The contents will vary by device; refer to the ROS2 teleop-twist-joy documentation for details. For example:
axis_linear: 1
axis_angular: 0
scale_linear: 0.5
scale_angular: 1.0
Create a launch file launch/joystick.launch.py that loads the joy and teleop_twist_joy nodes with your configuration file. An example launch file:
import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch_ros.actions import Node
def generate_launch_description():
config_file = os.path.join(
get_package_share_directory('common_robot'),
'config',
'joystick.yaml'
)
return LaunchDescription([
Node(
package='joy',
executable='joy_node',
name='joy_node'
),
Node(
package='teleop_twist_joy',
executable='teleop_node',
name='teleop_twist_joy',
parameters=[config_file],
remappings=[('/cmd_vel', '/${ROS_NAME}/cmd_vel')]
)
])
The get_package_share_directory function resolves the package installation path at runtime, ensuring the configuration file is located correctly regardless of where the package is installed.
cd ~/ros2_ws
colcon build --symlink-install
source install/setup.bash
ros2 launch common_robot joystick.launch.py