Skip to content

Commit a71cd7d

Browse files
committed
Added sample code
1 parent 846433c commit a71cd7d

3 files changed

Lines changed: 208 additions & 11 deletions

File tree

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,23 @@ These events are triggered by using or pressing the touchpad on the PS4 controll
8181
<tr><td>Touch Counter</td><td>3</td><td>57</td><td>Any Positive Number</td><td>0+ The number of times the touch pas has been touched/-1 Not currently touched</td></tr>
8282
</table>
8383

84+
## Sample Code
85+
86+
Reacting to an event would look like this:
87+
88+
'''python
89+
# If a button was pressed or released
90+
if ev_type == 1:
91+
92+
# React to the X button
93+
if code == 304 and value == 0:
94+
print("The X button was released")
95+
elif code == 304 and value == 1:
96+
print("The X button was pressed")
97+
'''
98+
99+
This is assuming the events file has been opened and a while loop has been initiated (see main.py or tank.py).
100+
84101
<a href="https://codeadam.ca">
85102
<img src="https://codeadam.ca/images/code-block.png" width="100">
86103
</a>

main.py

Lines changed: 128 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,137 @@
11
#!/usr/bin/env pybricks-micropython
2-
from pybricks.hubs import EV3Brick
3-
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor,
4-
InfraredSensor, UltrasonicSensor, GyroSensor)
2+
from pybricks.ev3devices import (Motor)
53
from pybricks.parameters import Port, Stop, Direction, Button, Color
64
from pybricks.tools import wait, StopWatch, DataLog
7-
from pybricks.robotics import DriveBase
8-
from pybricks.media.ev3dev import SoundFile, ImageFile
95

6+
import struct
107

11-
# This program requires LEGO EV3 MicroPython v2.0 or higher.
12-
# Click "Open user guide" on the EV3 extension tab for more information.
13-
8+
# This program uses the two PS4 sticks to control two EV3 Large Servo Motors using tank like controls
149

1510
# Create your objects here.
16-
ev3 = EV3Brick()
11+
left_motor = Motor(Port.B)
12+
right_motor = Motor(Port.C)
13+
left_speed = 0
14+
right_speed = 0
15+
16+
# Locat the event file you want to react to, on my setup the PS4 controller button events
17+
# are located in /dev/input/event4
18+
infile_path = "/dev/input/event4"
19+
in_file = open(infile_path, "rb")
20+
21+
# Define the format the event data will be read
22+
# See https://docs.python.org/3/library/struct.html#format-characters for more details
23+
FORMAT = 'llHHi'
24+
EVENT_SIZE = struct.calcsize(FORMAT)
25+
event = in_file.read(EVENT_SIZE)
26+
27+
# A helper function for converting stick values (0 to 255) to more usable numbers (-100 to 100)
28+
def scale(val, src, dst):
29+
return (float(val - src[0]) / (src[1] - src[0])) * (dst[1] - dst[0]) + dst[0]
30+
31+
# Create a loop to react to events
32+
# This loop react to all main PS4 button and sstick events. I ave left out buttons like
33+
# share and options, but can easily be added in by referring to the table on the
34+
# GitHub page: https://github.com/codeadamca/python-connect-ps4
35+
while event:
36+
37+
# Place event data into variables
38+
(tv_sec, tv_usec, ev_type, code, value) = struct.unpack(FORMAT, event)
39+
40+
# If a button was pressed or released
41+
if ev_type == 1:
42+
43+
# React to the X button
44+
if code == 304 and value == 0:
45+
print("The X button was released")
46+
elif code == 304 and value == 1:
47+
print("The X button was pressed")
48+
49+
# React to the Circle button
50+
elif code == 305 and value == 0:
51+
print("The Circle button was released")
52+
elif code == 305 and value == 1:
53+
print("The Circle button was pressed")
54+
55+
# React to the Triangle button
56+
elif code == 307 and value == 0:
57+
print("The Triangle button was released")
58+
elif code == 307 and value == 1:
59+
print("The Triangle button was pressed")
60+
61+
# React to the Square button
62+
elif code == 308 and value == 0:
63+
print("The Square button was released")
64+
elif code == 308 and value == 1:
65+
print("The Square button was pressed")
66+
67+
# React to the L1 button
68+
elif code == 310 and value == 0:
69+
print("The L1 button was released")
70+
elif code == 310 and value == 1:
71+
print("The L1 button was pressed")
72+
73+
# React to the R1 button
74+
elif code == 311 and value == 0:
75+
print("The R1 button was released")
76+
elif code == 311 and value == 1:
77+
print("The R1 button was pressed")
78+
79+
# React to the L2 button
80+
elif code == 312 and value == 0:
81+
print("The L2 button was released")
82+
elif code == 312 and value == 1:
83+
print("The L2 button was pressed")
84+
85+
# React to the R2 button
86+
elif code == 313 and value == 0:
87+
print("The R2 button was released")
88+
elif code == 313 and value == 1:
89+
print("The R2 button was pressed")
90+
91+
elif ev_type == 3: # Stick was moved
92+
93+
# The sticks often trigger non-stop events so I have commented this out,
94+
# it back in to get stick positions
95+
'''
96+
# React to the left stick vertical
97+
if code == 1:
98+
print("The left stick vertical is at ",value)
99+
left_speed = scale(value, (0,255), (100, -100))
100+
101+
# React to the left stick horizontal
102+
elif code == 0:
103+
print("The left stick horizontal is at ",value)
104+
105+
# React to the right stick vertical
106+
elif code == 4:
107+
print("The right stick vertical is at ",value)
108+
right_speed = scale(value, (0,255), (100, -100))
109+
110+
# React to the right stick horizontal
111+
elif code == 3:
112+
print("The right stick horizontal is at ",value)
113+
'''
114+
115+
# React to the Directional pad
116+
if code == 16 and value == -1:
117+
print("The horizontal directional pad is left")
118+
elif code == 16 and value == 1:
119+
print("The horizontal directional pad is right")
120+
elif code == 16 and value == 0:
121+
print("The horizontal directional pad is released")
122+
123+
elif code == 17 and value == -1:
124+
print("The vertical directional pad is up")
125+
elif code == 17 and value == 1:
126+
print("The horizontal directional pad is down")
127+
elif code == 17 and value == 0:
128+
print("The horizontal directional pad is released")
129+
130+
# Set motor speed
131+
left_motor.dc(left_speed)
132+
right_motor.dc(right_speed)
17133

