6 motors controlled via ethernet with game controller

Anything to do with programing the Arduino Platform.
User avatar
Bennachie
Posts: 113
Joined: Jul 6th, 2018, 11:38 am

Re: 6 motors controlled via ethernet with game controller

Post by Bennachie »

Here's some older code where I read a PC joystick using a little interface PCB set to hex output. You may find it useful to understand how the Joystick library works.

The board is this one-

http://www.hobbytronics.co.uk/usb-host-joystick

If you use it, remember to set it to hex output. Much easier to read with an Arduino.

Code: Select all

#include <Wire.h>
#define baudRate 115200    // Serial port baud rate
#include <Servo.h> 
#include "joystick.h"
Servo panservo;
Servo tiltservo;
Servo vertservo1;
Servo vertservo2;
Servo latservo;
Servo portaftservo;
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()  { 
  Serial.begin(baudRate);
  panservo.attach(2);  
  tiltservo.attach(3);
  vertservo1.attach(4);
  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(89);
  vertservo2.write(89);
  portaftservo.write(89);
  stbdaftservo.write(89);
  portfwdservo.write(89);
  stbdfwdservo.write(89);

  delay(2000);
  analogWrite(13, 255);
  delay(200);
  analogWrite(13, 0);
  delay(200);
  analogWrite(13, 255);
  delay(200);
  analogWrite(13, 0);
  
} 

void loop()  { 
   get_joystick();
   if(joystick_ok==1)
   {
      vertvalue1 = (255 - joystick.slider);
      vertvalue1scale = map(vertvalue1, 0, 255, 0, 179);
      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;
  } 
}
User avatar
Bennachie
Posts: 113
Joined: Jul 6th, 2018, 11:38 am

Re: 6 motors controlled via ethernet with game controller

Post by Bennachie »

For wiring in your MAX488 chips, the following may help-

Vcc is the power to the chip, 5 volts in this case.
This diagram shows full duplex RS422 mode. Simplest, but requires four data wires.
You can ignore the two termination resistors (called Rt on the diagram). You would only need those for really long cables. You won't need them for a typical tether of under 100m long. You do however need to use a cable where the wires are twisted together inside. Cat5 cable is good, a shielded twisted quad is ideal.

Image
Scanro
Posts: 22
Joined: Aug 15th, 2019, 4:30 am

Re: 6 motors controlled via ethernet with game controller

Post by Scanro »

Bennachie wrote:For wiring in your MAX488 chips, the following may help-

Vcc is the power to the chip, 5 volts in this case.
This diagram shows full duplex RS422 mode. Simplest, but requires four data wires.
You can ignore the two termination resistors (called Rt on the diagram). You would only need those for really long cables. You won't need them for a typical tether of under 100m long. You do however need to use a cable where the wires are twisted together inside. Cat5 cable is good, a shielded twisted quad is ideal.
Holy, I am not sure what to say other than thanks for the help man, I really appreciate it, this has really been the big hurdle for me. I will get right on getting to learn how to code in the joystick using the Arduino Joystick library. I also found a older post from KR2_Diving with some code, but he didn't have the sensors and used a PS2 controller. Again thanks for all of the help with the project so far.
User avatar
Bennachie
Posts: 113
Joined: Jul 6th, 2018, 11:38 am

Re: 6 motors controlled via ethernet with game controller

Post by Bennachie »

No problem.

I forgot to say I'm using Arduino Megas, but smaller Arduinos should be virtually the same, maybe a few pin assignments might need changed.

If you need further help, just ask. And keep us posted with how you are getting on.
Scanro
Posts: 22
Joined: Aug 15th, 2019, 4:30 am

Re: 6 motors controlled via ethernet with game controller

Post by Scanro »

Bennachie wrote:No problem.

I forgot to say I'm using Arduino Megas, but smaller Arduinos should be virtually the same, maybe a few pin assignments might need changed.

If you need further help, just ask. And keep us posted with how you are getting on.
I also use 2 Megas, since that's what we had laying around, the next challenge for me will be to get the Logitech 3D Pro Extreme Joystick to control the motors correctly, but I am sure that I can figure it out given enough time.
right now these are the following steps I need to go through before it is finished
1. Logitech joystick code
2. high quality camera
3. How to save the video from the ROV
4. placement of the different sensors
5. Putting it all together and test
Again thanks for the help :)
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: I also use 2 Megas, since that's what we had laying around, the next challenge for me will be to get the Logitech 3D Pro Extreme Joystick to control the motors correctly, but I am sure that I can figure it out given enough time.
right now these are the following steps I need to go through before it is finished
1. Logitech joystick code
2. high quality camera
3. How to save the video from the ROV
4. placement of the different sensors
5. Putting it all together and test
Again thanks for the help :)
As you're using a USB PC joystick I recommend the board I mentioned above. If you look at the documentation that comes with it, plus my old code above, you should be able to work out how to read the joystick. Each model of joystick is a little different, so the hardest bit is figuring out which commands on your joystick change which bits of the string from the board.

*Edit* Remember my note from above. If you connect your joystick to the hardware serial port, you need to power up the Arduino before powering up the joystick or it'll hang.

For a camera, look at cameras for drones and FPV flying. I've gone for a Runcam Split 2S. It outputs live composite video and records 1080p HD video to an onboard micro SD card. A better option than recording the video feed at the surface, I think.
Scanro
Posts: 22
Joined: Aug 15th, 2019, 4:30 am

Re: 6 motors controlled via ethernet with game controller

Post by Scanro »

Bennachie wrote:As you're using a USB PC joystick I recommend the board I mentioned above. If you look at the documentation that comes with it, plus my old code above, you should be able to work out how to read the joystick. Each model of joystick is a little different, so the hardest bit is figuring out which commands on your joystick change which bits of the string from the board.

*Edit* Remember my note from above. If you connect your joystick to the hardware serial port, you need to power up the Arduino before powering up the joystick or it'll hang.

For a camera, look at cameras for drones and FPV flying. I've gone for a Runcam Split 2S. It outputs live composite video and records 1080p HD video to an onboard micro SD card. A better option than recording the video feed at the surface, I think.
Thanks for the camera recommendation, I have looked into the board you recommended, they seem to be sold out of them and only for now sell 10 or more of it in a unassembled kit form, but I will keep an eye on it. I will give the coding another crack to see if I can't get it to make sense to me a work.
I also made a quick schematic of how I see the things go together right now.
Attachments
ROV circuit.png
ROV circuit.png (117.29 KiB) Viewed 7420 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 »

You have a PM.
Scanro
Posts: 22
Joined: Aug 15th, 2019, 4:30 am

Re: 6 motors controlled via ethernet with game controller

Post by Scanro »

I got output from the joystick, with the code from Bennachie, sadly I can't seem to figure out how to get the joystick code combined with the topside code and make it work.
Attachments
output.JPG
output.JPG (49.79 KiB) Viewed 7370 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 »

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.
Post Reply