Society of Robots - Robot Forum

Software => Software => Topic started by: madsci1016 on May 31, 2010, 09:58:21 PM

Title: PlayStation 2 Controller Arduino Library
Post by: madsci1016 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 (http://www.billporter.info/?p=219)

Title: Re: PlayStation 2 Controller Arduino Library
Post by: Xyver on May 31, 2010, 11:19:34 PM
Thank you!  I will use this one day.  Ive always wanted a playstation RC.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: Joker94 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
Title: Re: PlayStation 2 Controller Arduino Library
Post by: dunk 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 (http://www.billporter.info/wp-content/uploads//2010/05/PSX_example.zip) is just an empty file.)
Title: Re: PlayStation 2 Controller Arduino Library
Post by: flokos on June 01, 2010, 02:54:53 AM
Is there any adapter to interface it with arduino without having the plug cut.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: Joker94 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 (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
Title: Re: PlayStation 2 Controller Arduino Library
Post by: flokos on June 01, 2010, 03:42:43 AM
i will make an adapter my shelf cause i cant wait ...
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 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 (http://www.billporter.info/wp-content/uploads//2010/05/PSX_example.zip) is just an empty file.)

Oops. Fixed.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: Aligator 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
Title: Re: PlayStation 2 Controller Arduino Library
Post by: HyperNerd 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
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 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.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 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/ (http://hackaday.com/2010/06/02/hdsps-and-playstation-2-controllers-on-arduinos/)
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on June 02, 2010, 04:48:47 PM
Video Of a PS2 Controller + SAGAR robot.

PlayStation 2 Controller Arduino Library Demonstration (http://www.youtube.com/watch?v=LMgzF7qeeEY#ws)
Title: Re: PlayStation 2 Controller Arduino Library
Post by: HyperNerd 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
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on June 03, 2010, 06:46:16 AM
I'm gonna look at force feedback this weekend. I think it should be easy.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: KurtEck 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
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 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.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 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.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: KurtEck 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
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 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?
Title: Re: PlayStation 2 Controller Arduino Library
Post by: KurtEck 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
Title: Re: PlayStation 2 Controller Arduino Library
Post by: HyperNerd 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
Title: Re: PlayStation 2 Controller Arduino Library
Post by: Anoroc 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?

Title: Re: PlayStation 2 Controller Arduino Library
Post by: HyperNerd 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
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 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.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: KurtEck 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
Title: Re: PlayStation 2 Controller Arduino Library
Post by: HyperNerd 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
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 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.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 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.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on June 05, 2010, 12:45:22 PM
New Library (v1.0) finished.

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

All the options you could ever want. See example sketch for details.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: HyperNerd on June 05, 2010, 02:03:39 PM
Above post made me ^o^ ;D

But I have a question: what voltage do I need to apply to the grey (motor power) wire? I don't think it says anywhere in the download or on the blog page.

 -HyperNerd
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on June 05, 2010, 05:23:20 PM
It's in the photo on my blog page.

7.2 - 9V

I also said it in my post.

Try applying 7.6-9V to the rumble power wire.
:P
Title: Re: PlayStation 2 Controller Arduino Library
Post by: Joker94 on June 05, 2010, 08:02:00 PM
Great job MadSci  ;D

so i can make the following changes to get it to work for what i want it to do

Code: [Select]

if(ps2x.Button(PSB_PAD_UP)) {         //will be TRUE as long as button is pressed
     Serial.print("Up held this hard: ");
     Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);

to
Code: [Select]
if(ps2x.Button(PSB_PAD_UP)) {         //will be TRUE as long as button is pressed
     myservo.write(val)

exchange the serial.print function with a servo command to control my robot.

Great Job

Cheers

Joker94
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on June 05, 2010, 08:26:26 PM
if your goal was to have the servo move, and keep moving while you held the button down, change it to

Code: [Select]
if(ps2x.Button(PSB_PAD_UP))         //will be TRUE as long as button is pressed
     myservo.write(++val)

so that every time it loops and sees the button is pressed, it adds a little to the value you send the servo.

You could also get fancy with the pressure values and have the servo move faster the harder you press the button.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: Ro-Bot-X on June 09, 2010, 04:00:28 PM
Will it work with this one?
http://cgi.ebay.com/PS2-Sony-Playstation-2-Wireless-Game-Controller-Black-/250638410320?cmd=ViewItem&pt=AU_PC_Video_Games_Games&hash=item3a5b36a250#ht_4944wt_1133 (http://cgi.ebay.com/PS2-Sony-Playstation-2-Wireless-Game-Controller-Black-/250638410320?cmd=ViewItem&pt=AU_PC_Video_Games_Games&hash=item3a5b36a250#ht_4944wt_1133)
Title: Re: PlayStation 2 Controller Arduino Library
Post by: amando96 on June 09, 2010, 04:25:24 PM
^^^^ 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.

use a level shifter, it will eventually stop working.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on June 09, 2010, 11:35:21 PM
Will it work with this one?
http://cgi.ebay.com/PS2-Sony-Playstation-2-Wireless-Game-Controller-Black-/250638410320?cmd=ViewItem&pt=AU_PC_Video_Games_Games&hash=item3a5b36a250#ht_4944wt_1133 (http://cgi.ebay.com/PS2-Sony-Playstation-2-Wireless-Game-Controller-Black-/250638410320?cmd=ViewItem&pt=AU_PC_Video_Games_Games&hash=item3a5b36a250#ht_4944wt_1133)

It might. However, a lot of my research when I was writing this, people were stating to have much more luck interfacing with genuine Sony controllers, as opposed to fake knock offs. It's all the save protocol for the interface, but different tolerances on timing and such.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: Admin on June 10, 2010, 09:00:03 PM
Without looking at your code, a quick question . . .

What features in your PS2 code do you think could be added to WebbotLib's/Dunks PS2 code?
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on June 10, 2010, 09:36:01 PM
From the looks of it, here's what I have that Webbot doesn't.

My lib compares previous states, so you don't have to. So i have

buttonPressed(),  buttonReleased(), buttonHeld();

programs that will only return true if the button changed states from the last read. This will keep code from running more then once as a button is being held, if you don't want it to. There's also a global compare, that will let you skip all your button tests if nothing from the controller data changed.

I also support rumble/vibrate. Good for feedback. For example, I'm going to have SAGAR's controller vibrate every time SAGAR throws an error, like low voltage or other program error.

I also support full analog mode, meaning you can actually read how 'hard' a button is pressed. What I heard from someone is that they wanted to tie the red circle to brakes, and the harder they press, the more their robot brakes.

I might add a stick calibrate function like webbot has, as it looks useful.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: Webbot on June 11, 2010, 08:41:37 PM
I've also been researching the 'rumble' and 'pressure switches' via the Parallax Forum posts which give some good examples - albeit in Basic - of the commands to send so quite easy to set up.

WebbotLib currently supports the D pad as a digital joystick but once I've finished the button pressure code then you will be able to use the D pad and also the right set of buttons as analogue joysticks as well - giving 4 joysticks per controller !!

You may also want to look at adding the deadzone code as well (thanks Dunk) as it makes the joystick less sensitive.

EDIT: typo
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on June 11, 2010, 08:48:51 PM
I've also been researching the 'rumble' and 'pressure switches' via the Parallax Forum posts which give some good examples - albeit in Basic - of the commands to send so quite easy to set uo.


Well, your welcome to look at my code, it's in C. here's the command sequence for different modes

Code: [Select]
static byte enter_config[]={0x01,0x43,0x00,0x01,0x00};
static byte set_mode[]={0x01,0x44,0x00,0x01,0x03,0x00,0x00,0x00,0x00};
static byte set_bytes_large[]={0x01,0x4F,0x00,0xFF,0xFF,0x03,0x00,0x00,0x00};
static byte exit_config[]={0x01,0x43,0x00,0x00,0x5A,0x5A,0x5A,0x5A,0x5A};
static byte enable_rumble[]={0x01,0x4D,0x00,0x00,0x01};


set_bytes_large enables the analog pressures.

Quote
You may also want to look at adding the deadzone code as well (thanks Dunk) as it makes the joystick less sensitive.

I was actually going to take this one step further and offer different response curves: linear, exponential, etc. The controllers for the robots at work let you select curves and it is very nice. 
Title: Re: PlayStation 2 Controller Arduino Library
Post by: flokos on June 14, 2010, 03:15:35 AM
I tested it with an ps2 controller, its not the genuine but its  compatible with the ps2.
And it didnt work i tested  the wiring again and again but the only thing i managed to do is change the state of the analog button led.
And the pins are declared correctly.
What im doing wrong?
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on June 14, 2010, 06:58:25 AM
There's a couple things to check:

Tripple check the wiring.
Check to make sure you have the right board selected in the Arduino IDE.
You are using a 3.3V Arduino or a  5V with a level shifter?
Are your providing power to the controller?

If all else fails, I have read that the non-official Sony controllers are less likely to work then an official Sony controller. You might be out of luck.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: KurtEck on June 14, 2010, 07:07:40 AM
I tested it with an ps2 controller, its not the genuine but its  compatible with the ps2.
And it didn't work i tested  the wiring again and again but the only thing i managed to do is change the state of the analog button led.
And the pins are declared correctly.
What im doing wrong?
Which one?
I have found that they are not all equal.  I know at Lynmotion, they have had the best luck with: Madcatz, pelican and now their own version.   Also I know that most people have not had any luck with logitech controllers, although recently someone did report some success on the thread: http://www.lynxmotion.net/viewtopic.php?f=22&t=6405 (http://www.lynxmotion.net/viewtopic.php?f=22&t=6405)

Things to try:
Power: Some controllers appear to need power applied to their vibration motor power wire.

You may need to play with delays with the init code.  Some need a little more or less delays between commands.

Make sure you are passing the correct voltage to the power the controller.  Some say 3.3V, but I have had no problems with 5V.  But I know it does not work with 6+V (forgot to switch my IO pins from VS(battery) to VL(+5v) on Bot Board 2

Check to see how often your program is calling the PS2 code.  For example I had a test program that would read the PS2 and if something changed print it out and Loop.  The program did not work well?  By adding a delay in this loop it started working.  Why?  I think that while the receiver is answering my calls, it can not talk to the transmitter and so they never could sync and get updated information.

Good Luck
Kurt
Title: Re: PlayStation 2 Controller Arduino Library
Post by: flokos on June 14, 2010, 07:53:48 AM
Quote
You are using a 3.3V Arduino or a  5V with a level shifter?
I have a 5v arduino (arduino duemilanove).

I will check the delays and change them a little and il see whats happening.

Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on June 14, 2010, 09:38:45 AM
Non-Sony controller won't work with 5V Arduinos, unless you use a level shifter.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on June 14, 2010, 09:41:06 AM
This guy also found the right resistor values to make non-Sony controllers work with 5V arduinos.

http://www.billporter.info/?p=219&cpage=2#comment-106 (http://www.billporter.info/?p=219&cpage=2#comment-106)

Title: Re: PlayStation 2 Controller Arduino Library
Post by: flokos on June 14, 2010, 10:37:29 AM
il try to make it work with the resistors way ...
But even if doesn't work for me it is a great library.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: TrickyNekro on June 14, 2010, 10:43:48 AM
But I think you'll need a 3.3V regulator, the 3.3V from the Arduino may not be just enough to power the controller...

I noticed the original has an on board regulator... that should do the trick...

Never the less it's a nice controller... Once exams are over I should get down to get mine working too ;-)
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on June 14, 2010, 11:17:15 AM
My controller functions without the power pin connected at all, so he may not need a 3.3V regulator for it. There is a resistor value for the Vpwr pin in the link i provided.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on June 14, 2010, 11:20:00 AM
I think I might start adding code to see if I can increase compatibility. Something like if the controller is not accepting commands, it auto increases the delays.

Would those with these non-Sony controllers that re having problems PM me an email address so I can have you test some tweaks?
Title: Re: PlayStation 2 Controller Arduino Library
Post by: TrickyNekro on June 14, 2010, 11:29:18 AM
I really haven't used the Arduino, so I can't tell...

From what I 've been reading, all the issues comes from timing differences, perhaps in the clock line and
data/command line... When it gets to recognize falling or rising edge, etc etc etc...


BTW... The whole PS2 communication protocol... Isn't SPI slave device behavior...
I really don't remember reading this somewhere... where it could be a little obvious...
I haven't read the starting of this loooooong post or the code... So, sorry if used/mentioned...


But I have seen so many sites out there that don't mention this...
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on June 19, 2010, 08:14:28 AM
I've added a new version that has some error reporting, and might increase compatibility with some controllers. Try it and let me know if it works / what it says.

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



Edit: I JUST found out the original code writer had bad math for the clock speed, he was off by a order of magnitude. The clock is running at spec now, let me know if that fixed things. My controller seemed to handle 50kHz fine, but it's suppose to be 500kHz clock speed.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: Admin on June 19, 2010, 08:24:00 AM
I'm getting a summer student at work soon. He's supposedly interested in mostly just programming, so after a discussion with my group, we've been thinking letting him figure out how to get our robot fish to be controlled with a joystick or playstation controller.

I'll set him loose on this, and let him decide on the libraries . . . so basically you guys will have a beta/bug tester.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: TrickyNekro on June 19, 2010, 09:43:28 AM
Admin throwing a bone or something... But really now... I'm dying to work in a team like yours ;-)

Do you actually pay you enough for upkeeping a mustang or something near???
I could do anything... well not anything... you get the point...


Sorry for the sidetrack but couldn't really resist...
Title: Re: PlayStation 2 Controller Arduino Library
Post by: Admin on June 21, 2010, 07:58:40 AM
Quote
Do you actually pay you enough for upkeeping a mustang or something near???
An employee of the US Government, with my skills, makes twice as much as a university researcher, but half that of an industry employee.

I don't even own a car . . .
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on July 13, 2010, 04:59:04 PM
New lib version 1.3 added. Should increase compatibility over 1.2

@Admin, did you summer intern give up on me? Java and a Xbox controller instead? Kids these days...
Title: Re: PlayStation 2 Controller Arduino Library
Post by: Admin on July 14, 2010, 01:08:25 PM
@Admin, did you summer intern give up on me? Java and a Xbox controller instead? Kids these days...
well, we decided a computer needed to be in the loop . . .
Title: Re: PlayStation 2 Controller Arduino Library
Post by: KurtEck on July 27, 2010, 07:36:56 AM
Thanks again for all of your work on this!  I started to play with it last night.

For the fun of it, I keywords.txt file in the PSX library file to have the Arduino IDE show up the keywords in their appropriate colors.  Not sure if anyone is interested, but...

Code: [Select]
#######################################
# Syntax Coloring Map PS2X
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################

PS2X KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################
Button KEYWORD2
ButtonDataByte KEYWORD2
NewButtonState KEYWORD2
NewButtonState KEYWORD2
ButtonPressed KEYWORD2
ButtonReleased KEYWORD2
read_gamepad KEYWORD2
read_gamepad KEYWORD2
config_gamepad KEYWORD2
enableRumble KEYWORD2
enablePressures KEYWORD2
Analog KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################
PSB_SELECT LITERAL1
PSB_L3 LITERAL1
PSB_R3 LITERAL1
PSB_START LITERAL1
PSB_PAD_UP LITERAL1
PSB_PAD_RIGHT LITERAL1
PSB_PAD_DOWN LITERAL1
PSB_PAD_LEFT LITERAL1
PSB_L2 LITERAL1
PSB_R2 LITERAL1
PSB_L1 LITERAL1
PSB_R1 LITERAL1
PSB_GREEN LITERAL1
PSB_RED LITERAL1
PSB_BLUE LITERAL1
PSB_PINK LITERAL1
PSS_RX LITERAL1
PSS_RY LITERAL1
PSS_LX LITERAL1
PSS_LY LITERAL1


Again thanks
Kurt
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on August 14, 2010, 04:01:49 PM
Updated to v1.4

*      Removed redundant functions.
*      Fixed mode check to include two other possible modes the controller could be in.
*       Added debug code enabled by compiler directives. See below to enable debug mode.
*      Added button definitions for shapes as well as colors.

still @ http://www.billporter.info/?p=240 (http://www.billporter.info/?p=240)

Kurt, that file didn't do anything for me. Care to explain what it's suppose to do?
Title: Re: PlayStation 2 Controller Arduino Library
Post by: KurtEck on August 15, 2010, 07:41:08 AM
Hi Bill,

If the file is in the directory where your library files are .cpp/.h, when you load your sketch with the sketch editor, the keywords will be appropriately colored.  

So for example with this code:
Code: [Select]
   ps2x.read_gamepad();          //read controller and set large motor to spin at 'vibrate' speed

    // Wish the library had a valid way to verify that the read_gamepad succeeded... Will hack for now
    if ((ps2x.Analog(1) & 0xf0) != 0x70) {
        // Controller did not return Analog mode...
        byte szTemp[80];
        int ich;
        ich = Sprint((char*)szTemp, "PS2 - error(%x): %x %x: %x %x %x %x\r\n", ps2x.Analog(1), ps2x.Analog(3), ps2x.Analog(4),
                ps2x.Analog(5), ps2x.Analog(6), ps2x.Analog(7), ps2x.Analog(8));
        Serial.write(szTemp, ich);      
        return 100;
    }
...
    if(ps2x.ButtonPressed(PSB_START)) { // OK lets try "0" button for Start.

The methods: read_gamepad, Analog and ButtonPressed will show up in red.  Likewise the constant PSB_START will show up in blue.  It adds nothing to the functionality, but it does help to know I typed in something correctly...

Side note: I was running into cases where I was dropping out of Analog mode so added the above to detect it and not process the data like it was valid...

Thanks again for your work, I will download and try the new version.  I like that you added the button shapes as alternatives.  I normally used those in the past!

Kurt

P.S. - I updated my keywords.txt file to include the new stuff...
Code: [Select]
#######################################
# Syntax Coloring Map PS2X
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################

PS2X KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################
Button KEYWORD2
ButtonDataByte KEYWORD2
NewButtonState KEYWORD2
ButtonPressed KEYWORD2
ButtonReleased KEYWORD2
read_gamepad KEYWORD2
config_gamepad KEYWORD2
enableRumble KEYWORD2
enablePressures KEYWORD2
Analog KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################
PSB_SELECT LITERAL1
PSB_L3 LITERAL1
PSB_R3 LITERAL1
PSB_START LITERAL1
PSB_PAD_UP LITERAL1
PSB_PAD_RIGHT LITERAL1
PSB_PAD_DOWN LITERAL1
PSB_PAD_LEFT LITERAL1
PSB_L2 LITERAL1
PSB_R2 LITERAL1
PSB_L1 LITERAL1
PSB_R1 LITERAL1
PSB_GREEN LITERAL1
PSB_RED LITERAL1
PSB_BLUE LITERAL1
PSB_PINK LITERAL1
PSB_TRIANGLE LITERAL1
PSB_CIRCLE LITERAL1
PSB_CROSS LITERAL1
PSB_SQUARE LITERAL1
PSS_RX LITERAL1
PSS_RY LITERAL1
PSS_LX LITERAL1
PSS_LY LITERAL1

PSAB_PAD_RIGHT LITERAL1
PSAB_PAD_UP LITERAL1
PSAB_PAD_DOWN LITERAL1
PSAB_PAD_LEFT LITERAL1
PSAB_L2 LITERAL1
PSAB_R2 LITERAL1
PSAB_L1 LITERAL1
PSAB_R1 LITERAL1
PSAB_GREEN LITERAL1
PSAB_RED LITERAL1
PSAB_BLUE LITERAL1
PSAB_PINK LITERAL1
PSAB_TRIANGLE LITERAL1
PSAB_CIRCLE LITERAL1
PSAB_CROSS LITERAL1
PSAB_SQUARE LITERAL1
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on August 15, 2010, 08:29:18 AM
Side note: I was running into cases where I was dropping out of Analog mode so added the above to detect it and not process the data like it was valid...

My library locks the controller so you can't change it's mode using the analog button, so how are you dropping out of analog? The only way i know of is if you are not polling it enough, and if so, the read function will catch it not in the correct mode and reconfigure it before returning.

I'll try the keywords file again to see if i can get it to work, and add it to the lib release.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: KurtEck on August 15, 2010, 09:30:36 AM
My library locks the controller so you can't change it's mode using the analog button, so how are you dropping out of analog? The only way i know of is if you are not polling it enough, and if so, the read function will catch it not in the correct mode and reconfigure it before returning.
Not sure how it is happening.  I find that it sometimes reports no PS2 is available at startup, but then the read_gamepad works fine. Will Investigate.  Maybe the internal pull-ups are not strong enough and need external one on data line.  I had to do this on one of my robots connected to a BasicMicro Basic Atom Pro 28.

As for keywords.txt: If you goggle "Arduino keywords.txt" one of the returned entries is: http://www.arduino.cc/en/Hacking/LibraryTutorial (http://www.arduino.cc/en/Hacking/LibraryTutorial) .  In this tutorial it mentions:
Quote
Each line has the name of the keyword, followed by a tab (not spaces), followed by the kind of keyword

Kurt
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on August 15, 2010, 11:49:56 AM
I did find that via google, and skimmed it, but couldn't get the words to change color in the arduino ide. I only spent 10 minutes on it, so it may have just been i didn't do something right. I'll have to try it again.


Version 1.4 fixes an issue where some modes are not detected at startup, that may have been your issue as well. If you did a hard reset and it came back as not detected, it was because the controller was still in analog and my lib didn't like that.

there's also a //#define PS2X_DEBUG  and //define PS2X_COM_DEBUG you can un-comment that will dump all communications to serial if you think there's some wiring issue.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on September 13, 2010, 04:21:15 PM
I've got a heck of a youtube message asking for help, i'm going to bring it here to answer, so it can help others with similar problems.

The message:

Was actually up till 4am last night fighting my Duemilanove trying to get it to recognize the PS2 controller. Still haven't had any luck on that.

I used an PS2 Controller extension cord to save my PS2 Controller from having it's connector cut off. I'm starting to think the signal strength isn't good enough over that long of cable?


---------------------------------------------------------------------------------------------------------------------------



I've also been having a hard time sorting out the correct wiring on the Duemilanove. I've included a description of what I've been doing if you have time to look over it.

From your video it looked like you were numbering pins based off the silk screened numbers on the Pro Mini.

According to the example sketch:
"error = ps2x.config_gamepad(13,11,10,12); //setup GamePad(clock, command, attention, data) pins, check for error"

Which I assume means:
13=clock
11=command
10=attention
12=data

Based on the Pro-Mini's schematic: I translated those pin numbers to the following values:

PD5=clock
PB3=data
PB4=command
PB5=attention

Which on the Duemilanove:
PD5=digital 5
PB3=PWM 11 ?digital?
PB4=?digital? 12
PB5=?digital? 13


This is the error message that results from that setup:(along with every other setup I've tried)
"OUT:IN
1:FF
42:FF
0:FF
0:FF
0:FF
0:FF
0:FF
0:FF
0:FF
OUT:IN
1:FF
42:FF
0:FF
0:FF
0:FF
0:FF
0:FF
0:FF
0:FF
Controller mode not matched or no controller found
Expected 0x41 or 0x73, got FF
No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info (http://www.billporter.info) for troubleshooting tips"

There was one exception to this FF error and that was when I simply followed the silk screened pin numbers on the Duemilanove, I got this msg:(from memory)
"Controller mode not matched or no controller found
Expected 0x41 or 0x73, got 0
No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info (http://www.billporter.info) for troubleshooting tips"

The distinction being the 0 instead of the FF.


I have the controller's "5. red: power 3.3v" going to the Duemilanove's "3V3" next to "5V"
"4.black:ground" is going to the "Gnd" next to "5V" and "3V3".

I don't have "9. green: acknowledge"; "8. white unknown" or "3. grey: vibration motor power" connected to anything at the moment. I tried running the "3. grey" to both "5V" and a 9VDC powersupply and that didn't seem to improve anything.

I'm using a genuine Sony controller, but I've also tried using 5.6K and 180ohm resistors based on the comments on your blog. I did not have any of the 3rd type of resistor, but since I had a "3V3" 3.3VDC power source on my arduino it didn't seem necessary.

I hate to cut my extension cord short or even resort to cutting the extension cord out and removing the PS2 controller connector: but they seem to be the most significant distinction between my procedure and what everyone else has been doing.

Any advice is appreciated.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on September 13, 2010, 04:27:01 PM

According to the example sketch:
"error = ps2x.config_gamepad(13,11,10,12); //setup GamePad(clock, command, attention, data) pins, check for error"

Which I assume means:
13=clock
11=command
10=attention
12=data

Based on the Pro-Mini's schematic: I translated those pin numbers to the following values:

PD5=clock
PB3=data
PB4=command
PB5=attention

Which on the Duemilanove:
PD5=digital 5
PB3=PWM 11 ?digital?
PB4=?digital? 12
PB5=?digital? 13

This is your problem if I understand correctly. You don't need to do any sort of port:pin translating across different Arduino's. The library uses the Arduino's built in port:pin mapping scheme, so you just need to change the Arduino pin numbers ps2x.config_gamepad() call to whatever Arduino pins you are using. Pin 13 = clock for me, will also be pin 13 = clock for you; not pin 5.

So if you don't change
Code: [Select]
ps2x.config_gamepad(13,11,10,12); then you will be wiring your Duemilanove the same way I wired my pro-minil using pins 10-13.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: Striker121 on September 14, 2010, 06:19:48 PM
I'm having the EXACT same problem as you mentioned before with the "Expected 0x41 or 0x73, got FF" error. I have no idea what's wrong. The Arduino is a Duemilanove and it is an original Sony PS2 controller. It works on the PS2 fine and the wires are ALL wire correctly as far as both ports and a solid connection go. I've even tried modifying it so that the pins used are all PWM capable pins on the off chance that that might do something...it didn't. Any ideas as to what could be wrong? I'm really looking forward to getting my bot moving for once :) http://letsmakerobots.com/node/22451 (http://letsmakerobots.com/node/22451) There for anyone wondering what it is. EDIT: There is also a large bundle of copper wire in the center of the cord... no idea what that's for.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: KurtEck on September 14, 2010, 08:04:41 PM
One thing that you might try is an external pull-up resistor on the data line.  I have found that sometimes the weak pull-ups that are part of a processor are not sufficient on for some PS2 controllers.  I have found that one controller may work and another may not... This helped one one of my Arduino Megas.  I soldered in the pull-up inside the case of the wireless receiver.

Kurt
Title: Re: PlayStation 2 Controller Arduino Library
Post by: Striker121 on September 14, 2010, 08:11:34 PM
Well, that should be fairly easy to test, I'll likely get back to you tomorrow on how that worked (Or didn't, who knows).
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on September 15, 2010, 05:23:34 PM
I've noticed all the people having problems are using a Duemilanove board. I'm going to try it on a Duemilanove to see if that has anything to do with it.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on September 15, 2010, 07:07:53 PM
Well, i'll still try but this shows the Duemilanove works ok.

Scanalogic 2 Logic Analyzer Review / Tutorial / Demo (http://www.youtube.com/watch?v=IHQvA3lmObw&feature=player_embedded#ws)

Full blog link (http://store.curiousinventor.com/blog/scanalogic_2_logic_analyzer_review/)
Title: Re: PlayStation 2 Controller Arduino Library
Post by: Striker121 on September 18, 2010, 08:47:55 AM
Okay, I just tried using a 10K pullup between Data and VCC (Both 5v and 3.3V) Neither one worked. All gave the same "FF" etc error. Any ideas? EDIT: Perhaps taking the controller apart and soldering the pullup closer? (I think I've heard of that mattering with pullups(?))
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on September 18, 2010, 09:02:53 AM
Striker121, start by changing all the wires back to the same pins used in the example sketch. Double check the pin positions in the diagram on my website (http://www.billporter.info/?p=240).

What voltage are you running your controller at? 3.3V, or 5V?

EDIT:

Here's a quote from CuriousInventor regarding power:

"Red - Power: Many sites label this as 5V, and while this may be true for Play Station 1 controllers, we found several wireless brands that would only work at 3.3V. Every controller tested worked at 3.3V, and the actual voltage measured on a live Playstation talking to a controller was 3.4V. McCubbin  says that any official Sony controller should work from 3-5V. "
Title: Re: PlayStation 2 Controller Arduino Library
Post by: Striker121 on September 19, 2010, 12:04:31 PM
Holy crap it actually works now. Apparently it needed BOTH the pullup resistor AND the pins in the original positions.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on September 19, 2010, 12:16:18 PM
This was using the Arduino mega, correct?

So moving the pins around, and changing the pin numbers in the example sketch doesn't work?
Title: Re: PlayStation 2 Controller Arduino Library
Post by: Striker121 on September 19, 2010, 08:32:03 PM
No, this is on a Duemilanove. But yes, it would seem editing the pins in the sketch doesn't actually work. Had to move the wires to the original pins to get it working + have the 10K pullup on data.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: cyberdynewins on September 20, 2010, 06:30:42 PM
I've got a heck of a youtube message asking for help, i'm going to bring it here to answer, so it can help others with similar problems.

The message:

Was actually up till 4am last night fighting my Duemilanove trying to get it to recognize the PS2 controller. Still haven't had any luck on that.

I used an PS2 Controller extension cord to save my PS2 Controller from having it's connector cut off. I'm starting to think the signal strength isn't good enough over that long of cable?


---------------------------------------------------------------------------------------------------------------------------



I've also been having a hard time sorting out the correct wiring on the Duemilanove. I've included a description of what I've been doing if you have time to look over it.

From your video it looked like you were numbering pins based off the silk screened numbers on the Pro Mini.

According to the example sketch:
"error = ps2x.config_gamepad(13,11,10,12); //setup GamePad(clock, command, attention, data) pins, check for error"

Which I assume means:
13=clock
11=command
10=attention
12=data

Based on the Pro-Mini's schematic: I translated those pin numbers to the following values:

PD5=clock
PB3=data
PB4=command
PB5=attention

Which on the Duemilanove:
PD5=digital 5
PB3=PWM 11 ?digital?
PB4=?digital? 12
PB5=?digital? 13


This is the error message that results from that setup:(along with every other setup I've tried)
"OUT:IN
1:FF
42:FF
0:FF
0:FF
0:FF
0:FF
0:FF
0:FF
0:FF
OUT:IN
1:FF
42:FF
0:FF
0:FF
0:FF
0:FF
0:FF
0:FF
0:FF
Controller mode not matched or no controller found
Expected 0x41 or 0x73, got FF
No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info (http://www.billporter.info) for troubleshooting tips"

There was one exception to this FF error and that was when I simply followed the silk screened pin numbers on the Duemilanove, I got this msg:(from memory)
"Controller mode not matched or no controller found
Expected 0x41 or 0x73, got 0
No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info (http://www.billporter.info) for troubleshooting tips"

The distinction being the 0 instead of the FF.


I have the controller's "5. red: power 3.3v" going to the Duemilanove's "3V3" next to "5V"
"4.black:ground" is going to the "Gnd" next to "5V" and "3V3".

I don't have "9. green: acknowledge"; "8. white unknown" or "3. grey: vibration motor power" connected to anything at the moment. I tried running the "3. grey" to both "5V" and a 9VDC powersupply and that didn't seem to improve anything.

I'm using a genuine Sony controller, but I've also tried using 5.6K and 180ohm resistors based on the comments on your blog. I did not have any of the 3rd type of resistor, but since I had a "3V3" 3.3VDC power source on my arduino it didn't seem necessary.

I hate to cut my extension cord short or even resort to cutting the extension cord out and removing the PS2 controller connector: but they seem to be the most significant distinction between my procedure and what everyone else has been doing.

Any advice is appreciated.

Went out and bought a controller I was willing to cut the connector off of. Didn't work. I'm now blaming the Duemilanove as it's the only component which hasn't been swapped out yet.

Just ordered a 5v Pro Mini 16mhz.

Pending this new information on the 10K resistors: while the pro mini is in the mail: I'll have something to experiment with.

The good news is: this seems to vindicate the extension cord as a source of problems.(superstitious as it may have been.)

Edit:
new test.

Setup:
All pins normal
Extension Cord: YES
Resistors:
10K on data
Unconnected Wires:
Acknoledge, Unknown, Motor
Power:
5V & neighbor ground.

Output:
Code: [Select]
OUT:IN
1:FF
42:1
0:10
0:FE
0:FF
0:FF
0:FF
0:FF
0:FF
OUT:IN
1:FF
42:1
0:10
0:FE
0:FF
0:FF
0:FF
0:FF
0:FF
Controller mode not matched or no controller found
Expected 0x41 or 0x73, got 1
No controller found, check wiring, see readme.txt to enable debug. visit [url=http://www.billporter.info]www.billporter.info[/url] for troubleshooting tips
OUT:IN
1:FF
42:63
0:10
0:FE
0:FF
0:60
0:70
0:48
0:2
OUT:IN
1:FF
42:63
0:10
0:FE
0:FF
0:60
0:70
0:48
0:2
Controller mode not matched or no controller found
Expected 0x41 or 0x73, got 63
No controller found, check wiring, see readme.txt to enable debug. visit [url=http://www.billporter.info]www.billporter.info[/url] for troubleshooting tips
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on September 20, 2010, 06:37:33 PM
cyber,

Using the controller you are willing to damage,

power it off 5V, use the original pin positions, and add a 1-5K resistor between the data pin and 5V. It could be the same problem as Striker.

Title: Re: PlayStation 2 Controller Arduino Library
Post by: cyberdynewins on September 21, 2010, 02:19:02 AM
 ;D ;D ;D

If you could see my face right now. I couldn't be more happy if I had figured it out myself.

I used a 5.6K resistor from 5v to data., and there's a 10k resistor between the 5.6k+data and the arduino pin.

THANK YOU SO MUCH! Now I can get back to programming instead of sitting on my hands waiting for a package.(all too familiar of a feeling)
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on September 21, 2010, 08:27:38 AM
Glad it worked!

I used a 5.6K resistor from 5v to data., and there's a 10k resistor between the 5.6k+data and the arduino pin.

Can you explain this a little better? Did you use one resistor or two? How did you wire it?
Title: Re: PlayStation 2 Controller Arduino Library
Post by: cyberdynewins on September 21, 2010, 12:39:44 PM
Glad it worked!

I used a 5.6K resistor from 5v to data., and there's a 10k resistor between the 5.6k+data and the arduino pin.

Can you explain this a little better? Did you use one resistor or two? How did you wire it?

I maintained the original 10K resistor between the data wire and the arduino, and I used a 5.6K resistor to jumper from 5vdc to between the data wire and the 10k resistor.

The data wire has 2 resistors attached to it. A 5.6k going to 5vdc, and a 10K going to pin 12 on the arduino.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on September 21, 2010, 02:59:32 PM
Hmm, this might be my fault, but there's no need for the 10k resistor on the data wire.

Only the other wires need the resistors to drop the 5V from the Arduino down to 3.3V for the controller, but the data wire come from the controller's 3.3V to the Arduinio's 5V.

You might find it works ok if you remove both the 10k and 5k resistors, as all the 5k is doing is pulling up the voltage that the 10k is blocking the Arduino's own internal pull up resistor from doing.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: cyberdynewins on September 21, 2010, 03:57:46 PM
Negative. Spent most of troubleshooting time without either resistor. 10K is a relatively new addition. I could try removing the 10K resistor and using 5.6k alone?

Is their any measurements I can take with my fluke-meter to help explain why it's working?

Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on September 21, 2010, 04:43:24 PM
I could try removing the 10K resistor and using 5.6k alone?

Yes, try it.

Fluke won't be much good, it's better to have an oscope.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: Striker121 on September 21, 2010, 07:15:21 PM
Well, in my case it ran fine off of 3.3v and the Arduino read it fine, just needed the 10K resistor between data and VCC, nothing else.
EDIT: YOU SAW NOTHING! No mistakes here.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on September 21, 2010, 09:06:40 PM
10K resistor between data and ground.

do you mean data and Vcc?

Playstation controllers can only pull the data line down to ground, they need something else to pull it high. For me, the internal pull-up in the Arduino was enough.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: KurtEck on September 22, 2010, 07:56:08 AM
I am pretty sure he wired it as a pull-up.

I have run into this a couple of times, where sometimes the internal pull-up was sufficient and other times it was not. For example I was able to use a lynxmotion PS2 receiver hooked up to a basic atom pro 28 on pins 1-4 be enabling the built in pull-up. On another robot I wanted to do it again, So used same brand lynxmotion receiver and another Pro 28 and this time it did not work...

With your library with Arduino Mega/Seeeduno Mega, It was working most of the time for me without it...

Kurt
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on September 24, 2010, 08:47:03 PM
EDIT: YOU SAW NOTHING! No mistakes here.

LOL, guess so then.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on January 16, 2011, 12:14:22 AM
Version 1.5 out.

Now with more Guitar Hero!

PlayStation 2 Guitar Hero Controller Arduino Library Demonstration (http://www.youtube.com/watch?v=Dw4InYIeSGk#)

Added terms for Guitar Hero Controller. RED_FRET, GREEN_FRET, WHAMMY_BAR, etc.
Added controller detecting function to see if it's DualShock or Guitar Hero controller that's connected.
Fixed an issue with DEBUG mode.

All source for this demonstration available for download @ http://www.billporter.info/playstation-2-controller-arduino-library-v1-0/ (http://www.billporter.info/playstation-2-controller-arduino-library-v1-0/)
Title: Re: PlayStation 2 Controller Arduino Library
Post by: tpalmieri on January 16, 2011, 08:49:06 AM
Good Morning Bill,
Here is the code I started. I am really new to this and did what I could with my very limited knowledge. Hopefully you can get the idea of what I am trying to do in the code. This is for a robotic arm with 7 servo's controlled by a wireless lynxmotion PS2 controller . (4 servos controlled by joysticks, 2 controlled by pad and gripper servo from 2 buttons). I have the PS2 controller working using your library. I appreciate your time and help in pointing me in the right direction.

The code is attached in the .txt doc.

Thanks, Tom
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on January 16, 2011, 10:05:15 AM
Well first thing tpalmieri,

You are missing the config statement (  ps2x.config_gamepad(13,11,10,12);   )  in setup()

Then we have some other issues.

This doesn't work.

Code: [Select]
basepos = map (PSAB_PAD_RIGHT, 0, 255, 0, 180); // Scale Servo Positions from PS2 Controller
But it is the right approach for this section:

Code: [Select]
if(ps2x.Button(PSS_RX)) shoulderpos++;  // This is a joystick. Want 128 to 0 to move servo in one direction and 128 to 256 in the other.

 if(ps2x.Button(PSS_RY)) elbowpos++;

 if(ps2x.Button(PSS_LX)) wristpos++;

 if(ps2x.Button(PSS_LY)) wristrotatepos++;

 if(ps2x.Button(PSB_R2)) gripperpos++;

But to control the servos from the thumbsticks each should look like this:

shoulderpos = map(ps2x.Analog(PSS_RX),  0, 255, 0, 180);

Using the if() statements with ++ is only if you want to move a servo with a button.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: btmcmahan on May 06, 2011, 08:52:59 AM
Hey, I'm new to arduino so I'm sure this is going to be a silly question.  When I try to upload this code to my arduino mega, I get the following error:

"PS2X is not a type" and it highlights this code
Code: [Select]
PS2X ps2x; // create PS2 Controller Class
what should I do?  I've tried several of your previous versions too, and I always get the same code.
Title: Re: PlayStation 2 Controller Arduino Library
Post by: KurtEck on May 06, 2011, 09:00:34 AM
My guess is that you have not downloaded or expanded the library into your library and you have not imported it into your sketch.

In particular:
Have you downloaded and expanded his library into your arduino library directory?

Also when you have the Arduino IDE running with your sketch, you have you gone to the sketch menu/Import Library menu item and imported the library into your sketch?

Good Luck
Kurt
Title: Re: PlayStation 2 Controller Arduino Library
Post by: btmcmahan on May 06, 2011, 05:18:27 PM
Okay, I'm doing better.  I got the code onto the board, the problem was the file folder like you suggested.  My new problem is this:  Where do I attatch the Ack wire from the controller?  I can't seem to find the answer to that. And I may need to change what pin # it is, for the Mega.  What line of code dictates the Ack pin? 

Also, I want to change the pins for Data, Attn, Clk, and Comand too.  Am I right to change it in this line of code:
 error = ps2x.config_gamepad(13,11,10,12, true, true);   //setup pins and settings:  GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
Title: Re: PlayStation 2 Controller Arduino Library
Post by: madsci1016 on May 06, 2011, 05:42:21 PM
Hi btmcmahan,

Welcome to arduino!!

My library does not use the "Acknowledge" wire from the controller, so you don't have to connect it to anything.

For changing the other pins, yes that is the line of code to change the pin numbers in.

By the way, if you have any more trouble with my library, you can also try some of the suggestions in my 'Troubleshooting Guide (http://www.billporter.info/arduino-playstation-2-controller-library-troubleshooting-guide/)'
Title: Re: PlayStation 2 Controller Arduino Library
Post by: jdeck on May 08, 2011, 10:17:42 PM
Hello,

I am very new to Arduino. I am a mechanical engineering student and my only programming background is in MATLAB. I am working on a Senior Design project involving control of a 40lb tank robot using a wireless PS2 controller from Lynxmotion.

I have downloaded your library and looked through its capabilities. It is very impressive. It has more than enough functionality for my purposes, specifically the ability to control 3 DC motors with bidirectional speed control.

I was hoping that you could indicate to me how to read the joystick values from your library into a PWM signal. I really appreciate the help.

Edit: the best I can infer is using if ps2x.Button(RSS_RX) to read a value and then based on that using analogWrite(pin,scale)

Thank you,
John

Also I would like to clarify this point:
I prefer to have pins 10 and/or 11 available for PWM output, as pins 5 and 6 have known issues with low duty-cycle settings.  "The PWM outputs generated on pins 5 and 6 will have higher-than-expected duty cycles. This is because of interactions with the millis() and delay() functions, which share the same internal timer used to generate those PWM outputs. This will be noticed mostly on low duty-cycle settings (e.g 0 - 10) and may result in a value of 0 not fully turning off the output on pins 5 and 6."   Will there be any problems with using pins 5 and/or 6 based on this issue?