c++ - Arduino写入屏幕的速度

标签 c++ arduino gps

我无法让 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) 然后等待 50 毫秒。在此期间,GPS 字符不断到达。它们将存储在输入缓冲区中,直到 read() 被调用,OR 直到存储了 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图书馆。它比所有其他 GPS 库更小、更快、更可靠和更准确。即使你不使用它,你也应该阅读关于 choosing a serial port 的相关页面。和 troubleshooting .

NeoGPS、AltSoftSerial 和 NeoSWSerial 都可以从 Arduino IDE 库管理器中获得,在菜单 Sketch -> Include Library -> Manage Libraries 下。

关于c++ - Arduino写入屏幕的速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49973341/

相关文章:

c++ - 模板类 - 未解析的外部符号

c++ - 如何在 Linux 上守护一个 c/c++ 程序

mysql - 使用 Arduino 和 GPS/GPRS 模块的 GPS 数据不准确

java - 使用 shell 和 Java 读写串口

android - Gps 位置监听器停止

android - 为什么即使我的手机支持 A-GPS,Android GPS 在打开谷歌地图之前也无法找到位置

c++ - 如何最好地控制迭代方向?

c++ - 停止阻塞在 recv() 上的接收器线程

audio - SimpleSDAudio Error 48含义

android - 如何在应用程序中使用蓝牙 GPS 模块?