6 motors controlled via ethernet with game controller

Anything to do with programing the Arduino Platform.
Scanro
Posts: 22
Joined: Aug 15th, 2019, 4:30 am

Re: 6 motors controlled via ethernet with game controller

Post by Scanro »

Bennachie wrote:In my topside code, the variables I am using for the joystick inputs are slider, xaxis, yaxis and zaxis. Pretty much the first lines in the main loop.

I'm not sure how you are reading your joystick but looking at your graph, it seems you are succeeding in reading values from the stick and putting them in to some sort of variables.

Start with the vertical axis first. If you can get the value from one of your joystick axes and assign it to the variable 'slider', you should be able to get the verts working.

You could always post the code you are using to read the stick here and I could try to stitch it in to mine. I'm away from home all week so it may take me a bit of time to get around to it though.
Thanks, it is pretty much your joystick code, but with some minor changes here and there.
It would be amazing if you could help me stitch it together with your topside code, I will continue to try and I will get back to you if I get it working before you get it working. :)
Thanks for all the help so far and I hope that you have a good day. :)
- Rasmus

Code: Select all

#include <EasyTransfer.h>
#include <Wire.h>
#define baudRate 9600    // Serial port baud rate
#include <Servo.h> 
#include "joystick.h"

Servo panservo;
Servo tiltservo;
Servo vertservo1;
Servo vertservo2;
Servo latservo;
Servo portaftservo; n 
Servo stbdaftservo;
Servo stbdfwdservo;
Servo portfwdservo;
Servo dimmer;

int hat = 0x0F;
int panvalue = 2500;
int panvaluescale;
int tiltvalue = 2500;
int tiltvaluescale;
int buttons_1_8 = 0;
int buttons_9_13 = 0;
int vertvalue1 = 127;
int vertvalue2 = 127;
int vertvalue1scale;
int vertvalue2scale;
int portaftvalue = 127;
int portaftvaluescale;
int stbdaftvalue = 127;
int stbdaftvaluescale;
int stbdfwdvalue = 127;
int stbdfwdvaluescale;
int portfwdvalue = 127;
int portfwdvaluescale;
int dimmervalue;
int dimmervaluescale;
int dimmerlatch = 0;


void setup()  { 
  
  panservo.attach(2);  
  tiltservo.attach(3);
  vertservo1.attach(4,1000,2000);
  vertservo2.attach(5);
  portaftservo.attach(6);
  stbdaftservo.attach(7);
  portfwdservo.attach(8);
  stbdfwdservo.attach(9);
 // dimmer.attach(13);
  panservo.write(89);
  tiltservo.write(89);
  vertservo1.write(1000);
  vertservo2.write(89);
  portaftservo.write(89);
  stbdaftservo.write(89);
  portfwdservo.write(1000);
  stbdfwdservo.write(89);

Serial1.begin(baudRate);

  delay(20);
  analogWrite(13, 255);
  delay(20);
  analogWrite(13, 0);
  delay(20);
  analogWrite(13, 255);
  delay(20);
  analogWrite(13, 0);
  
} 

