summaryrefslogtreecommitdiff
path: root/gpslogger/gpslogger.ino
blob: fbfa08d1b7491d4ca7f526970451a0fd1b617a50 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*************************************************************************
* Arduino GPS Data Logger / Speed Meter / Odometer
* Distributed under GPL v2.0
* Written by Stanley Huang
* All rights reserved.
*************************************************************************/

#include <Arduino.h>
#include <Wire.h>
#include <SD.h>
#include <TinyGPS.h>
#include <SoftwareSerial.h>
#include "MicroLCD.h"
#include "datalogger.h"
#include "images.h"

#if defined(__AVR_ATmega644P__)
#define DISPLAY_MODES 2
#else
#define DISPLAY_MODES 1
#endif

#define USE_MPU6050 0

#define SD_CS_PIN SS
//#define SD_CS_PIN 7 // for microduino
//#define SD_CS_PIN 4 // for ethernet shield

LCD_SSD1306 lcd;

uint32_t start;
uint32_t distance = 0;
uint16_t speed = 0;
byte sat = 0;
uint16_t records = 0;
char heading[2];
bool acc = false;
byte displayMode = 0;

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

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

#define PMTK_SET_NMEA_UPDATE_10HZ "$PMTK220,100*2F"

TinyGPS gps;

// SD card
File sdfile;

CDataLogger logger;

void initScreen();

#if USE_MPU6050
bool initACC()
{
    if (MPU6050_init() != 0)
        return false;
    return true;

}
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);
    // log x/y/z of gyro meter
    logger.logData(PID_GYRO, data.value.x_gyro, data.value.y_gyro, data.value.z_gyro);
}
#endif

void processGPS()
{
    static long lastLat = 0;
    static long lastLon = 0;
    static uint32_t lastTime = 0;
    uint32_t time;

    // parsed GPS data is ready
    logger.dataTime = millis();

    uint32_t date;
    gps.get_datetime(&date, &time, 0);
    logger.logData(PID_GPS_TIME, time, date);

    speed = (uint16_t)(gps.speed() * 1852 / 100);
    if (speed < 1000) speed = 0;
    logger.logData(PID_GPS_SPEED, speed, 0);

    if (sat >= 3 && gps.satellites() < 3) {
        initScreen();
    }
    sat = gps.satellites();

    long lat, lon;
    gps.get_position(&lat, &lon, 0);
    logger.logData(PID_GPS_COORDINATES, (float)lat / 100000, (float)lon / 100000);

    if (logger.dataTime - lastTime >= 3000 && speed > 0) {
        if (lastLat == 0) lastLat = lat;
        if (lastLon == 0) lastLon = lon;

        int16_t latDiff = lat - lastLat;
        int16_t lonDiff = lon - lastLon;

        uint16_t d = latDiff * latDiff + lonDiff * lonDiff;
        if (d >= 100) {
            distance += sqrt(d);
            lastLat = lat;
            lastLon = lon;

            if (latDiff > 0) {
                heading[0] = 'N';
            } else if (latDiff < 0) {
                heading[0] = 'S';
            } else {
                heading[0] = ' ';
            }
            if (lonDiff > 0) {
                heading[1] = 'E';
            } else if (lonDiff < 0) {
                heading[1] = 'W';
            } else {
                heading[1] = ' ';
            }
        }
        lastTime = logger.dataTime;

        // flush file every several seconds
        logger.flushFile();
    }

    long alt = gps.altitude();
    logger.logData(PID_GPS_ALTITUDE, (float)alt);

    records++;
}

bool CheckSD()
{
    Sd2Card card;
    SdVolume volume;

    lcd.setCursor(0, 0);
    lcd.setFont(FONT_SIZE_MEDIUM);
    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";
        }

        lcd.clear();
        lcd.print(type);
        lcd.write(' ');
        if (!volume.init(card)) {
            lcd.print("No FAT!");
            return false;
        }

        uint32_t volumesize = volume.blocksPerCluster();
        volumesize >>= 1; // 512 bytes per block
        volumesize *= volume.clusterCount();
        volumesize >>= 10;

        sprintf(buf, "%dGB", (int)((volumesize + 511) / 1000));
        lcd.print(buf);
    } else {
        lcd.clear();
        lcd.print("SD");
        lcd.draw(cross, 32, 0, 16, 16);
        return false;
    }

    if (!SD.begin(SD_CS_PIN)) {
        lcd.setCursor(0, 0);
        lcd.print("Bad SD");
        return false;
    }
    return true;
}

