Assistance with 4 thruster ROV Programming

Hey guys,

I have been working on programming my 4 T100 thrusters to control forward/reverse and vertical movement of my ROV. I think that my code in running along the correct direction so far but I am unsure if it is completely right. Would you mind giving me some pointers and advice on how to proceed if my code is incorrect? I am posting it below:

 

#include <Servo.h>

 

Servo servo1;

Servo servo2;

Servo servo3;

Servo servo4;

const int servoRight = 8; //initializing servo pins

const int servoLeft = 7;

const int servoUP = 6;

const int servoDOWN = 5;

 

void setup() {

Serial.begin(9600); //turn serial on

servo1.attach(8); //set servo1 pin 8

servo2.attach(7); //set servo2 pin 7

servo3.attach(6); //set servo3 pin 6

Servo4.attach(5); //set servo4 pin 5

 

// send “stop” signal to ESC.

 

servo1.writeMicroseconds(1500);

servo2.writeMicroseconds(1500);

servo3.writeMicroseconds(1500);

servo4.writeMicroseconds(1500);

 

delay(5000); // 5 sec delay to allow the ESC to //recognize the stopped signal

 

}

 

void loop() {

 

if (Serial.available()) {

 

// Turning Left, input of TL in serial monitor means Turn Left

 

char a = Serial.read(); //stores first value of the serial input as a character

char b = Serial.read(); //checks if same as necessary input

char c = Serial.read(); // for int values only

char d = Serial.read(); //for int values only

Serial.write(a);

Serial.write(b);

Serial.write(c);

Serial.write(d);

 

if (a == ‘T’) {

if (b == ‘L’) {

servo1.writeMicroseconds(1700); // then gives commands

servo2.writeMicroseconds(1500);

Serial.write(“TL”);

}

 

// Turning Right

if (b == ‘R’) {

servo1.writeMicroseconds(1500);

servo2.writeMicroseconds(1700);

Serial.write(“TR”);

}

}

 

// Forward and Reverse

 

else {

if (a == ‘1’) {

if (b == ‘5’) {

servo1.writeMicroseconds(1500); //don’t move

servo2.writeMicroseconds(1500);

}

else if (b == ‘6’) {

servo1.writeMicroseconds(1600); //slow forward

servo2.writeMicroseconds(1600);

}

else if (b == ‘7’) {

servo1.writeMicroseconds(1700); //faster forward

servo2.writeMicroseconds(1700);

}

else if (b == ‘4’) {

servo1.writeMicroseconds(1400); //slow reverse

servo2.writeMicroseconds(1400);

}

else if (b == ‘3’) {

servo1.writeMicroseconds(1300); //faster reverse

servo2.writeMicroseconds(1300);

}

}

}

}

 

// Up and Down (Vertical Movement)

 

else {

if (a == ‘1’) {

if (b == ‘5’) {

servo3.writeMicroseconds(1500);

servo4.writeMicroseconds(1500);

}

else if (b == ‘6’) {

servo3.writeMicroseconds(1600);

servo4.writeMicroseconds(1600);

}

else if (b == ‘7’) {

servo3.writeMicroseconds(1700);

servo4.writeMicroseconds(1700);

}

else if (b == ‘4’) {

servo3.writeMicroseconds(1400);

servo4.writeMicroseconds(1400);

}

else if (b == ‘3’) {

servo3.writeMicroseconds(1300);

servo4.writeMicroseconds(1300);

}

}

}

}

 

else {

servo1.writeMicroseconds(1600); // if no forward command

servo2.writeMicroseconds(1600); // then just go straight

servo3.writeMicroseconds(1500); // if no vertical command

servo4.writeMicroseconds(1500); // stay level

}

}

Vaughn,

Thanks for posting. I think your code will result in the thruster outputs you are looking for. I’d recommend looking into ways to use a joystick or similar control method as I think it’s going to be very difficult to control the ROV with typed commands!

Best,

Rusty

Hey Rusty!

Thanks for getting back to me so quickly. I thought the same thing as well but when I attempt to compile the code in the arduino command prompt I keep getting hit with the errors shown below:

 

