Gamepad Controls

This tutorial will step through how to map install the necessary libraries to map your gamepad buttons for controlling the robot.

Installing Libraries

First of all we need to install the necessary libraries to ensure we can recognise the inputs. The main library we will be using is called evdev. evdev is a generic input event interface in the Linux kernel, it geralises raw input events from device drivers and makes them available through character devices in the /dev/input directory. This makes it perfect when we wish to use 3rd Party gamepads to control our robots.

Start by opening terminal on your Raspberry Pi and installing the following libraries.

sudo apt-get install python-dev
sudo apt-get install python-pip
sudo pip install evdev

Finding input events

Depending on the USB port and number of I/O devices you have plugged into your raspberry pi your gamepad will display differently. In the terminal you can type the following command to see all of the input events.

ls /dev/input

If you plug your USB in and out and repeat the code you should be able to see one of the events drop off the list. Once you know which event the gamepad is linked to you can type in the following command in terminal to see the raw inputs which we need to translate.

cat /dev/input/event0

Given that the Raspberry Pi 2 has four USB ports we can assume that the gamepad would be attached on one of four events so we can create a function to cycle through them and locate the correct controller.

from evdev import InputDevice, categorize, ecodes, KeyEvent
def find_controller():
event0 = InputDevice('/dev/input/event0')
event1 = InputDevice('/dev/input/event1')
event2 = InputDevice('/dev/input/event2')
event3 = InputDevice('/dev/input/event3')
controller_list = ["Logitech Gamepad F710", "Logitech Gamepad F310"]
for controller in controller_list:
if == controller:
gamepad = event0
elif == controller:
gamepad = event1
elif == controller:
gamepad = event2
elif == controller:
gamepad = event3
print("controller not found")
return gamepad
gamepad = find_controller()

Reading input

Once we have found our gamepad we can easily output the gamepad events. This will output all of the inputs form the gamepad.

gamepad = find_controller()
for event in gamepad.read_loop():

Key State

evdev allows us to detect the key state (Key Up or Key Down) and map various key codes. With this code you can map the individual key codes for the A, Y, B, X and Right Thumb buttons.

for event in gamepad.read_loop():
if event.type == ecodes.EV_KEY:
keyevent = categorize(event)
if keyevent.keystate == KeyEvent.key_down:

Event Properties

You would had noticed that some of the buttons aren’t mapped automatically so we need to look at the various properties of the events which will allow us to map the specific controller.

#unique code
for event in gamepad.read_loop():
for event in gamepad.read_loop():

Controlling your robot

for event in gamepad.read_loop():
x = event.code
if x == 2:
print('left wheels fw')
elif x == 5:
print('right wheels fw')
elif x == 310:
print('left wheels back')
elif x == 311:
print('right wheels back')
elif x == 3:
y = event.value
if y > 0:
print('pan right')
print('pan left')
elif x == 0:
y = event.value
if y > 0:
print 'left +', y
move(y/40, 'right', False)
elif y < 0: print 'right +', (y*-1) move((y*-1)/40, 'left', False) elif x == 1: y = event.value if y > 0:
print 'back', y
print 'fwd', (y*-1)
elif x == 4:
y = event.value
if y > 0:
print('tilt fwd')
print('tilt back')
elif x == 314:
elif x == 315:
print('servo reset')

Gamepad Controls
Tagged on: