Skip to content

Using Python to connect the LEGO EV3 brick to a PS4 controller.

Notifications You must be signed in to change notification settings

codeadamca/ev3-python-ps4

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 

Repository files navigation

LEGO® Mindstorms® EV3, Pthon, and a PS4 Controller

A basic snippet to use vanillia Python to react to a PS4 controller events.

This code was written to connect a LEGO EV3 Brick to a PS4 controller. The EV3 is running ev3dev and running a version of Python called Pybricks. However, the code reacting to the PS4 controller events and the mapping of different buttons should apply to any Python/PS4 project.

This code assumes that the PS4 controller has already been paired.

Event Files

When the PS4 is paried with the device it creates three event files. These files are updated when the PS4 controller experiecnes an event (such as a button press).

Using a terminal check out the contents of the /dev/input folder before and after you pair the Bluetooth device. You should notice three new event files. On my device these files where:

  • /dev/input/event2 (touchpad events)
  • /dev/input/event3 (controller movement, like tilting, shaking, etc...)
  • /dev/input/event4 (buttons, sticks, etc...)

Each event provides five values, but we only need the event ID, code, and value. Here is a list of all events I could map:

Button and Stick Events

With my device, the button and stick events were found in /dev/input/event4. If you're working on a PS4 project, these are probably the events you're looking for.

EventIDCodePossible ValuesDescription
X Button43040 or 10 Released/1 Pressed
Circle Button43050 or 10 Released/1 Pressed
Triangle Burron43070 or 10 Released/1 Pressed
Square Button43080 or 10 Released/1 Pressed
Share Button43140 or 10 Released/1 Pressed
Options Button43150 or 10 Released/1 Pressed
PS Button43160 or 10 Released/1 Pressed
Left Stick Push43170 or 10 Released/1 Pressed
Right Stick Push43180 or 10 Released/1 Pressed
L143100 or 10 Released/1 Pressed
R143110 or 10 Released/1 Pressed
L243120 or 10 Released/1 Pressed
R243130 or 10 Released/1 Pressed
Left Stick Horizontal Axis300 to 2550 Left/127 Middle/255 Right
Left Stick Vertical Axis310 to 2550 Top/127 Middle/255 Bottom
L2 Axis320 to 2550 Released/255 Completely Pressed
Right Stick Horizontal Axis330 to 2550 Left/127 Middle/255 Right
Right Stick Vertical Axis340 to 2550 Top/127 Middle/255 Bottom
R2 Axis340 to 2550 Left/127 Middle/255 Right
Directional Pad Horizontal316-1, 0 or 1-1 Right/0 Released, 1 Left
Directional Pad Vertical317-1, 0 or 1-1 Right/0 Released, 1 Left

Note that the left and right sticks often trigger ongoing events if the controller has any drift. My controller would continuously send events for left horizontal stick movememt alternating between 127 and 128.

Controller Movement

Movement events were found in /dev/input/event3.

These events are triggered by physically moving or tilting the controller. I'm not as confident with these. I could not tell the difference with codes one and two.

EventIDCodePossible ValuesDescription
Left/Right Tilt308192 to -81928192 Tilted as Far Left/0 No Tilt/-8192 Tilted As Far Right
Towards/Away Tilt318192 to -81928192 Tilted as Far Towards/0 No Tilt/-8192 Tilted As Far Away
Towards/Away Tilt328192 to -81928192 Tilted as Far Towards/0 No Tilt/-8192 Tilted As Far Away
Vertical Speed33Any NumberNegative is Down, Positive is Up
Accelleration34Any NumberNegative is Slowing Down, Positive is Speeding Up
Timer45Any Positive NumberThis seems to be the time in micro seconds that the controller has been turned on

Touch Pad Events

Movement events were found in /dev/input/event2.

These events are triggered by using or pressing the touchpad on the PS4 controller. I could not figure out the difference between codes 252 and 230 or 333 and 47.

EventIDCodePossible ValuesDescription
Touchpad Press12720 or 10 Released/1 Pressed
Touchpad Touch13250 or 10 Finger Removed/1 Finger Touched
Touchpad Touch13300 or 10 Finger Removed/1 Finger Touched
Two Finger Touch13330 or 10 Second Finger Removed/1 Second Finger Touched
Two Finger Touch3470 or 10 Second Finger Removed/1 Second Finger Touched
Touchpad X3531 to 1919Horizontal location of the finger, 0 Left/1919 Right
Touchpad Y3541 to 941Vertical location of the finger, 0 Top/941 Bottom
Touch Counter357Any Positive Number0+ The number of times the touch pas has been touched/-1 Not currently touched

Sample Code

Reacting to an event would look like this:

# If a button was pressed or released
if ev_type == 1:

    # React to the X button
    if code == 304 and value == 0:
        print("The X button was released")
    elif code == 304 and value == 1:
        print("The X button was pressed")

This is assuming the events file has been opened and a while loop has been initiated (see main.py or tank.py).


Repo Resources

About

Using Python to connect the LEGO EV3 brick to a PS4 controller.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages