by i-Bot » Wed Nov 14, 2007 10:04 am
by i-Bot
Wed Nov 14, 2007 10:04 am
In this thread there is a small part of the template for the Hitec PS2 controller.
http://robosavvy.com/forum/viewtopic.php?t=1797
The following is my template. It is bit more simple than the Hitec one, since I encode the buttons into a single byte.
The value from the joystick in this example is between 10 and 190. The button pressure is between 1 and 15
' Template program for PS2 wireless controller
' Richard Ibbotson November 2007
' PS2 wireless controller in DS2 mode connected to ETX/ERX
DIM rr AS BYTE ' first variable is reserved
DIM txchar AS BYTE ' store for cahracter to send to controller
DIM rxchar AS BYTE
'Empty the ERX buffer
begin:
ERX 19200, rxchar, bemt1
bemt1:
ERX 19200, rxchar, bemt2
bemt2:
' Set the PS2 controller to DS2 mode
txchar = "W"
ETX 19200, txchar
GETW:
ERX 19200, rxchar, GETW
'-----------------------------------------------------------------------
' Motion Startup Stuff, note this is special for double knee joins
PTP SETON
PTP ALLON
DIR G6A,1,0,0,0,1,0
DIR G6B,1,1,1,1,1,1
DIR G6C,0,0,0,0,0,0
DIR G6D,0,1,1,1,0,1
'GETMOTORSET G6A,1,1,1,1,1,1
'GETMOTORSET G6B,1,1,1,0,0,0
'GETMOTORSET G6C,1,1,1,0,0,1
'GETMOTORSET G6D,1,1,1,1,1,1
SPEED 5
MOTOR G6A
MOTOR G6B
MOTOR G6C
MOTOR G6D
'Example Gyro Setup Code
GYROSET G6A,0,0,0,0,0,0
GYROSET G6D,0,0,0,0,0,0
GYRODIR G6A,0,1,1,0,1,1
GYRODIR G6D,0,1,1,0,1,1
GYROSENSE G6A,0,200,200,0,0,0
GYROSENSE G6D,0,200,200,0,0,0
GOSUB Standard_pose
'-----------------------------------------------------------------------
MUSIC "CDE"
MAIN:
'Read the PS2 buttons
txchar ="X"
ETX 19200, txchar
GETX:
ERX 19200, rxchar, GETX
IF rxchar > 0 AND rxchar < 17 THEN
ON rxchar GOTO MAIN,K1,K2,K3,K4,K5,K6, K7, K8, K9, K10, K11, K12, K13, K14, K15, K16
ENDIF
GOTO MAIN
K1: ' Left Button
GOTO MAIN
K2: ' Down Button
GOTO MAIN
K3: ' Right Button
GOTO MAIN
K4: ' Up Button
GOTO MAIN
K5: ' Start Button Use to force restart
GOTO begin
K6: ' Right Joystick Button
'read the Right joystick position
txchar ="C"
ETX 19200, txchar
GETC:
ERX 19200, rxchar, GETC
SERVO 17,rxchar
GOTO MAIN
K7: ' Left Joystick Button
GOTO MAIN
K8: ' Select Button
GOTO MAIN
K9: ' Square Button
GOTO MAIN
K10:' Cross Button
GOTO MAIN
K11:' Circle Button
GOTO MAIN
K12:' Triangle Button
GOTO MAIN
K13:' R1 Button
' read the R1 button pressure
txchar ="P"
ETX 19200, txchar
GETP:
ERX 19200, rxchar, GETP
SPEED rxchar
MOVE G6B,100,70,80,100,100,
WAIT
SPEED 5
GOSUB Standard_Pose
GOTO MAIN
K14:' L1 Button
GOTO MAIN
K15:' R2 Button
GOTO MAIN
K16:' L2 Button
GOTO MAIN
'================================================
Standard_Pose:
MOVE G6A, 100,85,90,90,90,100
MOVE G6D, 100,85,90,90,90,100
MOVE G6B, 100,30,80,100,100,100
MOVE G6C, 100,30,80,100,100,100
WAIT
RETURN
'================================================
In this thread there is a small part of the template for the Hitec PS2 controller.
http://robosavvy.com/forum/viewtopic.php?t=1797
The following is my template. It is bit more simple than the Hitec one, since I encode the buttons into a single byte.
The value from the joystick in this example is between 10 and 190. The button pressure is between 1 and 15
' Template program for PS2 wireless controller
' Richard Ibbotson November 2007
' PS2 wireless controller in DS2 mode connected to ETX/ERX
DIM rr AS BYTE ' first variable is reserved
DIM txchar AS BYTE ' store for cahracter to send to controller
DIM rxchar AS BYTE
'Empty the ERX buffer
begin:
ERX 19200, rxchar, bemt1
bemt1:
ERX 19200, rxchar, bemt2
bemt2:
' Set the PS2 controller to DS2 mode
txchar = "W"
ETX 19200, txchar
GETW:
ERX 19200, rxchar, GETW
'-----------------------------------------------------------------------
' Motion Startup Stuff, note this is special for double knee joins
PTP SETON
PTP ALLON
DIR G6A,1,0,0,0,1,0
DIR G6B,1,1,1,1,1,1
DIR G6C,0,0,0,0,0,0
DIR G6D,0,1,1,1,0,1
'GETMOTORSET G6A,1,1,1,1,1,1
'GETMOTORSET G6B,1,1,1,0,0,0
'GETMOTORSET G6C,1,1,1,0,0,1
'GETMOTORSET G6D,1,1,1,1,1,1
SPEED 5
MOTOR G6A
MOTOR G6B
MOTOR G6C
MOTOR G6D
'Example Gyro Setup Code
GYROSET G6A,0,0,0,0,0,0
GYROSET G6D,0,0,0,0,0,0
GYRODIR G6A,0,1,1,0,1,1
GYRODIR G6D,0,1,1,0,1,1
GYROSENSE G6A,0,200,200,0,0,0
GYROSENSE G6D,0,200,200,0,0,0
GOSUB Standard_pose
'-----------------------------------------------------------------------
MUSIC "CDE"
MAIN:
'Read the PS2 buttons
txchar ="X"
ETX 19200, txchar
GETX:
ERX 19200, rxchar, GETX
IF rxchar > 0 AND rxchar < 17 THEN
ON rxchar GOTO MAIN,K1,K2,K3,K4,K5,K6, K7, K8, K9, K10, K11, K12, K13, K14, K15, K16
ENDIF
GOTO MAIN
K1: ' Left Button
GOTO MAIN
K2: ' Down Button
GOTO MAIN
K3: ' Right Button
GOTO MAIN
K4: ' Up Button
GOTO MAIN
K5: ' Start Button Use to force restart
GOTO begin
K6: ' Right Joystick Button
'read the Right joystick position
txchar ="C"
ETX 19200, txchar
GETC:
ERX 19200, rxchar, GETC
SERVO 17,rxchar
GOTO MAIN
K7: ' Left Joystick Button
GOTO MAIN
K8: ' Select Button
GOTO MAIN
K9: ' Square Button
GOTO MAIN
K10:' Cross Button
GOTO MAIN
K11:' Circle Button
GOTO MAIN
K12:' Triangle Button
GOTO MAIN
K13:' R1 Button
' read the R1 button pressure
txchar ="P"
ETX 19200, txchar
GETP:
ERX 19200, rxchar, GETP
SPEED rxchar
MOVE G6B,100,70,80,100,100,
WAIT
SPEED 5
GOSUB Standard_Pose
GOTO MAIN
K14:' L1 Button
GOTO MAIN
K15:' R2 Button
GOTO MAIN
K16:' L2 Button
GOTO MAIN
'================================================
Standard_Pose:
MOVE G6A, 100,85,90,90,90,100
MOVE G6D, 100,85,90,90,90,100
MOVE G6B, 100,30,80,100,100,100
MOVE G6C, 100,30,80,100,100,100
WAIT
RETURN
'================================================