sketch_apr02a:89: error: stray '' in program
sketch_apr02a:89: error: stray '' in program
sketch_apr02a:91: error: stray '' in program
sketch_apr02a:91: error: stray '' in program
sketch_apr02a:97: error: stray '' in program
sketch_apr02a:97: error: stray '' in program
sketch_apr02a:105: error: stray '' in program
sketch_apr02a:105: error: stray '' in program
sketch_apr02a:111: error: stray '' in program
sketch_apr02a:111: error: stray '' in program
sketch_apr02a:125: error: stray '' in program
sketch_apr02a:125: error: stray '' in program
sketch_apr02a:127: error: stray '' in program
sketch_apr02a:127: error: stray '' in program
sketch_apr02a:135: error: stray '' in program
sketch_apr02a:135: error: stray '' in program
sketch_apr02a:143: error: stray '' in program
sketch_apr02a:143: error: stray '' in program
sketch_apr02a:151: error: stray '' in program
sketch_apr02a:151: error: stray '' in program
sketch_apr02a:159: error: stray '' in program
sketch_apr02a:159: error: stray '' in program
sketch_apr02a:181: error: stray '' in program
sketch_apr02a:181: error: stray '' in program
sketch_apr02a:183: error: stray '' in program
sketch_apr02a:183: error: stray '' in program
sketch_apr02a:191: error: stray ‘' in program
sketch_apr02a:191: error: stray ‘' in program
sketch_apr02a:199: error: stray ‘' in program
sketch_apr02a:199: error: stray ‘' in program
sketch_apr02a:207: error: stray ‘' in program
sketch_apr02a:207: error: stray ‘' in program
sketch_apr02a:215: error: stray ‘' in program
sketch_apr02a:215: error: stray ‘' in program
sketch_apr02a.ino: In function ‘void setup()’:
sketch_apr02a:33: error: ‘Servo4’ was not declared in this scope
sketch_apr02a.ino: In function ‘void loop()’:
sketch_apr02a:89: error: ‘u2018T’ was not declared in this scope
sketch_apr02a:89: error: expected )' before 'u2019' sketch_apr02a:91: error: 'u2018L' was not declared in this scope sketch_apr02a:91: error: expected )’ before ‘u2019’
sketch_apr02a:97: error: ‘u201cTL’ was not declared in this scope
sketch_apr02a:105: error: ‘u2018R’ was not declared in this scope
sketch_apr02a:105: error: expected )' before 'u2019' sketch_apr02a:111: error: 'u201cTR' was not declared in this scope sketch_apr02a:125: error: 'u20181' was not declared in this scope sketch_apr02a:125: error: expected )’ before ‘u2019’
sketch_apr02a:127: error: ‘u20185’ was not declared in this scope
sketch_apr02a:127: error: expected )' before 'u2019' sketch_apr02a:135: error: 'u20186' was not declared in this scope sketch_apr02a:135: error: expected )’ before ‘u2019’
sketch_apr02a:143: error: ‘u20187’ was not declared in this scope
sketch_apr02a:143: error: expected )' before 'u2019' sketch_apr02a:151: error: 'u20184' was not declared in this scope sketch_apr02a:151: error: expected )’ before ‘u2019’
sketch_apr02a:159: error: ‘u20183’ was not declared in this scope
sketch_apr02a:159: error: expected )' before 'u2019' sketch_apr02a:181: error: 'a' was not declared in this scope sketch_apr02a:181: error: 'u20181' was not declared in this scope sketch_apr02a:181: error: expected )’ before ‘u2019’
sketch_apr02a:183: error: ‘b’ was not declared in this scope
sketch_apr02a:183: error: ‘u20185’ was not declared in this scope
sketch_apr02a:183: error: expected )' before 'u2019' sketch_apr02a:191: error: 'u20186' was not declared in this scope sketch_apr02a:191: error: expected )’ before ‘u2019’
sketch_apr02a:199: error: ‘u20187’ was not declared in this scope
sketch_apr02a:199: error: expected )' before 'u2019' sketch_apr02a:207: error: 'u20184' was not declared in this scope sketch_apr02a:207: error: expected )’ before ‘u2019’
sketch_apr02a:215: error: ‘u20183’ was not declared in this scope
sketch_apr02a:215: error: expected `)’ before ‘u2019’
sketch_apr02a.ino: At global scope:
sketch_apr02a:231: error: expected unqualified-id before ‘else’
sketch_apr02a:243: error: expected declaration before ‘}’ token

I’m not quite sure why I keep getting these "" errors when I have none in my program. What do you think could be causing this problem for me?

Hi Vaughn,

This is a tricky error! It’s cause by the fact that the quotes used around characters are special quotes (probably from copy and pasting from MS Word or similar).

This:

else if (b == ‘7’) {

has different quotes than this:

else if (b == '7') {

The compiler doesn’t recognize the fancy formatting of the first version. Try replacing all of those and it should work.

Best,

Rusty

Rusty,

I greatly appreciate your help and advice, I am having one more problem I could use your guidance with though. I fixed the problem above thanks to your advice but when I try to compile and upload my code it keeps telling me that my variables (a, b, c, d) are not defined in the void loop() where I have the code Serial.write(a); and it tells me this for each of the variables being written to. If you could just give me some advice where I am going wrong I would very much appreciate it.

Thanks,

-Vaughn

Rusty,

 

So okay I diagnosed what the problem might be. I believe that in my ‘else’ statements that the arduino command prompt does not like the I am comparing a char to integer values such as I am listing below:

 

else {
if (a == ‘1’) {
if (b == ‘5’) {

 

What do you think I could do in order to solve this problem? Do you think that instead of comparing ‘a’ to ‘1’ it would be more prudent to use a hex value for ‘1’ and other other subsequent integers that I will be equating to my chars?

 

Thanks so much for your time and help,

 

-Vaughn

The problem is that your decision structure looks like this:

if(Turncommand) {
turncode
} else {
forwardcode
} else {
verticalcode
}

The compiler is expecting an if before the second else, the first else closes the if statement, you can’t have two else conditions.

try this:

if(Turncommand) {
turncode
} else if(Forwardcommand) {
forwardcode
} else if(Verticalcommand){
verticalcode
} else {
invalid command, do nothing (maybe stop motors because communications have gone screwy)
}

-Jacob

Jacob,

 

Thank you very much for replying to me. I took the advice that you gave me and implemented it into my code and it was finally able to compile. I really appreciate your time and I will be connecting all of our devices shortly to see if this code will work successfully for our design.

 

Thanks!

 

-Vaughn