| 程序十一:使用 TinyGPSCustom 对象数组监视所有可见的卫星 (1)Arduino参考开源代码 
 复制代码/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序十一:使用 TinyGPSCustom 对象数组监视所有可见的卫星
*/
#include <TinyGPSPlus.h>//导入驱动库
#include <SoftwareSerial.h>
static const int RXPin = 9, TXPin = 8;//定义软串口接脚
static const uint32_t GPSBaud = 9600;
// TinyGPSPlus 对象
TinyGPSPlus gps;
//与 GPS 设备的串行连接
SoftwareSerial ss(RXPin, TXPin);
/* 
  来自 http://aprs.gids.nl/nmea/:
   
  $GPGSV
  
  视野中的 GPS 卫星
  
  例如:
$GPGSV,3,1,11,03,03,111,00,04,15,270,00,06,01,010,00,13,06,292,00*74   $GPGSV,3,2,11,14,25,170,00,16,57,208,39,18,67,296,40,19,40,246,00*74
$GPGSV,3,3,11,22,42,067,42,24,14,311,43,27,05,244,00,,,,*4D
  1 = 此周期中此类消息的总数
  2 = 消息编号
  3 = 视图中的 SV 总数
  4 = SV PRN 号码
  5 = 以度为单位的仰角,最大 90
  6 = 方位角,从真北的度数,000 到 359
  7 = SNR,00-99 dB(不跟踪时为空)
  8-11 = 关于第二个 SV 的信息,与字段 4-7 相同
  12-15=关于第三个 SV 的信息,与字段 4-7 相同
  16-19= 关于第四个 SV 的信息,与字段 4-7 相同
*/
static const int MAX_SATELLITES = 40;
TinyGPSCustom totalGPGSVMessages(gps, "GPGSV", 1); // $GPGSV 句子,第一个元素
TinyGPSCustom messageNumber(gps, "GPGSV", 2);      // $GPGSV 句子,第二个元素
TinyGPSCustom satsInView(gps, "GPGSV", 3);         // $GPGSV 句子,第三个元素
TinyGPSCustom satNumber[4]; // 稍后初始化
TinyGPSCustom elevation[4];
TinyGPSCustom azimuth[4];
TinyGPSCustom snr[4];
struct{
  bool active;
  int elevation;
  int azimuth;
  int snr;
} sats[MAX_SATELLITES];
void setup(){
  Serial.begin(9600);
  ss.begin(GPSBaud);
  Serial.println(F("SatelliteTracker.ino"));
  Serial.println(F("使用 TinyGPSCustom 监控卫星位置和信号强度"));
  Serial.print(F("测试 TinyGPSPlus 库 v.")); 
  Serial.println(TinyGPSPlus::libraryVersion());
  Serial.println(F("GY-NEO6MV2模块准备就绪"));
  Serial.println();
  
  // 初始化所有未初始化的 TinyGPSCustom 对象
  for (int i=0; i<4; ++i)
  {
    satNumber[i].begin(gps, "GPGSV", 4 + 4 * i); // offsets 4, 8, 12, 16
    elevation[i].begin(gps, "GPGSV", 5 + 4 * i); // offsets 5, 9, 13, 17
    azimuth[i].begin(  gps, "GPGSV", 6 + 4 * i); // offsets 6, 10, 14, 18
    snr[i].begin(      gps, "GPGSV", 7 + 4 * i); // offsets 7, 11, 15, 19
  }
}
void loop(){
  // 初始化所有未初始化的 TinyGPSCustom 对象
  if (ss.available() > 0)
  {
    gps.encode(ss.read());
    if (totalGPGSVMessages.isUpdated())
    {
      for (int i=0; i<4; ++i)
      {
        int no = atoi(satNumber[i].value());
        // Serial.print(F("SatNumber is ")); Serial.println(no);
        if (no >= 1 && no <= MAX_SATELLITES)
        {
          sats[no-1].elevation = atoi(elevation[i].value());
          sats[no-1].azimuth = atoi(azimuth[i].value());
          sats[no-1].snr = atoi(snr[i].value());
          sats[no-1].active = true;
        }
      }
      
      int totalMessages = atoi(totalGPGSVMessages.value());
      int currentMessage = atoi(messageNumber.value());
      if (totalMessages == currentMessage)
      {
        Serial.print(F("Sats=")); 
        Serial.print(gps.satellites.value());
        Serial.print(F(" Nums="));
        for (int i=0; i<MAX_SATELLITES; ++i)
          if (sats[i].active)
          {
            Serial.print(i+1);
            Serial.print(F(" "));
          }
        Serial.print(F(" 海拔="));//海拔
        for (int i=0; i<MAX_SATELLITES; ++i)
          if (sats[i].active)
          {
            Serial.print(sats[i].elevation);
            Serial.print(F(" "));
          }
        Serial.print(F(" 方位角="));//方位角
        for (int i=0; i<MAX_SATELLITES; ++i)
          if (sats[i].active)
          {
            Serial.print(sats[i].azimuth);
            Serial.print(F(" "));
          }
        
        Serial.print(F(" 信噪比="));//信噪比
        for (int i=0; i<MAX_SATELLITES; ++i)
          if (sats[i].active)
          {
            Serial.print(sats[i].snr);
            Serial.print(F(" "));
          }
        Serial.println();
        for (int i=0; i<MAX_SATELLITES; ++i)
          sats[i].active = false;
      }
    }
  }
}
 
 
 |