Connect PS3 Joystick to Raspberry 4b with ROS

Javier Eduardo Epeloa
7 min readDec 31, 2020

Example of usage of the packages teleop_twist_joy and joystick driver for ubuntu mate to connect a PS3 joystick and interact with real or simulated robots in ROS.

In this article I will explain how connect a PS3 joystick to a Raspberry Pi 4b (with 4gb of ram) to obtain a remote controller for your robot and configure them to boot up automaticaly when the RPi power up.

There are thousands of tutorials on Internet (google it!) about how install ROS on Raspberry 4b and how you can connect a PS3 joystick using ROS with teleop_twist_joy and joystick_drivers nodes, so i’m going to skip the installation part to focus on the hardware selection to build the node controller on ROS and the neccesary modifications to the configuration files to boot up it automaticaly.

In the links bellow there are the information to install ROS on a Raspberry PI 4b and the necessary packages for install the PS3 controller.

In this link there is the complete image file of an Ubuntu 18.04 with ROS melodic, the joystick_driver and teleop_twist_joy working and ready to use on a Raspberry Pi 4. The image are divided into 3 files, to restore the original, download them and in the same folder execute the next command:

cat joy_img* > joystick.img

(https://wiki.ros.org/teleop_twist_joy)

(http://wiki.ros.org/joystick_drivers)

Hardware selection

Tha main objetive is create a device that can work like a remote controller. The device must works with your own power source with a battery to power the RPi 4 for a couple of hours, all the parts must located inside of a box to protect the hardware.

I selected a plastic watertight box for insulated the hardware.

Fig 1. plastic watertight box

For the battery i’m used a power bank of 15000mAh (Gadnic)

Fig 2. Power bank

The Gadnic power bank have two USB connectors that can be used to charge external devices, in my case I used one of them to power the RPi. The micro USB connector of the Gadnic power bank is located in the middle of the USB ports and it’s used for charging the battery, I connected this port with a wire and pass them through the box to charge the device using a cell phone charger.

To cut the power of the RPi, I installed a swich in a lateral of the box. For installing this swich it’s necessary cut the wires of the micro USB and soldier them (red an black in the most of the cases, if you have doubts measure with a tester to find the power wires). The final circuit it’s very basic, but for avoid misunderstandings the schematic is bellow:

Fig 3. Schematic of the controller node
Fig 4. Final device with RPi 4 and battery mounted

Running the node and test it!

In order to install the RPi 4 image with ROS and the nodes to work with the PS3 joystick, you can use balenaEtcher (https://www.balena.io/etcher/) with the image that I shared on the top of this article. The image works perfect in a 32GB memory card. The only care that you have to take is select the correct target device, ¡must be the corresponding to the SD card!

Fig 5. Flashing SD card

Once you have flashed the SD image, insert the micro SD card into the RPi 4, connect the PS3 Joystick and power on the swich. The RPi 4 red led must turn on, and seconds after the green led will start blynking. After the RPi 4 boot up (in the most of the cases the time that you need to wait is less than 1 minute after power on the RPi 4 ) you can press the PS3 button and the leds in the upper part of the controller must stop blinking and only the led “1” still on. If that happens, means that the joystick it’s connected and working on!.

For this part, you need to connect the RPi 4 to a monitor or using ssh to remote access. If yo don’t know how can set up the WiFi connection and configuring ssh using the SD card, this tutorial works.

For the next steps I will assume that you can login into raspberry (user:pi ; pass: raspberry), type the next command in a console:

ssh -l pi <IP>

Where <IP> is the asigned by the router that the RPi4 is connected.

Configuration files

There is a configuration file inside of the config folder of the twist_teleop_joy package. To access to this folder you can type the following commands:

roscd teleop_twist_joy
cd config

Inside of this folder there is a file called ps3.config.yaml, you can access and edit this file using the following command:

sudo mcedit ps3.config.yaml
Fig 6. ps3.config.yaml file

You can use your favorite editor to see the content of this file if don’t like the midnigth commander.

The line scale_linear permit to modify the range of the linear velocity, and the scale_angular the angular velocity. In the case of this file the scale angular is set to -2. The minus value produce an inverted angular axis scale.

Automatic boot up

The RPi 4 imge was configure to auto upload the teleop_twist_joy package. For do that there is a file inside /lib/systemd/system called robot.service, for check the content type:

sudo mcedit robot.service
Fig 7. robot.service file

The most important part of this file is in the line bellow [service]. Looking in detail this part, this file call other in “/home/pi/joystick.sh” using the user “pi

For start/stop this service you can type:

sudo systemctl <star/stop> robot.service

You can use “start” or “stop” in the command above. If you wish deactivate/activate the robot.service type:

sudo systemctl <disable/enable> robot.service

by defect the robot.service is enabled and launch the teleop_twist_joy when the RPi 4 boot.

The Joystick.sh file is the responsible of launch the ROS teleop_twist_joy node, for see the content type:

sudo mcedit Joystick.sh
Fig 8. Joystick.sh file

The script contents two commands:

source /opt/ros/melodic/setup.bash
roslaunch --wait teleop_twist_joy teleop.launch

The first command it’s necessary to source the console and the second command launch the node. Look the parameter “- -wait” that is use to wait that the master node upload (in the case of the joystick node, the master node is configured in the robot, so this parameter permit to wait that the master node wake up)

Configuring the Network

I will assume that you need control a robot in where ROS is running, so the question is ¿how can connect the joystick with the master node?, to do that it’s necessary tell to ROS where is the master node. There are two ways to indicate the master node to ROS, one is typing in the console:

export ROS_MASTER_URI=http://<IP>:11311

The export command tell to ROS that the master node is located in a specific <IP> address over the network.

The other way is editing the file “/opt/ros/melodic/setup.bash” and adding the export command to the end of this file. I’m used this option because the Joystick.sh file, are called when the RPi 4 boot, automatically execute the source command and in consequence executes the export command.

The controller can be used to move a real or simulated robot. For testing it you can use a simulated robot and when all is running and working propelly use in a real situation with a real robot.

You can check in the ROS master node if the /cmd_vel topic is receiving something using the following command:

rostopic echo /cmd_vel

If all works fine you will see an output similar to this:

Fig 9. Output of /cmd_vel topic

This is the output of the /cmd_vel topic, in this case the node are publishing all zeros because nothing is sending by the controller, but if you hold the “X” button of the controller and move the left axis of the joystick you can see how the numers changes, this is the final indicator that all works fine! (this SD card image is configured like non-holonomic robot)

Controlling simulated robot with gazebo

The next video is an example of control a simulated robot with ROS and gazebo. I have a robot model running on gazebo in my notebook and the Joystick module connect to them.

The model running on the notebook with IP:192.168.1.100, and the Joystick have the IP:192.168.1.101. In the file /opt/ros/melodic/setup.bash I have setting the export line to the notebook IP:192.168.100.

If all works fine you can move the robot on the screen with the left stick of the controller!

--

--