void initScreen()
{
    lcd.clear();
    switch (displayMode) {
    case 0:
        lcd.setFont(FONT_SIZE_MEDIUM);
        lcd.setCursor(48, 1);
        lcd.write('.');
        lcd.setCursor(48, 6);
        lcd.write('.');
        lcd.setFont(FONT_SIZE_SMALL);
        lcd.setCursor(110, 0);
        lcd.write(':');
        lcd.setCursor(110, 3);
        lcd.print("kph");
        lcd.setCursor(64, 3);
        lcd.print("km/h");
        lcd.setCursor(76, 7);
        lcd.print("km");
        lcd.setCursor(104, 6);
        lcd.print("S:");
        break;
    default:
        lcd.setFont(FONT_SIZE_MEDIUM);
        lcd.setCursor(48, 0);
        lcd.write(':');
        lcd.setCursor(48, 3);
        lcd.write('.');
        lcd.setCursor(48, 6);
        lcd.write('.');
        lcd.setFont(FONT_SIZE_SMALL);
        lcd.setCursor(88, 1);
        lcd.write('.');
        lcd.setCursor(76, 4);
        lcd.print("kph");
        lcd.setCursor(76, 7);
        lcd.print("km");
    }
    lcd.setCursor(104, 6);
    lcd.print("S:");
}

#if USE_MPU6050
void displayMPU6050()
{
    accel_t_gyro_union data;
    char buf[8];
    MPU6050_readout(&data);

    int temp = (data.value.temperature + 12412) / 340;
    sprintf(buf, "TEMP%3dC", temp);
    lcd.setFont(FONT_SIZE_SMALL);
    lcd.setCursor(80, 2);
    lcd.print(buf);

    sprintf(buf, "AX%3d", data.value.x_accel / 160);
    lcd.setCursor(98, 3);
    lcd.print(buf);
    sprintf(buf, "AY%3d", data.value.y_accel / 160);
    lcd.setCursor(98, 4);
    lcd.print(buf);
    sprintf(buf, "AZ%3d", data.value.z_accel / 160);
    lcd.setCursor(98, 5);
    lcd.print(buf);

    sprintf(buf, "GX%3d", data.value.x_gyro / 256);
    lcd.setCursor(64, 3);
    lcd.print(buf);
    sprintf(buf, "GY%3d", data.value.y_gyro / 256);
    lcd.setCursor(64, 4);
    lcd.print(buf);
    sprintf(buf, "GZ%3d", data.value.z_gyro / 256);
    lcd.setCursor(64, 5);
    lcd.print(buf);
}
#endif

void setup()
{
    Wire.begin();
    lcd.begin();
    lcd.setFont(FONT_SIZE_MEDIUM);

    // init button
    pinMode(8, INPUT);

    CheckSD();

    lcd.setCursor(0, 2);
    lcd.print("ACC");
    lcd.draw(acc ? tick : cross, 32, 16, 16, 16);

    lcd.setCursor(0, 4);
    lcd.print("GPS");
    lcd.draw(cross, 32, 32, 16, 16);

    GPSUART.begin(GPS_BAUDRATE);
    logger.initSender();

#if USE_MPU6050
    acc = initACC();
#endif

    byte n = 0xff;
    uint32_t tm = 0;
    start = millis();
    char progress[] = {'-', '/', '|', '\\'};
    do {
        if (!GPSUART.available()) continue;
        if (n == 0xff) {
            // draw a tick (once)
            lcd.draw(tick, 32, 32, 16, 16);
            lcd.setCursor(0, 6);
            lcd.print("SAT ");
            n = 0;
        }
        char c = GPSUART.read();

        if (sat == 0 && millis() - tm > 100) {
            lcd.setCursor(32, 6);
            lcd.write(progress[n]);
            n = (n + 1) % 4;
            tm = millis();
        }

        if (!gps.encode(c)) continue;

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

        sat = gps.satellites();
        if (sat < 100) {
            lcd.setCursor(32, 6);
            lcd.printInt(sat);
        }
    } while (sat < 3 && millis() - start < 30000);

    //GPSUART.println(PMTK_SET_NMEA_UPDATE_10HZ);

    logger.openFile(LOG_TYPE_ROUTE, FLAG_CYCLING | FLAG_GPS | (acc ? FLAG_ACC : 0));

    initScreen();

	start = millis();
}

