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

0 Members and 1 Guest are viewing this topic.

Offline HyperNerd

  • Robot Overlord
  • ****
  • Posts: 139
  • Helpful? 5
  • N3rd1n80r
Re: PlayStation 2 Controller Arduino Library
« Reply #30 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
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 #31 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
« Last Edit: June 05, 2010, 05:26:21 PM by madsci1016 »

Offline Joker94

  • Supreme Robot
  • *****
  • Posts: 1,119
  • Helpful? 26
Re: PlayStation 2 Controller Arduino Library
« Reply #32 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

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: PlayStation 2 Controller Arduino Library
« Reply #33 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.

Offline Ro-Bot-X

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,431
  • Helpful? 25
  • Store: RoBotXDesigns.ca
Check out the uBotino robot controller!

Offline amando96

  • Robot Overlord
  • ****
  • Posts: 187
  • Helpful? 1
Re: PlayStation 2 Controller Arduino Library
« Reply #35 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.
Rorcle, 60% complete
AATV, 5% complete

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: PlayStation 2 Controller Arduino Library
« Reply #36 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


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.

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,666
  • Helpful? 169
    • Society of Robots
Re: PlayStation 2 Controller Arduino Library
« Reply #37 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?

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: PlayStation 2 Controller Arduino Library
« Reply #38 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.

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,166
  • Helpful? 111
Re: PlayStation 2 Controller Arduino Library
« Reply #39 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
« Last Edit: June 11, 2010, 08:46:41 PM by Webbot »
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: PlayStation 2 Controller Arduino Library
« Reply #40 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. 

Offline flokos

  • Jr. Member
  • **
  • Posts: 17
  • Helpful? 0
Re: PlayStation 2 Controller Arduino Library
« Reply #41 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?

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: PlayStation 2 Controller Arduino Library
« Reply #42 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.

Offline KurtEck

  • Robot Overlord
  • ****
  • Posts: 217
  • Helpful? 12
Re: PlayStation 2 Controller Arduino Library
« Reply #43 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

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

Offline flokos

  • Jr. Member
  • **
  • Posts: 17
  • Helpful? 0
Re: PlayStation 2 Controller Arduino Library
« Reply #44 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.


Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: PlayStation 2 Controller Arduino Library
« Reply #45 on: June 14, 2010, 09:38:45 AM »
Non-Sony controller won't work with 5V Arduinos, unless you use a level shifter.

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: PlayStation 2 Controller Arduino Library
« Reply #46 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


Offline flokos

  • Jr. Member
  • **
  • Posts: 17
  • Helpful? 0
Re: PlayStation 2 Controller Arduino Library
« Reply #47 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.

Offline TrickyNekro

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,207
  • Helpful? 15
  • 1.6L Peugeot 307 tuner
Re: PlayStation 2 Controller Arduino Library
« Reply #48 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 ;-)
For whom the interrupts toll...


P.S. I've been inactive for almost a year... Don't give promises but I'll try to complete my tutorials. I'll let you know when..

Cheers!

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: PlayStation 2 Controller Arduino Library
« Reply #49 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.

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: PlayStation 2 Controller Arduino Library
« Reply #50 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?

Offline TrickyNekro

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,207
  • Helpful? 15
  • 1.6L Peugeot 307 tuner
Re: PlayStation 2 Controller Arduino Library
« Reply #51 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...
For whom the interrupts toll...


P.S. I've been inactive for almost a year... Don't give promises but I'll try to complete my tutorials. I'll let you know when..

Cheers!

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: PlayStation 2 Controller Arduino Library
« Reply #52 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



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.
« Last Edit: June 19, 2010, 08:41:24 AM by madsci1016 »

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,666
  • Helpful? 169
    • Society of Robots
Re: PlayStation 2 Controller Arduino Library
« Reply #53 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.

Offline TrickyNekro

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,207
  • Helpful? 15
  • 1.6L Peugeot 307 tuner
Re: PlayStation 2 Controller Arduino Library
« Reply #54 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...
For whom the interrupts toll...


P.S. I've been inactive for almost a year... Don't give promises but I'll try to complete my tutorials. I'll let you know when..

Cheers!

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,666
  • Helpful? 169
    • Society of Robots
Re: PlayStation 2 Controller Arduino Library
« Reply #55 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 . . .

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: PlayStation 2 Controller Arduino Library
« Reply #56 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...

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,666
  • Helpful? 169
    • Society of Robots
Re: PlayStation 2 Controller Arduino Library
« Reply #57 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 . . .

Offline KurtEck

  • Robot Overlord
  • ****
  • Posts: 217
  • Helpful? 12
Re: PlayStation 2 Controller Arduino Library
« Reply #58 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

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: PlayStation 2 Controller Arduino Library
« Reply #59 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

Kurt, that file didn't do anything for me. Care to explain what it's suppose to do?
« Last Edit: August 14, 2010, 04:03:08 PM by madsci1016 »

 


Get Your Ad Here