PS 3 control for OC

PS 3 control for OC
Posted by Harvey Bonser at 2017-04-24 14:22:46

Using the Adafruit motor shield v2.0, Circuits@home Usb shield and an Arduino Uno to translate a ps 3 control to run the Perch. Original code from DIyTechies. We are not programmers so all changes were by trial and error. Hope it helps somebody. Our teams love it, it uses the same setup as the original control just that there is variable speed now. L2 and R2 are the dive/ surface control and the joysticks forward and aft.  Enjoy!

Put heat sinks on the mosfets on the motor shield.

    #include <Wire.h>
    #include <Adafruit_MotorShield.h>
    #include "utility/Adafruit_MS_PWMServoDriver.h"

/*
 DIYTechies.com
 June 5, 2016
 This is a modified version of the PS3BT.ino example sketch by Kristian Lauszus
1Make sure you #include <AFMotor.h>
2Create the AF_DCMotor object with AF_DCMotor(motor#, frequency)  , to setup the motor
H-bridge and latches. The constructor takes two arguments.
The first is which port the motor is connected to, 1, 2, 3 or 4.
frequency is how fast the speed controlling signal is.
For motors 1 and 2 you can choose
MOTOR12_64KHZ, MOTOR12_8KHZ,MOTOR12_2KHZ, orMOTOR12_1KHZ.
A high speed like 64KHz wont be audible but a low
speed like 1KHz will
use less power. Motors 3 & 4 are only possible to run at 1KHz and willignore any setting given
3 Then you can set the speed of the motor using setSpeed(speed)   where the speed  ranges from 0 (stopped) to 255 (full speed). You can set the speed whenever you want.
4. To run the motor, call run(direction) where direction is FORWARD, BACKWARD or RELEASE.
Of course, the Arduino doesn't actually know if the motor is 'forward' or
'backward', so if you want to change which way it thinks is forward, simply swap the two wires
from the motor to the shield.

} */

#include <PS3USB.h>                                                   //Include the necessary libraries.

Adafruit_MotorShield AFMS = Adafruit_MotorShield();


Adafruit_DCMotor *myMotor1 = AFMS.getMotor(1);
Adafruit_DCMotor *myMotor2 = AFMS.getMotor(2);
Adafruit_DCMotor *myMotor3 = AFMS.getMotor(3);
Adafruit_DCMotor *myMotor4 = AFMS.getMotor(4);


USB Usb;

PS3USB PS3(&Usb);

void setup() {
      AFMS.begin();
      myMotor1->setSpeed(255);
       myMotor2->setSpeed(255);
        myMotor3->setSpeed(255);
         myMotor4->setSpeed(255);
 
  Serial.begin(115200);                                              //Don't play with these values, they are needed to intialise the USB shield and connect to the Arduino serial monitor
  if (Usb.Init() == -1) {                                            
    Serial.print(F("\r\nOSC did not start"));
    while(1); //halt
  }
 
}
void loop()
{ front:
  Usb.Task();                                                        //Needed for USB
 
int speed1 = PS3.getAnalogHat(LeftHatY);                             //Here we create a variable called "speed" that reads the values of the X-AXIS on the right joystick on the PS3 (This is the steering values we read from the right thumbstick and send to the L298N).
int speed2 = PS3.getAnalogHat(RightHatY);
int speed3 = PS3.getAnalogButton(L2);
int speed4 = PS3.getAnalogButton(R2);

  if(PS3.PS3Connected || PS3.PS3NavigationConnected)
    
 
                                                                                                                                             
    if(speed1 <= 109)                                                 //Here we say that if the range I'm pressing on the thumbstick is 109 or less, meaning from NETURAL to 0 so I'm selecting the left postion of the thumstick.   
      {        
          myMotor1->setSpeed((109-speed1)*2.33);
         
           myMotor1->run(BACKWARD);
                                             
                                 
      }
      else if(speed1 > 108 && speed1 < 137)                            
      {
          myMotor1->run(RELEASE);
                 
          Serial.println(0);
      }
      else if (speed1 >= 138)                                                                                                                     
      {                                                       
           myMotor1->setSpeed((speed1-138)*2.18);

           myMotor1->run(FORWARD);        
                  
      }
       if(speed2 <= 109)                                                 //Here we say that if the range I'm pressing on the thumbstick is 109 or less, meaning from NETURAL to 0 so I'm selecting the left postion of the thumstick.   
      {        
          myMotor2->setSpeed((109-speed2)*2.33);
         
           myMotor2->run(BACKWARD);
                                             
          Serial.println((109-speed2)*2.33);
                                 
      }
      else if(speed2 > 108 && speed2 < 137)                            
      {
          myMotor2->run(RELEASE);
                 
          Serial.println(0);
      }
      else if (speed2 >= 138)                                                                                                                     
      {                                                       
           myMotor2->setSpeed((speed2-138)*2.18);

           myMotor2->run(FORWARD);        
                   
      }  
    if(speed3 <= 6)                                                 //Here we say that if the range I'm pressing on the thumbstick is 109 or less, meaning from NETURAL to 0 so I'm selecting the left postion of the thumstick.   
      {        
         myMotor3->setSpeed((speed3)); /*(56-speed3)*2.33)*/
         
         myMotor3->run(RELEASE);
                                             
         //Serial.println((109-speed3)*2.33);
          Serial.println(speed3);                         
     }
     
      else if (speed3 >= 8)                                                                                                                     
      {                                                       
           myMotor3->setSpeed((speed3));//chngd from 2.33  ((speed3-58)*2.18)

           myMotor3->run(FORWARD);    
      goto front;    //used so motor dues not reset
                  
                  }
    if(speed4 <= 6)                                                 //Here we say that if the range I'm pressing on the thumbstick is 109 or less, meaning from NETURAL to 0 so I'm selecting the left postion of the thumstick.   
     {        
          myMotor3->setSpeed((4));/*((56-speed4)*2.33)*/
         
       myMotor3->run(RELEASE);
              
     }
    
      else if (speed4 >=8)                                                                                                                     
      {                                                       
           myMotor3->setSpeed((speed4));//chngd from 2.33 ((speed4-58)*2.18)

           myMotor3->run(BACKWARD);        
                //Serial.println((speed4-58)*2.17);
                 Serial.println(speed4);
                  //Serial.println((speed3-58)*2.17);
                  Serial.println(speed3);
                  }
          
}
  

Please report website issues to info@seaperch.org

The SeaPerch logo is a Registered Trademark of the AUVSI Foundation.

Terms of Use & Privacy | Copyright 2013. All Rights Reserved.