2

Author Topic: PlayStation 2 Controller Arduino Library  (Read 21457 times)

0 Members and 1 Guest are viewing this topic.

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
PlayStation 2 Controller Arduino Library
« on: May 31, 2010, 09:58:21 PM »
I tweaked some code I found for interfacing a Playstation 2 Controller into an Arduino library. I spent countless days and could never get any of the pre-existing libraries to work.

Anyway, if you need it, enjoy. Download and details at

http://www.billporter.info/?p=219


Offline Xyver

  • Robot Overlord
  • ****
  • Posts: 125
  • Helpful? 0
Re: PlayStation 2 Controller Arduino Library
« Reply #1 on: May 31, 2010, 11:19:34 PM »
Thank you!  I will use this one day.  Ive always wanted a playstation RC.

Offline Joker94

  • Supreme Robot
  • *****
  • Posts: 1,119
  • Helpful? 26
Re: PlayStation 2 Controller Arduino Library
« Reply #2 on: June 01, 2010, 02:33:29 AM »
Brilliant Madsci1016

definatly needed for arduino, simplify what other mumbo jumbo that is out there!

Thanks and i am sure every one will appreciate it.

I'll test it out now

Cheers

Joker94

Offline dunk

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 1,086
  • Helpful? 21
Re: PlayStation 2 Controller Arduino Library
« Reply #3 on: June 01, 2010, 02:40:04 AM »
hey MadSci,
i'm sure Aduino code will be useful to some people
but do you know WebbotLib does PS2 controllers now?


