|
|
|
|
September 08, 2010, 12:38:01 PM
Welcome,
Guest
. Please
login
or
register
.
Did you miss your
activation email?
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
News
: SoR Robot Chat every friday night. Join us! (forum integration fix coming when I have time)
http://www.societyofrobots.com/robotforum/chat/
Robot Forum
Home
Help
Search
Login
Register
Robot Forum
>
Software
>
Software
>
PlayStation 2 Controller Arduino Library
Pages: [
1
]
2
3
Print
Author
Topic: PlayStation 2 Controller Arduino Library (Read 1875 times)
0 Members and 1 Guest are viewing this topic.
madsci1016
Supreme Robot
Helpful? 25
Online
Posts: 931
PlayStation 2 Controller Arduino Library
«
on:
May 31, 2010, 10: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
Logged
Click for my Website and info on my Robots and Projects!
Xyver
Full Member
Helpful? 0
Offline
Posts: 81
Re: PlayStation 2 Controller Arduino Library
«
Reply #1 on:
June 01, 2010, 12:19:34 AM »
Thank you! I will use this one day. Ive always wanted a playstation RC.
Logged
Joker94
Supreme Robot
Helpful? 19
Offline
Posts: 1,012
Re: PlayStation 2 Controller Arduino Library
«
Reply #2 on:
June 01, 2010, 03: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
Logged
dunk
Expert Roboticist
Supreme Robot
Helpful? 14
Offline
Posts: 1,042
Re: PlayStation 2 Controller Arduino Library
«
Reply #3 on:
June 01, 2010, 03: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.)
Logged
http://sites.google.com/site/mrdunk/
http://sites.google.com/site/mrdunk2/
flokos
Jr. Member
Helpful? 0
Offline
Posts: 16
Re: PlayStation 2 Controller Arduino Library
«
Reply #4 on:
June 01, 2010, 03:54:53 AM »
Is there any adapter to interface it with arduino without having the plug cut.
Logged
Joker94
Supreme Robot
Helpful? 19
Offline
Posts: 1,012
Re: PlayStation 2 Controller Arduino Library
«
Reply #5 on:
June 01, 2010, 04: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
Logged
flokos
Jr. Member
Helpful? 0
Offline
Posts: 16
Re: PlayStation 2 Controller Arduino Library
«
Reply #6 on:
June 01, 2010, 04:42:43 AM »
i will make an adapter my shelf cause i cant wait ...
Logged
madsci1016
Supreme Robot
Helpful? 25
Online
Posts: 931
Re: PlayStation 2 Controller Arduino Library
«
Reply #7 on:
June 01, 2010, 08:01:04 AM »
Quote from: dunk on June 01, 2010, 03: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?
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.
Logged
Click for my Website and info on my Robots and Projects!
Aligator
Jr. Member
Helpful? 0
Offline
Posts: 21
Re: PlayStation 2 Controller Arduino Library
«
Reply #8 on:
June 02, 2010, 02: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
Logged
HyperNerd
Robot Overlord
Helpful? 5
Offline
Posts: 137
N3rd1n80r
Re: PlayStation 2 Controller Arduino Library
«
Reply #9 on:
June 02, 2010, 07: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
Logged
There are 10 types of people in the world - those who understand binary, and those who don't.
madsci1016
Supreme Robot
Helpful? 25
Online
Posts: 931
Re: PlayStation 2 Controller Arduino Library
«
Reply #10 on:
June 02, 2010, 08: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.
Logged
Click for my Website and info on my Robots and Projects!
madsci1016
Supreme Robot
Helpful? 25
Online
Posts: 931
Re: PlayStation 2 Controller Arduino Library
«
Reply #11 on:
June 02, 2010, 10: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/
Logged
Click for my Website and info on my Robots and Projects!
madsci1016
Supreme Robot
Helpful? 25
Online
Posts: 931
Re: PlayStation 2 Controller Arduino Library
«
Reply #12 on:
June 02, 2010, 05:48:47 PM »
Video Of a PS2 Controller + SAGAR robot.
PlayStation 2 Controller Arduino Library Demonstration
Logged
Click for my Website and info on my Robots and Projects!
HyperNerd
Robot Overlord
Helpful? 5
Offline
Posts: 137
N3rd1n80r
Re: PlayStation 2 Controller Arduino Library
«
Reply #13 on:
June 03, 2010, 04: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
Logged
There are 10 types of people in the world - those who understand binary, and those who don't.
madsci1016
Supreme Robot
Helpful? 25
Online
Posts: 931
Re: PlayStation 2 Controller Arduino Library
«
Reply #14 on:
June 03, 2010, 07:46:16 AM »
I'm gonna look at force feedback this weekend. I think it should be easy.
Logged
Click for my Website and info on my Robots and Projects!
KurtEck
Full Member
Helpful? 5
Online
Posts: 98
Re: PlayStation 2 Controller Arduino Library
«
Reply #15 on:
June 03, 2010, 08: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:
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
Logged
madsci1016
Supreme Robot
Helpful? 25
Online
Posts: 931
Re: PlayStation 2 Controller Arduino Library
«
Reply #16 on:
June 03, 2010, 06: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.
Logged
Click for my Website and info on my Robots and Projects!
madsci1016
Supreme Robot
Helpful? 25
Online
Posts: 931
Re: PlayStation 2 Controller Arduino Library
«
Reply #17 on:
June 03, 2010, 07: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:
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, 07:07:49 PM by madsci1016
»
Logged
Click for my Website and info on my Robots and Projects!
KurtEck
Full Member
Helpful? 5
Online
Posts: 98
Re: PlayStation 2 Controller Arduino Library
«
Reply #18 on:
June 03, 2010, 07: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
Logged
madsci1016
Supreme Robot
Helpful? 25
Online
Posts: 931
Re: PlayStation 2 Controller Arduino Library
«
Reply #19 on:
June 03, 2010, 08: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?
Logged
Click for my Website and info on my Robots and Projects!
KurtEck
Full Member
Helpful? 5
Online
Posts: 98
Re: PlayStation 2 Controller Arduino Library
«
Reply #20 on:
June 03, 2010, 09:26:40 PM »
Sounds great. Now I just have to get off my... And start trying out the Arduino!
Kurt
Logged
HyperNerd
Robot Overlord
Helpful? 5
Offline
Posts: 137
N3rd1n80r
Re: PlayStation 2 Controller Arduino Library
«
Reply #21 on:
June 04, 2010, 06: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
-HyperNerd
Logged
There are 10 types of people in the world - those who understand binary, and those who don't.
Anoroc
Jr. Member
Helpful? 0
Offline
Posts: 35
Re: PlayStation 2 Controller Arduino Library
«
Reply #22 on:
June 04, 2010, 09: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?
Logged
HyperNerd
Robot Overlord
Helpful? 5
Offline
Posts: 137
N3rd1n80r
Re: PlayStation 2 Controller Arduino Library
«
Reply #23 on:
June 04, 2010, 11: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
Logged
There are 10 types of people in the world - those who understand binary, and those who don't.
madsci1016
Supreme Robot
Helpful? 25
Online
Posts: 931
Re: PlayStation 2 Controller Arduino Library
«
Reply #24 on:
June 04, 2010, 11: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.
Logged
Click for my Website and info on my Robots and Projects!
KurtEck
Full Member
Helpful? 5
Online
Posts: 98
Re: PlayStation 2 Controller Arduino Library
«
Reply #25 on:
June 05, 2010, 08: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:
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
Logged
HyperNerd
Robot Overlord
Helpful? 5
Offline
Posts: 137
N3rd1n80r
Re: PlayStation 2 Controller Arduino Library
«
Reply #26 on:
June 05, 2010, 09: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
Logged
There are 10 types of people in the world - those who understand binary, and those who don't.
madsci1016
Supreme Robot
Helpful? 25
Online
Posts: 931
Re: PlayStation 2 Controller Arduino Library
«
Reply #27 on:
June 05, 2010, 10: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.
Logged
Click for my Website and info on my Robots and Projects!
madsci1016
Supreme Robot
Helpful? 25
Online
Posts: 931
Re: PlayStation 2 Controller Arduino Library
«
Reply #28 on:
June 05, 2010, 11: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.
Logged
Click for my Website and info on my Robots and Projects!
madsci1016
Supreme Robot
Helpful? 25
Online
Posts: 931
Re: PlayStation 2 Controller Arduino Library
«
Reply #29 on:
June 05, 2010, 01: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.
Logged
Click for my Website and info on my Robots and Projects!
Pages: [
1
]
2
3
Print
Jump to:
Please select a destination:
-----------------------------
General Misc
-----------------------------
=> Misc
=> Robot Videos
-----------------------------
Software
-----------------------------
=> Software
-----------------------------
Electronics
-----------------------------
=> Electronics
-----------------------------
Mechanics and Construction
-----------------------------
=> Mechanics and Construction
Related Topics
Subject
Started by
Replies
Views
Last post
Motor Library for Arduino Question
Software
Dudekiller
3
1737
August 27, 2008, 12:52:21 PM
by
sonictj
Hacking through the Arduino Library
Software
reefat
8
4647
August 24, 2010, 08:47:26 PM
by
wlewis
NEED TESTERS: $50 Robot Arduino Library
Software
Razor Concepts
0
1051
April 09, 2009, 01:52:19 PM
by
Razor Concepts
Simple PID Library For Arduino
Software
Rindis
0
516
April 14, 2010, 11:29:39 AM
by
Rindis
Advertise on this Forum
Loading...