Gamepad Remote Control Tutorial

From Waveshare Wiki
Jump to: navigation, search

Install Gamepad Driver

sudo apt-get install jstest-gtk

Insert the gamepad signal transmitter into the USB connector of the Raspberry Pi, turn on the gamepad, and check whether the gamepad is connected.

ls /dev/input/

If the output is "js0", it means the gamepad is connected.
Change interface group and permissions:

sudo chgrp dialout /dev/input/js0

Check whether the modification is correct:

ls -l /dev/input/js0

The final permission and result are shown below:
Picture 1-02.png
Use the "jstest" command to check whether the gamepad is working, and test the corresponding numbers of each key on the wireless gamepad.

sudo jstest /dev/input/js0

Press each key on the gamepad and observe whether the output has changed.

Develop Gamepad Remote Control Function Pack

First, install the ros 2 remote control function pack of the Raspberry Pi:

sudo apt-get install ros-humble-joy

Enter the workspace and create the function pack:

cd ~/ugv02/src
ros2 pkg create ugv02_teleop

Create the configuration directory and launch directory:

cd /ugv02_teleop
mkdir config launch

And then create "ugv02_ps3.config.yaml" file in the "/ugv02/src/ugv02_teleop/config" to configure the remote control commands:

teleop_twist_joy_node:
  ros__parameters:
    axis_linear:  # The value of remote sensing index 1 represents the control for linear. You can observe the axes numbers by using the command "rostopic echo joy"
      x: 1
      y: 0
    scale_linear: # Low-speed motion linear maximum
      x: 0.25
      y: 0.25
    scale_linear_turbo: # The maximum value for high-speed linear motion
      x: 0.6

    axis_angular:  # Remote sensing index 0's indicate angular direction
      yaw: 3
    scale_angular:  # Maximum low-speed motion angular value
      yaw: 1.0
    scale_angular_turbo:  # Maximum high-speed motion angular value
      yaw: 1.2

    enable_button: 0       # Low speed, prevent accidental joystick touches, the left joystick control will only be triggered when the A button is pressed
    enable_turbo_button: -1 # High speed, prevent accidental joystick touches, the left joystick control will only be triggered when the B button is pressed

Next, you can create a "ugv02_teleop.launch.py" file in the "/ugv02/src/ugv02_teleop/launch", and compile the code for turning on the control nodes for the gamepad control

import os

from ament_index_python.packages import get_package_share_directory

import launch
import launch_ros.actions

def generate_launch_description():
    joy_config = launch.substitutions.LaunchConfiguration('joy_config')
    joy_dev = launch.substitutions.LaunchConfiguration('joy_dev')
    config_filepath = launch.substitutions.LaunchConfiguration('config_filepath')

    return launch.LaunchDescription([
        launch.actions.DeclareLaunchArgument('joy_vel', default_value='cmd_vel'),
        launch.actions.DeclareLaunchArgument('joy_config', default_value='ugv02_ps3'),   # Gamepad configuration file name 
        launch.actions.DeclareLaunchArgument('joy_dev', default_value='/dev/input/js0'), # The connected serial port of the gamepad 
        launch.actions.DeclareLaunchArgument('config_filepath', default_value=[
            launch.substitutions.TextSubstitution(text=os.path.join(
                get_package_share_directory('ugv02_teleop'), 'config', '')),
            joy_config, launch.substitutions.TextSubstitution(text='.config.yaml')]),

        launch_ros.actions.Node(
            package='joy', executable='joy_node', name='joy_node',
            parameters=[{
                'dev': joy_dev,
                'deadzone': 0.3,
                'autorepeat_rate': 20.0,
            }]),
        launch_ros.actions.Node(
            package='teleop_twist_joy', executable='teleop_node',
            name='teleop_twist_joy_node', parameters=[config_filepath],
            remappings={('/cmd_vel', launch.substitutions.LaunchConfiguration('joy_vel'))},
            ),
    ])

Add in the "CMakeLists.txt":

install(
  DIRECTORY config launch
  DESTINATION share/${PROJECT_NAME}
)

Note: only when the A button is pressed can the left joystick be triggered. Usage: turn on the chassis first, and then open the terminal.

sudo chgrp dialout  /dev/input/js0
ros2 launch ugv02_teleop ugv02_teleop.launch.py