#include <SoftwareSerial.h>
#include <TinyGPS.h>
#include <TFT_eSPI.h> // Graphics and font library for ILI9341 driver
#include"LIS3DHTR.h"
#include "RTC_SAMD51.h"
#include "DateTime.h"
#include <SPI.h>
#include <SD.h>
#include <FlashStorage_SAMD.h>
#define FLASH_DEBUG 0
#define TFT_GREY 0x5AEB
#define YELLOW 0xFFE0
#define RED 0xF800
FlashStorage(my_flash_store, int);
TFT_eSPI tft = TFT_eSPI();
LIS3DHTR<TwoWire> lis;
RTC_SAMD51 rtc;
int captureTime;
int SDWriteTime;
unsigned long avSDWriteTime =0;
int SDwriteCycleTime;
unsigned long start1;
unsigned long finish1;
unsigned long start2;
unsigned long finish2;
unsigned long currentMicros1;
unsigned long previousMicros1 =0;
unsigned long count2 = 0;
String buffer;
String filename;
unsigned long lastMillis = 0;
int stateOne = 0;
float flat = 0.00000000;
float flon = 0.00000000;
float GPSspeed = 0.0;
int GPSsatelites = 0;
int GPSprecision = 0;
int flashNumber;
uint16_t address = 0;
File txtFile;
TinyGPS gps;
SoftwareSerial ss(0, 1);
void setup()
{
EEPROM.get(address, flashNumber);
EEPROM.put(address, (int) (flashNumber + 1));
if (!EEPROM.getCommitASAP())
{
EEPROM.commit();
}
tft.init();
tft.setRotation(2);
tft.fillScreen(TFT_BLACK);
tft.setCursor(0, 0, 2);
tft.setTextColor(YELLOW, TFT_BLACK); tft.setTextSize(2);
tft.println(" GPS + G-Force ");
tft.println(" Data Logger ");
tft.println(" V.1.01 ");
tft.setTextColor(TFT_WHITE, TFT_BLACK); tft.setTextSize(2);
Serial.begin(115200);
delay(5000);
ss.begin(9600);
lis.begin(Wire1);
rtc.begin();
DateTime now = DateTime(F(__DATE__), F(__TIME__));
Serial.println("adjust time!");
rtc.adjust(now);
filename += now.day(); filename += "";
filename += now.hour(); filename += "_";
filename += String(flashNumber);
filename += ".txt";
if (!lis)
{
Serial.println("ERROR");
while(1);
}
lis.setOutputDataRate(LIS3DHTR_DATARATE_50HZ);
lis.setFullScaleRange(LIS3DHTR_RANGE_2G);
buffer.reserve(2048);
pinMode(LED_BUILTIN, OUTPUT);
if (!SD.begin())
{
tft.setTextColor(RED, TFT_BLACK); tft.setTextSize(2);
tft.println(" No SD card? ");
Serial.println("Card failed, or not present");
while (1);
}
txtFile = SD.open(filename, FILE_WRITE);
if (!txtFile)
{
Serial.print("error opening ");
Serial.println(filename);
while (1);
}
txtFile.write("Millis,Timestamp,Accel_X,Accel_Y,Accel_Z,Satelites,Speed(km/h),Latitude,Longitude,GPSprecision \r\n");
Serial.print("Simple TinyGPS library v. "); Serial.println(TinyGPS::library_version());
Serial.println("by Mikal Hart");
Serial.println();
}
void loop()
{
bool newData = false;
unsigned long chars;
unsigned short sentences, failed;
for (unsigned long currentMillis = millis(); millis() - currentMillis < 1000;)
{
while (ss.available())
{
start1 = micros();
char c = ss.read();
Serial.write(c);
if (gps.encode(c))
newData = true;
finish1 = micros();
}
captureTime = finish1 - start1;
float x_values, y_values, z_values;
x_values = lis.getAccelerationX();
y_values = lis.getAccelerationY();
z_values = lis.getAccelerationZ();
unsigned long currentMillisTwo = millis();
if ((currentMillisTwo - lastMillis) >= 20)
{
DateTime now = rtc.now();
buffer += currentMillisTwo; buffer += ",";
buffer += now.year(); buffer += "-";
buffer += now.month(); buffer += "-";
buffer += now.day(); buffer += " ";
buffer += now.hour(); buffer += ":";
buffer += now.minute(); buffer += ":";
buffer += now.second(); buffer += ",";
buffer += String(x_values,3); buffer += ",";
buffer += String(y_values,3); buffer += ",";
buffer += String(z_values,3); buffer += ",";
GPSsatelites = gps.satellites();
if (GPSsatelites == 255)
{
GPSsatelites = 0;
}
buffer += String(GPSsatelites); buffer += ",";
GPSspeed = gps.f_speed_kmph();
if (GPSspeed < 0)
{
GPSspeed = 0.0;
}
buffer += String(GPSspeed,3); buffer += ",";
buffer += String(flat,8); buffer += ",";
buffer += String(flon,8); buffer += ",";
GPSprecision = gps.hdop();
if (GPSprecision < 0)
{
GPSprecision = 0;
}
buffer += String(GPSprecision);
buffer += "\r\n";
lastMillis = currentMillisTwo;
}
unsigned int chunkSize = txtFile.availableForWrite();
if (chunkSize && buffer.length() >= chunkSize)
{
currentMicros1 = micros();
SDwriteCycleTime = currentMicros1 - previousMicros1;
previousMicros1 = currentMicros1;
if (stateOne == 0)
{
stateOne = 1;
digitalWrite(LED_BUILTIN, HIGH);
}
else
{
stateOne = 0;
digitalWrite(LED_BUILTIN, LOW);
}
start2 = micros();
txtFile.write(buffer.c_str(), chunkSize);
finish2 = micros();
SDWriteTime = finish2 - start2;
if (avSDWriteTime == 0)
{
avSDWriteTime = SDWriteTime;
}
avSDWriteTime = (avSDWriteTime*49 + SDWriteTime)/50;
buffer.remove(0, chunkSize);
}
}
if (newData)
{
unsigned long age;
gps.f_get_position(&flat, &flon, &age);
Serial.print("LAT=");
Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
Serial.print(" LON=");
Serial.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
Serial.print(" SAT=");
Serial.print(gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites());
Serial.print(" PREC=");
Serial.print(gps.hdop() == TinyGPS::GPS_INVALID_HDOP ? 0 : gps.hdop());
tft.fillScreen(TFT_BLACK);
tft.setCursor(0, 0, 2);
tft.print("LAT= ");tft.println(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
tft.print("LON= ");tft.println(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
tft.print("SATS= ");tft.println(gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites());
tft.print("PREC= ");tft.println(gps.hdop() == TinyGPS::GPS_INVALID_HDOP ? 0 : gps.hdop());
tft.print("SPEED= ");tft.println(gps.f_speed_kmph() == TinyGPS::GPS_INVALID_F_SPEED ? 0.0 : gps.f_speed_kmph());
tft.print("CAPT= ");tft.print(captureTime);tft.println(" uS");
tft.print("SDWr= ");tft.print(SDWriteTime);tft.println(" uS");
tft.print("aSDW= ");tft.print(avSDWriteTime);tft.println(" uS");
tft.print("SDWc= ");tft.print(SDwriteCycleTime);tft.println(" uS");
tft.print("FName= ");tft.print(filename);
}
else
{
tft.fillScreen(TFT_BLACK);
tft.setCursor(0, 0, 2);
tft.println("No GPS fix");
tft.print("CAPT= ");tft.println(captureTime);
}
gps.stats(&chars, &sentences, &failed);
Serial.print(" CHARS=");
Serial.print(chars);
Serial.print(" SENTENCES=");
Serial.print(sentences);
Serial.print(" CSUM ERR=");
Serial.println(failed);
if (chars == 0)
Serial.println("** No characters received from GPS: check wiring **");
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.