<分区>
更新:
正如 Pete Kirkham 正确指出的那样,println 将数字缩短为两个位置。我用普通的旧打印更新了我的代码,它仍然只切断了这两个。如果您希望它有 7 个小数位,则必须放置 Serial.print(some_number, 7) 。
谢谢皮特
原帖
我是 C++ 的新手,正在编写一个 arduino GPS 应用程序,并注意到一个值不断缩短到小数点后两位,而结构中的另一个值却没有。
这是结构声明
struct Poi {
char name[30];
double lat;
double lng;
};
这是实际的结构实例:
Poi poi_list[3] = {
{"AK Plaza W",36.9905263,127.0847449}
,
{"AK Plaza East",36.9905263,127.0861048}
,
{"Ramen Place",36.9905263,127.0895004}
}
现在,当我尝试查看 poi_list[0].lat 或 [1].lat 或 [2].lat 时,所有值都是 36.99 并且它会切断其余部分,其中 .lng 值始终是整个数字。
整个文件在github上here
#include "TinyGPS++.h"
#include "SoftwareSerial.h"
SoftwareSerial serial_connection(10, 11); //RX=pin 10, TX=pin 11
TinyGPSPlus gps;//This is the GPS object that will pretty much do all the grunt work with the NMEA data
TinyGPSLocation loc;
const double EIFFEL_TOWER_LAT = 36.96070;
const double EIFFEL_TOWER_LNG = 127.05692;
struct Poi {
char name[30];
double lat;
double lng;
};
Poi poi_list[13] = {
{"AK Plaza W",36.9905263,127.0847449}
,
{"AK Plaza East",36.9905263,127.0861048}
,
{"Ramen Place",36.9905263,127.0895004}
,
{"BX Center",36.9905263,127.0339894}
,
{"BX West",36.9905263,127.0350462}
,
{"Osan Chilis",36.9905263,127.0365375}
,
{"Work Main Gate",36.9905263,127.0219141}
,
{"Commissary",36.9905263,127.0024359}
,
{"PX Food Court",36.9905263,127.0002204}
,
{"PX Main entrance",36.9905263,126.9993058}
,
{"One Stop",36.9905263,127.0221126}
,
{"AMC",36.9905263,127.0425832}
,
{"Braii Republic",36.9905263,127.0446378}
};
int number_of_points = 13;
void setup()
{
Serial.begin(9600);//This opens up communications to the Serial monitor in the Arduino IDE
serial_connection.begin(9600);//This opens up communications to the GPS
Serial.println("GPS Start");//Just show to the monitor that the sketch has started
}
void loop()
{
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())
{
for(int i=0;i<number_of_points;i++){
Serial.print("Name: ");
Serial.println(poi_list[i].name);
Serial.print("Distance: ");
Serial.print(calculate_distance(poi_list[i]));
}
}
}
float calculate_distance(Poi point){
float distance = gps.distanceBetween(
gps.location.lat(),
gps.location.lng(),
point.lat,
point.lng);
Serial.print("lat is ");
Serial.print(point.lat);
Serial.println("");
Serial.print("longitutde is ");
Serial.print(point.lng);
return distance;
}