Home > ARM, Guides, USB > FEZ Panda & PS3 Controller

FEZ Panda & PS3 Controller

NB: The newest source code can now be found at github.

As Thomas origanally posted, the FEZ Panda can actually be used as a USB-Host: Fez Panda And USB Host. I have for long time wanted to use my PS3 DualShock 3 controllers for something useful (besides playing Playstation of course), therefore I thought it would be interesting to connect it to the FEZ Panda.

PS3 DualShock 3 Controller



I used a FEZ Panda II for the project, which is an enhanced version of FEZ Panda (the first one) with on-board microSD socket, Real Time Clock, and an easily accessible 40-pin female header that exposes the extra IOs. For more information see the original website: FEZ Panda II.

FEZ Panda II

But before you can use the FEZ Panda’s USB-Host functionality, you have to make a special USB-Host cable. See Thomas’ previous post for information: Fez Panda And USB Host. If you are new to FEZ see The Beginner Guide ebook.

Like many other USB joysticks the PS3 Controller uses the HID Protocol, but it has a lot of advanced functions, like built in 3-axis accelerometer, single axis gyro, rumble, LED status, and more.
It features two analog joysticks, triggers and a lot of buttons. The unique thing about PS3 controllers is that almost all the buttons can be read either as analog values or digital, directly from the controller. Another feature is the ability to communicate over either USB or Bluetooth. I have only enabled USB communication, but I will work on implementing Bluetooth as well.

After some Googling I discovered that the PS3 Controller is a bit different from ordinary joysticks. One example is that you have to send a special command, to request the PS3 controller to fx send button presses back:

43
44
45
46
47
48
try
{
    //request the PS3 controller to send button presses etc back
    //Host to device (0x00) | Class (0x20) | Interface (0x01), Set Report (0x09), Report Type (Feature 0x03) - Report ID (0xF4), Host to device (0x00) - Endpoint 0 (0x00), data, dataoffset, datalength
    raw.SendSetupTransfer(0x21, 0x09, 0x03F4, 0x0000, buf, 0x00, 0x04);
}

For information about the HID protocol used by the program, see the following link page 48. See under “Get_Report Request” and ” Set_Report Request” which are the commands used for the PS3 controller. For further details see this link at “9.3 USB Device Requests”.

For more information, see the code at the bottom of this post.

The program has lot of inspiration from the following guide for the Arduino USB Host shield.

You should also check out GHIElectronics wiki I contributed to.

