PS2 Robot Control: Establishing Communication.

02.03.04

By Beth Frye.

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


Too cool!
 
Step 1. Solder a 1K ohm pull-up resistor onto the Next Step processor board as shown. This connects from I/O pin 4 to the 5vdc line. This is the Data line. On some controllers, a pull-up will also be required on the Clock line, which is I/O pin 7. The Buttons and LEDs will not be useable in this configuration.
Figure 1-1.
 
Step 2. 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 2-1 shows what each of the PS2 cable wires are.
 


Figure 2-1.


Figure 2-2.
 
Step 3. Copy & paste the program from below into the editor and program the Basic Atom. After the program is running, click on the Terminal1 tab (item #28 in Figure 3-1 below). Change the Baud Rate to 57.6kbs (item #1). Then click Connect (item #6). If all is well, you should see numbers similar to the ones shown. Note that the program automatically puts the controller in Analog mode. Refer to Table 3-1 for a description of the bytes returned by the PS2 controller. Note that items 8 and 9 are the Digital representation of button presses. Refer to Table 3-2 for a Bit representation of button presses.
 

Figure 3-1.

 
1 Set the Baud Rate to 57.6kbs 17 on the D-Pad (000 - 255; 255 = full on)
2 Set to COM1 18 button (000 - 255; 255 = full on)
3 Set to No Parity 19 O button (000 - 255; 255 = full on)
4 Set to NoFlow Ct 20 X button (000 - 255; 255 = full on)
5 Set to Echo 21 button (000 - 255; 255 = full on)
6 Press to Connect / Disconnect 22 L1 (000 - 255; 255 = full on)
7 Controller Mode (Analog is 79) 23 R1 (000 - 255; 255 = full on)
8 On / Off for: D-Pad (on = 127), (191), (223), (239); Start (247); R3 (251); L3 (253); Select (254).
Off = 255
24 L2 (000 - 255; 255 = full on)
9 On / Off for: (on = 127); X (191); O (223); (239); R1 (247); L1 (251); R2 (253); L2 (254).
Off = 255
25 R2 (000 - 255; 255 = full on)
10 Right Joystick X axis
(000 - ~128 = left; ~128 - 255 = right)
26 Build
11 Right Joystick Y axis
(000 - ~128 = up; ~128 - 255 = down)
27 Debug
12 Left Joystick X axis
(000 - ~128 = left; ~128 - 255 = right)
28 Use Terminal1
13 Left Joystick Y axis
(000 - ~128 = up; ~128 - 255 = down)
29 Terminal2
14 on the D-Pad (000 - 255; 255 = full on) 30 Terminal3
15 on the D-Pad (000 - 255; 255 = full on) 31 Terminal4
16 on the D-Pad (000 - 255; 255 = full on)    

 Table 3-1

 
Table 3-2 and 3-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 3-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 3-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 3-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 4. Code by Nathan Scherdin. As a demonstration of the internal vibrating motors, the following program turns on the motors with button presses. For example, the smaller motor is turned on by the D-pad button, and is either on or off. The larger motor is turned on by the D-pad button, and is variable speed proportional to the pressure applied to the button. Note that the green wire on the PS2 controller cable needs to have 7.2 vdc applied for this to work.

 
DAT con P4
CMD con P5
CLK con P7
SEL con P6

temp1 var byte
index var byte
temp var byte(21)
cntr var byte
mode var byte

small var byte
large var byte

high CLK

test
low SEL
shiftout CMD,CLK,FASTLSBPRE,[$1\8,$43\8,$0\8,$1\8,$0\8] ;Config Enable
high SEL

low 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 Fix(Analog
high SEL

pause 100

low SEL
shiftout CMD,CLK,FASTLSBPRE,[$01\8,$4D\8,$00\8,$00\8,$01\8,$ff\8,$ff\8,$ff\8,$ff\8] ;Vibration Enable
high SEL

low SEL
shiftout CMD,CLK,FASTLSBPRE,[$01\8,$43\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8] ;Config Exit
high SEL
main
low SEL
shiftout CMD,CLK,FASTLSBPRE,[$1\8]
shiftin DAT,CLK,FASTLSBPOST,[mode\8]
high SEL

low SEL
shiftout CMD,CLK,FASTLSBPRE,[$1\8,$42\8]
for index = 0 to 2
shiftin DAT,CLK,FASTLSBPOST,[temp(index)\8]
next
high SEL

low SEL
shiftout CMD,CLK,FASTLSBPRE,[$1\8,$42\8,$0\8,large\8,small\8]
for index = 3 to 6
shiftin DAT,CLK,FASTLSBPOST,[temp(index)\8]
next
high SEL

serout S_OUT,i57600,[13,hex2 mode\2]
for index = 1 to 6
serout S_OUT,i57600,[" ",dec3 temp(index)\3]
next

small = temp(7)
large = temp(8)

goto main