HI All,
I have been working on a ROV project for the past while.
I did one version of the code which did work but I felt that the program had grown arms and legs plus the code was way too complex than it needed to be.
So I started again. This time, I have written a library so that the motor type definitions and initialization are hidden away.
The problem I am having is that I can only seem to get the right motor (starboard) to work when I push the x-y joystick forwards. It is the same when the joystick is pushed left or right (port of starboard) - only the motor on the right works.
I have tried another motor and checked the continuity of the cables on the umbilical, which buzzed out ok. Changing the motor made no difference.
I am getting y values back of -10 and +10 when the joystick is pushed forwards.
The PWM signals are changing on both sides of the motor driver board (L298N)
The hardware that I am using is
Arduino R3
L298N x2
Arduino 2 axis joysticks x 2.
The code that I am using is
/// Include the necessary libraries
#include <motor.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <HardwareSerial.h>
// Define the pins for the joysticks
#define X_JOYSTICK_PIN A0 //Joystick input Port/Starboard pin
#define Y_JOYSTICK_PIN A1 //Joystick input Forward/Reverse pin
#define Z_JOYSTICK_PIN A2 //Joystick input Vertical up/down pin
// Define the pins for the L298N motor controllers
// Starboard Motor L298 Pins
#define stbdPWMPin 3 //PWM pin Starboard Motor
#define stbdIn3Pin 2 //Starboard Motor In3
#define stbdIn4Pin 4 //Starboard Motor In4
// Port Motor L298 Pins
#define prtPWMPin 6 //PWM pin Port Motor
#define prtIn1Pin 5 //Port Motor In1
#define prtIn2Pin 7 //Port Motor In2
//Vertical Motor PWM Pins
#define vertPWMPin 10 //Vertical Motor PWM Pin
#define vertIn3Pin 8 //Vertical Motor In3 Pin
#define vertIn4Pin 9 //Vertical Motor In4 Pin
// Define variables to store the joystick values
float xValue;
float yValue;
float zValue;
float xMiddle, yMiddle, zMiddle;
float xMiddleHigh, xMiddleLow;
float yMiddleHigh, yMiddleLow;
float zMiddleLow, zMiddleHigh;
//define variables for the PWM values, In1, In2 (port motor), In3 In4 (starboard and vertical motors)
boolean prtIn1, prtIn2;
boolean stbdIn3, stbdIn4;
boolean vertIn3, vertIn4;
int prtPWM, stbdPWM, vertPWM;
// Define the motor objects for the motors
motor motorPort(prtPWMPin, prtIn1Pin, prtIn2Pin);
motor motorStbd(stbdPWMPin, stbdIn3Pin, stbdIn4Pin);
motor motorVert(vertPWMPin,vertIn3Pin, vertIn4Pin);
float fmap(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
int absPWM (int PWM ){
if (PWM < 0){
PWM = abs(PWM);
return PWM;
}
else {
return PWM;
}
}
// function to return a scaled value of -10 to +10V from the analogue pins
double scaled_value(int pin, int lower_lim, int upper_lim) {
int raw = analogRead(pin);
double scaled = fmap(raw, 0, 1023, lower_lim, upper_lim);
//scaled = scaled + 1; // add 1 so that 0 not returned at minimum
return scaled;
}
void setup() {
// Initialize the serial communication
Serial.begin(9600);
// Values for up/down joystick dead value
zMiddle = 0.2;
zMiddleHigh = zMiddle/2;
zMiddleLow = 0 - zMiddleHigh;
xMiddle = 0.2;
xMiddleHigh = xMiddle/2;
xMiddleLow = 0 - xMiddleHigh;
yMiddle = 0.2;
yMiddleHigh = yMiddle/2;
yMiddleLow = 0 - yMiddleHigh;
}
void loop() {
// Read the joystick values
xValue = scaled_value(X_JOYSTICK_PIN, -10, 10);
yValue = scaled_value(Y_JOYSTICK_PIN, -10, 10);
zValue = scaled_value(Z_JOYSTICK_PIN, -10, 10);
//delay(500);
//Serial.println("X Value ");
//Serial.print(xValue);
delay(500);
Serial.println("Y Value ");
Serial.print(yValue);
//delay(500);
//Serial.println("Z Value ");
//Serial.print(zValue);
// Control the motors using the L298N motor controllers
// For port & starboard motors
//-10 V = forwards. 10V = backwards
if (yValue < yMiddleLow) { //if Y joystick < 0 set motors forwards
prtIn1 = HIGH;
prtIn2 = LOW;
stbdIn3 = HIGH;
stbdIn4 = LOW;
prtPWM = map(yValue, -10, 10, -255, 255);
stbdPWM = map(yValue, -10, 10, -255, 255);
//check if PWM values are -ve
prtPWM = absPWM(prtPWM);
stbdPWM = absPWM(stbdPWM);
Serial.println ("PrtIn1");
Serial.print(prtIn1);
Serial.println ("PrtIn2");
Serial.print(prtIn2);
Serial.println("Prt PWM ");
Serial.print(prtPWM);
motorPort.setDirection(prtIn1, prtIn2);
motorPort.speed(prtPWM);
motorStbd.setDirection(stbdIn3, stbdIn4);
motorStbd.speed(stbdPWM);
} else if (yValue > yMiddleHigh) { // If Y Joystick > 0 set motors backwards
prtIn1 = LOW;
prtIn2 = HIGH;
stbdIn3= LOW;
stbdIn4 = HIGH;
prtPWM = map(yValue, -10, 10, -255, 255);
stbdPWM = map(yValue, -10, 10, -255, 255);
prtPWM = absPWM(prtPWM);
stbdPWM = absPWM(stbdPWM);
Serial.println ("PrtIn1");
Serial.print(prtIn1);
Serial.println ("PrtIn2");
Serial.print(prtIn2);
Serial.println("Prt PWM ");
Serial.print(prtPWM);
motorPort.setDirection(prtIn1, prtIn2);
motorPort.speed(prtPWM);
motorStbd.setDirection(stbdIn3, stbdIn4);
motorStbd.speed(stbdPWM);
} else if (yValue >= yMiddleLow && yValue <= yMiddleHigh){ // If Y value == 0, stop both motors
motorStbd.stop();
motorPort.stop();
}
if (xValue > xMiddleHigh) { // If X value > 0, turn to starboard
prtIn1 = HIGH;
prtIn2 = LOW;
stbdIn3= LOW;
stbdIn4 = LOW;
prtPWM = map(xValue, -10, 10, -255, 255);
prtPWM = absPWM(prtPWM);
motorPort.setDirection(prtIn1, prtIn2);
motorPort.speed(prtPWM);
motorStbd.stop();
} else if (xValue < xMiddleLow) { // If x value <0, turn to port
prtIn1 = LOW;
prtIn2 = LOW;
stbdIn3= HIGH;
stbdIn4 = LOW;
stbdPWM = absPWM(stbdPWM);
motorPort.stop();
motorStbd.setDirection(stbdIn3, stbdIn4);
motorStbd.speed(stbdPWM);
} else if (xValue >= xMiddleLow && xValue <= xMiddleHigh){
motorStbd.stop();
}
// For vertical motor
//Serial.print("zValue: ");
//Serial.println(zValue);
//delay(500);
if (zValue > zMiddleHigh) { //If Z Value > 0, set vertical motor forwards
vertIn3 = HIGH;
vertIn4 = LOW;
vertPWM = map(zValue, -10, 10, -255, 255);
vertPWM = absPWM(vertPWM);
motorVert.setDirection(vertIn3, vertIn4);
motorVert.speed(vertPWM);
} else if (zValue < zMiddleLow){ // If Z Value < 0, set vertical motor backwards
vertIn3 = LOW;
vertIn4 = HIGH;
vertPWM = map(zValue, -10, 10, -255, 255);
vertPWM = absPWM(vertPWM);
motorVert.setDirection(vertIn3, vertIn4);
motorVert.speed(vertPWM);
} else if (zMiddleLow >= zValue <= zMiddleHigh ){
motorVert.stop();
}
// Print the motor speeds to the serial monitor for debugging
//Serial.print("Port Motor Speed: ");
//Serial.println(prtPWM);
//Serial.print("Starboard Motor Speed: ");
//Serial.println(stbdPWM);
//Serial.print("Vertical Motor Speed: ");
//Serial.println(vertPWM);
// Add a small delay to prevent the motors from changing too quickly
delay(100);
}
I have checked and double checked my code, but I can't see anything obvious.
Can anyone else see what the problem is please?