我在使用TinyGPS库解析Lat和Lon时遇到问题。此库是否与 RFduino 兼容?我可以通过将空白草图加载到 RFduino 然后打开串行监视器来读取 NMEA 字符串,因此我知道 GPS 数据正在通过串行端口,但是当我尝试将 Lat 或 Lon 放入变量中时,它会用999999999填充变量。我正在通过BLE将这些数据发送到机器人。如果我不尝试获取 GPS 数据,我可以在纬度或纬度变量中发送我想要的任何值,它会出现在我的自定义 Android 应用程序中。我在某处读到软串行库不适用于 rfduino。这是真的吗?如果没有,我将能够通过硬串行端口打印数据,从而使故障排除变得更加容易。 下面我附上了我在RFduino上使用的代码。任何建议将不胜感激。
// CODE //
#include <RFduinoBLE.h>
#include <TinyGPS.h>
TinyGPS gps;
long lat = 5; //Load lat/lon with junk value for testing
long lon = 6;
char latBuf[20];
char lonBuf[20];
void setup() {
// this is the data we want to appear in the advertisement
// (if the deviceName and advertisementData are too long to fix into the 31 byte
// ble advertisement packet, then the advertisementData is truncated first down to
// a single byte, then it will truncate the deviceName)
RFduinoBLE.advertisementData = "ledbtn";
// start the BLE stack
RFduinoBLE.begin();
Serial.begin(9600);//For GPS Communication
}
void loop(){
char c = byte(Serial.read());
gps.encode(c);
gps.get_position(&lat,&lon); // get latitude and longitude
// send position as char[]
String latString = String(lat);
String lonString = String(lon);
latString.toCharArray(latBuf, 20);
lonString.toCharArray(lonBuf, 20);
RFduinoBLE.send(lonBuf, 20);
}
void RFduinoBLE_onDisconnect()
{
}
void RFduinoBLE_onReceive(char *data, int len)
{
RFduinoBLE.send(lonBuf, 20);
}
我看到的一个问题:loop() 每次执行循环时都试图读出 GPS 坐标。 这种方法有两个问题:1)环路不会等到串行数据准备就绪,2)环路不会等到接收到的GPS数据有效。
通过阅读 http://arduino.cc/en/Tutorial/ReadASCIIString 和 http://arduiniana.org/libraries/tinygps/,我建议将loop()重写为如下所示的内容:
loop() {
char c;
float fLat, fLon;
unsigned long fix_age;
static unsigned long previous_fix_age = 0;
// If nothing to read; do nothing.
// Read as many characters as are available.
while (Serial.available() > 0) {
// Tell the GPS library about the new character.
c = Serial.read();
gps.encode(c);
gps.f_get_position(&flat, &flon, &fix_age);
if (fix_age != TinyGPS::GPS_INVALID_AGE && fix_age != previous_fix_age) {
// new GPS data is valid, new, and ready to be printed
previous_fix_age = fix_age; // remember that we've reported this data.
String latString = String(lat);
...the rest of the code you already have to print the lat and lon.
}
}
}
关于previous_fix_age的代码在那里,以便循环仅在从GPS收到新的定位时才打印坐标。