void displaySpeedDistance()
{
    uint16_t elapsed = (millis() - start) / 1000;
    uint16_t n;

    // display elapsed time (mm:ss)
    lcd.setFlags(FLAG_PAD_ZERO);
    lcd.setFont(FONT_SIZE_SMALL);
    lcd.setCursor(98, 0);
    lcd.printInt(n = elapsed / 60, 2);
    elapsed -= n * 60;
    lcd.setCursor(116, 0);
    lcd.printInt(elapsed, 2);
    lcd.setFlags(0);

    if (sat < 3) return;

    // display speed
    lcd.setFont(FONT_SIZE_XLARGE);
    n = speed / 1000;
    if (n >= 100) {
        lcd.setCursor(0, 0);
        lcd.printInt(n, 3);
    } else {
        lcd.setCursor(16, 0);
        lcd.printInt(n, 2);
    }
    lcd.setCursor(56, 0);
    n = speed - n * 1000;
    lcd.printInt(n / 100);

    // display distance
    lcd.setCursor(0, 5);
    lcd.printInt(n = distance / 1000, 3);
    lcd.setCursor(56, 5);
    n = distance - n * 1000;
    lcd.printInt(n / 100);

    // display average speed
    if (elapsed > 60) {
        uint16_t avgSpeed = distance * 36 / elapsed / 10;
        lcd.setFont(FONT_SIZE_MEDIUM);
        lcd.setCursor(102, 1);
        lcd.printInt(avgSpeed, 3);
    }
}

#if DISPLAY_MODES > 1
void displayTimeSpeedDistance()
{
    uint32_t elapsed = millis() - start;
    uint16_t n;

    // display elapsed time (mm:ss:mm)
    n = elapsed / 60000;
    if (n >= 100) {
        lcd.setCursor(0, 0);
        lcd.printInt(n, 3);
    } else {
        lcd.setCursor(16, 0);
        lcd.printInt(n, 2);
    }
    elapsed -= n * 60000;
    lcd.setFlags(FLAG_PAD_ZERO);
    lcd.setCursor(56, 0);
    lcd.printInt(n = elapsed / 1000, 2);
    elapsed -= n * 1000;
    lcd.setCursor(96, 1);
    lcd.setFont(FONT_SIZE_SMALL);
    lcd.printInt(n = elapsed / 10, 2);
    lcd.setFlags(0);

    if (sat < 3) return;

    // display speed
    lcd.setFont(FONT_SIZE_LARGE);
    n = speed / 1000;
    if (n >= 100) {
        lcd.setCursor(0, 3);
        lcd.printInt(n, 3);
    } else {
        lcd.setCursor(16, 3);
        lcd.printInt(n, 2);
    }
    lcd.setCursor(56, 3);
    n = speed - n * 1000;
    lcd.printInt(n / 100);

    // display distance
    lcd.setCursor(0, 6);
    lcd.printInt(n = distance / 1000, 3);
    lcd.setCursor(56, 6);
    n = distance - n * 1000;
    lcd.printInt(n / 100);
}
#endif

void displayMinorInfo()
{
    lcd.setFont(FONT_SIZE_SMALL);
    lcd.setCursor(0, 0);
    lcd.write(heading[0]);
    lcd.write(heading[1]);

    lcd.setFlags(0);
    lcd.setCursor(98, 7);
    lcd.printInt(records, 5);
    if (sat < 100) {
        lcd.setCursor(116, 6);
        lcd.printInt((uint16_t)sat, 2);
    }
}

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

#if USE_MPU6050
    processACC();
#endif

#if DISPLAY_MODES > 1
    switch (displayMode) {
    case 0:
        displaySpeedDistance();
        break;
    default:
        displayTimeSpeedDistance();
    }

    if (digitalRead(8) == 0) {
        delay(50);
        if (digitalRead(8) == 0) {
            displayMode = (displayMode + 1) % DISPLAY_MODES;
            while (digitalRead(8) == 0);
            initScreen();
        }
    }
#else
    displaySpeedDistance();
#endif

    displayMinorInfo();
}