Don't ad-block us - support your favorite websites. We have safe, unobstrusive, robotics related ads that you actually want to see - see here for more.
0 Members and 1 Guest are viewing this topic.
/* ROV controller sketch. PS2X library by Bill Porter used.What needs to be done - hmmm*/#include <PS2X_lib.h> //for v1.6PS2X ps2x; // create PS2 Controller Class//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;//Motor1 is the right motor, when you look at the ROV from its front. Motor2 is the left motor. Motor3 is the top-motor.int M1forwardPin = 0; //To input1 on the first L293D chipint M1backwardPin = 1; //To input2, changes the direction of motor1int PWM_M1_Pin = 3; //To enable1 on the first L293D chip. Maybe the bank-motors PWM will also be connected here.int M2forwardPin = 2; //To input3int M2backwardPin = 4; //To input4int PWM_M2_Pin = 5; //To enable2 on the first L293Dint M3upPin = 7; //To input1 on second L293D chipint M3downPin = 8; //To input2int PWM_M3_Pin = 6; //To enable1 on the second L293D chipint steerfactor = 1.5; // 255/steerfactor = turn speed when going forward or backwards at the same time a side is pressedint idle = 0; // This is the analog value for the PWM on motor3int diveIncrement = 10; // The analog value that the will increase or decrease the idle value//int bankleftPin = 8;//int bankrightPin = 9;void setup(){Serial.begin(57600); //CHANGES for v1.6 HERE!!! **************PAY ATTENTION************* error = ps2x.config_gamepad(13,11,10,12, false, false); //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error if(error == 0){ Serial.println("Found Controller, configured successful"); Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;"); Serial.println("holding L1 or R1 will print out the analog stick values."); Serial.println("Go to [url=http://www.billporter.info]www.billporter.info[/url] for updates and to report bugs."); } else if(error == 1) Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit [url=http://www.billporter.info]www.billporter.info[/url] for troubleshooting tips"); else if(error == 2) Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit [url=http://www.billporter.info]www.billporter.info[/url] for troubleshooting tips"); else if(error == 3) Serial.println("Controller refusing to enter Pressures mode, may not support it. "); //Serial.print(ps2x.Analog(1), HEX); type = ps2x.readType(); switch(type) { case 0: Serial.println("Unknown Controller type"); break; case 1: Serial.println("DualShock Controller Found"); break; } pinMode(M1forwardPin, OUTPUT);pinMode(M1backwardPin, OUTPUT);pinMode(M2forwardPin, OUTPUT);pinMode(M2backwardPin, OUTPUT);pinMode(M3upPin, OUTPUT);pinMode(M3downPin, OUTPUT);pinMode(PWM_M1_Pin, OUTPUT);pinMode(PWM_M2_Pin, OUTPUT);pinMode(PWM_M3_Pin, OUTPUT);//pinMode(bankrightPin, OUTPUT);//pinMode(bankleftPin, OUTPUT);}void loop(){//needed for the PS2X libraryif(error == 1) //skip loop if no controller found return; ps2x.read_gamepad(); //read controller analogWrite(PWM_M1_Pin, 255); //Goes to enable1 and makes the first pair of input pins on the first L293D chip live analogWrite(PWM_M2_Pin, 255); //Goes to enable2 and makes the second pair of input pins on the first L293D chip live analogWrite(PWM_M3_Pin, idle); //Goes to enable1 and makes the first pair of input pins on the second L293D chip live if(ps2x.Button(PSB_PAD_UP)) { //will be TRUE as long as button is pressed digitalWrite(M1forwardPin, HIGH); //Motor1 og Motor2 turns and make the ROV go forward digitalWrite(M2forwardPin, HIGH); Serial.print("ROV forward"); Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC); } if(ps2x.Button(PSB_PAD_RIGHT)){ digitalWrite(M1forwardPin, HIGH); //Motor1 drives forward, Motor2 backwards, which makes the ROV turn to the right digitalWrite(M2backwardPin, HIGH); Serial.print("ROV right turn"); Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC); } if(ps2x.Button(PSB_PAD_LEFT)){ digitalWrite(M1backwardPin, HIGH); //Motor2 now drives forward, Motor1 backwards, which makes the ROV turn to the left digitalWrite(M2forwardPin, HIGH); Serial.print("ROV left turn"); Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC); } if(ps2x.Button(PSB_PAD_DOWN)){ digitalWrite(M1backwardPin, HIGH); //Motor1 and Motor2 turns on and make the ROV go backwards digitalWrite(M2backwardPin, HIGH); Serial.print("ROV backward"); Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC); } //Up and right key pressed at the same time makes the ROV go forward, turning slightly to the right. if((ps2x.Button(PSB_PAD_UP)) && (ps2x.Button(PSB_PAD_RIGHT))){ digitalWrite(M1forwardPin, HIGH); //Motor1 goes forward digitalWrite(M2forwardPin, HIGH); //Motor2 goes forward analogWrite(PWM_M2_Pin, (255/steerfactor)); //Motor2 tho running at limited speed Serial.print("ROV forward + turn right"); } //Up and left key pressed at the same time makes the ROV go forward, turning slightly to the left. if((ps2x.Button(PSB_PAD_UP)) && (ps2x.Button(PSB_PAD_LEFT))){ digitalWrite(M1forwardPin, HIGH); //Motor1 goes forward digitalWrite(M2forwardPin, HIGH); //Motor2 goes forward analogWrite(PWM_M1_Pin, (255/steerfactor)); //Motor1 tho running at limited speed Serial.print("ROV forward + turn left"); } //Down and right key pressed at the same time makes the ROV go backward, turning slightly to the right. if((ps2x.Button(PSB_PAD_DOWN)) && (ps2x.Button(PSB_PAD_RIGHT))){ digitalWrite(M1backwardPin, HIGH); //Motor1 goes backward digitalWrite(M2backwardPin, HIGH); //Motor2 goes backward analogWrite(PWM_M2_Pin, (255/steerfactor)); //Motor2 tho running at limited speed Serial.print("ROV backward + right"); } //Down and left key pressed at the same time makes the ROV go backward, turning slightly to the left. if((ps2x.Button(PSB_PAD_UP)) && (ps2x.Button(PSB_PAD_LEFT))){ digitalWrite(M1backwardPin, HIGH); //Motor1 goes backward digitalWrite(M2backwardPin, HIGH); //Motor2 goes backward analogWrite(PWM_M1_Pin, (255/steerfactor)); //Motor1 tho running at limited speed Serial.print("ROV backward + left"); } //FAILSAFES //Up, down and left key pressed at the same time will result in turning left if((ps2x.Button(PSB_PAD_UP)) && (ps2x.Button(PSB_PAD_LEFT)) && (ps2x.Button(PSB_PAD_DOWN))){ digitalWrite(M1backwardPin, HIGH); //Motor2 goes backward digitalWrite(M2forwardPin, HIGH); //Motor1 goes forward Serial.print("Failsafe, up + down + left = left"); } //Up, down and right key pressed at the same time will result in turning right if((ps2x.Button(PSB_PAD_UP)) && (ps2x.Button(PSB_PAD_RIGHT)) && (ps2x.Button(PSB_PAD_DOWN))){ digitalWrite(M1forwardPin, HIGH); //Motor2 goes forward digitalWrite(M2backwardPin, HIGH); //Motor1 goes backward Serial.print("Failsafe, up + down + right = right"); } //Up, right and left pressed at the same time will result in the ROV going forward if((ps2x.Button(PSB_PAD_UP)) && (ps2x.Button(PSB_PAD_RIGHT)) && (ps2x.Button(PSB_PAD_LEFT))){ digitalWrite(M1forwardPin, HIGH); //Motor2 goes forward digitalWrite(M2forwardPin, HIGH); //Motor1 goes forward Serial.print("Failsafe, up + left + right = up"); } //Down, right and left pressed at the same time will result in the ROV going backward if((ps2x.Button(PSB_PAD_DOWN)) && (ps2x.Button(PSB_PAD_RIGHT)) && (ps2x.Button(PSB_PAD_LEFT))){ digitalWrite(M1backwardPin, HIGH); //Motor2 goes backward digitalWrite(M2backwardPin, HIGH); //Motor1 goes backward Serial.print("Failsafe, down + left + right = down"); } //divefactor //Checks if the idle variable is within the acceptable range and changes it if not. if (idle < 0) { idle = 0; } if (idle > 255) { idle = 255; } //If L2 is pressed, the variable "idle" will increase by the value of the diveIncrement if(ps2x.Button(PSB_L2)) { idle = idle + diveIncrement; Serial.println("L2 pressed, idle increase to: "); Serial.print(idle); } //If L3 is pressed, the variable "idle" will decrease by the value of the diveIncrement if(ps2x.Button(PSB_L3)) { idle = idle - diveIncrement; Serial.println("L3 pressed, idle decrease to: "); Serial.print(idle); } //simple up and down of the ROV with R2 and R3 if(ps2x.Button(PSB_R2)) //is R2 pressed? { analogWrite (PWM_M3_Pin, 255); digitalWrite (M3upPin, HIGH); Serial.println("R2 pressed, climb at full speed"); } else if(ps2x.Button(PSB_R3)) //Else is R3 pressed? { analogWrite (PWM_M3_Pin, 255); digitalWrite (M3downPin, HIGH); Serial.println("R3 pressed, dive at full speed"); } else //Niether pressed? Then go back to idle-mode { //Turn m3 on digitalWrite (M3downPin, HIGH); analogWrite (PWM_M3_Pin, idle); } delay(50);}