GPS仪表-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2209|回复: 0

GPS仪表

[复制链接]
发表于 2020-2-19 21:22 | 显示全部楼层 |阅读模式
    本项目使用GPS模块实现速度测量,arduino需要安装TinyGPS PLUS库,屏幕显示速度、方向、高度,根据时间自动调节屏幕亮度。在首次使用时需要使用U-Center(https://www.u-blox.com/en/product/u-center)修改波特率(以下代码GPS使用115200与M5通讯)1.M5连接电脑
2.按住A键选择9600波特率(GPS默认)
3.在U-center中修改波特率115200
4.在以后的使用中选择115200与电脑进行通讯
注:如果你想保持GPS为默认的115200波特率,你需要在以下代码中将hSerial.begin(115200)改为hSerial.begin(9600),GPS的刷新频率将保持为1Hz
Sample.jpg
[mw_shl_code=arduino,true]#include <M5Stack.h>#include <TinyGPS++.h>

TinyGPSPlus tGPS;
HardwareSerial hSerial(2);
bool enableAutoSleep = true;

void serialThroughMode();
void updateScreen(TinyGPSPlus);
void updateDirection(int);

void setup()
{
  M5.begin();
  M5.Lcd.setTextSize(2);
  M5.Lcd.drawString("Select Baud To Connect PC", 0, 90);
  M5.Lcd.drawString("9600", 30, 200);
  M5.Lcd.drawString("Battery", 110, 200);
  M5.Lcd.drawString("115200", 220, 200);
  M5.Lcd.setTextFont(4);
  Serial.begin(115200);
  pinMode(BUTTON_A_PIN, INPUT_PULLUP);
  pinMode(BUTTON_B_PIN, INPUT_PULLUP);
  pinMode(BUTTON_C_PIN, INPUT_PULLUP);

  // Speaker Noise Reduce
  M5.Speaker.mute();
  pinMode(25, OUTPUT);
  digitalWrite(25, LOW);

  M5.Power.begin();
  M5.Power.setPowerVin(false);
  delay(2000);

  // Serial Through Mode (To Setup Module)
  M5.update();
  if (M5.BtnA.isPressed())
  {
    hSerial.begin(9600);
    serialThroughMode();
  }
  if (M5.BtnB.isPressed())
  {
    enableAutoSleep = false;
  }
  if (M5.BtnC.isPressed())
  {
    hSerial.begin(115200);
    serialThroughMode();
  }

  // You need configure Baud of the GPS module before use
  hSerial.begin(115200);
  delay(1000);
  M5.Lcd.fillScreen(TFT_BLACK);
  //M5.Lcd.setRotation(5);
}

void loop()
{
  // Manage Power
  if (enableAutoSleep)
  {
    while (!M5.Power.isCharging())
    {
      M5.Power.lightSleep(SLEEP_SEC(3));
    }
  }

  // Receive GPS Data
  while (hSerial.available() > 0)
  {
    tGPS.encode(hSerial.read());
  }

  // Change Night Mode to Adjust Brightness
  bool isNight = tGPS.time.hour() + 9 >= 18 || tGPS.time.hour() + 9 <= 6;
  if (isNight)
  {
    M5.Lcd.setBrightness(60);
  }
  else
  {
    M5.Lcd.setBrightness(200);
  }

  // Draw Information
  updateScreen(&tGPS);
}

// Update Screen Info
unsigned int oldAlt = 0;
unsigned int oldSpeed = 0;
unsigned int oldHeading = 0;
void updateScreen(TinyGPSPlus *gps)
{
  int nowAlt = (int)(gps->altitude.meters());
  int nowSats = gps->satellites.value();
  int nowSpeed = (int)(gps->speed.kmph() - 0.5);
  int nowHeading = (int)(gps->course.deg());

  M5.Lcd.setTextColor(TFT_WHITE);
  if (nowSats >= 3)
  {
    if (nowAlt != oldAlt)
    {
      M5.Lcd.setTextSize(2);
      M5.Lcd.fillRect(0, 0, 230, 47, TFT_BLACK);
      M5.Lcd.drawString(String(nowAlt) + "m    ", 0, 0);
    }
    if (nowSpeed != oldSpeed)
    {
      M5.Lcd.setTextSize(3);
      M5.Lcd.fillRect(15, 60, 300, 150, TFT_BLACK);
      M5.Lcd.drawString(String(nowSpeed), 15, 60, 7);
    }
    if (nowHeading != oldHeading)
    {
      updateDirection(nowHeading);
    }
  }
  else
  {
    M5.Lcd.setTextSize(3);
    M5.Lcd.fillRect(10, 60, 300, 150, TFT_BLACK);
    M5.Lcd.drawString("------", 10, 60, 7);
    delay(1000);
  }
  M5.Lcd.setTextSize(1);
  M5.Lcd.drawString("km/h", 260, 210);

  oldAlt = nowAlt;
  oldSpeed = nowSpeed;
  oldHeading = nowHeading;
}

// Update Direction Info
void updateDirection(int nowCourse)
{
  const int width = (int)(45.0 / 2.0 + 1.0);
  String dispStr;
  uint16_t strColor;

  if (nowCourse > 360 || nowCourse < 0)
  {
    dispStr = "-- ";
  }
  else if (nowCourse >= 360 - width || nowCourse <= 0 + width)
  {
    dispStr = "N  ";
    strColor = TFT_RED;
  }
  else if (45 - width <= nowCourse && nowCourse <= 45 + width)
  {
    dispStr = "NE ";
    strColor = TFT_ORANGE;
  }
  else if (90 - width <= nowCourse && nowCourse <= 90 + width)
  {
    dispStr = "E  ";
    strColor = TFT_YELLOW;
  }
  else if (135 - width <= nowCourse && nowCourse <= 135 + width)
  {
    dispStr = "SE ";
    strColor = TFT_GREENYELLOW;
  }
  else if (180 - width <= nowCourse && nowCourse <= 180 + width)
  {
    dispStr = "S  ";
    strColor = TFT_GREEN;
  }
  else if (225 - width <= nowCourse && nowCourse <= 225 + width)
  {
    dispStr = "SW ";
    strColor = TFT_CYAN;
  }
  else if (270 - width <= nowCourse && nowCourse <= 270 + width)
  {
    dispStr = "W  ";
    strColor = TFT_DARKCYAN;
  }
  else if (315 - width <= nowCourse && nowCourse <= 315 + width)
  {
    dispStr = "NW ";
    strColor = TFT_PURPLE;
  }

  M5.Lcd.setTextSize(2);
  M5.Lcd.setTextColor(strColor);
  M5.Lcd.fillRect(230, 0, 90, 47, TFT_BLACK);
  M5.Lcd.drawString(dispStr, 240, 0);
}

// -----------------------------------------------------------
// Serial Through Mode to Configure GPS Module from PC
void serialThroughMode()
{
  M5.Lcd.drawString("Serial Through Mode", 0, 120);
  while (1)
  {
    while (hSerial.available() > 0)
    {
      Serial.write(hSerial.read());
    }
    while (Serial.available() > 0)
    {
      hSerial.write(Serial.read());
    }
  }
}[/mw_shl_code]


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-27 17:46 , Processed in 0.079120 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表