28 December 2012

RetroPie & a SIXAXIS over bluetooth

So after I got Raspbmc up and running without any issues I decided to go straight for the retro games emulator.

For this I used the following:

So from a fresh boot of Raspbian we need to tweak a few things first.
From the raspi-config screen you need to do:
  • Expand the filesystem to fill the SD card.
  • Change the memsplit to 128 or 192.
  • Change the password for user pi.
  • Overclock (optional and at your own risk)
Once that has been done and the Pi has rebooted, log in and run the following commands:

sudo apt-get update
sudo apt-get upgrade -y

Now we can move on to setting up RetroPie.

First we need to install some dependencies:

sudo apt-get install git dialog -y

And now to download and run the RetroPie setup script:

git clone git://github.com/petrockblog/RetroPie-Setup.git
cd RetroPie-Setup/
chmod +x retropie_setup.sh
sudo ./retropie_setup.sh

Now you have 2 options to install RetroPie - Binaries and Source.
I prefer to use Source so let's select that option.
Now we can choose what to install. If you want everything just leave it as is, but if there are some emulators you'll never use, untick them now to save time and space.
Run the install. Wait 3-4 hours to RetroPie to compile.

Once that is completed you'll have a debug output with any issues encountered (hopefully none).
Reboot your Pi.

Now you have RetroPie installed and you can run it with:
emulationstation

But we're not done yet. I have a PS3 controller I want to use with both EmulationStation and RetroArch over bluetooth.

So to get that working we need the following dependencies:

sudo apt-get install bluez-utils bluez-compat bluez-hcidump libusb-dev libbluetooth-dev joystick checkinstall -y

Give it a few minutes to install.

Once that has completed, let's see if our Bluetooth dongle is detected (for mine I had to make sure it was plugged into one of the Pi's USB ports - not a USB hub).

hciconfig

You should get something similar to this output:

pi@raspberrypi ~ $ hciconfig
hci0: Type: BR/EDR Bus: USB
BD Address: 00:02:72:BF:BC:8F ACL MTU: 1022:8 SCO MTU: 121:3
UP RUNNING PSCAN
RX bytes:16777722 acl:289271 sco:0 events:116 errors:0
TX bytes:2561 acl:53 sco:0 commands:56 errors:0


The key thing we're looking for here is the UP RUNNING PSCAN. If it says DOWN - you've got an issue with your dongle's chipset - or as in my case you've plugged it into a USB hub.

Now we can pair the dongle with the controller using Sixpair.

wget http://www.pabr.org/sixlinux/sixpair.c
gcc -o sixpair sixpair.c -lusb


With your controller plugged in, run the following:

sudo ./sixpair

You'll get something similar to the following output:

Current Bluetooth master: 00:02:72:BF:BC:8F
Setting master bd_addr to: 00:02:72:BF:BC:8F


