2015年4月13日 星期一

Bluetooth USB + RPi2 + PS3 Dualshock


新機體打算用 Raspberry Pi 2 作為主板。操控方面,原先打算用智能手機接駁 RPi 的 WiFi 熱點,然後透過在 RPi 上的網頁進行操作。可是擔心反應不夠快及會有訊號遺失的問題,所以在想有甚麼其他的可能性。編寫手機程式用 Socket 溝通是其中一個;利用機械人界慣常使用的 PS2 手掣又是一個。在尋找 RPi 對應 PS2 手掣的過程中,發現了 RPi 加上藍牙 2.0 USB 後便能直接跟 PS3 Sixaxis DualShock 通訊,不像 PS2 要加入其他線路,於是嘗試了一下。

首先進行更新:
pi@raspberrypi ~ $ sudo apt-get update
Get:1 http://mirrordirector.raspbian.org wheezy Release.gpg [490 B]
Get:2 http://raspberrypi.collabora.com wheezy Release.gpg [836 B]
Get:3 http://mirrordirector.raspbian.org wheezy Release [14.4 kB]
    ::    ::    ::    ::
Ign http://mirrordirector.raspbian.org wheezy/non-free Translation-en
Ign http://mirrordirector.raspbian.org wheezy/rpi Translation-en
Fetched 7001 kB in 23s (302 kB/s)
Reading package lists... Done

安裝藍牙相關的工具:
pi@raspberrypi ~/ps3_sixpair $ sudo apt-get install bluez-utils bluez-compat bluez-hcidump checkinstall libusb-dev  libbluetooth-dev joystick
Reading package lists... Done
Building dependency tree       
Reading state information... Done
    ::    ::    ::    ::
Setting up inputattach (1:1.4.3-1) ...
Setting up joystick (1:1.4.3-1) ...
Setting up libbluetooth-dev (4.99-2) ...

插入藍牙 USB 並檢查是否跟 RPi 對應。出現如以下文字代表對應:
pi@raspberrypi ~/ps3_sixpair $ hciconfig
hci0: Type: BR/EDR  Bus: USB
 BD Address: 11:11:11:11:11:11  ACL MTU: 678:8  SCO MTU: 48:10
 UP RUNNING PSCAN 
 RX bytes:1257 acl:0 sco:0 events:48 errors:0
 TX bytes:458 acl:0 sco:0 commands:48 errors:0

下載 PS3 手掣配對程式:
pi@raspberrypi ~ $ mkdir ps3_sixpair
pi@raspberrypi ~ $ cd ps3_sixpair/
pi@raspberrypi ~/ps3_sixpair $ wget http://www.pabr.org/sixlinux/sixpair.c
--2015-04-10 22:23:34--  http://www.pabr.org/sixlinux/sixpair.c
Resolving www.pabr.org (www.pabr.org)... 62.210.16.61
Connecting to www.pabr.org (www.pabr.org)|62.210.16.61|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4022 (3.9K) [text/x-csrc]
Saving to: `sixpair.c'

100%[========================================================>] 4,022     --.-K/s  in 0s

2015-04-10 22:23:35 (45.1 MB/s) - `sixpair.c' saved [4022/4022]

編譯 PS3 手掣配對程式:
pi@raspberrypi ~/ps3_sixpair $ gcc -o sixpair sixpair.c -lusb

