Skip to main content

Robot controlled with a gamepad

Robot and gamepad

For simple control of a mobile robot, it is definitely worth using a gamepad. Even for people who have no experience with computer games, pad control will be very intuitive. It will enable precision and greater control over wheel speeds, which will increase safety for the robot itself and its surroundings. In this article, we will discuss how to control it easier than, for example, from the keyboard with teleop_twist_keyboard.

QUICK NOTE

This article is based on a publicly available joy2twist repository combining gamepad control in the Robot Operating System 2 (ROS 2).

Joy to Twist

Joy2Twist package creates a ROS 2 node, allowing control the ROS-powered mobile and manipulation robots with Logitech F710 gamepad. Joy2Twist node converts sensor_msgs/Joy message to geometry_msgs/Twist in order to provide velocity commands for the robot. Therefore, this package is compliant with any other gamepad controllers which are able to publish the sensor_msgs/Joy message.

Quick start guide

  1. Connect a gamepad USB receiver to the USB port of robot.

  2. Make sure the gamepad is in DirectInput Mode (switch in front o the pad with letters D and X -> select D).

  3. Download joy2twist Docker image in the robot:

docker pull husarion/joy2twist:humble
  1. Create a configuration file:
nano joy2twist.yaml
  1. Paste the content inside save quit
joy2twist.yaml
/**:
ros__parameters:
linear_velocity_factor:
fast: 1.0
regular: 0.5
slow: 0.2

angular_velocity_factor:
fast: 1.0
regular: 0.5
slow: 0.2

# This button mapping should be adjusted to the specific controller
# The following map is suited for Logitech F710
button_index_map:
axis:
angular_z: 0 # Left joystick
linear_x: 3 # Right joystick
linear_y: 2 # Right joystick
dead_man_switch: 4 # LB
fast_mode: 7 # RT
slow_mode: 5 # RB
  1. Press <ctrl + o> and then <ctrl + x> to save and quit.

  2. Run the following Docker command:

docker run -d \
--name joy2twist \
--restart unless-stopped \
--device /dev/input \
--volume $PWD/joy2twist.yaml:/joy2twist.yaml \
husarion/joy2twist:humble \
ros2 launch joy2twist gamepad_controller.launch.py joy2twist_params_file:=/joy2twist.yaml

gamepad-legend

ROSbot demo quick start

  1. Steps 1-3 are same as above.

  2. Clone the joy2twist repository:

git clone https://github.com/husarion/joy2twist.git
  1. Create and modify if needed (see comments) the .env file based on the .env.template file:
cd joy2twist/demo
cp .env.template .env
nano .env
  1. Run the example:
docker compose -f compose.rosbot.yaml up
QUICK NOTE

In case of any problems visit joy2twist repository for more information.

ROS API

In order for your robot to work with the joy2twist package, it must work on the following ROS topics for full compatibility. The joy2twist ROS 2 node is translating joy topic to cmd_vel topic with the following interface:

Publishes

  • /cmd_vel (geometry_msgs/Twist)

Subscribes

  • /joy (sensor_msgs/Joy)

Parameters

Following parameters change the joystick's axes mapped to given robot axes of freedom. For more information about parameter values, refer to the joy package wiki page.

  • ~axis_linear_x (int, default: 3)
  • ~axis_linear_y (int, default: 2)
  • ~axis_angular_z (int, default: 0)

The robot can be operated at 3 scales of speed depending on pressed buttons. It's possible to adjust velocity scaling factors using a config file. The Units are m/s for linear movement and rad/s for angular movement.

  • fast (float, default: 1)
  • regular (float, default: 0.5)
  • slow (float, default: 0.2)

Button mapping

ButtonFunction
LBenable driving
RBslow driving mode
RTfast driving mode

Gamepad with ROS 2 - examples

The basic premise of the technology is that most gamepads operate on Bluetooth wireless transmission. Robots, on the other hand, can be equipped with Bluetooth receivers that allow for remote control using a device such as a gamepad. Depending on the robot model, control can include a variety of functions, such as movement, object manipulation, or even executing simple programming tasks.

In some examples and repositories a gamepad can be used to control the ROSbot. For example:

Left bumper (Enable Movement) is used as a dead man's switch, acting analogously to teach pendants in industrial robots. To make a move, this button must be pressed all the time.

QUICK NOTE

Tested with the Logitech F710 gamepad together with ROSbot 2R, ROSbot 2 PRO and ROSbot XL.

Summary

In summary, controlling robots with gamepads is an interesting technology that can have many applications. It can be used for learning programming and robot control, as well as for improving safety in dangerous places or simply having fun. The joy2twist package is a great example of the application of the ROS environment in mobile robotics in practice.


by Jan Brzyk Husarion

Do you need any support with completing this project or have any difficulties with software or hardware? Feel free to describe your thoughts on our community forum: https://community.husarion.com/ or to contact with our support: support@husarion.com