We have to connect the Arduino UNO board to the PC using a USB cable.
Note: In the given string X and Y coordinates are in degrees so we have to convert it into radians.
Sr.No | Range of angle | Directions |
337.5° to 360° and 0° to 22.5° | North | |
2. | 22.5° to 67.5° | Northeast |
3. | 67.5° to 112.5° | East |
4. | 112.5° to 157.5° | Southeast |
5. | 157.5° to 202.5° | South |
6. | 202.5° to 247.5° | Southwest |
7. | 247.5° to 292.5° | West |
8. | 292.5° to 337.5° | Northwest |
#include <math.h>
#define CAMP_LAT 18.556283 // Camp Latitude (destination)
#define CAMP_LON 73.955067 // Camp Longitude (destination)
#define R 6371 // Earth's radius in kilometers
void setup() {
Serial.begin(9600); //Initialize serial communication
}
void loop() {
if (Serial.available()) { // Check for data on Serial monitor
String gpsString = Serial.readString();
gpsString.trim(); // Remove any leading/trailing whitespace
double soldierLat, soldierLon;
if (parseCoordinates(gpsString, soldierLat, soldierLon)) { // Extract latitude and longitude from the string
double distance = calculateDistance(soldierLat, soldierLon, CAMP_LAT, CAMP_LON); // Calculate distance to camp
String direction = calculateDirection(soldierLat, soldierLon, CAMP_LAT, CAMP_LON); // Determine compass direction
Serial.print("Distance to camp: ");
Serial.print(distance, 2); // Display distance with 2 decimal places on Serial monitor
Serial.println(" km ");
Serial.print("Direction to camp: ");
Serial.println(direction); // Display compass direction (e.g., N, NE, E) on serial monitor
}
}
}
/* Parses GPS coordinates from a custom-formatted string */
bool parseCoordinates(String gpsString, double &lat, double &lon) {
if (gpsString.startsWith("X=")) {
int xIndex = gpsString.indexOf("X="); // Find the position of "X="
int yIndex = gpsString.indexOf(",Y="); // Find the position of ",Y="
// Extract latitude and longitude substrings and convert to double
lat = gpsString.substring(xIndex + 2, yIndex).toDouble();
lon = gpsString.substring(yIndex + 3).toDouble(); // Extract longitude correctly
return true;
} else {
Serial.println("Error: Invalid GPS format");
return false;
}
}
/* Calculates the great-circle distance between two GPS coordinates using the Haversine formula.*/
double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
double dLat = radians(lat2 - lat1); // Convert latitude difference to radians
double dLon = radians(lon2 - lon1); // Convert longitude difference to radians
lat1 = radians(lat1); // Convert initial latitude to radians
lat2 = radians(lat2); // Convert final latitude to radians
// Apply Haversine formula
double a = sin(dLat / 2) * sin(dLat / 2) + cos(lat1) * cos(lat2) * sin(dLon / 2) * sin(dLon / 2);
double c = 2 * atan2(sqrt(a), sqrt(1 - a));
return R * c; // Return distance in kilometers
}
/* Calculates the compass direction (bearing) from the current position to the camp.*/
String calculateDirection(double lat1, double lon1, double lat2, double lon2) {
double dLon = radians(lon2 - lon1); // Convert longitude difference to radians
lat1 = radians(lat1); // Convert current latitude to radians
lat2 = radians(lat2); // Convert destination latitude to radians
// Calculate bearing using trigonometric functions
double y = sin(dLon) * cos(lat2);
double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
double bearing = atan2(y, x); // Compute initial bearing in radians
bearing = degrees(bearing); // Convert bearing to degrees
// Normalize bearing to 0-360 degrees
if (bearing < 0) {
bearing += 360;
}
// Convert bearing to compass direction
if (bearing >= 337.5 || bearing < 22.5) return "N";
else if (bearing >= 22.5 && bearing < 67.5) return "NE";
else if (bearing >= 67.5 && bearing < 112.5) return "E";
else if (bearing >= 112.5 && bearing < 157.5) return "SE";
else if (bearing >= 157.5 && bearing < 202.5) return "S";
else if (bearing >= 202.5 && bearing < 247.5) return "SW";
else if (bearing >= 247.5 && bearing < 292.5) return "W";
else return "NW";
}
CAMP_LAT
and CAMP_LON
→ Latitude and longitude of the camp (destination). R
→ This is the Earth's radius in kilometers.Serial.begin(9600);
→ Initializes serial communication at 9600 baud rate to receive GPS coordinates.
parseCoordinates()
.
calculateDistance()
to compute the distance (in kilometers) between the soldier and the camp.calculateDistance();
a = sin²(Δlat/2) + cos(lat1) * cos(lat2) * sin²(Δlon/2)
c = 2 * atan2(√a, √(1−a))
(R)
for distance in kilometers.
calculateDirection();
and converts it to compass directions (e.g., N, NE, E).calculateDirection();
y = sin(Δlon) * cos(lat2)
x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(Δlon)
bearing = atan2(y, x)