下載 PS3 手掣通訊程式:
pi@raspberrypi ~/ps3_sixpair $ wget http://sourceforge.net/projects/qtsixa/files/QtSixA%201.5.1/QtSixA-1.5.1-src.tar.gz
--2015-04-10 22:40:50--  http://sourceforge.net/projects/qtsixa/files/QtSixA%201.5.1/QtSixA-1.5.1-src.tar.gz
Resolving sourceforge.net (sourceforge.net)... 216.34.181.60
Connecting to sourceforge.net (sourceforge.net)|216.34.181.60|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://sourceforge.net/projects/qtsixa/files/QtSixA%201.5.1/QtSixA-1.5.1-src.tar.gz/download [following]
--2015-04-10 22:40:51--  http://sourceforge.net/projects/qtsixa/files/QtSixA%201.5.1/QtSixA-1.5.1-src.tar.gz/download
Connecting to sourceforge.net (sourceforge.net)|216.34.181.60|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://downloads.sourceforge.net/project/qtsixa/QtSixA%201.5.1/QtSixA-1.5.1-src.tar.gz?r=&ts=1428676851&use_mirror=ncu [following]
--2015-04-10 22:40:51--  http://downloads.sourceforge.net/project/qtsixa/QtSixA%201.5.1/QtSixA-1.5.1-src.tar.gz?r=&ts=1428676851&use_mirror=ncu
Resolving downloads.sourceforge.net (downloads.sourceforge.net)... 216.34.181.59
Connecting to downloads.sourceforge.net (downloads.sourceforge.net)|216.34.181.59|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://ncu.dl.sourceforge.net/project/qtsixa/QtSixA%201.5.1/QtSixA-1.5.1-src.tar.gz [following]
--2015-04-10 22:40:52--  http://ncu.dl.sourceforge.net/project/qtsixa/QtSixA%201.5.1/QtSixA-1.5.1-src.tar.gz
Resolving ncu.dl.sourceforge.net (ncu.dl.sourceforge.net)... 140.115.17.45
Connecting to ncu.dl.sourceforge.net (ncu.dl.sourceforge.net)|140.115.17.45|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 930296 (908K) [application/x-gzip]
Saving to: `QtSixA-1.5.1-src.tar.gz'

100%[========================================================>] 930,296    964K/s  in 0.9s

2015-04-10 22:40:53 (964 KB/s) - `QtSixA-1.5.1-src.tar.gz' saved [930296/930296]

pi@raspberrypi ~/ps3_sixpair $ tar xfvz QtSixA-1.5.1-src.tar.gz
QtSixA-1.5.1/
QtSixA-1.5.1/qtsixa/
QtSixA-1.5.1/qtsixa/game-profiles/
QtSixA-1.5.1/qtsixa/game-profiles/etracer_accel
    ::    ::    ::
QtSixA-1.5.1/INSTALL
QtSixA-1.5.1/Makefile
QtSixA-1.5.1/README
QtSixA-1.5.1/TODO
QtSixA-1.5.1/manual.pdf
pi@raspberrypi ~/ps3_sixpair $ cd QtSixA-1.5.1/sixad

編譯 PS3 手掣通訊程式:
pi@raspberrypi ~/ps3_sixpair/QtSixA-1.5.1/sixad $ make
mkdir -p bins
g++ -O2 -Wall -Wl,-Bsymbolic-functions sixad-bin.cpp bluetooth.cpp shared.cpp textfile.cpp -o bins/sixad-bin `pkg-config --cflags --libs bluez` -lpthread -fpermissive
sixad-bin.cpp: In function 'int main(int, char**)':
sixad-bin.cpp:84:20: warning: taking address of temporary [-fpermissive]
g++ -O2 -Wall -Wl,-Bsymbolic-functions sixad-sixaxis.cpp sixaxis.cpp shared.cpp uinput.cpp textfile.cpp -o bins/sixad-sixaxis -lpthread -lrt
g++ -O2 -Wall -Wl,-Bsymbolic-functions sixad-remote.cpp remote.cpp shared.cpp uinput.cpp textfile.cpp -o bins/sixad-remote -lrt
g++ -O2 -Wall -Wl,-Bsymbolic-functions sixad-raw.cpp sixaxis.cpp shared.cpp uinput.cpp textfile.cpp -o bins/sixad-raw
g++ -O2 -Wall -Wl,-Bsymbolic-functions sixad-3in1.cpp sixaxis.cpp shared.cpp uinput.cpp textfile.cpp -o bins/sixad-3in1

安裝 PS3 手掣通訊程式:
pi@raspberrypi ~/ps3_sixpair/QtSixA-1.5.1/sixad $ sudo checkinstall

checkinstall 1.6.2, Copyright 2009 Felipe Eduardo Sanchez Diaz Duran
           This software is released under the GNU GPL.


The package documentation directory ./doc-pak does not exist. 
Should I create a default set of package docs?  [y]: y

Preparing package documentation...OK