void loop()  { 
   get_joystick();
   if(joystick_ok==1)
   {
      vertvalue1 = (255-tdata[6]);
      vertvalue1scale = map(vertvalue1, 0, 255, 0, 180);
      vertservo1.write(vertvalue1scale);
      vertvalue2 = (255 - joystick.joystick_z);
      vertvalue2scale = map(vertvalue2, 0, 255, 0, 179);
      vertservo2.write(vertvalue2scale);
      portaftvalue = (((joystick.joystick_rz) - (joystick.joystick_x) - (joystick.joystick_y)) + 255);
      if (portaftvalue >= 255){
        portaftvalue = 255;
      }
      if (portaftvalue <= 0){
        portaftvalue = 0;
      }
      stbdaftvalue = (((joystick.joystick_x) - (joystick.joystick_y) - (joystick.joystick_rz)) + 255);
      if (stbdaftvalue >= 255){
        stbdaftvalue = 255;
      }
      if (stbdaftvalue <= 0){
        stbdaftvalue = 0;
      }
      portfwdvalue = (((joystick.joystick_x) - (joystick.joystick_y) + (joystick.joystick_rz)));
      if (portfwdvalue >= 255){
        portfwdvalue = 255;
      }
      if (portfwdvalue <= 0){
        portfwdvalue = 0;
      }
      stbdfwdvalue = (512 - ((joystick.joystick_x) + (joystick.joystick_y) + (joystick.joystick_rz)));
      if (stbdfwdvalue >= 255){
        stbdfwdvalue = 255;
      }
      if (stbdfwdvalue <= 0){
        stbdfwdvalue = 0;
      }
      portaftvaluescale = map(portaftvalue, 0, 255, 0, 179);
      portaftservo.write(portaftvaluescale);
      stbdaftvaluescale = map(stbdaftvalue, 0, 255, 0, 179);
      stbdaftservo.write(stbdaftvaluescale);
      portfwdvaluescale = map(portfwdvalue, 0, 255, 0, 179);
      portfwdservo.write(portfwdvaluescale);
      stbdfwdvaluescale = map(stbdfwdvalue, 0, 255, 0, 179);
      stbdfwdservo.write(stbdfwdvaluescale);
      hat = joystick.hat;
      if (hat == 0x06 or hat == 0x07 or hat == 0x08){
        if (panvalue < 10000){
          panvalue = panvalue + 1;
          panvaluescale = map(panvalue, 0, 5000, 0, 179);
          panservo.write(panvaluescale);
        }
      
  }
      if (hat == 0x02 or hat == 0x03 or hat == 0x04){
        if (panvalue > 0){
          panvalue = panvalue - 1;
          panvaluescale = map(panvalue, 0, 5000, 0, 179);
          panservo.write(panvaluescale);
        }
      
  }
      if (hat == 0x08 or hat == 0x01 or hat == 0x02){
        if (tiltvalue < 4500){
          tiltvalue = tiltvalue + 1;
          tiltvaluescale = map(tiltvalue, 0, 5000, 0, 179);
          tiltservo.write(tiltvaluescale);
        }
      
  }
      if (hat == 0x04 or hat == 0x05 or hat == 0x06){
        if (tiltvalue > 500){
          tiltvalue = tiltvalue - 1;
          tiltvaluescale = map(tiltvalue, 0, 5000, 0, 179);
          tiltservo.write(tiltvaluescale);
        }
      
  }
      buttons_1_8 = joystick.buttons_1_8;
      if (bitRead(buttons_1_8, 0) == 1 and hat == 0x0F){
        panvalue = 2500;
        panvaluescale = map(panvalue, 0, 5000, 0, 179);
        panservo.write(89);
        tiltvalue = 2500;
        tiltvaluescale = map(tiltvalue, 0, 5000, 0, 179);
        tiltservo.write(89);
      }
      buttons_9_13 = joystick.buttons_9_13;
      if ((bitRead(buttons_9_13, 3) == 1) and dimmerlatch == 0 and dimmervalue < 10){
        dimmervalue = dimmervalue +1;
        dimmervaluescale = map(dimmervalue, 0, 10, 0, 255);
        analogWrite(13, dimmervaluescale);
        dimmerlatch = 1;
      }
      if ((bitRead(buttons_9_13, 4) == 1) and dimmerlatch == 0 and dimmervalue > 0){
        dimmervalue = dimmervalue -1;
        dimmervaluescale = map(dimmervalue, 0, 10, 0, 255);
        analogWrite(13, dimmervaluescale);
        dimmerlatch = 1;
      }
      if (bitRead(buttons_9_13, 3) == 0 and bitRead(buttons_9_13, 4) == 0)
      dimmerlatch = 0;
    } 
 // Serial.print('X');
 // Serial.println(vertvalue1scale);
 // Serial.print('Y');
 // Serial.println(joystick.joystick_y);
 // Serial.print('Z');
 // Serial.println(joystick.joystick_z);
 // Serial.print('rz');
 // Serial.println(joystick.joystick_rz);
 // Serial.print('knap');
 // Serial.println(joystick.buttons_1_8);
 // delay(100);
}
Scanro
Posts: 22
Joined: Aug 15th, 2019, 4:30 am

Re: 6 motors controlled via ethernet with game controller

Post by Scanro »

