Interfacing the PS2 wireless controller to Robot controller
The PS2 wireless controller is a standard controller for the PlayStation 2 and is identical to the original DualShock controller for the Play Station console. It features twelve analog (pressure-sensitive) buttons ( Χ, O, Π, Δ, L1, R1, L2, R2, Up, Down, Left and Right), five digital button (L3, R3 Start, Select and the analog mode button) and two analog sticks.
Here we have interfaced the PS2 controller with robot controller. We are showing how to interface a rover with robot controller and the rover is controlled with PS2 joystick. Only Analog stick is used to control the rover.We followed the standard PS2 protocol for realizing the communication algorithm, identical to the SPI protocol. Our program on the arduino detects Analog stick values and decides the movements.
Connection Details:
The PS2 receiver CLK line and ATT lines are held normally high. The ATT operates like the Slave Select line under SPI. You pull it low to tell the controller you are talking to it and then send it back high once a communications cycle is complete. CMD is the data line to the controller and DATA is the data coming from the controller. Here in our application we are not using the acknowledge pin.
The Data pin (1) on the receiver is connected to the D12 pin on arduino. The Command pin (2) is connected to D11. The Attention pin (6) is connected to D10.The clock pin (7) is connected to D13. The 4th pin on receiver is GND and 5this VCC(3.3V). The 2-pin RMC connector K3 is used to power up the board. The power is in between 7 to 12V. K11 and K13 are 2-pin RMC connectors where the motor is connected.K9 is a 2-pin RMC connector where we connect the appropriate voltage for DC motor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
#include <PS2X_lib.h> //for v1.6 #include <Servo.h> PS2X ps2x; // create PS2 Controller Class Servo myservo; //right now, the library does NOT support hot pluggable controllers, meaning //you must always either restart your Arduino after you conect the controller, //or call config_gamepad(pins) again after connecting the controller. int error = 0; byte type = 0; byte vibrate =0; int L1=6,L2=4,R1=5,R2=7,X1,Y1,X2,Y2; int P1=2,P2=3,S1=8; int pos = 0; void setup() { Serial.begin(57600); myservo.attach(9); // attaches the servo on pin 9 to the servo object pinMode(P1, OUTPUT); // setting D2 (PWM pin) as output pinMode(P2, OUTPUT); // setting D3 (PWM pin) as output pinMode(S1, OUTPUT); // setting D8 (Standby pin) as output pinMode(L1, OUTPUT); //Setting left side motor pin as output pinMode(L2, OUTPUT); //Setting left side motor pin as output pinMode(R1, OUTPUT); //Setting right side motor pin as output pinMode(R2, OUTPUT); //Setting right side motor pin as output ps2x.config_gamepad(13,11,10,12, false, false); //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error digitalWrite(P1,HIGH); //setting the pin as high digitalWrite(P2,HIGH); //setting the pin as high digitalWrite(S1,HIGH); //setting the pin as high } void loop(){ ps2x.read_gamepad(false, false); //read controller delay(50); if(((Y1=(ps2x.Analog(PSS_RY))) <=10)&&(((X1=(ps2x.Analog(PSS_RX))) >=120)&&((X1=(ps2x.Analog(PSS_RX))) <=140))) // reading and comparing the analog value of the stick { forward(); Serial.println("FORWARD"); // forward movement } if(((Y1=(ps2x.Analog(PSS_RY))) >=240)&&(((X1=(ps2x.Analog(PSS_RX))) >=120)||((X1=(ps2x.Analog(PSS_RX))) <=140))) // reading and comparing the analog value of the stick { backward();Serial.println("BACKWARD"); // reverse movement } if(((Y1=(ps2x.Analog(PSS_RX))) <=10)&&(((X1=(ps2x.Analog(PSS_RY))) >=120)||((X1=(ps2x.Analog(PSS_RY))) <=140))) // reading and comparing the analog value of the stick { left(); Serial.println("LEFT"); // left movement } if(((Y1=(ps2x.Analog(PSS_RX))) >=240)&&(((X1=(ps2x.Analog(PSS_RY))) >=120)||((X1=(ps2x.Analog(PSS_RY))) <=140))) // reading and comparing the analog value of the stick { right();Serial.println("RIGHT"); // right movement } if(((Y1=(ps2x.Analog(PSS_RY))) ==127)&&((X1=(ps2x.Analog(PSS_RX))) ==128)) // reading and comparing the analog value of the stick { digitalWrite(L2,LOW) ;digitalWrite(L1,LOW);digitalWrite(R1,LOW);digitalWrite(R2,LOW); // stop movement } } void forward() //function for forward movement { digitalWrite(L2,HIGH) ; digitalWrite(L1,LOW); digitalWrite(R1,HIGH); digitalWrite(R2,LOW); } void backward() //function for reverse movement { digitalWrite(L2,LOW) ; digitalWrite(L1,HIGH); digitalWrite(R1,LOW); digitalWrite(R2,HIGH); } void left() //function for left movement { digitalWrite(L1,LOW) ; digitalWrite(L2,LOW); digitalWrite(R1,HIGH); digitalWrite(R2,LOW); } void right() //function for right movement { digitalWrite(L2,HIGH) ; digitalWrite(L1,LOW); digitalWrite(R1,LOW); digitalWrite(R2,LOW); } void _stop() //function for stop movement { digitalWrite(L1,LOW); digitalWrite(L2,LOW); digitalWrite(R1,LOW); digitalWrite(R2,LOW); } |
Leave a Reply
You must be logged in to post a comment.