Arduino写入屏幕的速度
Arduino writing speed to screen
我无法让我的 GPS 更新速度。它显示文本,如果我将 GPS 速度从位置更新循环中取出,它会显示一次当前速度,然后无法更新。有什么想法吗?
void loop() {
while (serial_connections.available()) {
gps.encode(serial_connections.read());
if (gps.location.isUpdated()) {
DText = Serial.println(gps.speed.mps());
DSat = Serial.println(gps.satellites.value());
}
display.clearDisplay(); // clears last number
display.display(); // writes clear to screen
display.setCursor(10, 5); //Set drawing posision
display.print(DText); // what to draw
display.setCursor(35, 5);
display.print(" MPS");
display.setCursor(10, 18);
display.print(DSat);
display.setCursor(35, 18);
display.print(" Sat");
display.display(); // writes to the screen
delay (50);
}
}
it shows the current speed once, then fails to update. Any idea?
您的草图将所有时间都花在了更新显示和等待上。这是正在发生的事情:
1) 当一个字符可用时,它被读取并传递给 encode
。
2) 然后更新显示,这需要一些时间。你没有给我们整个程序,也没有确定硬件,所以我真的不能说需要多长时间。
3) 然后等待 50ms。在此期间,GPS 字符不断到达。它们将存储在输入缓冲区中,直到 read()
被调用, 或 直到 64 个字符被存储。然后它们将被丢弃。
在 9600(我猜),可能已经到达 50 个字符。现在输入缓冲区快满了。
4) while
循环测试再次执行,读取并解析第二个字符(第 1 步),更新显示(没有新信息可用,第 2 步),然后等待另一个50 毫秒。
15 毫秒后,输入缓冲区已满,Arduino 开始忽略字符。当 50 毫秒延迟完成时,35 个字符已丢失(在 9600)。
这会阻止成功解析接收到的(部分)NMEA 句子,并且速度不会得到更新。草图将继续用旧信息更新显示,然后再等待一段时间,这会导致更多字符丢失。
需要重新设计循环结构,以便仅在有新信息可用时更新显示,并且永远不要使用延迟:
#include <LiquidCrystal.h> ???
LiquidCrystal display; ???
#include <NMEAGPS.h>
NMEAGPS gps;
gps_fix fix;
// Here are three different ways to connect the GPS:
#define gpsPort Serial1
//#include <AltSoftSerial.h>
//AltSoftSerial gpsPort; // two specific pins required (8 & 9 on an UNO)
//#include <NeoSWSerial.h>
//NeoSWSerial gpsPort( 3, 4 );
void setup()
{
Serial.begin( 9600 );
gpsPort.begin( 9600 );
}
void loop()
{
// Read and parse any available characters from the GPS device
if (gps.available( gpsPort )) {
// Once per second, a complete fix structure is ready.
fix = gps.read();
Serial.print( F("Speed: ") );
float speed = 0.0;
if (fix.valid.speed) {
speed = fix.speed_kph() * 1000.0 / 3600.0;
Serial.print( speed );
}
Serial.println();
Serial.print( F("Sats: ") );
if (fix.valid.satellites)
Serial.println( fix.satellites );
Serial.println();
// Update the display ONCE PER SECOND
display.clearDisplay(); // clears last number
display.display(); // writes clear to screen
display.setCursor(10, 5); //Set drawing posision
if (fix.valid.speed)
display.print( speed ); // what to draw
display.setCursor(35, 5);
display.print(" MPS");
display.setCursor(10, 18);
if (fix.valid.satellites)
display.print( fix.satellites );
display.setCursor(35, 18);
display.print(" Sat");
display.display(); // writes to the screen
}
}
这使用了我的 NeoGPS library. It is smaller, faster, more reliable and more accurate than all other GPS libraries. Even if you don't use it, you should read the related pages about choosing a serial port and troubleshooting.
NeoGPS、AltSoftSerial 和 NeoSWSerial 都可以从 Arduino IDE 库管理器中获得,在菜单 Sketch -> Include Library -> Manage Libraries.
我无法让我的 GPS 更新速度。它显示文本,如果我将 GPS 速度从位置更新循环中取出,它会显示一次当前速度,然后无法更新。有什么想法吗?
void loop() {
while (serial_connections.available()) {
gps.encode(serial_connections.read());
if (gps.location.isUpdated()) {
DText = Serial.println(gps.speed.mps());
DSat = Serial.println(gps.satellites.value());
}
display.clearDisplay(); // clears last number
display.display(); // writes clear to screen
display.setCursor(10, 5); //Set drawing posision
display.print(DText); // what to draw
display.setCursor(35, 5);
display.print(" MPS");
display.setCursor(10, 18);
display.print(DSat);
display.setCursor(35, 18);
display.print(" Sat");
display.display(); // writes to the screen
delay (50);
}
}
it shows the current speed once, then fails to update. Any idea?
您的草图将所有时间都花在了更新显示和等待上。这是正在发生的事情:
1) 当一个字符可用时,它被读取并传递给 encode
。
2) 然后更新显示,这需要一些时间。你没有给我们整个程序,也没有确定硬件,所以我真的不能说需要多长时间。
3) 然后等待 50ms。在此期间,GPS 字符不断到达。它们将存储在输入缓冲区中,直到 read()
被调用, 或 直到 64 个字符被存储。然后它们将被丢弃。
在 9600(我猜),可能已经到达 50 个字符。现在输入缓冲区快满了。
4) while
循环测试再次执行,读取并解析第二个字符(第 1 步),更新显示(没有新信息可用,第 2 步),然后等待另一个50 毫秒。
15 毫秒后,输入缓冲区已满,Arduino 开始忽略字符。当 50 毫秒延迟完成时,35 个字符已丢失(在 9600)。
这会阻止成功解析接收到的(部分)NMEA 句子,并且速度不会得到更新。草图将继续用旧信息更新显示,然后再等待一段时间,这会导致更多字符丢失。
需要重新设计循环结构,以便仅在有新信息可用时更新显示,并且永远不要使用延迟:
#include <LiquidCrystal.h> ???
LiquidCrystal display; ???
#include <NMEAGPS.h>
NMEAGPS gps;
gps_fix fix;
// Here are three different ways to connect the GPS:
#define gpsPort Serial1
//#include <AltSoftSerial.h>
//AltSoftSerial gpsPort; // two specific pins required (8 & 9 on an UNO)
//#include <NeoSWSerial.h>
//NeoSWSerial gpsPort( 3, 4 );
void setup()
{
Serial.begin( 9600 );
gpsPort.begin( 9600 );
}
void loop()
{
// Read and parse any available characters from the GPS device
if (gps.available( gpsPort )) {
// Once per second, a complete fix structure is ready.
fix = gps.read();
Serial.print( F("Speed: ") );
float speed = 0.0;
if (fix.valid.speed) {
speed = fix.speed_kph() * 1000.0 / 3600.0;
Serial.print( speed );
}
Serial.println();
Serial.print( F("Sats: ") );
if (fix.valid.satellites)
Serial.println( fix.satellites );
Serial.println();
// Update the display ONCE PER SECOND
display.clearDisplay(); // clears last number
display.display(); // writes clear to screen
display.setCursor(10, 5); //Set drawing posision
if (fix.valid.speed)
display.print( speed ); // what to draw
display.setCursor(35, 5);
display.print(" MPS");
display.setCursor(10, 18);
if (fix.valid.satellites)
display.print( fix.satellites );
display.setCursor(35, 18);
display.print(" Sat");
display.display(); // writes to the screen
}
}
这使用了我的 NeoGPS library. It is smaller, faster, more reliable and more accurate than all other GPS libraries. Even if you don't use it, you should read the related pages about choosing a serial port and troubleshooting.
NeoGPS、AltSoftSerial 和 NeoSWSerial 都可以从 Arduino IDE 库管理器中获得,在菜单 Sketch -> Include Library -> Manage Libraries.