PS2 controller code question Brushless motor Arduino

Spot for your general beginner questions.
Post Reply
ljrc10
Posts: 4
Joined: Dec 27th, 2015, 7:16 pm

PS2 controller code question Brushless motor Arduino

Post by ljrc10 »

Hello guys

I'm totally new in the ROV world and arduino. I recently choose to build one as part one of my school project but I find some issues trying to make my motors work with a PS2 controller.

I have 3 motors
With one small joystick I made two motors spin to one direction and the other one moving to the other direction using right and left
I basically did what is showing in this picture http://imgur.com/b1o2OfE


This is my code:

#include <Servo.h>
Servo firstESC;
Servo secondESC;
int throttlePin = A2;
int throttlePin0 = A0;
int firstESCPin = A0;
int secondESCPin = A2;

void setup()
{
firstESC.attach(9);
secondESC.attach(8);
}

void loop()
{
int throttle = analogRead(throttlePin);
throttle = map(throttle, 0, 1023, 0, 179);
int throttle0 = analogRead(throttlePin0);
throttle0 = map(throttle0, 0, 1023, 0, 179);

firstESC.write(throttle);
secondESC.write(throttle0);
}


Now, I want to do the same thing but using a PS2 controller and I have no idea how to do the connections in the arduino and how to make a good code. I already have the PS2 library.
I tried check some things of the bill porter library but I got confused with actually what could help me. I already found a way to connect my PS2 controller to the protoboard without opened it and I tried to use a random code once and the light of the controller got on but nothing happen at all. I wonder if someone can help how to adpat my code just to spin 2 motors to one side and 1 motor to other one.

Thanks!
rossrov
Posts: 383
Joined: Feb 28th, 2013, 5:01 pm
Location: Australia

Re: PS2 controller code question Brushless motor Arduino

Post by rossrov »

Welcome ljrc10. I'm surprised no-one has responded to your post yet. As to the PS2, I cannot help. You look to be off to a good start so why not just keep the joystick you are already using, and just add a second stick? Instead of sending the mapped stick variables direct to the servo function, you can "print" them to serial port, sending down the tether to a second Arduino, which reads the serial and then use those values in the servo command.
Another possibility is that if you do not need a long tether, use 4 wires and put a battery and regulated 5 volt supply (LM7805 or better) in the control box, and use just one Arduino the same as you have done. Joysticks to tether, tether to Arduino in ROV.
User avatar
KR2_Diving
Posts: 391
Joined: Aug 30th, 2012, 11:43 am
Location: Currently: NW Suburbs of Chicago. Originally: NE Wisconsin

Re: PS2 controller code question Brushless motor Arduino

Post by KR2_Diving »

Hey ljrc10!
Sorry! Not sure how i missed your questions!

I would be happy to have a quick look at what you have so far...

I have worked quite a bit with Bill Porters PS2 Libraries, and would be happy to see if i can help you out!

Can you post your code so far?

Ryan
"KR2_Diving"
ljrc10
Posts: 4
Joined: Dec 27th, 2015, 7:16 pm

Re: PS2 controller code question Brushless motor Arduino

Post by ljrc10 »

Hey guys! Thanks for the help :)

As I said, I'm new on arduino so I'm not sure if my code is right or make sense. I got this code online from a guy who said I could find more help here, since what I'm trying to do is a little bit different. I tried to mix with what I already have, but I'm not sure if what I did is right.

This is my code so far


#include <PS2X_lib.h>
#include <Servo.h>

PS2X ps2x;
Servo servo;


byte vibrate = 0;

int modo = 1;
int distance = 0;
int distanceC1 = 0;
int distanceC2 = 0;
int throttlePin = A2;
int servoPin = A2;

void setup() {
ps2x.config_gamepad(13, 11, 10, 12, true, true);
servo.attach(9);
}


void loop()
{
ps2x.read_gamepad(false, vibrate);
if (ps2x.ButtonPressed(PSB_START)) {
automatico();
}
else if(ps2x.ButtonPressed(PSB_SELECT)) {
manual();
}

{
int throttle = analogRead(throttlePin);
throttle = map(throttle, 0, 1023, 0, 179);
servo.write(throttle);
}
}
void manual() {
if (ps2x.ButtonPressed(PSB_PAD_UP)) {
up();
}
else if (ps2x.ButtonPressed(PSB_PAD_LEFT)) {
left();
}
else if (ps2x.ButtonPressed(PSB_PAD_RIGHT)) {
right();
}
else if (ps2x.ButtonPressed(PSB_PAD_DOWN)) {
down();
}
else {
stop();
}
}

void automatico() {
servo.write(90);
delay(200);
distancia = analogRead(0);
if (distance >= 14) {
up();
}
else {
parado();
servo.write(0);
delay(200);
distanceC1 = analogRead(0);
servo.write(180);
delay(400);
distanceC2 = analogRead(0);
servo.write(90);
delay(200);
if (distanceC1 > distanceC2) {
left();
delay(200);
stop();
}
else if (distanceC2 > distanceC1) {
right();
delay(200);
stop();
}
else {
left();
delay(200);
stop();
}
}
}

void up() {
}
void down() {
}
void right() {
}
void left() {
}
void stop() {
}
rossrov
Posts: 383
Joined: Feb 28th, 2013, 5:01 pm
Location: Australia

Re: PS2 controller code question Brushless motor Arduino

Post by rossrov »

