summaryrefslogtreecommitdiff
path: root/gpslogger/gpslogger.ino
blob: 198b1d9673cfbc8786ce06bd01bfc51bd9d6a049 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*************************************************************************
* Reference code for generic GPS data logger
* Works with Freematics GPS Data Logger Kit
* Visit http://freematics.com for more information
* Distributed under GPL v2.0
* Written by Stanley Huang
*************************************************************************/

#include <Arduino.h>
#include <SPI.h>
#include <SD.h>
#include <TinyGPS.h>
#include "config.h"
#if USE_SOFTSERIAL
#include <SoftwareSerial.h>
#endif
#include "datalogger.h"

#if USE_MPU6050
#include <Wire.h>
#include <MPU6050.h>
#endif

#if defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
#define GPSUART Serial2
#elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega32U4__)
#define GPSUART Serial1
#else
#define GPSUART Serial
#endif

TinyGPS gps;

CDataLogger logger;

bool acc = false;

#if USE_MPU6050
bool initACC()
{
  return MPU6050_init() == 0;
}

void processACC()
{
  accel_t_gyro_union data;
  MPU6050_readout(&data);
  logger.dataTime = millis();
  // log x/y/z of accelerometer
  logger.logData(PID_ACC, data.value.x_accel, data.value.y_accel, data.value.z_accel);
}
#endif

void processGPS()
{
  long lat, lon;
  int speed = 0;
  int16_t alt = 0;
  uint32_t date, time;

  logger.dataTime = millis();
  gps.get_datetime(&date, &time, 0);
  logger.logData(PID_GPS_TIME, (int32_t)time);

  speed = (int)(gps.speed() * 1852 / 100000);
  logger.logData(PID_GPS_SPEED, speed);

  //logger.logData(PID_GPS_SAT_COUNT, (int)gps.satellites());

  alt = gps.altitude() / 100;
  if (alt > -10000 && alt < 10000) {
    logger.logData(PID_GPS_ALTITUDE, alt);
  }

  gps.get_position(&lat, &lon, 0);
  logger.logData(PID_GPS_LATITUDE, lat);
  logger.logData(PID_GPS_LONGITUDE, lon);
}

bool CheckSD()
{
  Sd2Card card;
  //SdVolume volume;

  pinMode(SS, OUTPUT);
  if (card.init(SPI_HALF_SPEED, SD_CS_PIN)) {
    const char* type;
    char buf[20];

    switch (card.type()) {
      case SD_CARD_TYPE_SD1:
        type = "SD1";
        break;
      case SD_CARD_TYPE_SD2:
        type = "SD2";
        break;
      case SD_CARD_TYPE_SDHC:
        type = "SDHC";
        break;
      default:
        type = "SDx";
    }
    SerialRF.print(type);
    SerialRF.print(' ');
  } else {
    SerialRF.println("No SD");
    return false;
  }

  if (!SD.begin(SD_CS_PIN)) {
    SerialRF.println("Bad SD");
    return false;
  }
  return true;
}

void setup()
{
  logger.initSender();
  SerialRF.println("Init...");

#if ENABLE_DATA_LOG
  if (CheckSD()) {
    int index = logger.openFile();
    SerialRF.print("File ID:");
    SerialRF.println(index);
  }
#endif

#if USE_MPU6050
  Wire.begin();
  acc = initACC();

  SerialRF.print("ACC:");
  SerialRF.println(acc ? "YES" : "NO");
#endif

  GPSUART.begin(GPS_BAUDRATE);
  delay(500);

  while (!GPSUART.available()) {
    SerialRF.println("Waiting...");
    delay(500);
  }
  // init GPS
  SerialRF.println("Searching...");
  for (;;) {
    if (!GPSUART.available()) continue;
    char c = GPSUART.read();
    if (!gps.encode(c)) break;
  }
}

void loop()
{
#if ENABLE_DATA_LOG
  static uint32_t lastTime = 0;
  // flush file every 3 seconds
  if (logger.dataTime - lastTime >= 3000) {
    lastTime = logger.dataTime;
    logger.flushFile();
  }
#endif

#if USE_MPU6050
  if (acc) {
    processACC();
  }
#endif

  if (GPSUART.available()) {
    char c = GPSUART.read();
    if (gps.encode(c)) {
      processGPS();
    }
  }
}