Skip to content

Commit 190e4ec

Browse files
committed
Update PS3 controller tutorial for python3
Fixed up the sample code for python3. Also fixed inconsitent capitalization of words and a few other minor things. Fixes ev3dev#321
1 parent ae89b5f commit 190e4ec

1 file changed

Lines changed: 24 additions & 23 deletions

File tree

docs/tutorials/using-ps3-sixaxis.md

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,45 @@ author: "@antonvh"
88
{:toc}
99

1010
The cool thing about the PS3 gamepad is that it's a normal Bluetooth device and
11-
connects directly to the EV3. You can easily run programs in brickman and use
12-
the pad without another computer or laptop.
11+
connects directly to the EV3. You can easily run programs in Brickman and use
12+
the gamepad without another computer or laptop.
1313

1414
# What you need
1515
- A PS3 gamepad (also known as Sixaxis controller or Dualshock 3)
16-
- A mini-usb / usb cable
17-
- A working ssh and internet connection to the Ev3 (or other ev3dev device)
18-
- ev3-ev3dev-jessie-2015-12-30.img or later
16+
- A USB mini-B to USB A cable (like the one that comes with the EV3)
17+
- A working ssh and internet connection to the EV3 (or other ev3dev device)
18+
- ev3-ev3dev-jessie-2016-12-21.img or later
1919

2020
# Connection
2121
The PS3 pairing process in Brickman is a little strange, but works fine. Stick
2222
exactly to these steps:
2323

24-
1. On the Ev3 brick go to 'Wireless and Networks' > 'Bluetooth'
24+
1. On the EV3 brick go to 'Wireless and Networks' > 'Bluetooth'
2525
2. Make sure Bluetooth is Powered and the brick is Visible.
26-
3. Connect the gamepad via a mini usb cable to the Ev3. I used the large usb
27-
port next to the micro SD slot.
26+
3. Connect the gamepad via a mini USB cable to the EV3. I used the large USB
27+
port next to the microSD slot.
2828
4. Under Devices a 'PLAYSTATION(R) 3 controller' should show up. But don't pair!
2929
4. Remove the USB cable again.
3030
5. Press the PS3 button on the gamepad.
3131
6. The brick now asks "Authorize service HID?" Press "Accept"
3232

3333
You're done! Whenever you press the PS3 button on the gamepad now, it will try
34-
to connect to the ev3 brick. Nice!
34+
to connect to the EV3 brick. Nice!
3535

36-
If brickman doesn't work or if you don't have a display, like on a BrickPi,
37-
`bluetoothctl` is the way to go. The gentoo linux guys wrote [a nice tutorial](https://wiki.gentoo.org/wiki/Sony_DualShock)
36+
If Brickman doesn't work or if you don't have a display, like on a BrickPi,
37+
`bluetoothctl` is the way to go. The Gentoo Linux guys wrote [a nice tutorial]
38+
(https://wiki.gentoo.org/wiki/Sony_DualShock)
3839

3940

4041
# Running motors with a PS3 sixaxis controller
41-
Now on to Python. In python we need the evdev (without a 3) to read gamepad
42+
Now on to Python. In python we need the `evdev` module (without a 3) to read gamepad
4243
events. Here's a quick program that will take the right stick Y axis and use it
4344
to set the speed of a motor in port A. Note that motor control is in a separate
4445
thread. That's because controlling the motors is much slower than reading the
4546
gamepad. Multithreading synchronizes both.
4647

47-
{% highlight python %}
48-
#!/usr/bin/env python
48+
```python
49+
#!/usr/bin/env python3
4950
__author__ = 'Anton Vanhoucke'
5051

5152
import evdev
@@ -61,15 +62,15 @@ def scale(val, src, dst):
6162
src: tuple
6263
dst: tuple
6364
64-
example: print scale(99, (0.0, 99.0), (-1.0, +1.0))
65+
example: print(scale(99, (0.0, 99.0), (-1.0, +1.0)))
6566
"""
6667
return (float(val - src[0]) / (src[1] - src[0])) * (dst[1] - dst[0]) + dst[0]
6768

6869
def scale_stick(value):
6970
return scale(value,(0,255),(-100,100))
7071

7172
## Initializing ##
72-
print "Finding ps3 controller..."
73+
print("Finding ps3 controller...")
7374
devices = [evdev.InputDevice(fn) for fn in evdev.list_devices()]
7475
for device in devices:
7576
if device.name == 'PLAYSTATION(R)3 Controller':
@@ -86,7 +87,7 @@ class MotorThread(threading.Thread):
8687
threading.Thread.__init__(self)
8788

8889
def run(self):
89-
print "Engine running!"
90+
print("Engine running!")
9091
while running:
9192
self.motor.run_direct(duty_cycle_sp=speed)
9293

@@ -103,19 +104,19 @@ for event in gamepad.read_loop(): #this loops infinitely
103104
speed = scale_stick(event.value)
104105

105106
if event.type == 1 and event.code == 302 and event.value == 1:
106-
print "X button is pressed. Stopping."
107+
print("X button is pressed. Stopping.")
107108
running = False
108109
break
109-
{% endhighlight %}
110+
```
110111

111-
Copy this code into a file on the Ev3 brick to run it. If you do
112-
`sudo chmod +x your_file_name.py`, you can even run it from the brickman interface!
112+
Copy this code into a file on the EV3 brick to run it. If you do
113+
`chmod +x your_file_name.py`, you can even run it from the Brickman interface!
113114

114-
# The complete event type and code mapping of the ps3 controller
115+
# The complete event type and code mapping of the PS3 controller
115116
I mapped out all codes for you! Here they are:
116117
{% include /util/screenshot.html source="/images/Website/sixaxis_event_codes.png" %}
117118

118119
# The result: a remote controlled robot
119-
How cool! No computer needed. Just a gamepad and the ev3 brick.
120+
How cool! No computer needed. Just a gamepad and the EV3 brick.
120121

121122
{% include /util/youtube-embed.html youtube_video_id="brfgF3D5c4k" %}

0 commit comments

Comments
 (0)