Files
GPS_RTC_Clock/GPS_com.cpp
135 fc069bd63e WIP
прошивка по записи RTC для старых часов rally
2024-07-28 20:57:14 +03:00

190 lines
6.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;
volatile bool GPS_sec = false; // flag for GPS-PPS
unsigned long GPSMillis;
// set interrupt flag
void gps_interrupt() {
GPSMillis = 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
lcd.setCursor(2, 0); GPS_PPS_LCD = !GPS_PPS_LCD;
lcd.print((GPS_PPS_LCD) ? " " : ":");
GPS_PPS();
}
GPS_read_seconds(); // continue reading buffer
}
void GPS_PPS() { // do something on the flip of the GPS second
int32_t drift = (RTCMillis - GPSMillis); // % (4294967295UL);
//DEBUG_PORT.println("DEBUG[GPS_PPS()] Start");
if (drift > 900000)
drift -= 1000000;
else if (drift < -900000)
drift += 1000000;
//DEBUG_PORT.println("DEBUG[GPS_loop()] drift: " + String(drift));
lcd.setCursor((16 - sizeof(String(drift))), 1);
lcd.print(drift);
if (drift > 5000 && !gps_on) GPS_ON();
if (gps_on) { // do only when needed
pulse_count += 1;
if (pulse_count > 2) { // 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;
}
void GPS_OFF() {
if (gps_on) { // only if NOT off
gps_on = false;
//Serial.println("GPS: OFF"); // debug
}
}
//End