Go Down

Topic: Code Check for ROV (Read 916 times) previous topic - next topic

mech_eng

I've cleaned the code up using some of the above suggestions.

Here it is:
Code: [Select]

//-------------------------------------------------------------------

#include <Servo.h>
Servo servo;

//PINS
const int  M1A = 20; // M1---M4 ARE HORIZONTAL THRUSTERS
const int  M1B = 21;
const int  M2A = 22;
const int  M2B = 23;
const int  M3A = 24;
const int  M3B = 25;
const int  M4A = 26;
const int  M4B = 27;
const int  M5A = 28; // M5---M6 ARE VERTICAL THRUSTERS
const int  M5B = 29;
const int  M6A = 30;
const int  M6B = 31;
const int  M12 = 44; // M1 and M2 PWM
const int  M34 = 45; // M3 and M4 PWM
const int  M56 = 46; // M5 and M6 PWM
const int  servopin = 2;

//INCOMING DATA
const char SOP = '<';
const char EOP = '>';
enum {
  Case, Speed };


//VARIABLES
int whichNumber = Case;
int Casev, Speedv;
int pwmright;            //1
int pwmleft;             //2
int pwmfwd;              //3
int pwmrev;              //4
int pwmcw;               //5
int pwmccw;              //6
int pwmsurface;          //7
int pwmdive;             //8
int pwmpitch;            //9
int valservo;            //10


//-------------------------------------------------------------------


void setup ()
{
  Serial.begin (9600);
  pinMode (M1A, OUTPUT);
  pinMode (M1B, OUTPUT);
  pinMode (M2A, OUTPUT);
  pinMode (M2B, OUTPUT);
  pinMode (M3A, OUTPUT);
  pinMode (M3B, OUTPUT);
  pinMode (M4A, OUTPUT);
  pinMode (M4B, OUTPUT);
  pinMode (M5A, OUTPUT);
  pinMode (M5B, OUTPUT);
  pinMode (M6A, OUTPUT);
  pinMode (M6B, OUTPUT);
  pinMode (M12, OUTPUT);
  pinMode (M34, OUTPUT);
  pinMode (M56, OUTPUT);
  servo.attach(servopin);

}

//-------------------------------------------------------------------


void processNumber (int x)
{

  switch (whichNumber)
  {

  case Case:
    Casev = x;
    whichNumber = Speed;
    break;

  case Speed:
    Speedv = x;
    whichNumber = Case;
    break;


  }



//-------------------------------------------------------------------

void processInput ()
{
  static float receivedNumber = 0;
  static boolean False = false;
  byte c = Serial.read ();

  switch (c)
  {

  case EOP: 
    if (False)
      processNumber (- receivedNumber);
    else
      processNumber (receivedNumber);

    // fall through to start a new number
  case SOP:
    receivedNumber = 0;
    False = false;
    break;

  case '0' ... '9':
    receivedNumber *= 10;             // receivedNumber = recievedNumber*10
    receivedNumber += c - '0';      //receivedNumber = recievedNumber + (c - '0')
    break;

  case '-':
    False = true;
    break;

  }



//-------------------------------------------------------------------

void loop ()
{
 
  Serial.print(127);
  if (Serial.available ())
 
  processInput ();
 
 
  int  var1 = Casev;
  int  var2 = Speedv;

  switch (var1)
  {

    //RIGHT 
  case 1:
    pwmright = var2;
    moveright();
    break;

    //LEFT
  case 2:
    pwmleft = var2;
    moveleft();
    break;

    //FWD
  case 3:
    pwmfwd = var2;
    fwd();
    break;

    //REV
  case 4:
    pwmrev = var2;
    rev();
    break;

    //CW
  case 5:
    pwmcw = var2;
    cw();
    break;

    //CCW
  case 6:
    pwmccw = var2;
    ccw();
    break;

    //SURFACE
  case 7:
    pwmsurface = var2;
    surface();
    break;

    //DIVE
  case 8:
    pwmdive = var2;
    dive();
    break;

    //PITCH
  case 9:
    pwmpitch = var2;
    pitch();
    break;

    //SERVO   
  case 10:
    valservo = var2;           
    claw();   
    break;

  }

}
//-------------------------------------------------------------------1

void moveright()
{
}

//------------------------------------------------------------------- 2

void moveleft()
{
}

//------------------------------------------------------------------- 3

void fwd()
{
}

//------------------------------------------------------------------- 4

void rev()
{
}

//------------------------------------------------------------------- 5

void cw()
{
}

//------------------------------------------------------------------- 6

void ccw()
{
}

//------------------------------------------------------------------- 7

void surface()
{
}

//------------------------------------------------------------------- 8

void dive()
{
}

//------------------------------------------------------------------- 9

void pitch()
{
}

//------------------------------------------------------------------- 10

void claw()
{

  valservo = map(valservo, 0, 255, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  servo.write(valservo);                  // sets the servo position according to the scaled value
}


//-------------------------------------------------------------------




Data will be transmitted over 50' of wire using RS-232, how reliable can I expect this to be if the data transmission line is ran next to the power line of the ROV which will carry 20 amps? Also, would this code work using RS 485 communications?

Am I missing anything obvious here?


Graynomad

Quote
Also, would this code work using RS 485 communications?

RS-232, RS-485, makes no difference to your code if you use 485 in full duplex mode. Do you have 2 extra wires?

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

mech_eng

Should have 3 unused twisted pairs

zoomkat

You can run the rs232 type communication over cat3 four conductor phone wire (tx, rx, ground, and power/spare).
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

mech_eng

If I wanted to go the full duplex RS 485 route, would this require 4 MAXRS485 convertors? Basically keeping one in transmit mode and one in receive mode on both sides of the CAT5 cable at all times and leaving the code as is?

Go Up