Robofun 機器人論壇

標題: arduino GPS問題請教 [打印本頁]

作者: James    時間: 2018-12-28 13:02
標題: arduino GPS問題請教
想請問一下各位大大,能不能幫小弟解釋一下這個GPS的程式碼


#include<SoftwareSerial.h>
SoftwareSerial GPSModule(10, 11); // TX, RX
int updates;
int failedUpdates;
int pos;
int stringplace = 0;

String timeUp;
String nmea[15];
String labels[12] {"Time: ", "Status: ", "Latitude: ", "Hemisphere: ", "Longitude: ", "Hemisphere: ", "Speed: ", "Track Angle: ", "Date: "};
void setup() {
Serial.begin(57600);
GPSModule.begin(9600);
}

void loop() {
Serial.flush();
GPSModule.flush();
while (GPSModule.available() > 0)
{
GPSModule.read();

}
if (GPSModule.find("$GPRMC,")) {
String tempMsg = GPSModule.readStringUntil('\n');
for (int i = 0; i < tempMsg.length(); i++) {
if (tempMsg.substring(i, i + 1) == ",") {
nmea[pos] = tempMsg.substring(stringplace, i);
stringplace = i + 1;
pos++;
}
if (i == tempMsg.length() - 1) {
nmea[pos] = tempMsg.substring(stringplace, i);
}
}
updates++;
nmea[2] = ConvertLat();
nmea[4] = ConvertLng();
for (int i = 0; i < 9; i++) {
Serial.print(labels[i]);
Serial.print(nmea[i]);
Serial.println("");
}

}
else {

failedUpdates++;

}
stringplace = 0;
pos = 0;
}

String ConvertLat() {
String posneg = "";
if (nmea[3] == "S") {
posneg = "-";
}
String latfirst;
float latsecond;
for (int i = 0; i < nmea[2].length(); i++) {
if (nmea[2].substring(i, i + 1) == ".") {
latfirst = nmea[2].substring(0, i - 2);
latsecond = nmea[2].substring(i - 2).toFloat();
}
}
latsecond = latsecond / 60;
String CalcLat = "";

char charVal[9];
dtostrf(latsecond, 4, 6, charVal);
for (int i = 0; i < sizeof(charVal); i++)
{
CalcLat += charVal[i];
}
latfirst += CalcLat.substring(1);
latfirst = posneg += latfirst;
return latfirst;
}

String ConvertLng() {
String posneg = "";
if (nmea[5] == "W") {
posneg = "-";
}

String lngfirst;
float lngsecond;
for (int i = 0; i < nmea[4].length(); i++) {
if (nmea[4].substring(i, i + 1) == ".") {
lngfirst = nmea[4].substring(0, i - 2);
//Serial.println(lngfirst);
lngsecond = nmea[4].substring(i - 2).toFloat();
//Serial.println(lngsecond);

}
}
lngsecond = lngsecond / 60;
String CalcLng = "";
char charVal[9];
dtostrf(lngsecond, 4, 6, charVal);
for (int i = 0; i < sizeof(charVal); i++)
{
CalcLng += charVal[i];
}
lngfirst += CalcLng.substring(1);
lngfirst = posneg += lngfirst;
return lngfirst;
}
作者: 超新手    時間: 2018-12-28 13:05
讀取 GPS 的資料並顯示出來




歡迎光臨 Robofun 機器人論壇 (https://robofun.net/forum/) Powered by Discuz! X3.2