Files
GPS_RTC_Clock/GPS_com.cpp
2025-06-14 22:26:02 +03:00

216 lines
7.5 KiB
C++

/*
GPS_com.cpp
MIT License
Copyright (c) 2023 hdrlux
*/
#include <Arduino.h> // for all the 'standard' Arduino stuff
#include <NMEAGPS.h> // https://github.com/SlashDevin/NeoGPS
#include <TimeLib.h> // https://github.com/PaulStoffregen/Time
#include "GPS_com.h"
#include "RTC_com.h" // to set the RTC
#include "Serial_AdjustBaud.h" // for adjust serial port baudrate
#include "LocalDateTime.h"
#define cfg_pin_GPS_PPS 2
#define cfg_pin_GPS_Rx 8
#define cfg_pin_GPS_Tx 9
// add the static modifier to limit visibility of these variables to just this file
static byte GPS_PPS_PIN = cfg_pin_GPS_PPS; // Pin 2 = NANO INT0, GPS PPS interrupt signal on this Pin
static bool gps_on; // flag for GPS ON/OFF
static int pulse_count = 0;
static time_t gps_seconds_t = 0; // GPS time
static byte gps_sats = 0;
static bool GPScolon = false;
volatile bool GPS_sec = false; // flag for GPS-PPS
unsigned long GPSMicros;
// set interrupt flag
void gps_interrupt() {
GPSMicros = micros();
GPS_sec = true;
}
NMEAGPS gps; // This parses the GPS characters
gps_fix fix; // This holds on to the latest values
//#include <AltSoftSerial.h>
#include <GPSport.h> // use only for soft-serial
//#define gpsPort Serial
//#define GPS_PORT_NAME "Serial"
#define GPS_PORT_NAME "AltSoftSerial"
/***** Notice ********************************************
Edit file \Arduino\libraries\NeoGPS\src\NMEAGPS_cfg.h
UnComment line //#define NMEAGPS_PARSE_ZDA
only process the NMEA sentences GGA, RMC en ZDA
*********************************************************/
/****** Notice *******************************************
Edit file \Arduino\libraries\NeoGPS\src\NeoTime.h
change both instances of the const name 'DAYS_PER_WEEK'
to something else, they conflict with TimeLib.h
*********************************************************/
// Compile check processing for both RMC & ZDA message [both contain date & time]
// Less dependent on GPS chip config
#ifndef NMEAGPS_PARSE_RMC
#error You must define NMEAGPS_PARSE_RMC in NMEAGPS_cfg.h!
#endif
#ifndef NMEAGPS_PARSE_ZDA
#error You must define NMEAGPS_PARSE_ZDA in NMEAGPS_cfg.h!
#endif
#ifndef NMEAGPS_PARSE_GGA
#error You must define NMEAGPS_PARSE_GGA in NMEAGPS_cfg.h!
#endif
const long POSIX_Y2K_offset = 946684800; // = 10957 days = 30 years, NeoTime (GPS) & TimeLib (RTC) library use different Epoch year, 2000 & 1970
const long offset_28y = 883612800; // = 10227 days = 28 years, simple test for 2038 bug
const long offset_26w = 15724800; // = 182 days = 26 weeks, simple test for timezone
const char baud19200 [] PROGMEM = "PUBX,41,1,3,3,19200,0";
const char baud38400 [] PROGMEM = "PUBX,41,1,3,3,38400,0";
const char GGA_off [] PROGMEM = "PUBX,40,GGA,0,0,0,0";
const char GLL_off [] PROGMEM = "PUBX,40,GLL,0,0,0,0";
const char GSA_off [] PROGMEM = "PUBX,40,GSA,0,0,0,0";
const char GSV_off [] PROGMEM = "PUBX,40,GSV,0,0,0,0";
const char VTG_off [] PROGMEM = "PUBX,40,VTG,0,0,0,0";
const char RMC_off [] PROGMEM = "PUBX,40,RMC,0,0,0,0";
const char ZDA_off [] PROGMEM = "PUBX,40,ZDA,0,0,0,0";
const char RMC_on [] PROGMEM = "PUBX,40,RMC,0,1,0,0";
const char ZDA_on [] PROGMEM = "PUBX,40,ZDA,0,1,0,0";
// bool GPS_PPS_LCD;
void GPS_setup() {
DEBUG_PORT.println(F("DEBUG[GPS_setup()] start"));
GPS_sec = false;
if (detRate(cfg_pin_GPS_Rx) != 38400) {
DEBUG_PORT.println(F("DEBUG[GPS_setup()] baudrate not 38400"));
gpsPort.begin(detRate(cfg_pin_GPS_Rx)); // set PC to same baudrate for debug messages
gps.send_P(&gpsPort, (const __FlashStringHelper *) baud38400);
gpsPort.flush();
delay(100);
gpsPort.end();
}
else
DEBUG_PORT.println(F("DEBUG[GPS_setup()] baudrate already 38400"));
gpsPort.begin(38400);
delay(100);
gps.send_P(&gpsPort, (const __FlashStringHelper *) GGA_off);
delay(100);
gps.send_P(&gpsPort, (const __FlashStringHelper *) GLL_off);
delay(100);
gps.send_P(&gpsPort, (const __FlashStringHelper *) GSA_off);
delay(100);
gps.send_P(&gpsPort, (const __FlashStringHelper *) GSV_off);
delay(100);
gps.send_P(&gpsPort, (const __FlashStringHelper *) VTG_off);
delay(100);
gps.send_P(&gpsPort, (const __FlashStringHelper *) RMC_off);
delay(100);
gps.send_P(&gpsPort, (const __FlashStringHelper *) ZDA_off);
delay(100);
gps.send_P(&gpsPort, (const __FlashStringHelper *) RMC_on);
delay(100);
gps.send_P(&gpsPort, (const __FlashStringHelper *) ZDA_on);
delay(100);
pinMode(GPS_PPS_PIN, INPUT_PULLUP); // enable pullup on interrupt pin
attachInterrupt(digitalPinToInterrupt(GPS_PPS_PIN), gps_interrupt, RISING); // 100ms HIGH at start of second
GPS_ON();
}
void GPS_loop() {
if (GPS_sec) { // do after GPS PPS interrupt
GPS_sec = false; // clear flag
GPScolon = true;
lcd.setCursor(10, 0);
lcd.print(":");
GPS_PPS();
DEBUG_PORT.println("PPS: " + String(GPSMicros));
}
else if (micros() - GPSMicros >= 500000 && GPScolon) {
lcd.setCursor(10, 0);
lcd.print(" ");
GPScolon = false;
}
GPS_read_seconds(); // continue reading buffer
}
void GPS_PPS() { // do something on the flip of the GPS second
int32_t drift = (int32_t)(GPSMicros - RTCMicros); // % (4294967295UL);
// Корректировка переполнения (wrap-around)
if (drift > 900000) // Если разница > 0.9 сек
drift -= 1000000; // Корректируем на -1 сек
else if (drift < -900000) // Если разница < -0.9 сек
drift += 1000000; // Корректируем на +1 сек
//DEBUG_PORT.println("DEBUG[GPS_loop()] drift: " + String(drift));
// Отображение дрейфа в правом нижнем углу LCD (16x2)
String driftStr = String(drift);
if (String(drift).length() > 6) driftStr = " error";
// Вычисляем позицию для выравнивания справа: 16 - длина строки
lcd.setCursor(16 - driftStr.length(), 1);
lcd.print(driftStr);
//if (abs(drift) > 20000 && !gps_on) GPS_ON();
if (RTC_sync && !gps_on) {
GPS_ON();
}
if (gps_on) { // do only when needed
if (pulse_count == 0) {
lcd.setCursor(2, 0);
lcd.print("S");
}
pulse_count += 1;
if (pulse_count > 4) { // skip first PPS-Pulses, to make shure time is from satellite
if (gps_seconds_t != 0) { // do only if value is set
// gps_seconds_t += offset_28y; // debug & testing only!
// gps_seconds_t += offset_26w; // debug & testing only!
SetRTC(TZ_Sec(gps_seconds_t)); // sync RTC with GPS
GPS_OFF();
}
}
}
}
void GPS_read_seconds() {
//Serial.println("DEBUG[GPS_read_second()] Start");
while (gps.available(gpsPort)) {
fix = gps.read();
if (fix.valid.time && fix.valid.date) {
gps_seconds_t = fix.dateTime + POSIX_Y2K_offset; // convert for different epoch year
}
if (fix.valid.satellites) {
gps_sats = fix.satellites;
}
}
}
void GPS_ON() {
DEBUG_PORT.println("DEBUG[GPS_ON()] Start");
gps_on = true;
gps_seconds_t = 0; // make shure GPS serial is alive before setting
pulse_count = 0;
lcd.setCursor(2, 0);
lcd.print("S");
}
void GPS_OFF() {
if (gps_on) { // only if NOT off
gps_on = false;
RTC_sync = false;
//Serial.println("GPS: OFF"); // debug
lcd.setCursor(2, 0);
lcd.print(" ");
}
}
//End