-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUserFunctions.cpp
More file actions
526 lines (444 loc) · 14.3 KB
/
Copy pathUserFunctions.cpp
File metadata and controls
526 lines (444 loc) · 14.3 KB
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// User data functions. Modify these functions for your data items.
#include "UserTypes.h"
#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"
#include <MicroNMEA.h>
//------------------------------------------------------------------------------
char nmeaBuffer[100];
MicroNMEA nmea(nmeaBuffer, sizeof(nmeaBuffer));
DS3231 rtc;
MPU6050 mpu(0x69);
HardwareTimer timer_ReadGPS(1);
HardwareTimer timer_BatVolt(2);
DateTime RTC_now;
DateTime GPS_now, StartRTCLogTime, tempDT;
//DateTime LogStart_time;
DateTime Last_RTC;
uint32_t LogEnd_time;
uint32_t CurrentDataReads = 0;
static uint32_t startMicros;
uint32_t tms = 0;
volatile uint16_t lastBatVolt = 0;
volatile uint32_t counter_GPSTrig = 0;
volatile uint32_t counter_BatVolt = 0;
volatile uint32_t counter_ReadGPS = 0;
volatile uint32_t timepulse_GPS = 0;
volatile uint32_t timepulse_RTC = 0;
volatile int32_t time_tmp = 0;
char buf_RTC[20];
//==========================================================
// PROGRAM FLAGS
//==========================================================
volatile uint16_t GPSppsTriggered = 0;
volatile uint16_t RTCppsTriggered = 0;
volatile uint16_t ReadGPSTriggered = 0;
volatile uint16_t BatVoltTriggered = 0;
volatile uint16_t StartNewFileTriggered = 0;
volatile uint8_t RTCBusy = 0;
void GPS_Process();
//==========================================================
// INTERRUPT HANDLERS
//==========================================================
void GPSppsHandler(void)
{
uint8_t irqState = digitalRead(GPS_PPS_IRQ);
GPSppsTriggered = GPS_PPS_MASK & irqState;
if (irqState) {
counter_GPSTrig++;
}
digitalWrite(LED_BLUE, GPSppsTriggered);
}
void RTCppsHandler(void)
{
RTCppsTriggered = RTC_PPS_MASK;
#ifndef HEARTBEAT
digitalWrite(LED_RED, counter_BatVolt & 1);
#endif
if (counter_BatVolt == 0)
timer_BatVolt.refresh();
counter_BatVolt++;
}
void BatVoltHandler(void)
{
BatVoltTriggered = BAT_VOLT_MASK;
}
void ReadGPSHandler(void)
{
ReadGPSTriggered = READ_GPS_MASK;
}
//==========================================================
// ACQUIRE DATA RECORD
//==========================================================
bool acquireData(data_t* data) {
// data->GPStime = GPS_now.unixtime();
data->GPStime = micros();
data->RTCtime = rtc.now().unixtime();
// data->RTCtime = RTC_now.unixtime();
data->Status = GPSppsTriggered | RTCppsTriggered | BatVoltTriggered;
mpu.getMotion6(&data->ax, &data->ay, &data->az,
&data->gx, &data->gy, &data->gz);
// data->temp = mpu.getTemperature() / 340 + 36.53; //
data->temp = mpu.getTemperature();
if (BatVoltTriggered != 0) {
lastBatVolt = analogRead(BAT_VOLT_PIN);
counter_BatVolt = 0;
}
data->batVolt = lastBatVolt;
// console.println(counter_BatVolt);
// GPSppsTriggered = 0;
RTCppsTriggered = BatVoltTriggered = 0;
#ifdef HEARTBEAT
if (micros() >= tms) {
tms = micros() + HEARTBEAT;
digitalWrite(LED_GREEN, HIGH);
} else {
digitalWrite(LED_GREEN, LOW);
}
#endif
CurrentDataReads++;
if (data->RTCtime < LogEnd_time) {
return true;
} else {
return false;
}
}
void SetLogEndTime() {
StartRTCLogTime = rtc.now();
LogEnd_time = StartRTCLogTime.unixtime() + LOG_TIME_PER_FILE;
CurrentDataReads = 0;
}
void GPS_Process() {
bool validmsg = false;
uint32_t tout;
nmea.clear();
tout = millis() + 5000;
do {
while (gps.available() && !validmsg) {
char c = gps.read();
validmsg = nmea.process(c);
}
console.println("=======");
console.println(nmea.getSentence());
console.println(nmea.getMessageID());
} while ((nmea.getMessageID()[0] != 'R') && (millis() < tout));
GPS_now.setyear(nmea.getYear()); GPS_now.setmonth(nmea.getMonth()); GPS_now.setday(nmea.getDay());
GPS_now.sethour(nmea.getHour()); GPS_now.setminute(nmea.getMinute()); GPS_now.setsecond(nmea.getSecond());
// console.println(nmea.getYear());
// console.println(GPS_now.year());
}
void RTC_AdjustToGPS() {
GPS_Process();
digitalWrite(LED_GREEN, HIGH);
if ((abs(GPS_now.unixtime() - rtc.now().unixtime()) >= GPS_RTC_TIME_DIFF) && (GPS_now.year() != 2048) && nmea.isValid()) {
rtc.adjust(GPS_now);
console.println(F("RTC is adjustet to GPS."));
} else {
console.println(F("RTC is not adjustet to GPS (no fix).\nYou may leave it as is or set it manually from the menu."));
}
strncpy(buf_RTC, "YYYY.MM.DD hh:mm:ss\0", 20);
console.print(F("RTC: ")); //RTC
RTC_now = rtc.now();
console.println(RTC_now.format(buf_RTC));
digitalWrite(LED_GREEN, LOW);
}
//==========================================================
// HARDWARE HUMAN INTERFACE
//==========================================================
void ledBlink(int led, int dly) {
digitalWrite(led, HIGH);
delay(dly);
digitalWrite(led, LOW);
// delay(dly);
}
//void ledTest() {
// ledBlink (LED_GREEN, 200);
// ledBlink (LED_RED, 200);
//}
//==========================================================
// GPS CONFIG
//==========================================================
void GPS_SetMin() {
MicroNMEA::sendSentence(gps, "$PUBX,40,GLL,0,0,0,0,0,0");
MicroNMEA::sendSentence(gps, "$PUBX,40,VTG,0,0,0,0,0,0");
MicroNMEA::sendSentence(gps, "$PUBX,40,GSA,0,0,0,0,0,0");
MicroNMEA::sendSentence(gps, "$PUBX,40,GSV,0,0,0,0,0,0");
MicroNMEA::sendSentence(gps, "$PUBX,40,GGA,0,1,0,0,0,0");
MicroNMEA::sendSentence(gps, "$PUBX,40,RMC,0,1,0,0,0,0");
}
void GPS_SetDebug() {
MicroNMEA::sendSentence(gps, "$PUBX,40,GLL,0,1,0,0,0,0");
MicroNMEA::sendSentence(gps, "$PUBX,40,VTG,0,1,0,0,0,0");
MicroNMEA::sendSentence(gps, "$PUBX,40,GSA,0,1,0,0,0,0");
MicroNMEA::sendSentence(gps, "$PUBX,40,GSV,0,1,0,0,0,0");
MicroNMEA::sendSentence(gps, "$PUBX,40,GGA,0,1,0,0,0,0");
MicroNMEA::sendSentence(gps, "$PUBX,40,RMC,0,1,0,0,0,0");
}
//==========================================================
// SETUP
//==========================================================
void userSetup() {
//==========================================================
// INDICATION INITIALIZATION
//==========================================================
pinMode(BOARD_BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_BLUE, OUTPUT);
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_BLUE, LOW);
gps.begin(9600);
// ------ Disable or enable certain messages (Disabling is useful to conserve time for processing)
GPS_SetMin();
uint32_t tout = millis();
while (!gps.available() && ((millis() - tout) < 1000) ) { }
if ((millis() - tout) < 1000) {
delay(500);
ledBlink(LED_BLUE, 1000);
delay(500);
ledBlink(LED_BLUE, 1000);
}
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
Wire.setClock(400000);
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
rtc.begin(); // RTC initialization
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(__DATE__, __TIME__));
}
rtc.write(0x0E, 0); // RTC 1 Hz pulse
mpu.initialize();
if (mpu.getDeviceID() > 0) {
// console.print(F("MPU ID: ")); console.println(mpu.getDeviceID());
delay(500);
ledBlink(LED_RED, 1000);
delay(500);
ledBlink(LED_RED, 1000);
}
pinMode(GPS_PPS_IRQ, INPUT);
attachInterrupt(GPS_PPS_IRQ, GPSppsHandler, CHANGE);
pinMode(RTC_PPS_IRQ, INPUT_PULLUP); // RTC pulse is LOW active
attachInterrupt(RTC_PPS_IRQ, RTCppsHandler, FALLING);
pinMode(BAT_VOLT_PIN, INPUT_ANALOG); // ADC pin for battery monitoring
//==========================================================
// BATTERY VOLTAGE TIMER INITIALIZATION
//==========================================================
timer_BatVolt.pause();
// Set up period
timer_BatVolt.setPeriod(BAT_VOLT_RATE); // in microseconds
// Set up an interrupt on channel 1
timer_BatVolt.setChannel1Mode(TIMER_OUTPUT_COMPARE);
timer_BatVolt.setCompare(TIMER_CH1, 1); // Interrupt 1 count after each update
timer_BatVolt.attachCompare1Interrupt(BatVoltHandler);
// Refresh the timer's count, prescale, and overflow
timer_BatVolt.refresh();
//==========================================================
// GPS READ DATA TIMER INITIALIZATION
//==========================================================
/*
timer_ReadGPS.pause();
// Set up period
timer_ReadGPS.setPeriod(GPS_READ_INTERVAL); // in microseconds
// Set up an interrupt on channel 1
timer_ReadGPS.setChannel1Mode(TIMER_OUTPUT_COMPARE);
timer_ReadGPS.setCompare(TIMER_CH1, 1); // Interrupt 1 count after each update
timer_ReadGPS.attachCompare1Interrupt(ReadGPSHandler);
// Refresh the timer's count, prescale, and overflow
timer_ReadGPS.refresh();
// Start the timer counting
// timer_ReadGPS.resume();
*/
RTC_AdjustToGPS();
// Start the BatVolt timer counting
timer_BatVolt.resume();
}
void GPS_Debug(uint8_t isDebug) {
GPS_SetDebug();
while (!isDebug || !console.available()) {
if (console.available()) { // If anything comes in Serial (USB),
gps.write(console.read()); // read it and send it out Serial1 (pins 0 & 1)
}
if (gps.available()) { // If anything comes in Serial1 (pins 0 & 1)
console.write(gps.read()); // read it and send it out Serial (USB)
}
}
GPS_SetMin();
}
void GPS_Menu() {
uint8_t ext = 0;
while (!ext) {
do {
delay(10);
} while (console.available() && console.read() >= 0);
console.println();
console.println(F("GPS menu:"));
console.println(F("d - Debug mode"));
console.println(F("c - Config mode"));
console.println(F("x - exit"));
while (!console.available()) {
yield();
}
char c = tolower(console.read());
// Discard extra Serial data.
do {
delay(10);
} while (console.available() && console.read() >= 0);
if (c == 'd') {
console.println(F("\nGPS Debug - type any character to stop\n"));
GPS_Debug(true);
} else if (c == 'c') {
console.println(F("\nGPS Config - RESET to stop\n"));
GPS_Debug(false);;
} else if (c == 'x') {
ext = 1;
} else {
console.println(F("Invalid entry"));
}
}
}
void RTC_Show() {
console.println(F("\nRTC Ddebug - type any character to stop\n"));
while (!console.available()) {
// GPS_Process();
if (RTCppsTriggered) {
// char buf_RTC[100];
strncpy(buf_RTC, "YYYY.MM.DD hh:mm:ss\0", 20);
console.print(F("\nRTC: ")); //RTC
RTC_now = rtc.now();
console.println(RTC_now.format(buf_RTC));
RTCppsTriggered = 0;
}
// char buf_GPS[100];
// strncpy(buf_GPS, "DD.MM.YYYY hh:mm:ss\0", 100);
// console.print(F("GPS: ")); //GPS
// console.println(GPS_now.format(buf_GPS));
// delay(1000);
}
}
void RTC_Console_Set() {
byte Year, Month, Day, Hour, Minute, Second;
boolean GotString = false;
char InChar;
byte Temp1, Temp2;
char InString[19];
char Date[10];
char Time[10];
byte j = 0;
console.println(F("\nEnter UTC Date and Time in this format: 27-11-2017 12:34:56\n"));
while (!GotString) {
if (console.available()) {
InChar = console.read();
if (InChar == 0x0D) { // Carriage return +
if (console.read() == 0x0A) // +Line feed (CRLF)
GotString = true;
}
else {
InString[j] = InChar;
j += 1;
}
}
}
InString[j] = '\0';
console.println(InString);
if (j != 19) {
console.println(F("\nWrong input!\n"));
return;
}
strncpy(Date, InString, 10); Date[10] = '\0';
strncpy(Time, &InString[11], 8); Time[8] = '\0';
// console.println(Date);
// console.println(Time);
rtc.adjust(DateTime(Date, Time));
}
void RTC_Menu() {
uint8_t ext = 0;
while (!ext) {
do {
delay(10);
} while (console.available() && console.read() >= 0);
console.println();
console.println(F("RTC menu:"));
console.println(F("g - show UTC time"));
// console.println(F("p - sync RTC to GPS time"));
console.println(F("s - set manually UTC time"));
console.println(F("x - exit"));
while (!console.available()) {
yield();
}
char c = tolower(console.read());
// Discard extra Serial data.
do {
delay(10);
} while (console.available() && console.read() >= 0);
if (c == 'g') {
RTC_Show();
} else if (c == 's') {
RTC_Console_Set();
} else if (c == 'p') {
RTC_AdjustToGPS();
} else if (c == 'x') {
ext = 1;
} else {
console.println(F("Invalid entry"));
}
}
}
void GetUnitID(char* unitID) {
// *unitID = "001";
}
void FATdateTime(uint16_t* date, uint16_t* time) {
// User gets date and time from GPS or real-time
// clock in real callback function
// while (RTCBusy) {
// yield();
// }
// DateTime now = rtc.now();
// return date using FAT_DATE macro to format fields
*date = FAT_DATE(rtc.now().year(), rtc.now().month(), rtc.now().day());
// return time using FAT_TIME macro to format fields
*time = FAT_TIME(rtc.now().hour(), rtc.now().minute(), rtc.now().second());
}
// Print a data record.
void printData(Print * pr, data_t* data) {
/*if (startMicros == 0) {
startMicros = data->RTCtime;
}*/
pr->print(data->GPStime);
pr->write(',');
// pr->print(data->RTCtime);
tempDT = data->RTCtime;
strncpy(buf_RTC, "YYYY-MM-DD hh:mm:ss\0", 20);
pr->print(tempDT.format(buf_RTC));
pr->write(',');
pr->print(data->Status);
pr->write(',');
pr->print(data->ax);
pr->write(',');
pr->print(data->ay);
pr->write(',');
pr->print(data->az);
pr->write(',');
pr->print(data->gx);
pr->write(',');
pr->print(data->gy);
pr->write(',');
pr->print(data->gz);
pr->write(',');
pr->print(data->temp / 340 + 37); // last number must be 36.53 but we presume to use 37 for rounding
pr->write(',');
pr->println(data->batVolt);
}
// Print data header.
void printHeader(Print * pr) {
// pr->println(BAT_VOLT_RATE);
pr->println(F("Station_code\tMPU-001"));
pr->println(F("Sampling_rate\t100.0000"));
pr->print(F("Start_date\t")); strncpy(buf_RTC, "DD.MM.YYYY\0", 20); pr->println(StartRTCLogTime.format(buf_RTC));
pr->print(F("Start_time\t")); strncpy(buf_RTC, "hh:mm:ss\0", 20); pr->println(StartRTCLogTime.format(buf_RTC));
pr->println(F("GPSTime,RTCTime,Status,ax,ay,az,gx,gy,gz,temp,batVolt"));
}