134+
# Read the next event
135+
event = in_file.read(EVENT_SIZE)
18136

19-
# Write your program here.
20-
ev3.speaker.beep()
137+
in_file.close()

tank.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env pybricks-micropython
2+
from pybricks.ev3devices import (Motor)
3+
from pybricks.parameters import Port, Stop, Direction, Button, Color
4+
from pybricks.tools import wait, StopWatch, DataLog
5+
6+
import struct
7+
8+
# This program uses the two PS4 sticks to control two EV3 Large Servo Motors using tank like controls
9+
10+
# Create your objects here.
11+
left_motor = Motor(Port.B)
12+
right_motor = Motor(Port.C)
13+
left_speed = 0
14+
right_speed = 0
15+
16+
# Locat the event file you want to react to, on my setup the PS4 controller button events
17+
# are located in /dev/input/event4
18+
infile_path = "/dev/input/event4"
19+
in_file = open(infile_path, "rb")
20+
21+
# Define the format the event data will be read
22+
# See https://docs.python.org/3/library/struct.html#format-characters for more details
23+
FORMAT = 'llHHi'
24+
EVENT_SIZE = struct.calcsize(FORMAT)
25+
event = in_file.read(EVENT_SIZE)
26+
27+
# A helper function for converting stick values (0 to 255) to more usable numbers (-100 to 100)
28+
def scale(val, src, dst):
29+
return (float(val - src[0]) / (src[1] - src[0])) * (dst[1] - dst[0]) + dst[0]
30+
31+
# Create a loop to react to events
32+
while event:
33+
34+
# Place event data into variables
35+
(tv_sec, tv_usec, ev_type, code, value) = struct.unpack(FORMAT, event)
36+
37+
# If a button was pressed or released
38+
if ev_type == 1:
39+
40+
# React to the X button
41+
if code == 304 and value == 0:
42+
print("The X button was released")
43+
if code == 304 and value == 1:
44+
print("The X button was pressed")
45+
46+
elif ev_type == 3: # Stick was moved
47+
48+
# React to the left stick
49+
if code == 1:
50+
left_speed = scale(value, (0,255), (100, -100))
51+
52+
# React to the right stick
53+
if code == 4:
54+
right_speed = scale(value, (0,255), (100, -100))
55+
56+
# Set motor speed
57+
left_motor.dc(left_speed)
58+
right_motor.dc(right_speed)
59+
60+
# Read the next event
61+
event = in_file.read(EVENT_SIZE)
62+
63+
in_file.close()

0 commit comments

Comments
 (0)