Features

  • All buttons work – including analog functionality
  • Rumble functionality
  • LED control
  • Supports multiple controllers through a USB Hub
  • Pitch and Roll calculation. The values are calculated using the onboard 3-axis accelerometer. See my previous post for information: Guide: Gyro and Accelerometer Kalman filtering, with the Arduino. And here is the link for the datasheet for the Kionix KXPA4, which is equivalent to KXPC4, which is used inside the DualShock 3 controller.
  • Set and get Bluetooth address (this will be used to implement Bluetooth later)
  • Read Status (plugged, power status, and Bluetooth)

  • Video
    The following video demonstrates all the futeres:



    Developer tools
    Here are two good USB analyzer programs to see the USB Descriptors, Interfaces, Endpoints and more: USBView (Windows) and USB Prober (Mac).

    When reading the PS3 Controller, it will send out a bunch of bytes. To see these bytes, use the following program (works with both Linux, Max, and Windows – see the folder “testgui”). Some people has reported that it does not work under windows. Alternative see the following program instead (Windows only): SimpleHIDWrite.

    For information about the protocol used by the PS3 Controller, see the following link.

    For more information about the USB Protocol and how it works see the following links:
    A simple intro to USB
    A lot of information and good programs for both windows, linux and mac

    The Software
    PS3_Controller_V1.3

    NB
    If you use a Fez Panda like me, you will recieve the following error: “An unhandled exception of type ‘System.InvalidOperationException’ occurred in GHIElectronics.NETMF.USBHost.dll”, you can just press “Continue” – it just can’t find the USB host, because the USB plug is used by the USB client. You have to make a speciel USB host cable see this guide for further details: FEZ Panda and USB Host (http://blog.tkjelectronics.dk/2011/03/fez-panda-and-usb-host/).

    Furthermore you have to connect the Mode pin (labeled MOD) to ground using a wire, to enabled debugging through COM1. Also the RX (COM1 In), are not allowed to float, so you have to use a pullup resistor, or connect it to a USB-Serial adapter. You can also use the USB-Serial adapter to program it via the serialport, to do so go to “Properties -> .NET Micro Framework” and change “Transport” to “Serial”, and “Device” to your USB-Serial adapter’s COM-port.

    All the code is licensed under a Creative Commons Attribution Noncommercial Share-Alike license, see this link for details: http://creativecommons.org/licenses/by-nc-sa/3.0/

    Update
    Bluetooth is now supported, see the wiki and my new post for more details.

    Categories: ARM, Guides, USB Tags:
    1. September 1st, 2011 at 14:58 | #1

      Hi. I am very interested if you made this work with bluetooth. I concider doing the controller side of my UGV project with the panda and a bluetooth module.

    2. Lauszus
      September 1st, 2011 at 16:05 | #2

      Actually I have already updated the code to work via bluetooth. Have a look at the wiki for more information and the code: http://wiki.tinyclr.com/index.php?title=PS3_Controller :)

    3. September 1st, 2011 at 19:34 | #3

      I saw your posting on the Wiki. This is fantastic, thank you for your work and contribution! I ordered some bluetooth mini USB dongles off of e-bay and I’ll be using it to control the robot I’m working on.

      @Lauszus

    4. Lauszus
      September 2nd, 2011 at 09:56 | #4

      @Thomas Kisner
      Thank you very much. That is exactly why I started developing it. I really needed a nice remote for a future robot :)

    5. Tom
      December 17th, 2011 at 04:24 | #5

      hello i was wondering what kind of resistor we used for the pull up resistor?

    6. Lauszus
      December 17th, 2011 at 04:30 | #6

      The USB-protocol says 15k, so I will recommend that, but I actually used a 12k resistor, as I didn’t have any 15k resistors laying around at the time.
      You should see our other post, which describes it in detail: http://blog.tkjelectronics.dk/2011/03/fez-panda-and-usb-host/
      Regards
      Lauszus

    7. Tom
      January 1st, 2012 at 23:53 | #7

      I was actually talking about when i dont have com in plug into my computer. This is my problem. It works fine when im connected to my computer. Once im not connected to my computer it doesn’t work. I realized that i need to hook 5v into com in because it can’t be floating. What resistor should i use for that? I tried 10k but it isn’t working. @Lauszus

    8. Lauszus
      January 2nd, 2012 at 03:05 | #8

      @Tom
      The problem is that the FEZ Panda wouldn’t reset properly when the COM-rx port is floating. Did you remember to connect a wire from the MODE pin to ground? You only need to hook it up to 3.3V -- 10k works fine for me.

    9. Tom
      January 3rd, 2012 at 22:05 | #9

      Okay so sorry for all the questions. I plug 3v3 volts into a 10k resistor then i plug the other end of the 10k resistor into COM-rx. I have mode connected to ground and i have 5V going from my bluetooth cable. I hit reset then the ps3 button but nothing happens. =( @Lauszus

    10. Lauszus
      January 3rd, 2012 at 22:25 | #10

      That’s okay :)
      Hmm that sounds strange. Do you plug in in to COM port 1? Are you sure your modified USB host cable is working? Have you got a USB to serial adapter like this one: http://www.ebay.com/itm/USB-2-0-TTL-UART-6PIN-CP2102-Module-Serial-Converter-C-/220864149466?pt=LH_DefaultDomain_2&hash=item336c8777da
      I recommend buying one so you can connect it to COM port 2 and see what it prints out. It might be a problem with your bluetooth dongle -- I need to know when it stops to say if thats the problem.

    11. Tom
      January 3rd, 2012 at 22:55 | #11

      It works now!!! Yes i have the USB to serial you recommend. I’m not sure what was going on i just started to connect different things. This is the end result. 3.3v connect to COM-rx via 10k resistor. Mode set to ground and 5v to the bluetooth dongle. Don’t know why it wasn’t working before! Thanks for all your help!@Lauszus

    12. Lauszus
      January 4th, 2012 at 02:09 | #12

      @Tom
      Nice! :) Glad I could help!

    13. marco
      January 18th, 2012 at 02:03 | #13

      Hi Lauszus,

      I am trying to use the Xbee dongle with FEZ Panda I USB Host function, any ideas or experiences?

    14. Lauszus
      January 18th, 2012 at 09:13 | #14

      @marco
      Why do you want to do that? You can just hook it up to one of the UART ports (labeled COM1, COM2 etc). The FEZ Panda has 4 UART ports, so it shouldn’t be a problem finding one to hook it up to. See the brochure for more info: http://www.ghielectronics.com/downloads/FEZ/Panda/Broch_FEZ_Panda.pdf

      If you still want to hook it up via USB, you should check out this page: http://www.ghielectronics.com/downloads/NETMF/Library%20Documentation/Index.html

      I have a Xbee myself, but usually I just connect hook it up to my microcontrollers via this breakout board: http://www.adafruit.com/products/126 :)

    1. February 17th, 2013 at 15:03 | #1