Frindo

Frindo/Projects/ROS 2 robot

ROS 2 robot, part 2: add a game controller

Keyboard driving works, but each key press only sends one fixed speed, so the robot lurches from one speed to the next. A game controller gives you proportional control: push the stick a little and the robot creeps, push it all the way and it goes at full speed, and a diagonal push drives a smooth curve. It also gives you a hold-to-drive button: the robot only moves while you hold it, and stops as soon as you let go.

In this part you connect a PlayStation DualSense controller to the Raspberry Pi over Bluetooth, and start everything with one command.

Allow an hour.


What you need

You need a Frindo running ROS 2, finished up to the end of part 1.

Parts to buy

Item Qty Step
Sony PlayStation 5 DualSense wireless controller 1 2
USB-C cable to charge the controller 1 2

The controller connects straight to the Pi's built-in Bluetooth, so you need nothing else on the robot. Charge it before you start.

Note

Other controllers can work with ROS 2, but their buttons and sticks are often numbered differently, and some will not pair with Linux over Bluetooth. This guide's settings are for the DualSense. If you use another controller, step 4 shows you how to find its numbers.


Step 1: Set up Bluetooth

Log in to the Pi from your computer, as in part 1:

ssh you@frindo.local

Ubuntu Server does not include the Bluetooth software, so install it first:

sudo apt install -y bluez

The DualSense also needs two Bluetooth settings changed. Out of the box, Linux pairs with the controller but then refuses to use it, and nothing tells you why. These commands make the two changes:

sudo sed -i -E 's/^#?ClassicBondedOnly=.*/ClassicBondedOnly=false/' /etc/bluetooth/input.conf
sudo sed -i -E 's/^#?Class = .*/Class = 0x000100/' /etc/bluetooth/main.conf

Check both took effect. Each command should print one line, without a # at the start:

grep ClassicBondedOnly /etc/bluetooth/input.conf
grep "^Class" /etc/bluetooth/main.conf

Note

ClassicBondedOnly=false lets Bluetooth input devices connect without the strictest form of pairing, which the DualSense never completes with Linux. That is a sensible trade on a robot.

Make sure the controller driver loads every time the Pi starts:

echo hid_playstation | sudo tee /etc/modules-load.d/hid-playstation.conf

Finally, give yourself permission to read game controllers. Without this, ROS 2 cannot see the controller at all, and again nothing tells you why:

sudo usermod -aG input $USER
sudo reboot

Wait a minute, then log in again.


Step 2: Pair the controller

If the controller has been paired with a PlayStation, phone or computer before, turn Bluetooth off on that device first. Otherwise the controller keeps trying to connect to it instead of the Pi.

Start the Bluetooth tool. Your prompt changes to [bluetooth]#:

bluetoothctl

Type these, one per line:

power on
pairable on
agent on
default-agent
scan on

Now put the controller into pairing mode: hold down the Create button (the small button to the left of the touchpad) and the PS button together, until the light around the touchpad flashes quickly in pairs.

Within a few seconds a line appears with the controller's address and the name DualSense Wireless Controller, for example:

[NEW] Device 7C:66:EF:12:34:56 DualSense Wireless Controller

Use your own controller's address in place of 7C:66:EF:12:34:56 below:

scan off
pair 7C:66:EF:12:34:56
trust 7C:66:EF:12:34:56
connect 7C:66:EF:12:34:56
exit

The light on the controller should stop flashing and stay on. trust means the Pi remembers the controller, so you only pair it once.

Tip

To reconnect later, press the PS button on its own. Don't hold Create as well: that puts the controller back into pairing mode, where it waits for a new device and won't reconnect to the Pi.


Step 3: Install the controller software

Two standard ROS 2 packages do the work:

sudo apt install -y ros-jazzy-joy ros-jazzy-teleop-twist-joy
Package What it does
joy Reads the controller and publishes its sticks and buttons on the ROS 2 topic /joy
teleop_twist_joy Turns /joy into speed commands on /cmd_vel, which the MC1 driver follows

Neither package knows anything about Frindo or MC1. The connection between them is the /cmd_vel topic, which is the standard way to tell any ROS 2 robot how fast to go.

If you downloaded the Frindo files in part 1 before this guide was published, update them now:

cd ~/frindo && git pull && cd ~

Step 4: Check the Pi can see the controller

ros2 run joy joy_enumerate_devices

You should see a heading and one row naming the controller:

ID : GUID                             : GamePad : Mapped : Joystick Device Name
 0 : 05004c05...                      :    true :   true : PS5 Controller

A heading with no row under it means you are not in the input group yet. Check the last command in step 1, and reboot.

Now check the stick and button numbers. Start the controller reader in one terminal:

ros2 run joy joy_node --ros-args -p autorepeat_rate:=20.0

Open a second terminal, log in to the Pi, and watch what it reads:

ros2 topic echo /joy

First look at it with your hands off the controller, so you know what "nothing pressed" looks like. The two triggers rest at 1.0 rather than 0, which is normal.

Then check these, which are the ones Frindo's settings use:

Do this You should see
Push the left stick forward axes position 1 goes up towards 1.0
Push the left stick left axes position 0 goes up towards 1.0
Hold R1 buttons position 5 changes to 1
Hold L1 buttons position 4 changes to 1
Press Options (the small button to the right of the touchpad) buttons position 9 changes to 1

