Need help optimizing playstation controller code
bulkhead
Posts: 405
I've been trying to get this code to work with a wireless playstation 2 video game controller. I timed the getPacket() routine at roughly 5.6ms, and I tested it with a regular wired controller and it works fine. However, it does not seem to work with a wireless controller. I used viewport's 20MHz prop scope function to monitor the signals going from a playstation to the controller, and the data is sent exactly the same way for both wired and wireless controllers. The only difference I can think of is that the wireless controllers actually require that the packet be sent faster. The actual playstation console transfers the whole packet in roughly 0.5 ms.
What I need is a way to make this routine more efficient somehow by converting it into assembly or through some other manner. My target is to get this running at 10x the speed, but any improvement would help (perhaps the threshold speed for wireless controllers is somewhere in between what the playstation does and what my code currently does). Anhy suggestions on improving the runtime speed of this program would be appreciated, thanks.
EDIT 8/20: Here is the info I got from running ViewPort's 20MHz scope program and connecting pins 0-3 to a playstation (original, not ps2) and 1 wired/2 different wireless controllers. Note that the data bytes appear to be exactly the same for both wired and wireless controllers.
EDIT 8/20: updated version down a few posts (runs twice as fast as this version)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I'm new to the propeller!
Post Edited (bulkhead) : 8/21/2007 2:08:02 AM GMT
What I need is a way to make this routine more efficient somehow by converting it into assembly or through some other manner. My target is to get this running at 10x the speed, but any improvement would help (perhaps the threshold speed for wireless controllers is somewhere in between what the playstation does and what my code currently does). Anhy suggestions on improving the runtime speed of this program would be appreciated, thanks.
EDIT 8/20: Here is the info I got from running ViewPort's 20MHz scope program and connecting pins 0-3 to a playstation (original, not ps2) and 1 wired/2 different wireless controllers. Note that the data bytes appear to be exactly the same for both wired and wireless controllers.
EDIT 8/20: updated version down a few posts (runs twice as fast as this version)
'Translated from BS2 version of playstation controller code VAR long attPin long cmdPin long clkPin long datPin long idx 'loop counter long psxOut 'byte to controller long psxIn 'byte from controller long ID, ThumbL, ThumbR, Status, JoyRX, JoyRY, JoyLX, JoyLY long timer CON _xinfreq = 5_000_000 ' 5 MHz external crystal _clkmode = xtal1 + pll16x ' 5 MHz crystal multiplied → 80 MHz start = %00000001 '0x01 getDat = %01000010 '0x42 Inverted=1 Direct=0 clockMode=Direct PUB startUp(ddat, cmd, att, clk) clkPin:=clk attPin:=att cmdPin :=cmd datPin:=ddat 'INIT dira[noparse][[/noparse]attPin]~~ dira[noparse][[/noparse]cmdPin]~~ dira[noparse][[/noparse]clkPin]~~ dira[noparse][[/noparse]datPin]~ outa[noparse][[/noparse]attPin]~~ 'deselect PSX controller outa[noparse][[/noparse]cmdPin]~ outa[noparse][[/noparse]clkPin]:=!clockMode 'release clock PUB Get_PSX_Packet outa[noparse][[/noparse]attPin]~ 'select controller psxOut:=start 'send "start" PSX_TxRx psxOut:=getDat 'send "get data" PSX_TxRx ID:=psxIn 'save controller type psxOut := 0 PSX_TxRx Status:= psxIn 'should be $5A ("ready") PSX_TxRx ThumbL := psxIn 'get PSX data PSX_TxRx ThumbR := psxIn PSX_TxRx JoyRX := psxIn PSX_TxRx JoyRY := psxIn PSX_TxRx JoyLX := psxIn PSX_TxRx JoyLY := psxIn outa[noparse][[/noparse]attPin] ~~ 'deselect controller PUB PSX_TxRx repeat idx from 0 to 7 outa[noparse][[/noparse]cmdPin]:= (((psxOut>>idx)&1)==1) outa[noparse][[/noparse]clkPin]:=clockMode psxIn&= !(1<<idx) if(ina[noparse][[/noparse]datPin]) psxIn |= (1<<idx) outa[noparse][[/noparse]clkPin]:= !clockMode
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I'm new to the propeller!
Post Edited (bulkhead) : 8/21/2007 2:08:02 AM GMT
Comments
-- SPIN does not optimize constant expressions without the constant(...) directive (otherwise !clockMode is an additional operation).
-- (xxx&1)==1 seems equivalent to xxx
-- I should also suggest "loop-unrolling", not because the loop is very expensive, but you than can profit from knowing idx being 0,1,2,3,4,5,6,7
-- There should also be an minor speed-up using the return value
Even if this will not be an improvement with speed it will be an improvement in readability
Post Edited (deSilva) : 8/19/2007 10:55:08 PM GMT
I feel energized, tonight I write!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Parallax Forums - If you're ready to learn, we're ready to help.
I can esily get a wired PS2 controller to work, but never a wireless. Lynxmotuion have their "own" wireless controller that they have working, but the information on their website may help.
Mike
I have reviewed the MBasic code for communication with the wireless PS2 controller receiver with the the Atom 28 and have noticed the following difference:
The MBasic code does not check for the $5A "Ready" response from the controller after receiving the controller type response. It immediately receives the two button bytes from the controller followed by the right and left joystick position values.
Regards,
TCIII
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
If you are going to send·a Robot·to save the world, you·better make sure it likes it the way it is!
I'll be following this thread as i'd be interested in how one would accomplish reading a PS2 controller w/the Prop...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
E3 = Thought
http://folding.stanford.edu/·- Donating some CPU/GPU downtime just might lead to a cure for cancer! My team stats.
Well CJ, I look forward to your assembly driver since I don't feel like learning assembly right now just to write the driver.
deSilva, I will try those changes and see how much faster it is and whether it is fast enough.
I've had luck getting a Pelican Chameleon wireless controller working on the Javelin stamp running quite slowly . I found out that it returns different Status and ID bytes than normal wired controllers. However, I just got another Pelican Chameleon wireless controller, and this one is slightly different because it has a manual channel selector on the controller and receiver (my other chameleon didn't have that). I used ViewPort's scope to verify that this wireless controller behaves JUST like a wired one (same status and ID bytes). Perhaps the only difference is that it requires fast data transmission whereas the wired controllers are fine with slow transmissions.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I'm new to the propeller!
I did forget to mention that with the code I have, I do get some response with the wireless controllers. When I toggle the "analog" button that switches between the analog (joystick+ buttons) and digital (buttons only) mode, I get a change in the ID byte from 224 (dec) to 249 (dec), although I don't know whether the 224 corresponds to the analog or digital mode. The correct values for the ID should be 65 (dec) or 0x41 hex for digital mode and 115 (dec) or 0x73 hex for analog mode.
Here is a link to some info I had when I worked on ps2 controllers with the Javelin stamp (it's in the Completed Projects section): http://forums.parallax.com/showthread.php?p=573796 The attached file in that post, "WirelessPS2.txt" has a summary of the expected Status/ID/button info bytes.
Here is the part of the code that I changed that is still non functional with wireless controllers, but works fine with wired ones (now runs 2x as fast, thanks to deSilva's suggestions). Looks like it will need to be converted to assembly.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I'm new to the propeller!
Post Edited (bulkhead) : 8/21/2007 2:17:29 AM GMT
the assembly code should be capable of whatever speed is needed for the wireless controllers
not really any more lines than the spin version
don't be afraid of Propasm, it is very easy to use. my first real assembly work EVER was the N64 and GameCube controller code in the object exchange
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Parallax Forums - If you're ready to learn, we're ready to help.
Congrats on a very nice use of Viewport! Those measurements look fantastic. Looks like you would like a feature where Viewport decodes data for you automatically? Ie- tell Viewport that one bit trace is the clock signal while another is the data? cj's code looks very appropriate- Propeller assembly is rather fun once you get started!
Hanno
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
E3 = Thought
http://folding.stanford.edu/·- Donating some CPU/GPU downtime just might lead to a cure for cancer! My team stats.