(Note: When you first run sixpair, the two addresses above will be different - mine are the same because I've already paired the dongle with the controller.)

Now we just need SixaD to manage the controller.

wget http://sourceforge.net/projects/qtsixa/files/QtSixA%201.5.1/QtSixA-1.5.1-src.tar.gz
tar xfvz QtSixA-1.5.1-src.tar.gz
cd QtSixA-1.5.1/sixad
make
sudo mkdir -p /var/lib/sixad/profiles
sudo checkinstall


I've used checkinstall here as it creates a Deb package to help you uninstall it later on. Just hit enter at the prompts and it'll install.

We'll need to start the sixad daemon at startup:

sudo update-rc.d sixad defaults

And start the service now:

sudo service sixad start

Now unplug the controller and press the PS button. You should see the lights go back and forth and it'll rumble for a second or two. Congrats - it works!

(NOTE: As of the latest Raspbian image (2012-02-22), your controller will probably keep flashing and not rumble. The controller has connected but isn't reading the SixaD profile correctly. You can continue to complete this guide but you'll need to create and use my controller configs manually. Looking into a fix for this.)

But we're not finished yet. We need to set up the controller to work inside EmulationStation and RetroArch.

First off let's create a sixad profile for the controller.

sudo nano /var/lib/sixad/profiles/default

Add the following content:

enable_leds 1
enable_joystick 1
enable_input 0
enable_remote 0
enable_rumble 1
enable_timeout 0
led_n_auto 1
led_n_number 1
led_anim 1
enable_buttons 1
enable_sbuttons 1
enable_axis 1
enable_accel 0
enable_accon 0
enable_speed 0
enable_pos 0

Now reboot with the following:

sudo reboot

Once you've logged in, press the PS button again on the controller. It should rumble like before.
This time we've disabled the motion controls as they have some screwy behaviour when setting up controllers in EmulationStation.

Now run emulationstation - you should get the option to configure you're controller.
When you've entered your controls and pressed a keyboard key to save, press F4 to exit emulationstation.
This will have written a file called es_input.cfg to ~/.emulationstation/
You can delete this file if you need to reconfigure your controller, or edit it to do it manually.

My es_input.cfg contains the following:

JOYNAME PLAYSTATION(R)3 Controller (00:24:33:62:62:5A)
BUTTON 0 8
BUTTON 3 7
BUTTON 4 1
BUTTON 5 4
BUTTON 6 2
BUTTON 7 3
BUTTON 10 10
BUTTON 11 9
BUTTON 13 5
BUTTON 14 6

(Note: If you are going to copy this file make sure you change the MAC address at the top. You can get your controllers MAC address using hciconfig after you've paired it.)

Now to configure RetroArch for the controller.

Run the following command:

retroarch-joyconfig >> ~/RetroPie/configs/all/retroarch.cfg

Configure your controls - this will append the configuration to the end of ~/RetroPie/configs/all/retroarch.cfg.

The end of my config file has the following:

input_player1_joypad_index = "0"
input_player1_a_btn = "13"
input_player1_b_btn = "14"
input_player1_x_btn = "12"
input_player1_y_btn = "15"
input_player1_l_btn = "10"
input_player1_r_btn = "11"
input_player1_l2_btn = "8"
input_player1_r2_btn = "9"
input_player1_l3_btn = "1"
input_player1_r3_btn = "2"
input_player1_start_btn = "3"
input_player1_select_btn = "0"
input_player1_left_btn = "7"
input_player1_up_btn = "4"
input_player1_right_btn = "5"
input_player1_down_btn = "6"
input_player1_l_x_plus_axis = "+0"
input_player1_l_y_plus_axis = "+1"
input_player1_l_x_minus_axis = "-0"
input_player1_l_y_minus_axis = "-1"
input_player1_r_x_plus_axis = "+2"
input_player1_r_y_plus_axis = "+3"
input_player1_r_x_minus_axis = "-2"
input_player1_r_x_minus_axis = "-3"
input_exit_emulator_btn = "16"

I added in the last line to exit the emulator using the PS button.

Now we're on the home stretch! We just need to add some roms in.
I copied mine from my PC with a USB pen.
The roms need to go in the following folder: /home/pi/RetroPie/roms
Each emulator has a separate folder. You can use startx if you find that easier to manage the files -but make sure you log out before running emulationstation.

Run emulationstation now and test some games out. All should be working fine!

Special thanks go to:
Florian - http://petrockblog.wordpress.com/retropie/
Themaister - http://themaister.net/retroarch.html
Aloshi - http://aloshi.com/emulationstation
falkTX - http://qtsixa.sourceforge.net/
Pabr - http://www.pabr.org/sixlinux/sixlinux.en.html
Stanislav Kopp - http://booting-rpi.blogspot.co.uk/

31 comments:

  1. This is an excellent tutorial. I already have installed RetroPie, but I'll try to configure the PS3 controller the moment my Bluetooth dondle arrives. Thank you very much :)

    ReplyDelete
  2. I got my bluetooth dongle yesterday and I could make it work! There was a moment of despair when I realised my usb hub didn't work, so I couldn't plug my keyboard, bluetooth dongle, and controller at the same time, but I made it work with the command "at". I set the list of commands that I needed to run beforehand in a separate file, and told it to run them 2 minutes later. Then I unplugged the keyboard and plugged the controller. After ten minutes, I checked if it had worked and voilá!

    I have a question, though. It works well for all emulators except Megadrive and neogeo (not mame, gngeo). The character moves using the arrows, but the other buttons (jumping, shooting, etc.) don't work. I have realised that there is no directory in ~/RetroPie/configs for these emulators. Does that have anything to do with this problem? Do you know how to configure them?

    Again, thank you very much :)

    ReplyDelete
  3. I get this error:

    /usr/lib/gcc/arm-linux-gnueabihf/4.6/../../../arm-linux-gnueabihf/crt1.o: In function `_start':
    (.text+0x34): undefined reference to `main'
    collect2: ld returned 1 exit status

    when i try to compile the sixpair script. Help?

    ReplyDelete
    Replies
    1. Nevermind. restarted and it worked.

      Delete
    2. There looks to have been a lot of changes to the latest Raspbian image and the RetroPie script since I wrote this guide so I think it's time to do a full build again from scratch and update the guide with anything new to look out for.

      Delete
    3. In my system there was no crt1.o but I could fix it with installing the -dev package
      sudo apt-get install libclang-dev
      After reboot it compiled.

      Delete
  4. my controller just keeps blinking, after trying to pair, everything else seems to be working fine but can't get this thing to pair

    ReplyDelete
    Replies
    1. After pressing the PS button to pair can you try the following?

      jstest /dev/input/js0

      This should show you feedback from any input into the controller.

      If that works then it could be an issue with new firmware on the Raspberry Pi

      Delete
    2. Ok I've rebuild my Pi using the latest image and it does appear that the new firmware is messing up SixaD. It does still pair the controller but the lights keep flashing, no rumble and it isn't reading the config properly to disable the accelerometers.

      I'll do some digging and see if we can get a fix for this.

      In the mean time you can still use the controller over Bluetooth, you'll just need to create/edit the input configs for EmulationStation and RetroArch manually (take them from my examples if needed).

      Delete
    3. Any update on this issue? I'm trying to get it to work with no luck.

      Delete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Is it possible to update to the latest version? I hear it's worked out a lot of kinks, or is this dead in the water?

    ReplyDelete
  7. I can't find instructions anywhere to set up the PS3 controller to work via USB (if I don't have a bluetooth dongle). Is it not possible? Or is it so easy that I shouldn't need help? The RPi recognizes that it is there, but when I run jstest it doesn't recognize any button presses.

    ReplyDelete
    Replies
    1. Hi
      I am in the same boat as you. Did you manage to connect this via USB?

      Delete
  8. Anyone knowing his way in Linux tried with ps3joy?
    http://www.ros.org/wiki/ps3joy/Tutorials/PairingJoystickAndBluetoothDongle

    ReplyDelete
  9. This works so well! Alltough i have some problems and i am in dire need of help. I have my frontend setup to boot on startup because its gonna be inside a project box with no keyboard present.
    Now when i reboot it goes into the frontend however the the sixaxis wont initialize when i press the ps button. I have to exit the frontend and press the ps button and restart the frontend. kinda annoying, is there any workaround on this? Either so the frontend boots after the sixaxis is activated or making the sixaxis activate inside the frontend? Sorry for my bad english.

    ReplyDelete
  10. can't get this to work, ran emulation station setup with PS3 controller plugged into USB and ~/.emulationstation/es_input.cfg looks nothing like what's in this tutorial, it's an XML file, how can I convert the example you've provided to XML?

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. I really hope someone can help with this since I'm practically on the verge of tears here!

      I have installed Emulation station and paired my PS3 controller over bluetooth. This all seems fine but when I boot my Pi I get straight in to the Apple II Emulator and the controller won't respond to D-pad presses or any other buttons for that matter. It does however respond to keyboard commands so I can scroll right and I get 'Cave Story'.

      Then, once I select Cave Story the controller works OK, though there is some serious input lag (is this normal for bluetooth connect controllers?).

      Basically I cannot control the main menu with my PS3 controller even though it is connected OK. Also, is there a way I can boot Emulation Station in to a main menu without it loading the first available emulator each time?

      Please help! This is a show stopper for me in my quest to set up my retro pie!

      Thanks

      Delete
    3. That took me a while. Your es_input.cfg is most likely borked. Watch the different kind of quotes in the config file. For me I could not use emulationstation input configuration on the sixaxis controller because it would always recognise an axis movement when I released a button.

      Then I had a file which is a good template, which had all the quotes messed up and a couple of settings wrong.

      here is the heart of my working es_input.cfg
      you will need to get rid of the underscores (replace _input by input)



      <_input name="a" type="button" id="13" value="1" />
      <_input name="b" type="button" id="14" value="1" />
      <_input name="down" type="button" id="6" value="1" />
      <_input name="left" type="button" id="7" value="1" />
      <_input name="menu" type="button" id="16" value="1" />
      <_input name="pagedown" type="button" id="10" value="1" />
      <_input name="pageup" type="button" id="11" value="1" />
      <_input name="right" type="button" id="5" value="1" />
      <_input name="select" type="button" id="12" value="1" />
      <_input name="up" type="button" id="4" value="1" />



      The emulators use their own mapping, so that is most likely the reason it works for you...

      Delete
  11. Thank you sir for this most informative blog! You've saved me hours of grief by posting this here. I was thinking of doing a similar write up once I got mine up and running but you've done an excellent job. Thank you sir +1000 internets for you!

    ReplyDelete
    Replies
    1. OH, forgot to add, I'm not sure if you have a youtube video showing off your setup but it would be nice to see the final product up and running. I'm still undecided if I want to use my PS3 controller or maybe a retro NES controller for the final product. In either case this helped me to at least get this set up with the equipment I already have.

      Delete
  12. Has anyone run into the issue where they get the PS3 controller to connect through bluetooth perfectly, once and then never again? I've run into it. No amount of starting over, going through all the steps again will get the controller to connect. The controller works perfectly through USB, the MAC address for the controller is paired with the bluetooth, but nothing. Any suggestions?

    ReplyDelete
  13. I was able to use DualShock 3 successfully, but leds keep blinking all the time, even afeter pairing, but it's working.

    I'm after answers and found your blog, your PS3 controller keeps blinking or not? Any solution to this?

    Ps.: Did you find a way to customize controls for Osmose emulator? It's kind annoying using left analog instead dpad to play Master System games.

    ReplyDelete
    Replies
    1. I also have the LEDs blinking constantly. I'd like to disable this as it will most likely run the batteries down faster.

      Delete
  14. Do you know if you can run two SIXAXIS controllers from one BT dongle on the Pi?

    ReplyDelete
    Replies
    1. Yes you can run 2 controllers off the same Bluetooth dongle. (I'm doing it right now) Just pair both using sixpair. Restart sixad (or reboot) and don't forget to hit the PS button on both while it's booting.

      Delete
  15. For me, everything works fine, until I press the PS-button. Then, nothing happens! Absolutely nothing, not even the leds start flashing like when you normally connect the controller to a PS3 system. And neither the Pi, nor the PS3 register that the Button is pressed.

    I'm operating my Pi via SSH, because I don't have a spare USB hub for a keyboard. Could this be an issue?

    Thank you for your help so far
    I hope there's anyone who can help me :)
    Schrankwand

    ReplyDelete
  16. Hi there and thanks for jour tutorial.

    Did you find a way to fix blinking leds ?

    Had found something about modifying bluetooth.cpp but still no luck, must force legacy mode or don't work at all (says no battery...)

    ReplyDelete
  17. I have a erro when compile.

    How to fix this ?

    $ sudo 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:89:9: error: ‘close’ was not declared in this scope
    close(ctl);
    ^~~~~
    sixad-bin.cpp:89:9: note: suggested alternative: ‘clone’
    close(ctl);
    ^~~~~
    clone
    sixad-bin.cpp:100:13: error: ‘sleep’ was not declared in this scope
    sleep(2);
    ^~~~~
    sixad-bin.cpp:109:13: error: ‘close’ was n

    ReplyDelete
  18. How do I make money off of gambling? - Work-to-Earn Money
    In addition bet365 to the betting and bonuses, there are also the online gambling platforms that offer งานออนไลน์ no deposit 바카라 게임 bonuses that have value 해외야구 to 토토사이트 bet on.

    ReplyDelete