PS2 Robot Control: Establishing Communication.

03.30.04

By Beth Frye.

Establishing a fast and reliable connection between a Sony-brand PS2 controller and the Basic Stamp 2 processor. Note, the Basic Stamp 2 processor cannot properly read the left vertical joystick.


Too cool!
 
Step 1. Plug in the PS2 cable as shown. Be extremely careful to observe polarity on the power connector. Black is ground and yellow is +5vdc. The black wire is actually the red/shield with black heat shrink covering them. Figure 1-1 shows what each of the PS2 cable wires are.
 


Figure 1-1.


Figure 1-2.
 
Step 2. Copy & paste the program from below into the editor and program the Basic Stamp 2. After the program is running, choose COM1 (item #1 in Figure 2-1 below). Change the Baud Rate to 9600 (item #2). Note, you will need to turn the controller's Analog mode on. If all is well, you should see numbers similar to the ones shown. Refer to Table 2-1 for a description of the bytes returned by the PS2 controller. Note that items 12 and 13 are the Digital representation of button presses. Refer to Table 2-2 and 2-3 for a Bit representation of button presses.
 

Figure 2-1.

 
1 Set to COM1 12 On / Off for: D-Pad (on = 127), (191), (223), (239); Start (247); R3 (251); L3 (253); Select (254).
Off = 255
2 Set the Baud Rate to 9600kbs 13 On / Off for: (on = 127); X (191); O (223); (239); R1 (247); L1 (251); R2 (253); L2 (254).
Off = 255
3 Leave Parity as None 14 Right Joystick X axis
(000 - ~128 = left; ~128 - 255 = right)
4 Leave Data Bits set to 8 15 Right Joystick Y axis
(000 - ~128 = up; ~128 - 255 = down)
5 Leave Flow Control set to Off 16 Left Joystick X axis
(000 - ~128 = left; ~128 - 255 = right)
6 Transmitting light 17 Left Joystick Y axis
(000 - ~128 = up; ~128 - 255 = down)
7 Leave Data Terminal Ready unchecked 18 Capture
8 Leave Ready to Send unchecked 19 Macros
9 Receiving light 20 Pauses Debug input
10 Data Set Ready light 21 Clears Debug window
11 Clear to Send light 22 Closes the Debug window
    23 Leave Echo Off unchecked

 Table 2-1

 
Table 2-2 and 2-3 illustrate which bits go low when the corresponding button is pressed.
 
The arrow buttons are on what is commonly called the "D-Pad," which is the button pad on the left. L3 and R3 are the little-known Left and Right joystick buttons; trigger these by pressing down on the joysticks. R1, R2, L1, and L2 are the "shoulder buttons," that is, they are located on the controller's "shoulders."
Item 8 in Figure 3 (Active Low)
High Byte Low Byte
Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
Start R3 L3 Select
Table 2-2
 
Item 9 in Figure 3 (Active Low)
High Byte Low Byte
Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
X O R1 L1 R2 L2
Table 2-3
 
The two analog joysticks on a Sony-brand controller will not neatly return to a middle value of 128. The following value ranges are what the joysticks have been known to return to.
  
Left X Left Y Right X Right Y
125 - 140 125 - 140 125 - 140 110 - 125

Table 2-4

 
If you need extreme accuracy with the analog joysticks, go with a Madcatz-brand controller. I've used a wireless Madcatz Lynx controller (coincidence, really!) with perfect analog joystick accuracy. They always returned to the middle value of 128.
 

Step 3. Code by Jim Frye. This is a demonstration program that will report the push button and joystick information to the debug screen. Note that the green wire on the PS2 controller cable needs to have 7.2 vdc applied for this to work.

 
'{$STAMP BS2}
clk CON 7
att CON 6
dat CON 4
cmd CON 5
PS2B1 VAR Byte
PS2B2 VAR Byte
PS2B3 VAR Byte
PS2B4 VAR Byte
PS2B5 VAR Byte
PS2B6 VAR Byte
char VAR Byte
temp1 VAR Word
temp2 VAR Word
temp3 VAR Byte

' Read PS2 Controller
read_PS2

LOW att

SHIFTOUT cmd, clk, LSBFIRST, [$01,$42]
SHIFTIN dat, clk, 3, [char]
temp3 = char

SHIFTIN dat, clk, 3, [char]
PS2B1 = char

SHIFTIN dat, clk, 3, [char]
PS2B2 = char

SHIFTIN dat, clk, 3, [char]
PS2B3 = char

SHIFTIN dat, clk, 3, [char]
PS2B4 = char

SHIFTIN dat, clk, 3, [char]
PS2B5 = char

SHIFTIN dat, clk, 3, [char]
PS2B6 = char

HIGH att

temp1 = ((PS2B3 * 4) + 250) MIN 350 MAX 1150 'MIN 250 MAX 1250
temp2 = ((PS2B4 * 4) + 250) MIN 350 MAX 1150 'MIN 250 MAX 1250

PULSOUT 12, temp1
PULSOUT 13, temp2
PAUSE 20

' DEBUG DEC temp1, " ", DEC PS2B3, " ", DEC temp2, " ", DEC PS2B4, CR
' debug dec temp3, " ", cr
' DEBUG BIN PS2B1, " ", BIN PS2B2, " ", DEC PS2B3, " ", DEC PS2B4, " ", DEC PS2B5, " ", DEC PS2B6, CR

exit_read_PS2

GOTO read_PS2

RETURN