I have two T100 thrusters that I am trying to control with a program I wrote that runs in the Arduino IDE. I was able to compile it fine and the thrusters rotate after the 5 second delay but when I enter commands in the serial monitor nothing happens.
I have attached my code and would love some feedback. Also, because this seems like it would be a pretty common/useful program I would imagine someone has completed this task before so if anyone has any helpful links that would be great.
It would not allow me to attach my program to this post so I have to copy paste:
#include <Servo.h>
Servo servo1;
Servo servo2;
const int servoRight = 8;
const int servoLeft = 7;
void setup() {
Serial.begin(9600); //turn serial on
servo1.attach(8);
servo2.attach(8);
// send "stop" signal to ESC.
servo1.writeMicroseconds(1500);
servo2.writeMicroseconds(1500);
// delay to allow the ESC to recognize the stopped signal
delay(5000);
}
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("TL");
}
}
// Forward and Reverse
else {
if (a == '1') {
if (b == '5') {
servo1.writeMicroseconds(1500);
servo2.writeMicroseconds(1500);
}
else if (b == '6') {
servo1.writeMicroseconds(1600);
servo2.writeMicroseconds(1600);
}
else if (b == '7') {
servo1.writeMicroseconds(1700);
servo2.writeMicroseconds(1700);
}
else if (b == '4') {
servo1.writeMicroseconds(1400);
servo2.writeMicroseconds(1400);
}
else if (b == '3') {
servo1.writeMicroseconds(1300);
servo2.writeMicroseconds(1300);
}
}
}
}
else {
servo1.writeMicroseconds(1600); // if no forward command
servo2.writeMicroseconds(1600); // then just go straight
}
}
You’ve got a good start but there are a few issues I see:
You’ve got both servos attached to the same pin (pin 8) so they will both do the same thing at all times.
Your program ends with an “else” statement that sets both thrusters to 1600. This runs if there is no serial input. The problem is that this is in the loop, so it is running very fast. If you enter input through serial, it is probably doing the correct thing, but the loop runs again right away and since there is no new serial input, it just sets the motor outputs back to 1600. I would remove the else statement at the end so the motors stay where you last left them.
Alternatively, you could add a delay, such as delay(1000) to cause any input command to execute for 1 second and then switch back to slow forward.
Can you tell us more about your setup and the goal? You’re going to have to enter input very quickly to be able to accurately control the ROV with this code. Perhaps I can give you some better suggestions.
Thank you, it works now! But you are right I am going to have to enter input very quickly which will make controlling my ROV tough. I would love to hear any suggestions that you have.
There are a number of things you could do. I think the simplest recommendation would be to connect another Arduino at the surface and attach it to several joysticks or buttons so that you could physically provide inputs. The Arduino would read in those inputs and then send them through Serial to the Arduino onboard the ROV.
Another option, if you have more software skills, is to write a program on the computer using a language such as C++, Python, or NodeJS, that would read a joystick controller and send those values to the Arduino.
I too will be using arduino(mega) and a logitech wingman pro 3d extreme for controlling my ROV. I have been gathering code and researching multiple sites for data on how to accomplish this task. I will (hopefully) be controlling 4- T100 thrusters, 3 servos, 2 soleniods valves, a transducer, camera and lights with the joystick. If I make any further headway, I will post the info for you to use. I currently have three L293D Motor Drive Shield Expansion Boards for Arduino Mega, 2 Arduino Mega 2560’s, 1 USB 2.0 shield and 1 Ethernet Shield. I will see if I can use a 5-port ethernet hub/switch in the ROV to make these connections to the laptop on the surface via cat6e shielded cable. In addition, I ordered 8 additional Arduino Uno’s, just in case, and to give me some flexibility in design.