here is the mapping info I have gathered so far. The number says the position of the affected value in serial monitor (I didn't count that &JOY) and the colour and position tells the position in the serial plotter.
The up down (+ -) on the joystick refers to the small little throttle thing below the logitech branding. The idea for now is to get the up down to control the thrusters that make the ROV go up and down, then the tilt x controlling if it goes forward or backwards, the Y tilt to control side to side movement and then the mix of those two to get it to turn somewhat on the spot.
Attachments
com4 mapping.JPG
com4 mapping.JPG (124.29 KiB) Viewed 10336 times
mapping.JPG
mapping.JPG (69.48 KiB) Viewed 10336 times
User avatar
Bennachie
Posts: 113
Joined: Jul 6th, 2018, 11:38 am

Re: 6 motors controlled via ethernet with game controller

Post by Bennachie »

Ok so x is fore/aft, y is lateral and +- is vertical
Can you confirm directions for me? Normally, the following is true with joysticks-

Forward is x=255
Backward is x=0
Left is y=0
Right is y=255
Up is +-=255
Down is +-=0

Is that correct for your stick?

I can't access my code until next week so it'll be a while before I have a go.
User avatar
Bennachie
Posts: 113
Joined: Jul 6th, 2018, 11:38 am

Re: 6 motors controlled via ethernet with game controller

Post by Bennachie »

And another thing. Looks like you are using serial port 1 for the stick. You'll need another serial port for the comms between topside & vehicle. I will assume this to be serial port 0, but you can change it.

I've also never written code for two serial ports at the same time, and assume you need to be using 9600 baud for the stick. It might get complicated switching baud rates between the ports so I may try it, or may have everything running at 9600 baud instead. Usually I use 115,200 baud.
Scanro
Posts: 22
Joined: Aug 15th, 2019, 4:30 am

Re: 6 motors controlled via ethernet with game controller

Post by Scanro »

Bennachie wrote:Ok so x is fore/aft, y is lateral and +- is vertical
Can you confirm directions for me? Normally, the following is true with joysticks-

Forward is x=255
Backward is x=0
Left is y=0
Right is y=255
Up is +-=255
Down is +-=0

Is that correct for your stick?

I can't access my code until next week so it'll be a while before I have a go.
X=0 is forward here
X= 255 is backwards
Y is correct
Down +- = 225
Up +-= 0
Interesting that they have switched this around
Scanro
Posts: 22
Joined: Aug 15th, 2019, 4:30 am

Re: 6 motors controlled via ethernet with game controller

Post by Scanro »

Bennachie wrote:And another thing. Looks like you are using serial port 1 for the stick. You'll need another serial port for the comms between topside & vehicle. I will assume this to be serial port 0, but you can change it.

I've also never written code for two serial ports at the same time, and assume you need to be using 9600 baud for the stick. It might get complicated switching baud rates between the ports so I may try it, or may have everything running at 9600 baud instead. Usually I use 115,200 baud.
I tried setting it to 115,200 baud, but then it only spits out values for a little over 1.5 lines, so I assumed that it didn't work with such a high rate.
User avatar
Bennachie
Posts: 113
Joined: Jul 6th, 2018, 11:38 am

Re: 6 motors controlled via ethernet with game controller

Post by Bennachie »

Scanro wrote:
Bennachie wrote:Ok so x is fore/aft, y is lateral and +- is vertical
Can you confirm directions for me? Normally, the following is true with joysticks-

Forward is x=255
Backward is x=0
Left is y=0
Right is y=255
Up is +-=255
Down is +-=0

Is that correct for your stick?

I can't access my code until next week so it'll be a while before I have a go.
X=0 is forward here
X= 255 is backwards
Y is correct
Down +- = 225
Up +-= 0
Interesting that they have switched this around
Ok, no probs
User avatar
Bennachie
Posts: 113
Joined: Jul 6th, 2018, 11:38 am

Re: 6 motors controlled via ethernet with game controller

Post by Bennachie »

Scanro wrote:
Bennachie wrote:And another thing. Looks like you are using serial port 1 for the stick. You'll need another serial port for the comms between topside & vehicle. I will assume this to be serial port 0, but you can change it.

I've also never written code for two serial ports at the same time, and assume you need to be using 9600 baud for the stick. It might get complicated switching baud rates between the ports so I may try it, or may have everything running at 9600 baud instead. Usually I use 115,200 baud.
I tried setting it to 115,200 baud, but then it only spits out values for a little over 1.5 lines, so I assumed that it didn't work with such a high rate.
Could be the cable you are using between the various components. The higher the baud rate the better your wiring has to be.

I'll aim for 9600 baud.
Scanro
Posts: 22
Joined: Aug 15th, 2019, 4:30 am

Re: 6 motors controlled via ethernet with game controller

Post by Scanro »

Bennachie wrote:
Scanro wrote:
Bennachie wrote:And another thing. Looks like you are using serial port 1 for the stick. You'll need another serial port for the comms between topside & vehicle. I will assume this to be serial port 0, but you can change it.

I've also never written code for two serial ports at the same time, and assume you need to be using 9600 baud for the stick. It might get complicated switching baud rates between the ports so I may try it, or may have everything running at 9600 baud instead. Usually I use 115,200 baud.
I tried setting it to 115,200 baud, but then it only spits out values for a little over 1.5 lines, so I assumed that it didn't work with such a high rate.
Could be the cable you are using between the various components. The higher the baud rate the better your wiring has to be.

I'll aim for 9600 baud.
I didn't really think of that, right now we are just using those standard cheap arduino cables in all kinds of colors. We do plan on getting some better wires, but first when we have everything working and wired correctly.
Scanro
Posts: 22
Joined: Aug 15th, 2019, 4:30 am

Re: 6 motors controlled via ethernet with game controller

Post by Scanro »

I did a bit of testing today, set the baudRate in the firmware to the usb host board to 115200 and set it up to that in the program too, uploaded it to the Arduino and checked the plotter. The plotter gives outputs at 9600, but as soon as I set it to 115200, then it flatlines. I am not sure if this is the Arduino coding software that is at fault here or something else.
Post Reply