I have a project to make a boat with waypoint gps navigation, I have combined the two codes (waypoint and compass heading). but when I enter the code l298n for the motor driver, for some reason the motor doesn't respond. is there something wrong in writing my code?
the picture on the serial monitor is like this, but the motor still doesn't respond. is there a miss in writing the code?
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <QMC5883LCompass.h>
#define RX_PIN 3
#define TX_PIN 2
#define LED_PIN 7
#define GPS_BAUD 9600
#define WAYPOINT_THRESHHOLD 5
#define LEFT_MOTOR 8 // Enable/speed motors Right
#define RIGHT_MOTOR 9 // Enable/speed motors Left
#define IN_1 4 // L298N in1 motors Rightx
#define IN_2 5 // L298N in2 motors Right
#define IN_3 6 // L298N in3 motors Left
#define IN_4 7 // L298N in4 motors Left
TinyGPSPlus gps;
QMC5883LCompass compass;
SoftwareSerial ss(RX_PIN, TX_PIN);
int calcLeftMotor(double relBearing) {
if (relBearing > 0)
return 255;
else
return 1.425 * relBearing + 255;
}
int calcRightMotor(double relBearing) {
if (relBearing <= 0)
return 255;
else
return -1.425 * relBearing + 255;
}
//KALAU NGASIH RAW DATA MAGNETIK (if give raw magnetic data)
double calcHeading (int x, int y) {
return -atan2(x, y) * 180 / 3.141592654;
}
void setup() {
Serial.begin(9600);
ss.begin(GPS_BAUD);
pinMode(LED_PIN, OUTPUT); //LED
pinMode(LEFT_MOTOR, OUTPUT);
pinMode(RIGHT_MOTOR, OUTPUT);
pinMode(IN_1, OUTPUT);
pinMode(IN_2, OUTPUT);
pinMode(IN_3, OUTPUT);
pinMode(IN_4, OUTPUT);
Serial.println("Waypoint Tracker");
//init compass
compass.init();
}
void loop() {
double targetLat = -7.013606;
double targetLon = 107.638358;
while (ss.available() > 0)
gps.encode(ss.read());
double dist = 99999999;
if (gps.location.isValid()) {
double dist = distance(targetLat, targetLon, gps.location.lat(), gps.location.lng());
Serial.print("distance to target: ");
Serial.println(dist);
if (dist < WAYPOINT_THRESHHOLD)
goForward();
else
stop();
// if (dist < WAYPOINT_THRESHHOLD)
// digitalWrite(LED_PIN, HIGH);
//
// if (dist > WAYPOINT_THRESHHOLD)
// digitalWrite(LED_PIN, LOW);
double bearing = 0; //target angle
compass.read(); // reads from compass
double heading = calcHeading(compass.getX(), compass.getY());
double relBearing = bearing - heading;
int leftSpeed = calcLeftMotor(relBearing);
int rightSpeed = calcRightMotor(relBearing);
Serial.print(" Heading: "); Serial.println(heading);
Serial.print("Left: "); Serial.print(leftSpeed); Serial.print(" Right: "); Serial.println(rightSpeed);
Serial.println(" ");
delay (1000);
analogWrite(LEFT_MOTOR, leftSpeed);
analogWrite(RIGHT_MOTOR, rightSpeed);
}
// else {
// Serial.println ("GPS NOT FOUND");
// delay (1000);
// }
}
double distance(double lat1, double lon1, double lat2, double lon2)
{
// Conversion factor from degrees to radians (pi/180)
const double toRadian = 0.01745329251;
// First coordinate (Radians)
double lat1_r = lat1 * toRadian;
double lon1_r = lon1 * toRadian;
// Second coordinate (Radians)
double lat2_r = lat2 * toRadian;
double lon2_r = lon2 * toRadian;
// Delta coordinates
double deltaLat_r = (lat2 - lat1) * toRadian;
double deltaLon_r = (lon2 - lon1) * toRadian;
// Distance
double a = sin(deltaLat_r / 2) * sin(deltaLat_r / 2) + cos(lat1_r) * cos(lat2_r) * sin(deltaLon_r / 2) * sin(deltaLon_r / 2);
double c = 2 * atan2(sqrt(a), sqrt(1 - a));
double distance = 6371 * c * 1000;
return distance;
}
void goForward() {
digitalWrite(IN_1, HIGH);
digitalWrite(IN_2, LOW);
digitalWrite(IN_3, HIGH);
digitalWrite(IN_4, LOW);
}
void goBackward() {
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, HIGH);
digitalWrite(IN_3, LOW);
digitalWrite(IN_4, HIGH);
}
void stop() {
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, LOW);
digitalWrite(IN_3, LOW);
digitalWrite(IN_4, LOW);
}
void goRight() {
digitalWrite(IN_1, HIGH);
digitalWrite(IN_2, LOW);
digitalWrite(IN_3, LOW);
digitalWrite(IN_4, LOW);
}
void goLeft() {
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, LOW);
digitalWrite(IN_3, HIGH);
digitalWrite(IN_4, LOW);
}