dunk.
(PS. http://www.billporter.info/wp-content/uploads//2010/05/PSX_example.zip is just an empty file.)

Offline flokos

  • Jr. Member
  • **
  • Posts: 17
  • Helpful? 0
Re: PlayStation 2 Controller Arduino Library
« Reply #4 on: June 01, 2010, 02:54:53 AM »
Is there any adapter to interface it with arduino without having the plug cut.

Offline Joker94

  • Supreme Robot
  • *****
  • Posts: 1,119
  • Helpful? 26
Re: PlayStation 2 Controller Arduino Library
« Reply #5 on: June 01, 2010, 03:33:36 AM »
i use single wires with crimp pins on the end to connect to the pins of the PS2 like the ones in the following link that came with my arduino
http://www.littlebirdelectronics.com/products/Premium-Jumper-Wire-10%252dPack-M%252dF-12%22-Green.html

you can make your own by buying some crimp pins and crimping a pin to both sides of the wire and use either a housing as they use or some heat shrink works well to, and looks just as cool.

Cheers

Joker94

Offline flokos

  • Jr. Member
  • **
  • Posts: 17
  • Helpful? 0
Re: PlayStation 2 Controller Arduino Library
« Reply #6 on: June 01, 2010, 03:42:43 AM »
i will make an adapter my shelf cause i cant wait ...

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: PlayStation 2 Controller Arduino Library
« Reply #7 on: June 01, 2010, 07:01:04 AM »
hey MadSci,
i'm sure Aduino code will be useful to some people
but do you know WebbotLib does PS2 controllers now?


I'm aware, and have already played around with that too. I think both platforms are excellent platforms, and each have strengths and weaknesses for different projects. I wanted to make this one out of an Arduino. I'm also trying webbotlib on a Baby Orangutan.

Quote
(PS. http://www.billporter.info/wp-content/uploads//2010/05/PSX_example.zip is just an empty file.)


Oops. Fixed.

Offline Aligator

  • Jr. Member
  • **
  • Posts: 25
  • Helpful? 0
Re: PlayStation 2 Controller Arduino Library
« Reply #8 on: June 02, 2010, 01:33:17 AM »
hi madsci

could you please post a list of what colour wire connect to what pin of the arduino

thanks you

Aligator

Offline HyperNerd

  • Robot Overlord
  • ****
  • Posts: 139
  • Helpful? 5
  • N3rd1n80r
Re: PlayStation 2 Controller Arduino Library
« Reply #9 on: June 02, 2010, 06:25:21 AM »
Quote
could you please post a list of what colour wire connect to what pin of the arduino

From the linked page in the OP:

PIN     Name             Color
10        Attention     Yellow
11        Command     Orange
12        Data              Brown
13       Clock            Blue

Hope this helps ;)

 -HyperNerd
There are 10 types of people in the world - those who understand binary, and those who don't.

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: PlayStation 2 Controller Arduino Library
« Reply #10 on: June 02, 2010, 07:43:58 AM »
^^^^ Thanks for answering him hyper.

Like hyper posted, the wiring diagram is on my website.

Also note, I am using a 5V arduino without level shifting and it worked fine with the controller also powered with 5V. Hell, it worked fine without the controller powered at all, oddly enough.

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: PlayStation 2 Controller Arduino Library
« Reply #11 on: June 02, 2010, 09:26:06 AM »
Someone emailed it to HACK-a-DAY!!!!!

I'm on hack-a-day!!!! Awesome!!!

http://hackaday.com/2010/06/02/hdsps-and-playstation-2-controllers-on-arduinos/

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: PlayStation 2 Controller Arduino Library
« Reply #12 on: June 02, 2010, 04:48:47 PM »
Video Of a PS2 Controller + SAGAR robot.

PlayStation 2 Controller Arduino Library Demonstration

Offline HyperNerd

  • Robot Overlord
  • ****
  • Posts: 139
  • Helpful? 5
  • N3rd1n80r
Re: PlayStation 2 Controller Arduino Library
« Reply #13 on: June 03, 2010, 03:51:23 AM »
Nice one!

I have a suggestion for the library if possible: force feedback. As of yet, I don't know whether the motors are triggered by a command through the SPI bus, or whether they are simply linked to the grey wire (motor power).

Force feedback could be really useful for robots which are driven remotely via a wireless cam for example, with a non-contact IR bumper triggering the vibration motor in the controller.

 -HyperNerd
There are 10 types of people in the world - those who understand binary, and those who don't.

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: PlayStation 2 Controller Arduino Library
« Reply #14 on: June 03, 2010, 06:46:16 AM »
I'm gonna look at force feedback this weekend. I think it should be easy.

Offline KurtEck

  • Robot Overlord
  • ****
  • Posts: 217
  • Helpful? 12
Re: PlayStation 2 Controller Arduino Library
« Reply #15 on: June 03, 2010, 07:43:51 AM »
Hi Bill, great work!

Now I am torn.  I have been thinking of playing some with an Arduino Mega and one of the things I was going to port over was my PS2 code that I have working on the Axon2.  But you beat me to it!

Now I am trying to decide which way to go.  A couple of things I did differently was that my Read from the controller returned a structure, that had a union with defining simple bytes as well as individual bits for each button.  No big deal for me to convert to your definition.

But the other thing I do with my code is I only want to process a button press once.    My code in the main file does something like:
Code: [Select]

PS2PACKET g_ps2p;
PS2PACKET g_ps2pPrev;
...
if (PS2_Query(g_ps2p.bData, sizeof(g_ps2p)))   // do the query
{
    if (!g_ps2p.s.fNStart && g_ps2pPrev.s.fNStart)
    {
        ...
    }
...
    g_ps2pPrev = g_ps2p;   // Save the old state away...
}
So I am trying to figure out a good way to save away the state from the previous query and comparing against the previous loop.  Note in the above it is assumed that if the value is high, the button is not pressed. 

Suggestions?
Thanks again for your work!
Kurt

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: PlayStation 2 Controller Arduino Library
« Reply #16 on: June 03, 2010, 05:14:57 PM »
Hmm,

I do like your structure setup, however I think for newbie users that don't understand structs that a "If button pressed True/false" like what i did is easy for them to understand / use.

But for power users I can see this being annoying. What if I have a function that just returns the two bytes of button states? Then you can store it as an unsigned int, mask whatever buttons you want out of it, or bitwise compare the last value with the new one to see if any button states changed?

if (last int ^ current int)

would only return true if 1 or more buttons changed states.

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: PlayStation 2 Controller Arduino Library
« Reply #17 on: June 03, 2010, 06:03:53 PM »
So, I liked the idea you gave me so much, I added it directly to the library. Now the example code is :

Code: [Select]

  if (psx.NewButtonState())
  {
      if(psx.Button(PSB_SELECT))
        Serial.print("Select\n");
      if(psx.Button(PSB_L3))
       Serial.print("L3\n");
      if(psx.Button(PSB_R3))
       Serial.print("R3\n");
      if(psx.Button(PSB_START))
       Serial.print("Start\n");
      if(psx.Button(PSB_PAD_UP))
       Serial.print("Up\n");
      if(psx.Button(PSB_PAD_RIGHT))
       Serial.print("Right\n");
      if(psx.Button(PSB_PAD_LEFT))
       Serial.print("LEFT\n");
      if(psx.Button(PSB_PAD_DOWN))
       Serial.print("DOWN\n");
      if(psx.Button(PSB_L1))
       Serial.print("L1\n");
      if(psx.Button(PSB_R1))
       Serial.print("R1\n");
      if(psx.Button(PSB_L2))
       Serial.print("L2\n");
      if(psx.Button(PSB_R2))
       Serial.print("R2\n");
      if(psx.Button(PSB_GREEN))
       Serial.print("Triangle\n");
      if(psx.Button(PSB_RED))
       Serial.print("Circle\n");
      if(psx.Button(PSB_PINK))
       Serial.print("Square\n");
      if(psx.Button(PSB_BLUE))
        Serial.print("X\n");
  }

So only when a button is first pressed will any of that run. Now when i press a button, it's only printed out to my terminal once, and won't print out again till i release and press the button a second time.

Does this help you?
I also added a function to get the 2 raw bytes of button states to do as you please.

EDIT: I realize the flaw in this is that you press a second button while holding the first, both buttons will process again. I think I'm going to add a third function that just returns what buttons changed states.
« Last Edit: June 03, 2010, 06:07:49 PM by madsci1016 »

Offline KurtEck

  • Robot Overlord
  • ****
  • Posts: 217
  • Helpful? 12
Re: PlayStation 2 Controller Arduino Library
« Reply #18 on: June 03, 2010, 06:43:38 PM »
Sounds great.

For about 90+% of the cases, you only want to process the buttons once which your new functions should help out with.

However there are a few cases, where I want to do something until I release the button.  Example, my Mech-brat.  On it, I have two buttons (I think L1 and R1) that when I press them, I want the Left gun or right gun to start firing and continue to fire until I release the button.

Actually the code in the case I mention is slightly more complicated.  That is when you release it, it does not turn the gun off, until it senses that gun has fired and then it changes the state of the picoswitch... But that is off topic.

Kurt

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: PlayStation 2 Controller Arduino Library
« Reply #19 on: June 03, 2010, 07:33:50 PM »
Ok, so here's the low down on the new functions:

psx.Button(this_button) is TRUE if this_button is currently pressed
psx.NewButtonState() will return TRUE if any button changed state
psx.NewButtonState(this_button) will return TRUE if this_button changed state
psx.ButtonPressed(this_button) will return TRUE if this_button was just pressed
psx.ButtonReleased(this_button) will return TRUE if this_button was just released

I think that this should cover all possible uses for a button. Anyone have any other suggestions?

Offline KurtEck

  • Robot Overlord
  • ****
  • Posts: 217
  • Helpful? 12
Re: PlayStation 2 Controller Arduino Library
« Reply #20 on: June 03, 2010, 08:26:40 PM »
Sounds great.  Now I just have to get off my... And start trying out the Arduino!

Kurt

Offline HyperNerd

  • Robot Overlord
  • ****
  • Posts: 139
  • Helpful? 5
  • N3rd1n80r
Re: PlayStation 2 Controller Arduino Library
« Reply #21 on: June 04, 2010, 05:13:36 AM »
Nice!

I've ordered a PS2 extension cable to hack up for the controller connector, so I should be able to try all this out next week ;D

 -HyperNerd
There are 10 types of people in the world - those who understand binary, and those who don't.

Offline Anoroc

  • Full Member
  • ***
  • Posts: 48
  • Helpful? 0
Re: PlayStation 2 Controller Arduino Library
« Reply #22 on: June 04, 2010, 08:06:43 AM »
This is freakin' awesome. I'm nowhere near ready to tackle this just yet, (revisiting my first attempt at building a robot), but my gears are turning. This may be a "no-brainer" for you robot building vets; Could the shoulder buttons be used to rotate a camera?


Offline HyperNerd

  • Robot Overlord
  • ****
  • Posts: 139
  • Helpful? 5
  • N3rd1n80r
Re: PlayStation 2 Controller Arduino Library
« Reply #23 on: June 04, 2010, 10:31:43 AM »
I see no reason why the shoulder buttons could not be used if you used the appropriate software / hardware, ie a servo.

Just write some code which changes the position of the servo if one of the buttons is pressed.

 -HyperNerd
There are 10 types of people in the world - those who understand binary, and those who don't.

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: PlayStation 2 Controller Arduino Library
« Reply #24 on: June 04, 2010, 10:48:32 PM »
Ok, after hours of coding, I have it so you can set the pins, (hopefully) read the pressure values, and activate the rumble.

It took a major redesign of how the original code configured the controller, because well, the original code didn't work. Really the code already released should be locking the controller so you can't change the analog, digital mode.

Will post it all tomorrow after I clean it up and verify analog pressures.

Offline KurtEck

  • Robot Overlord
  • ****
  • Posts: 217
  • Helpful? 12
Re: PlayStation 2 Controller Arduino Library
« Reply #25 on: June 05, 2010, 07:49:56 AM »
Hi Bill, sounds good.

One thing I have found in different implementations of this, is that sometimes the PS2 will for some reason drop out of Analog mode.  This happens for example if you don't call the PS2 query function often enough.  On my Axon Brat this happens if I was controlling the brat with my XBEE controller and then decide to use the PS2. 

So the query function my implementation for the Axon2 on Webbotlib looks like:
Code: [Select]
boolean PS2_Query(unsigned char *paData, int cb)
{
int iLoopCnt;
unsigned char bMode;

// Hack to not have this loop forever in case of error
for (iLoopCnt = 0; iLoopCnt < 10; iLoopCnt++)
{
//spiDeviceSelect(PS2_device, TRUE);
pin_low(PS2_SEL);


// shiftout CMD,CLK,FASTLSBPRE,[$1\8]
gameByte(0x1);

//shiftin DAT,CLK,FASTLSBPOST,[DS2Mode\8]
bMode = gameByte(0x42);

if ((bMode & 0xf0) == 0x70)
{
// we are in an analog mode so continue to read in the extra bytes.
gameByte(0); // ignore first byte which should return 0x5a
while (cb > 0)
{
*paData++ = gameByte(0);
cb--;
}

// And clear our select line...
pin_high(PS2_SEL);

return TRUE;
}

pin_high(PS2_SEL);
clockWaitms(1);

pin_low(PS2_SEL);
// Not initialized properly, try to send out the correct init sequences.

pin_low(PS2_SEL);
// shiftout CMD,CLK,FASTLSBPRE,[$1\8,$43\8,$0\8,$1\8,$0\8] ;CONFIG_MODE_ENTER
static unsigned char _aps2_2a[] = {0x01, 0x43, 0x0, 0x1, 0x0};
_PS2_ShiftOut(_aps2_2a, sizeof(_aps2_2a));
pin_high(PS2_SEL);
clockWaitms(1);

pin_low(PS2_SEL);
// shiftout CMD,CLK,FASTLSBPRE,[$01\8,$44\8,$00\8,$01\8,$03\8,$00\8,$00\8,$00\8,$00\8] ;SET_MODE_AND_LOCK
static unsigned char _aps2_3[] = {0x01, 0x44, 0, 1, 3, 0, 0, 0, 0};
_PS2_ShiftOut(_aps2_3, sizeof(_aps2_3));
pin_high(PS2_SEL);
clockWaitms(1);

pin_low(PS2_SEL);
// shiftout CMD,CLK,FASTLSBPRE,[$01\8,$43\8,$00\8,$00\8,$5A\8,$5A\8,$5A\8,$5A\8,$5A\8] ;CONFIG_MODE_EXIT_DS2_NATIVE
static unsigned char _aps2_5[] = {0x01, 0x43, 0, 0, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a};
_PS2_ShiftOut(_aps2_5, sizeof(_aps2_5));
pin_high(PS2_SEL);
// clockWaitms(1);

}

// If we got here it means that we tried a number of times and it failed, so set up a set of default values and return.

paData[0] = 255;
paData[1] = 255;
paData[2] = 128;
paData[3] = 128;
paData[4] = 128;
paData[5] = 128;

return FALSE;
}
In the above code, each time I do a query I verify that I am still in Analog mode. 

Note: this function is not totally correct.  It is fine for me as I don't read in the Analog pressures, but in a more general case, I should be checking to see if the caller is asking for additional bytes and if so, then I should verify the current mode is  0x79 and not 0x73.  If not enough data is being returned need to use some of the configuration stuff I removed (like entering into native mode...).

In your new code are you always returning the additional information in the query, or do you let the caller configure it as to not take the time to shift in the additional 12 bytes of data, unless they are actually interested in the analog pressures.

Again sounds great.  Will be nice to specify which pins to use. 

Thanks again for your hard work on this.
Kurt

Offline HyperNerd

  • Robot Overlord
  • ****
  • Posts: 139
  • Helpful? 5
  • N3rd1n80r
Re: PlayStation 2 Controller Arduino Library
« Reply #26 on: June 05, 2010, 08:40:41 AM »
The cable arrived, I hacked it up and made a breadboard-friendly connector with some perfboard and a 9-pin header.

The library works perfectly with an official Sony DualShock 2 controller, but I can't seem to get it to work with a BW-333B Tri-format Steering Wheel (no idea of the manufacturer).

On a normal PS2 console (fat version), the wheel responds perfectly and all functions (including pedals) work as they should.
When I connect it to the arduino and run the exampole program, the wheel doesn't power up or respond at all.

I'm using the onboard 3.3v regulator to power the controller, and the arduino is powered from the USB port.

I hope you guys can help.

 -HyperNerd
There are 10 types of people in the world - those who understand binary, and those who don't.

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: PlayStation 2 Controller Arduino Library
« Reply #27 on: June 05, 2010, 09:51:23 AM »
Kurt,

I have put thought into the problem, however, from what I can tell, just querying the controller for it's state also causes it to send button states, or clear them from it's buffer. This would set up the scenario that my "justpressed" and "justreleased" functions would work incorrectly, as they compare the new state to the last state. But if I poll the controller twice in one "read_gamepad" function, what state do i compare to the last state? If the timming was right, i could miss the justpressed and just released states.

I think I'm just going to track time since last call, and if it's over a threshold, send the configuration strings again.

Hyper:

Man, that's an odd one. communications between a controller and a guitarhero guitar hero have been documented, however I haven't found a wheel yet. See if you can find someone who captured the traffic to it.

A wild guess may be that the controller uses the second power wire (rumble power) as maybe it's electronics require more then the PS2 controller power wire can source. Try applying 7.6-9V to the rumble power wire.

Also, check to make sure the controller isn't drawing to much from the arduino's small 3.3V regulator.

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: PlayStation 2 Controller Arduino Library
« Reply #28 on: June 05, 2010, 10:00:11 AM »
Also, to answer your question, I default to Analog mode, and you have to call

 ps2x.enableRumble();
 ps2x.enablePressures();

to increase the data returned from the controller.

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: PlayStation 2 Controller Arduino Library
« Reply #29 on: June 05, 2010, 12:45:22 PM »
New Library (v1.0) finished.

http://www.billporter.info/?p=240

All the options you could ever want. See example sketch for details.

 


Get Your Ad Here

data_list