*** No known documentation files were found. The new package 
*** won't include a documentation directory.

Please write a description for the package.
End your description with an empty line or EOF.
>> PS3 Controller for Raspberry Pi
>> 

*****************************************
**** Debian package creation selected ***
*****************************************

This package will be built according to these values: 

0 -  Maintainer: [ root@raspberrypi ]
1 -  Summary: [ PS3 Controller for Raspberry Pi ]
2 -  Name:    [ sixad ]
3 -  Version: [ 20150410 ]
4 -  Release: [ 1 ]
5 -  License: [ GPL ]
6 -  Group:   [ checkinstall ]
7 -  Architecture: [ armhf ]
8 -  Source location: [ sixad ]
9 -  Alternate source location: [  ]
10 - Requires: [  ]
11 - Provides: [ sixad ]
12 - Conflicts: [  ]
13 - Replaces: [  ]

Enter a number to change any of them or press ENTER to continue: 

Installing with make install...

========================= Installation results ===========================
install -d /etc/default/
install -d /etc/init.d/
install -d /etc/logrotate.d/
install -d /usr/bin/
install -d /usr/sbin/
install -d /var/lib/sixad/
install -d /var/lib/sixad/profiles/
install -m 644 sixad.default /etc/default/sixad
install -m 755 sixad.init /etc/init.d/sixad
install -m 644 sixad.log /etc/logrotate.d/sixad
install -m 755 sixad /usr/bin/
install -m 755 bins/sixad-bin /usr/sbin/
install -m 755 bins/sixad-sixaxis /usr/sbin/
install -m 755 bins/sixad-remote /usr/sbin/
install -m 755 bins/sixad-3in1 /usr/sbin/
install -m 755 bins/sixad-raw /usr/sbin/
install -m 755 sixad-dbus-blocker /usr/sbin/
Installation is Complete!

======================== Installation successful ==========================

Copying files to the temporary directory...OK

Stripping ELF binaries and libraries...OK

Compressing man pages...OK

Building file list...OK

Building Debian package...OK

Installing Debian package...OK

Erasing temporary files...OK

Writing backup package...OK
OK

Deleting temp dir...OK


**********************************************************************

 Done. The new package has been installed and saved to

 /home/pi/ps3_sixpair/QtSixA-1.5.1/sixad/sixad_20150410-1_armhf.deb

 You can remove it from your system anytime using: 

      dpkg -r sixad

**********************************************************************

把 PS3 手掣以 USB 線接到 RPi 進行測試對接:
pi@raspberrypi ~/ps3_sixpair/QtSixA-1.5.1/sixad $ sudo sixad -start
sixad-bin[3370]: started
sixad-bin[3370]: sixad started, press the PS button now
sixad-bin[3370]: unable to connect to sdp session
sixad-bin[3370]: Connected Sony Computer Entertainment Wireless Controller (0A:C6:3E:4D:2C:86)

成功後,可以執行 justest 檢查按鍵情況:
pi@raspberrypi ~/ps3_sixpair/QtSixA-1.5.1/sixad $ sudo jstest

Axes:  0:     0  1:     0  2:     0  3:     0  4:-32767  5:-32767  6:-32767  7:-32767  8:-32767  9:-32767 10:-32767 11:-32767 12:-32767 13:-32767 14:-32767 15:-32767 16:-32767 17:-32767 18:-32767 19:-32767 20:-32767 21:-32767 22:-32767 23:-32767 24:-32767 25:-32767 26:-32767

Buttons:  0:off  1:off  2:off  3:off  4:off  5:off  6:off  7:off  8:off  9:off 10:off 11:off 12:off 13:off 14:off 15:off 16:off 17:off 18:off

要是想在重新開機後配對 PS3 手掣,就執行:
pi@raspberrypi ~/ps3_sixpair/QtSixA-1.5.1/sixad $ sudo update-rc.d sixad defaults

參考網址:
http://www.raspians.com/trying-again/
http://booting-rpi.blogspot.ro/2012/08/dualshock-3-and-raspberry-pi.html
https://github.com/petrockblog/RetroPie-Setup/wiki/Setting-up-a-PS3-controller

沒有留言: