RaspberryPiでWiiコントローラを使用してみる

RaspberryPiでWiiコントローラを使用してみる

RaspberryPiで工作するときにいろいろなコントローラの入力を受け付けられれば自由度が上がるだろうということでメモ。 GitHub上にWiimoteというプロダクトがあるのでそれを使います。これはBluetooth経由でWiiコントローラのデータを受け取ることができるようにするもので、Buletooth層に関してはbluezというパッケージを使います。

GitHub - the-raspberry-pi-guy/Wiimote: All the necessary code and commands for using a Wiimote with the Raspberry Pi (Python)

GitHubの紹介にもあるのですが、下記の動画を見れば簡単に設定ができるようです。

Raspberry Pi - How to use a Wiimote! - YouTube

使用したハードウェア

今回はRaspberryPiの少し古いバージョン(2015-05-05-raspbian-wheezy)を使用しています。新しいものではもしかしたらそのままではうまくいかないかもしれません。

言わずと知れたWiiコントローラ(初代となります。)

f:id:ueponx:20160408080849j:plain

使用しているBTのアダプタはBluetooth|BT-Micro3H2X|PLANEXを使っています。1台2役BluetoothWi-FiのコンボUSBアダプタなのでUSBの少ないRaspberryPiでも有効利用ができるものです。(結構お値段高いですけど・・・)

f:id:ueponx:20160408080957j:plain

あとはソフトウエア側の設定になります。

ソフトウエアの設定

ダウンロード

gitを使ってダウンロードし、手順通りsh setup.shを実行してセットアップを実行します。

~ $ git clone https://github.com/the-raspberry-pi-guy/Wiimote.git
~ $ cd Wiimote/
~/Wiimote $ sh setup.sh
取得:1 http://mirrordirector.raspbian.org wheezy Release.gpg [490 B]
取得:2 http://mirrordirector.raspbian.org wheezy Release [14.4 kB]
ヒット http://archive.raspberrypi.org wheezy Release.gpg
ヒット http://raspberrypi.collabora.com wheezy Release.gpg
ヒット http://raspberrypi.collabora.com wheezy Release
ヒット http://archive.raspberrypi.org wheezy Release
取得:3 http://mirrordirector.raspbian.org wheezy/main armhf Packages [6,910 kB]
ヒット http://raspberrypi.collabora.com wheezy/rpi armhf Packages
ヒット http://archive.raspberrypi.org wheezy/main armhf Packages
無視 http://raspberrypi.collabora.com wheezy/rpi Translation-ja_JP
無視 http://raspberrypi.collabora.com wheezy/rpi Translation-ja
無視 http://archive.raspberrypi.org wheezy/main Translation-ja_JP
無視 http://raspberrypi.collabora.com wheezy/rpi Translation-en
無視 http://archive.raspberrypi.org wheezy/main Translation-ja
無視 http://archive.raspberrypi.org wheezy/main Translation-en
取得:4 http://mirrordirector.raspbian.org wheezy/contrib armhf Packages [23.6 kB]
取得:5 http://mirrordirector.raspbian.org wheezy/non-free armhf Packages [49.3 kB]
取得:6 http://mirrordirector.raspbian.org wheezy/rpi armhf Packages [592 B]
無視 http://mirrordirector.raspbian.org wheezy/contrib Translation-ja_JP
無視 http://mirrordirector.raspbian.org wheezy/contrib Translation-ja
無視 http://mirrordirector.raspbian.org wheezy/contrib Translation-en
無視 http://mirrordirector.raspbian.org wheezy/main Translation-ja_JP
無視 http://mirrordirector.raspbian.org wheezy/main Translation-ja
無視 http://mirrordirector.raspbian.org wheezy/main Translation-en
無視 http://mirrordirector.raspbian.org wheezy/non-free Translation-ja_JP
無視 http://mirrordirector.raspbian.org wheezy/non-free Translation-ja
無視 http://mirrordirector.raspbian.org wheezy/non-free Translation-en
無視 http://mirrordirector.raspbian.org wheezy/rpi Translation-ja_JP
無視 http://mirrordirector.raspbian.org wheezy/rpi Translation-ja
無視 http://mirrordirector.raspbian.org wheezy/rpi Translation-en
6,999 kB を 47秒 で取得しました (148 kB/s)
パッケージリストを読み込んでいます... 完了
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています
状態情報を読み取っています... 完了
以下の特別パッケージがインストールされます:
  bluez libcap-ng0
推奨パッケージ:
  bluez-alsa bluez-cups bluez-gstreamer
以下のパッケージが新たにインストールされます:
  bluetooth bluez libcap-ng0
アップグレード: 0 個、新規インストール: 3 個、削除: 0 個、保留: 89 個。
945 kB のアーカイブを取得する必要があります。
この操作後に追加で 2,203 kB のディスク容量が消費されます。
取得:1 http://mirrordirector.raspbian.org/raspbian/ wheezy/main libcap-ng0 armhf 0.6.6-2 [12.3 kB]
取得:2 http://mirrordirector.raspbian.org/raspbian/ wheezy/main bluez armhf 4.99-2 [895 kB]
取得:3 http://mirrordirector.raspbian.org/raspbian/ wheezy/main bluetooth all 4.99-2 [37.5 kB]
945 kB を 2秒 で取得しました (426 kB/s)
以前に未選択のパッケージ libcap-ng0 を選択しています。
(データベースを読み込んでいます ... 現在 78609 個のファイルとディレクトリがイン ストールされています。)
(.../libcap-ng0_0.6.6-2_armhf.deb から) libcap-ng0 を展開しています...
以前に未選択のパッケージ bluez を選択しています。
(.../bluez_4.99-2_armhf.deb から) bluez を展開しています...
以前に未選択のパッケージ bluetooth を選択しています。
(.../bluetooth_4.99-2_all.deb から) bluetooth を展開しています...
man-db のトリガを処理しています ...
libcap-ng0 (0.6.6-2) を設定しています ...
bluez (4.99-2) を設定しています ...
[ ok ] Reloading system message bus config...done.
[ ok ] Starting bluetooth: bluetoothd rfcomm.
bluetooth (4.99-2) を設定しています ...
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています
状態情報を読み取っています... 完了
以下のパッケージが新たにインストールされます:
  python-cwiid
アップグレード: 0 個、新規インストール: 1 個、削除: 0 個、保留: 89 個。
23.8 kB のアーカイブを取得する必要があります。
この操作後に追加で 79.9 kB のディスク容量が消費されます。
取得:1 http://mirrordirector.raspbian.org/raspbian/ wheezy/main python-cwiid armhf 0.6.00+svn201-3 [23.8 kB]
23.8 kB を 3秒 で取得しました (7,641 B/s)
以前に未選択のパッケージ python-cwiid を選択しています。
(データベースを読み込んでいます ... 現在 78715 個のファイルとディレクトリがイン ストールされています。)
(.../python-cwiid_0.6.00+svn201-3_armhf.deb から) python-cwiid を展開しています...
python-cwiid (0.6.00+svn201-3) を設定しています ...
python-support のトリガを処理しています ...

エラーが発生しなければ完了です。同時にインストールしたBuletoothサービスが正常に動作しているかを以下のコマンドで確認します。これが失敗しているとコントローラとの通信できません。

~/Wiimote $ sudo service bluetooth status
[ ok ] bluetooth is running.

okが表示されているので正常にうごいているようです。

動作テスト

Gitで取得したなかにpythonで書かれたサンプルがあるのでそれを使用してテストを行います。Exampleディレクトリの中にあるwiimote.pyがサンプルになります。 起動するとコントローラの1ボタンと2ボタンの同時押しでペアリングが開始され、センサーの状態がRaspberryPi側に返されます。終了はプラス(+)ボタンとマイナス(-)の同時押しになります。

もし、Wiiコントローラが接続されていないと以下のようなエラーが出力されます。

~/Wiimote $ cd Example/
~/Wiimote/Example $ ls
wiimote.py
~/Wiimote/Example $ python wiimote.py
Please press buttons 1 + 2 on your Wiimote now ...
No wiimotes found
Cannot connect to your Wiimote. Run again and make sure you are holding buttons 1 + 2!

接続が成功すると以下のような出力が行われます。

pi@raspberrypi ~/Wiimote/Example $ python wiimote.py
Please press buttons 1 + 2 on your Wiimote now ...

Wiimote connection established!

Go ahead and press some buttons

Press PLUS and MINUS together to disconnect and quit.

Up pressed
Up pressed
Left pressed
Left pressed
Right pressed
Right pressed
Down pressed
Down pressed
Button A pressed
Button A pressed
Button B pressed
Button B pressed
Button B pressed
Button 1 pressed
Button 1 pressed
Button 2 pressed
Button 2 pressed
Button 1 pressed
(0, 0, 0)
(125, 119, 151)
(121, 129, 157)
(126, 128, 152)
(135, 104, 130)
(133, 105, 129)
(127, 125, 156)
(126, 126, 155)
Minus Button pressed
Minus Button pressed

Closing connection ...
<cwiid.Wiimote object at 0x????????>

~/Wiimote/Example $

途中アナログ値(傾きなど)の取得ができますが、HOMEボタンを押しながら傾けると値の取得ができます。

自宅にヌンチャクなどがないので微妙なのですが、基本的にはこれで動作が確認できました。サンプルのpythonファイルをいじっていけば自由にコントローラを使うことができそうです。

/* -----codeの行番号----- */