Positions are counted from 0, so the first number in each list is position 0.

If your numbers are different, for example because you are using another controller, edit ~/frindo/projects/ros2/files/frindo_gamepad.yaml to match. For a different reset button, see the start of frindo_pad_reset.py in the same folder. If pushing the stick forward makes the number go down, put a minus sign in front of both linear scale values in that file.

Press Ctrl-C in both terminals when you have finished.


Step 5: Drive the robot

For the first run, stand the robot on its block so the wheels are off the table.

Start everything with one command:

ros2 launch ~/frindo/projects/ros2/files/frindo_gamepad.launch.py

This starts the MC1 driver, the controller reader, the speed converter and the reset button, and switches the motors on after 5 seconds. It also puts MC1 into its faster NORMAL mode, so Frindo can reach its full speed.

Control Does
Hold R1 Drive. Nothing moves unless you are holding it
Left stick Forward, backward and turning, all on one stick
Hold L1 as well Turbo, the robot's full speed
Press Options Reset: switches the motors back on after a fault. See the next section

With the left stick:

Once everything works on the block, put the robot on the floor and drive. Press Ctrl-C to stop everything, and shut down as in part 1 when you have finished.

Next: part 3, adding a camera, is coming soon.


If the robot stops and MC1's red light flashes

MC1 has found a problem and stopped the motors on purpose, for example because a wheel was blocked or the battery is low. This is called a fault. MC1 keeps the motors off until it is told to carry on, so the robot can't suddenly drive off again by itself. Holding R1 does nothing until then.

  1. Find out why. Look at the terminal where you started the launch file. A red line appears when the fault happens:

    [ERROR] [frindo_pad_reset]: MC1 FAULT ENCODER_STALL: the motors are stopped. ... Press Options to reset

    The word after MC1 FAULT is the reason. The Faults table in part 1 says what each one means and what to check.

  2. Deal with the cause, for example free a jammed wheel or charge the battery. If you don't, the same fault comes straight back.

  3. Press Options on the controller. The terminal shows Reset: motors switched back on and MC1's light goes back to normal. Hold R1 and drive on.

Warning

Don't press the reset button on MC1 itself. It restarts MC1, which cuts its USB connection, and the driver does not reconnect by itself, so nothing works until you restart the launch file. If you have pressed it, press Ctrl-C and run the ros2 launch command again.


How it keeps the robot safe

The hold-to-drive button. When you let go of R1, teleop_twist_joy sends a stop and the robot stops.

But that only works while the controller and the software are still working. If the controller goes to sleep, runs flat, or drops its Bluetooth connection, it simply goes quiet. Nothing sends a stop, and the robot would carry on with the last command it was given.

So the launch file also turns on the driver's watchdog. If no command arrives for half a second, the driver stops the robot. The controller reader repeats the controller's state 20 times a second, even when you hold the stick still, so while everything works the watchdog never gets in the way.

The DualSense goes to sleep by itself after a few minutes without use. That is the watchdog's everyday job, not a rare case. Press the PS button to wake it up.

Note

In part 1, keyboard driving runs without the watchdog, because the keyboard sends one command per key press and the watchdog would stop the robot between presses. With a controller, always keep it on.

The watchdog covers software and wireless problems. The real emergency stop is still the robot's switch, or an emergency stop button on J3 if you have fitted one.


If something doesn't work

What you see What to do
joy_enumerate_devices shows a heading but no controller Not in the input group. See the end of step 1, and reboot
Paired and connected, but no controller appears The Bluetooth settings in step 1 did not take effect. Check both grep commands, then sudo systemctl restart bluetooth and reconnect
The controller will not pair Turn off Bluetooth on any other device it has paired with, then try step 2 again
The controller won't reconnect Press only the PS button. Holding Create and PS together puts it into pairing mode instead. If PS alone doesn't work, check you ran trust in step 2
The robot does nothing Are you holding R1? Check buttons position 5 changes in step 4
The robot stops while you hold the stick The controller has gone to sleep or lost its connection. Press PS. If it keeps happening, check the controller's battery
The robot turns the wrong way Put a minus sign in front of both angular scale values in frindo_gamepad.yaml. Do not change the motor or encoder direction settings, which are already right
The robot creeps with nobody touching the controller The stick does not quite centre. Raise deadzone in the launch file from 0.10 to 0.15
Turbo makes no difference MC1 is in SLOW mode, which limits power whatever you ask for. The launch file sets NORMAL, so check it started without errors
The robot stops and MC1's red light flashes A fault. See If the robot stops and MC1's red light flashes
Options does nothing Check buttons position 9 changes when you press it, as in step 4. If you see The MC1 driver is not running, the driver has stopped: press Ctrl-C and launch again
Nothing works after pressing MC1's reset button, or after unplugging its USB cable The driver has lost its connection and does not reconnect by itself. Press Ctrl-C and launch again
The motors did not switch on The switch-on command ran before the driver connected. Press Options
/dev/mc1 errors, or other driver problems See the table at the end of part 1

This guide is also on GitHub, released under CC BY-SA 4.0. Spotted a mistake? hello@frindo.org