Hello Everyone,
I am new to Arduino world and this forum. I am working on a project where I want to have a self-driving car go to a GPS-programmed destination using given coordinates (latitude and longitude). The car should go the destination and come back to starting point (will have to save initial position = starting point).
The project is long, but i chopped the code to only include the GPS part and the functions to start and stop the car.
I don't know how to enter my destination coordinates for the car to follow. Please see the attached code, it has a lot of description to say what the end product will be, but i removed most other functions that are not needed.
I am using Arduino UNO and NEO-6M GPS module
Please help me!
Thanks!
DJ
===============================================================
Code:
/* By Kaswa
*
- The goal of this project is to similate a self-driving car.
- Procedure:
-
- A set of coordinates (lat and long) will be saved to a variable for the GPS module to tell the car where to go.
-
- GPS module will communicate with sattelite to get current coordinates and save those to a different variable.
- These coordinates will be used for the car to come back to initial position from where it will be sent.
-
- Once ready, Green LED will blind rapidly to indicate that the car is ready to go.
-
- The rear motor of the car will be instructed to begin the forward motion to take the car to the destination entered
- at step 1, and the car should avoid any obstable on its way by using the Ping sensors mounted to the front, and sides.
-
- Once at destination, car will stop, Green and Red will blind for 4 seconds, then car will used coordinates saved for initial
- position to be loaded to the GPS and return to initial starting point.
- */
/*
- HC-SR04 Ping distance sensor:
- VCC to arduino 5v
- GND to arduino GND
- Trig to Arduino: Right - pin13, ctr - pin11, left - pin9
- Echo to Arduino: Right - pin 12, ctr - pin10, left - pin8
*/
// GPS Library and Software Serial
#include "TinyGPS++.h"
#include "SoftwareSerial.h"
// Pin assignments
#define trigLeft 9 // Trigger Left Sensor
#define trigCtr 11 // Trigger Center Sensor
#define trigRight 13 // Trigger Right Sensor
#define echoLeft 8 // Echo Left Sensor
#define echoCtr 10 // Echo Center Sensor
#define echoRight 12 // Echo Right Sensor
#define front1 A0 // Motor 1, out 1
#define front2 A1 // Motor 1, out 2
#define rear1 A2 // Motor 2, out 3
#define rear2 A3 // Motor 2, out 4
#define enA 6 // Enable sig for mot1 PWM, black wire
#define enB 5 // Enable sig for mot2 PWM, brown wire
#define gpsTX 4 // Transmit signal PIN, purple wire GPS --> Yellow wire Arduino
#define gpsRX 3 // Receive signal PIN, blue wire
// Setting up Serial Port and GPS Module
SoftwareSerial serial_connection(gpsTX, gpsRX);
TinyGPSPlus gps; // Will convert NMEA Data to readable format
// Values initialization
int initSP = 200; // Rear Motor Initial spin speed
int maxSP = 150; // Rear Motor Max spin speed
int minRange = 20; // Minimum Ping Senor range needed
long duration; // Duration used to calculate distance for Ping Sensor
long distance; // Distance for center Ping Sensor
long distanceLeft; // Distance for left side Sensor
long distanceRight; // Distance for right side sensor
float xInit; // Car GPS Initial Latitude
float yInit; // Car GPS Initial Longitude
float xNew; // Car GPS Destination Latitude ** SET THIS TO A DEFINED VALUE i.e xNew = 39.797767 **
float yNew; // Car GPS Destination Longitude ** SET THIS TO A DEFINED VALUE i.e yNew = -85.973077 **
float flat; // GPS latitude variable
float flon; // GPS longitude variable
// Code Begins
void setup()
{
Serial.begin (9600);
serial_connection.begin(9600); // This opens up communications to the GPS module
// Ping Sensor Pins
pinMode(trigLeft, OUTPUT);
pinMode(echoLeft, INPUT);
pinMode(trigCtr, OUTPUT);
pinMode(echoCtr, INPUT);
pinMode(trigRight, OUTPUT);
pinMode(echoRight, INPUT);
// Motor Pins
pinMode(front1, OUTPUT);
pinMode(front2, OUTPUT);
pinMode(rear1, OUTPUT);
pinMode(rear2, OUTPUT);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
}
void loop() // Main Program Loop
{
GPSloop();
xInit = flat;
yInit = flon;
/*How do you load xNew and yNew to the GPS so that we can then call the rear motor fonction to send the car to that
- location?
- */
rearMotfwd();
brake();
}
//GPS Code taken from Arduino_GPS/Arduino_GPS.ino at master · LessonStudio/Arduino_GPS · GitHub
void GPSloop()
{
while(serial_connection.available()) // While there are characters to come from the GPS
{
gps.encode(serial_connection.read()); // This feeds the serial NMEA data into the library one char at a time
}
if(gps.location.isUpdated())//This will pretty much be fired all the time anyway but will at least reduce it to only after a package of NMEA data comes in
{
//Get the latest info from the gps object which it derived from the data sent by the GPS unit
Serial.println("Latitude:");
Serial.println(gps.location.lat(), 6);
Serial.println("Longitude:");
Serial.println(gps.location.lng(), 6);
}
}
void rearMotfwd() // Rear Motor Forward Motion Function
{
// Forward Rear Motor
digitalWrite(rear1, HIGH); // These two lines set the direction of the motor
digitalWrite(rear2, LOW); // to forward
analogWrite(enB, maxSP); // Gives the speed for the motor to start moving
//delay(300);
}
// pingSens(); // This function checks if there's any obstacle in front of car
}
void brake() // Function to brake the car/ stop its motion
{
analogWrite(enB, 0); // Removes the speed from the motor
digitalWrite(rear1, LOW);
digitalWrite(rear2, LOW);
digitalWrite(rear1, LOW); // These two lines revert the direction
digitalWrite(rear2, HIGH); // of the motor
analogWrite(enB, maxSP); // Apply the speed
delay(300);
analogWrite(enB, 0); // Removes the speed from the motor
digitalWrite(rear1, LOW);
digitalWrite(rear2, LOW);
}
GPSPortion.ino (5.59 KB)