FEZ Panda & PS3 Controller via Bluetooth
NB: The newest source code can now be found at github.
As you might have seen, I finally got the PS3 Controller working via Bluetooth. Before you read any further, you should read my previous post first and also see the wiki for more information.
I won’t go into to much detail about the Bluetooth Protocol, as it is very complex – the specification is about 2000 pages! But instead I will describe the steps needed for it to work. If you got more specific questions, feel free to post a comment below. You should also have a look at my code at the bottom of this post, as it is well commented.
The Bluetooth connection works via a Bluetooth dongle (Cambridge Silicon Radio(CSR) based) – this one is confirmed to work: Bluetooth Dongle – connected to the Fez Panda via USB Host.
The HCI ACL Data Packet has the following structure:
buf[0] buf[1] buf[2] buf[3]
0 4 8 11 12 16 24 31 MSB
.-+-+-+-+-+-+-+-|-+-+-+-|-+-|-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
| HCI Handle |PB |BC | Data Total Length | HCI ACL Data Packet
.-+-+-+-+-+-+-+-|-+-+-+-|-+-|-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
buf[4] buf[5] buf[6] buf[7]
0 8 16 31 MSB
.-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
| Length | Channel ID | Basic L2CAP header
.-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
buf[8] buf[9] buf[10] buf[11]
0 8 16 31 MSB
.-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
| Code | Identifier | Length | Control frame (C-frame)
.-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-. (signaling packet format)
The bluetooth communication works in the following steps:
1. First you have to set up the Bluetooth dongle using the HCI protocol.
2. Then you wait for the incoming request from the PS3 Controller.
3. When the request arrives, you accept, and change the role, so the FEZ Board, becomes the master of the connection.
4. Then you listen on the Bulkin endpoint. The controller will then send a L2CAP connection request, for the HCI Control channel (PSM: 0x11).
5. You respond by sending an connection response. First with the result: pending, and then with the result success.
6. Then you have to send a configuration request. The controller will then respond with a configuration request as well.
7. You then respond, and the controller will then respond, that the channel have been successfully established.
8. The program then repeats the steps from 4-7, but this time it sets up the HID Interrupt (PSM: 0x13) channel.
9. Just like for USB connection, the host (FEZ Board), has to sent a special command to make the PS3 Controller sent buttons reports ect. back. It is a little different for bluetooth. This time you have to send a Set Feature Report with the following data:
For more information see this patch made for linux.
10. Then just listen on the bulkin endpoint on the interrupt channel, and you will see the bytes changing, when you push a button, or use the joysticks.
For information about the Bluetooth Protocol. See this pdf.
Take a good look at the categories: “HCI COMMANDS AND EVENTS” Volume 2 p. 458, “HCI ACL Data Packets” Volume 2 p.428 and “SIGNALING PACKET FORMATS” Volume 3 p. 54.
See this pdf, for standard BT HID Commands, used by the program, at “Bluetooth HID L2CAP Protocol Specification” p. 50.
I used this program to sniff the Bluetooth Commands sent from my mac: PacketLogger.app. It is developed by apple, so it is mac only! If you are on a mac, this is what the program decoded: PS3Controller_Protocol.pklg
The Protocol and Service Multiplexor (PSM) used by the L2CAP Protocol (HID_Control and HID_Interrupt), are defined here.
Video Demonstration
Here is the code used in the video: PS3BluetoothNXT
The Software
PS3 Controller V2.01 (Bluetooth is now enabled, joysticks and digital button functionality, rumble and leds works – still working on gyro, accelerometer and analog button reading via Bluetooth)
NB: 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
Gyro, accelerometer, status and analog buttons readings is now supported. Check out the wiki for more information. The code can also be downloaded here: PS3 Controller V2.1.
Another Update
The Navigation and Motion controllers are now also supported. You should check out the wiki for more information. The code can also be downloaded here: PS3 Controller V2.2.
Demonstration of the three controllers
Here is the code used in the video: PS3BluetoothNXTVersion2
hey very nice, i am looking forward to implementing your code, though i have not yet looked at it, i was wondering why the analog sticks only had 8 bit resolution?
@Eddie
Because Sony decided too π It only sends one byte (8 bit), per jostick-axis, so you can not get more resolution than that.
@Eddie
Everything in this world costs.
First thought could be that the MCU inside the PS3 only has 8-bits of ADC, or maybe they have used an external 8-bit ADC for the measurement.
Second thought could be that they would like to keep the data transfers minimal, and that they thought two bits more wouldn’t do any noticeable difference.
So yes, Lauszus is just right – they decided to do so, and so is it!
Makes perfect sense guys, besides imagine 32 bit res on a game controller , we could probably move through the game pixle by pixle .
Why would you need such a precision? π But I agree, that it would be nice if they had provided something like a 10 bit resolution!
Hello,
I recently bought this bluetooth apdapter on ebay
http://www.ebay.com/itm/180654603383?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1439.l2649#ht_3726wt_1054
using the code I get the following on the serial com port
Initialized
Bluetooth Dongle Connected
CSR Initialized
Init State
HCI Reset Complete
ACL Data Packet Length: 1021
SCO Data Packet Length: 180
Total ACL Data Packets: 4
Total SCO Data Packets: 1
HCI Version: 3
HCI Revision: 3
LMP Version: 3
Manufactor ID: 10
LMP Subversion: 3
Local Bluetooth Address: 00:1F:81:00:02:50
Wait For Incoming Connect Request
but when I pressed the ps3 button but it won’t connect
any idea
please help
Thanks
Kelvin
OK
So I figured out how to change the bluetooth address in the controller but
now I get this HCI command error message:
PS3 Controller: 1 – Disconnected
Bluetooth Dongle Connected
CSR Initialized
Init State
HCI Reset Complete
ACL Data Packet Length: 1021
SCO Data Packet Length: 180
Total ACL Data Packets: 4
Total SCO Data Packets: 1
HCI Version: 3
HCI Revision: 3
LMP Version: 3
Manufactor ID: 10
LMP Subversion: 3
Local Bluetooth Address: 00:1F:81:00:02:50
Wait For Incoming Connect Request
Bluetooth Dongle Disconnected
PS3 Controller: 1 – Connected
PS3 Controller: 1 – LeftHatX: 0 – LeftHatY: 0 – RightHatX: 0 – RightHatY: 0
PS3 Controller: 1 – Left – Got BT Address: 0x00 0x15 0x83 0x15 0xA3 0x10
PS3 Controller: 1 – Right – Set BT Address: 0x00 0x1F 0x81 0x00 0x02 0x50
PS3 Controller: 1 – Left – Got BT Address: 0x00 0x1F 0x81 0x00 0x02 0x50
PS3 Controller: 1 – Disconnected
Bluetooth Dongle Connected
CSR Initialized
Init State
HCI Reset Complete
ACL Data Packet Length: 1021
SCO Data Packet Length: 180
Total ACL Data Packets: 4
Total SCO Data Packets: 1
HCI Version: 3
HCI Revision: 3
LMP Version: 3
Manufactor ID: 10
LMP Subversion: 3
Local Bluetooth Address: 00:1F:81:00:02:50
Wait For Incoming Connect Request
Incoming Request
Remote Name:
PLAYSTATION(R)3 Controller
Authentication Enable Complete
Connected to Device: 00:1E:3D:B5:0C:C6
Remote Version Information – Connection Handle: 0x0000 LMP Version: 0x03 Manufactor Name: 0x000A LMP SubVersion: 0x0D22
Connection Packet Type Changed – Connection Handle: 0x0000 Packet Type: 0x3318
Write Link Policy Settings Complete
Write Link Supervision Timeout Complete
Set AFH Host Channel Classification Complete
Scan Disabled
QOS Setup Complete
HCI Command Failed – Error: 0x0C Command: 0x0C3F
HCI Command Failed – Error: 0x0C Command: 0x0C3F
HCI Command Failed – Error: 0x0C Command: 0x0C3F
HCI Command Failed – Error: 0x0C Command: 0x0C3F
HCI Command Failed – Error: 0x0C Command: 0x0C3F
HCI Command Failed – Error: 0x0C Command: 0x0C3F
HCI Command Failed – Error: 0x0C Command: 0x0C3F
HCI Command Failed – Error: 0x0C Command: 0x0C3F
HCI Command Failed – Error: 0x0C Command: 0x0C3F
HCI Command Failed – Error: 0x0C Command: 0x0C3F
HCI Command Failed – Error: 0x0C Command: 0x0C3F
HCI Command Failed – Error: 0x0C Command: 0x0C3F
HCI Command Failed – Error: 0x0C Command: 0x0C3F
HCI Command Failed – Error: 0x0C Command: 0x0C3F
Help please
Kelvin
It looks like the “Set AFH Host Channel Classification Command” fails. See this link: https://www.bluetooth.org/docman/handlers/downloaddoc.ashx?doc_id=229737 at page 628 volume 2. Try just to skip this step. It might not be necessary, but I decided not to remove it, as I still haven’t got the analog buttons reading, gyro, accelerometer and status working π
Try to just skip the step by changing this:
case HCI_QOS_SETUP:
if (hciflag(hci_event.HCI_FLAG_QOS_SETUP_COMPLETE))
{
WriteSerial(“QOS Setup Complete”);
hci_afh_host_channel_classification();
hci_state = HCI_AFH_HOST_CHANNEL_CLASSIFICATION2;
}
break;
To this:
case HCI_QOS_SETUP:
if (hciflag(hci_event.HCI_FLAG_QOS_SETUP_COMPLETE))
{
WriteSerial(“QOS Setup Complete”);
WriteSerial(“HCI Done”);
l2cap_state = L2CAP_EV_CONTROL_SETUP;
hci_state = HCI_DONE_STATE;
}
break;
Thanks Lauszus, it works like you said and didn’t work just like you said also.
No analog, gyros, or rumble when I changed to your code.
Hope someone may offer some advise.
Rumble works. You just have to send the command just like you set the LED π
Yeah me too, but I simply can’t find anybody with any experience with it.
@Kelvin
I got it working see the wiki for more information.
Hello, I’m trying to get this to work… I already have 3 different USB BT dongles so I don’t really want to buy another one. I’m using the ChipWorkX atm but I have a Panda2 ordered that should arrive in the next few weeks… even though I’d like to avoid having to do the modification on the Panda.
The code is failing very early with an Exception in the BulkReadingThread method with the command BulkInPipe.TransferData(BulkInBuffer, 0, BulkInBuffer.Length);
After activating all the debug output this is what I get in the output:
CSR Initialized
0F 04 00 01 00 00 00 00 00 00 00 00 00 00 00 00
Command Status
Command Status – Complete: 0x0000
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
#### Exception System.Exception – 0xffffffff (7) ####
#### Message:
#### GHIElectronics.NETMF.USBHost.USBH_RawDevice+Pipe::TransferPipe_Helper [IP: 0000] ####
#### GHIElectronics.NETMF.USBHost.USBH_RawDevice+Pipe::TransferData [IP: 001c] ####
#### PS3ControllerBluetooth.Bluetooth::IntReadingThread [IP: 0027] ####
#### Exception System.Exception – 0xffffffff (8) ####
#### Message:
#### GHIElectronics.NETMF.USBHost.USBH_RawDevice+Pipe::TransferPipe_Helper [IP: 0000] ####
#### GHIElectronics.NETMF.USBHost.USBH_RawDevice+Pipe::TransferData [IP: 001c] ####
#### PS3ControllerBluetooth.Bluetooth::BulkReadingThread [IP: 0027] ####
Eine Ausnahme (erste Chance) des Typs “System.Exception” ist in GHIElectronics.NETMF.USBHost.dll aufgetreten.
Exception was thrown – try reconnecting the bluetooth dongle
==============================
01/01/2009 00:01:20
Exception was thrown: System.Exception
GHIElectronics.NETMF.USBHost.USBH_RawDevice+Pipe::TransferPipe_Helper
GHIElectronics.NETMF.USBHost.USBH_RawDevice+Pipe::TransferData
PS3ControllerBluetooth.Bluetooth::BulkReadingThread
And this is what I get on COM2:
Bluetooth Dongle Connected
CSR Initialized
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Exception was thrown – try reconnecting the bluetooth dongle
Exception was thrown – try reconnecting the bluetooth dongle
Exception was thrown – try reconnecting the bluetooth dongle
Exception was thrown – try reconnecting the bluetooth dongle
Exception was thrown – try reconnecting the bluetooth dongle
Bluetooth Dongle Disconnected
Any idea what might be wrong? Thanks a lot in advance for your help… and thanks for putting that much effort into it!
Regards, Christian
Hmm, try adjusting these values (Look in Bluetooth.cs):
IntInPipe.TransferTimeout = 5;
BulkInPipe.TransferTimeout = 5;
And see if it helps.
It looks like it throws an exception, when it’s trying to reset the bluetooth dongle. Try skipping that part and see if it works then.
Alternatively buy the dongle I used, as it is guaranteed to be supported:
http://www.ebay.com/itm/220704691620?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1439.l2649#ht_1477wt_1189.
What versions of the Bluetooth protocol are your dongles using and do they support EDR?
Regards
Lauszus
@Lauszus
Thanks for the reply. The Timeout you mentioned is already set to 5 in the code I’m using.
I have 3 bt controller, of the ones where I know for sure it’s 3.0 + EDR and 2.1+EDR. Here is the link to the 3.0 Controller (though it’s in French): http://www.conrad.fr/micro_cle_usb_bluetooth_30_hs_classe_2_conrad_p_49993_50081_1041932_1039003#descriptif and the 2.1 Controller from the same site: http://www.conrad.fr/micro_cle_usb_bluetooth_21_classe_2_conrad_p_49993_50081_838564_655360
For now I’ll just do a few more tests and then wait till my Panda2 arrives and I’ll try it on that one as well. Maybe it’s something related to the ChipWorkX module.
Either way I’ll let you know if I find anything… else I’ll also get the module you linked but it seems to be pretty much the same as the one I got..
Yes it is set to 5, just try to set it to some other value, like 10 or something and see if it helps. I set it to 5 when using it with my FEZ Rhino and 10 when using it with my FEZ Panda.
It might be related to the ChipWorkX module, as I have not tested the code using that module – only FEZ Panda II and FEZ Rhino. So I would recommend using the FEZ Panda together with a special USB Host cable: http://blog.tkjelectronics.dk/2011/03/fez-panda-and-usb-host/
Thank you. I will really like to know if you get it working, as it might be a something in the code.
Regards
Lauszus
The error could also be due to the solution file. Try creating a new project and then just add the classes and the required references. Then build the solution again and see if it works π
Hello i was looking at the video you posted an noticed you are using the joysticks to move the car. I’m having trouble figuring out how to do that. Did you post the code you used for the video somewhere? Or could you possible point me in the right direction?
@Joseph
The code used in the video can be found at the wiki: http://wiki.tinyclr.com/index.php?title=PS3_Controller#Video_Demonstration. This is the direct link: http://wiki.tinyclr.com/images/3/32/PS3BluetoothNXT.zip
I recommend using the newest version and then just add the necessary parts from “PS3BluetoothNXT.zip” π
I guess im confused on the “necessary parts”. Do i need to add the motor shield file and copy and paste the program.cs file into the other program.cs file?
It depends on your motorshield. I just made my own class for the NXT Shield. What motorshield are you using? If you make a equivalent class, you just have to add that class and the code found in “Program.cs” for it to work.
I have a H-bridge Motor Driver 1A here is the datasheet http://www.sparkfun.com/datasheets/IC/SN754410.pdf I’m going to try and look at your code more closely tonight and see if i can manage to change your’s to work with mine
thanks!@Lauszus
Beware that there are no internal protection dioded like on the L293D as I used, for the NXT Shield. So you have to add the external. See the discussion on Sparkfun for more info: http://www.sparkfun.com/products/315.
It should be so hard to adapt the code to yours – you only need to change the pins. Just write again if you need further help π
@Lauszus
Hello,
It took a while but I finally got the adapter cable made, changed my TTL2USB converters to use 5V instead of the 3.3V when the Panda is powered via USB and tested my BT dongles. All three give me the same errors that I had on the ChipWorkX. So it seems your code isn’t the problem… I’ll try to look into this a bit more and if I can’t fix it will decide if it’s worth it for me to buy yet another dongle or just live without it…
Thanks a lot for your great work and if I find any hint of what might be “wrong” with my dongles I’ll let you know.
@Christian
Okay. It sounds like it is the bluetooth dongle. It took me three tries before I got one that was working perfectly. You can get bluetooth dongles on ebay for as low as 3$ including shipping, so you can just order a couple of different ones and then hope that at least one of them.
Thank you, I will release a library for using it together with an Arduino USB Host Shield soon π
Lauszus: I can’t find any private message feature, so I write to here π I need some help (or working code :)) about USB host and Bluetooth communication. I would like to setup serial connection between 2 bluetooth device, one in the computer and another in the FEZ Panda. If it has a good price I would pay for it… Could you reply to my e-mail address? Thanks a lot
@Csicso
Hi,
See my email reply π
hello i have a few usb dongles and a mega adk. My problem is that with my dbt-120 (hacked firmware that i cant get off) it shows an hci error as soon as i plug it in, i can post the error code when plug it in next. I also have a rocketfish one that is 2.1+erd compliant that shows error 5 sometimes and other times just shows up as unknown device connected. What can i change to make it work or am i going to have to get a new dongle? email me if you can or reply here and i will find it thank you for any help you can provide.
@Dragracekid
I would just buy a new dongle from ebay. See the following list of confirmed dongles: https://github.com/TKJElectronics/USB_Host_Shield_2.0/wiki#wiki-Hardware
I have no idea how to get the original firmware back on the bluetooth dongle.
Just to make sure: are you using my library for Arduino (https://github.com/TKJElectronics/USB_Host_Shield_2.0) or the library for the GHI Electronics devices? Because the library for Arduino should support any bluetooth dongle, but the code for the GHI Electronics devices only supports CSR based dongles.
Regards
Lauszus
Once I get home I will check I may be using the wrong library I’d feel dumb but happy thank you so much for the quick response
@dragracekid
Your welcome π
this is the code that keeps showing up when i plug the dbt-120 in any ideas?
HCI Command Failed: 0C 03 0C
sorry for the double post but i forgot to mention i still get unknown device on the other one. i may just buy one from ebay lol
@Dragracekid
The error you are getting is “command disallowed” (0x0C) and the command that caused the event is hci_reset (OCF = 0x03, OGF = (0x03< <2) = 0x0C). For more information see the following pdf at page 594, 810 and 979.
Try to increase the following value to 2000 and see it that makes any difference.
If not, I recommend buying a new dongle.
Regards
Lauszus
sadly it didnt work i hate buying things off ebay but ill have to thank you so much for your help
@Dragracekid
If you don’t like buying stuff at ebay, you could just buy one at your local computer store π I just bought from ebay, because it’s cheeper, but also so other people could easily buy a working dongle!
Regards
Lauszus
hi
did u ever try with a HC-05 Bluetooth instead of the USB shield to make it more cheaper?
eBay link:
http://www.ebay.com/itm/400562862516?ssPageName=STRK:MEWAX:IT&_trksid=p3984.m1438.l2649
did it will work with a cheap PS3 wireless controller like this:
http://www.ebay.com/itm/400562862516?ssPageName=STRK:MEWAX:IT&_trksid=p3984.m1438.l2649
this will be for my telescope:
http://www.youtube.com/watch?v=HsHg9M8athE (sound muted = “Learning to fly” by pink floyd)
@sigi
You can’t use a module like that. The problem is that it doesn’t support Bluetooth HID, so it can be used.
hi
i’m trying to download the code, but i just couldn’t
it keeps giving me this
”
Forbidden
You don’t have permission to access /index.php on this server.
Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.”
@amine
Hi Amine.
Thank you for noticing and mentioning the problem which should now be fixed.
Please try and download the code again.
Regards Thomas