Just at a glance - I may have missed something. I'm guessing that you are using the analog input as a preset of some sort? The function stop() should be called parado(). Put something in that function, or it won't do anything. Distancia should be distance, as distancia has not been defined. The compiler would have shown an error here. You can use "auto format" in the Arduino editor to make the text easier to read.
User avatar
KR2_Diving
Posts: 391
Joined: Aug 30th, 2012, 11:43 am
Location: Currently: NW Suburbs of Chicago. Originally: NE Wisconsin

Re: PS2 controller code question Brushless motor Arduino

Post by KR2_Diving »

ljrc10 wrote:I tried to mix with what I already have, but I'm not sure if what I did is right.
I am going to guess you have a pot or something connected to A0 to control speed?

I am also thinking that the way the code is written, you will have to HOLD either START or SELECT for anything to happen...

As another side note, there is no test to confirm the controller is actually connected and working.
I'm jumping on a transatlantic flight tomorrow night and i will see if i can throw something together to pass the time and to get you pointed in the right direction...

Can you clarify what you are hoping to achieve? How do you want to control your ROV? For example: Will you be using a combination of buttons, knobs, and the PS2 controller?

Ryan
"KR2_Diving"

P.S. Don't suppose the code at the link below looks familiar?
https://www.robocore.net/modules.php?na ... pic&t=4092
ljrc10
Posts: 4
Joined: Dec 27th, 2015, 7:16 pm

Re: PS2 controller code question Brushless motor Arduino

Post by ljrc10 »

Hey guys thanks for all the help! I'm sorry take some days, I was traveling back home from a long trip.

This forum on the link is the one I used too. I actually have a code from other person from there, but I'm not actually sure if any of that is something that can help.

And yes this something that looks like my connection right now (I uploaded the picture I used to do mine):

http://imgur.com/dvFgjyl

But instead of just having one pot connected to A0 I have other one too connected to A2
So basically, I have 3 brushless motor, I can move 2 motors foward and back if I pull the joystick up and down (using A0), and I can move the third motor foward and back if I move right and left (using A2). I want to be able to do exactly the same but with a PS2 controller. Maybe use the START to turn on and off too.
I don't know if it is possible do the same using what I already have or if I need to change too much.


This is the code I used with the small joystick

#include <Servo.h>
Servo firstESC;
Servo secondESC;
int throttlePin = A2;
int throttlePin0 = A0;
int firstESCPin = A0;
int secondESCPin = A2;

void setup()
{
firstESC.attach(9);
secondESC.attach(8);
}

void loop()
{
int throttle = analogRead(throttlePin);
throttle = map(throttle, 0, 1023, 0, 179);
int throttle0 = analogRead(throttlePin0);
throttle0 = map(throttle0, 0, 1023, 0, 179);

firstESC.write(throttle);
secondESC.write(throttle0);
}


I will be using the PS2 controller just to move this 3 motors. If I already be able get some help with that (which would be awesome) maybe I can think about put some flashlight. But for now, that's the only thing I have in mind.
User avatar
subn00b
Posts: 10
Joined: Oct 18th, 2015, 12:11 am
Location: Sydney Australia

Re: PS2 controller code question Brushless motor Arduino

Post by subn00b »

Here is my code, using Bill Porters library, which reads the analog values from both PS2 sticks and then sends it across a serial connection to another Arduino. Hope this helps!

Code: Select all

// ROV_Controller_v06
// Arduino Micro

#include <SoftwareSerial.h>
#include <SoftEasyTransfer.h>
#include <PS2X_lib.h>  //for v1.6

//create two objects
SoftEasyTransfer ETout; 
SoftwareSerial XBee(4, 5); // RX, TX

struct SEND_DATA_STRUCTURE{
  //put your variable definitions here for the data you want to receive
  //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
  byte value1;
  byte value2;
  byte value3;
  byte value4;
  byte extra;
};

// PS2X stuff
PS2X ps2x; // create PS2 Controller Class
int error = 0; 
byte type = 0;
byte vibrate = 0;

SEND_DATA_STRUCTURE txdata;

void setup() {
  Serial.begin(9600);
  XBee.begin(9600);
  //start the library, pass in the data details and the name of the serial port.
  ETout.begin(details(txdata), &XBee);

  PS2X_setup();

  txdata.value1=128;
  txdata.value2=128;
  txdata.value3=128;
  txdata.value4=128;
  txdata.extra=128;
}

void loop() {
  txdata.extra=0;
  ps2x.read_gamepad();
  
  txdata.value1=ps2x.Analog(PSS_LY);
  txdata.value2=ps2x.Analog(PSS_LX);
  txdata.value3=ps2x.Analog(PSS_RY);
  txdata.value4=ps2x.Analog(PSS_RX);

  ETout.sendData();
}


void PS2X_setup() {
 error = ps2x.config_gamepad(12,10,9,11, true, true);   //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 http://www.billporter.info for updates and to report bugs.");
 } 
  else if(error == 1)
   Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit http://www.billporter.info for troubleshooting tips");
  else if(error == 2)
   Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit http://www.billporter.info for troubleshooting tips");
  else if(error == 3)
   Serial.println("Controller refusing to enter Pressures mode, may not support it. ");

   type = ps2x.readType(); 
     switch(type) {
       case 0:
        Serial.println("Unknown Controller type");
       break;
       case 1:
        Serial.println("DualShock Controller Found");
       break;
       case 2:
         Serial.println("GuitarHero Controller Found");
       break;
     }
}
rossrov
Posts: 383
Joined: Feb 28th, 2013, 5:01 pm
Location: Australia

Re: PS2 controller code question Brushless motor Arduino

Post by rossrov »

Hi subn00b.

Maybe post this and the ROV servo/ESC code in the Control Systems and Tethers or the Arduino sections. Ready-made ROV control system!

Ross
Post Reply