-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobd-logger.ino
277 lines (217 loc) · 5.23 KB
/
obd-logger.ino
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
#include <Arduino.h>
#include <FreematicsPlus.h>
#include "datalogger.h"
#include "recorder.h"
#include "config.h"
#include "debug.h"
#define BUF 1024
TaskHandle_t Task1;
TaskHandle_t Task2;
TaskHandle_t Task3;
QueueHandle_t queue;
FreematicsESP32 sys;
DataLogger logger;
Recorder recorder;
COBD obd;
bool obdConnected = false;
GPS_DATA *gd = 0;
uint32_t lastGPStime = 0;
unsigned long _lastMillis = 0;
void setup()
{
// init LED pin
pinMode(PIN_LED, OUTPUT);
pinMode(PIN_LED, HIGH);
delay(1000);
// initialize USB serial
Serial.begin(115200);
Serial.print("Hardware: ESP32 ");
Serial.print(ESP.getCpuFreqMHz());
Serial.print("MHz ");
Serial.print(getFlashSize() >> 10);
Serial.println("MB Flash");
while (!sys.begin())
;
DEBUG_PRINT((String) "Freematics firmware version: " + sys.version);
recorder.begin();
uint8_t *dataSerial = (uint8_t *)malloc(BUF);
queue = xQueueCreate(100, sizeof(dataSerial));
if (queue == NULL)
{
Serial.println("Error creating the queue");
}
logger.setQueue(queue);
setupGps();
setupObd();
xTaskCreatePinnedToCore(
Task1Loop, /* Task function. */
"Task1", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task1, /* Task handle to keep track of created task */
1 /* pin task to core 1 */
);
xTaskCreatePinnedToCore(
Task2Loop, /* Task function. */
"Task2", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
3, /* priority of the task */
&Task2, /* Task handle to keep track of created task */
1 /* pin task to core 1 */
);
xTaskCreatePinnedToCore(
Task3Loop, /* Task function. */
"Task3", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
2, /* priority of the task */
&Task3, /* Task handle to keep track of created task */
1 /* pin task to core 1 */
);
pinMode(PIN_LED, LOW);
}
byte m_state = 0;
bool checkState(byte flags) { return (m_state & flags) == flags; }
void setState(byte flags) { m_state |= flags; }
void setupGps()
{
if (!checkState(STATE_GPS_FOUND))
{
Serial.print("GPS...");
if (sys.gpsBegin(GPS_SERIAL_BAUDRATE, false))
{
setState(STATE_GPS_FOUND);
Serial.println("OK");
//waitGPS();
}
else
{
sys.gpsEnd();
Serial.println("NO");
}
}
}
void setupObd()
{
obd.begin(sys.link);
}
void logGPSData()
{
// issue the command to get parsed GPS data
if (checkState(STATE_GPS_FOUND) && sys.gpsGetData(&gd) && lastGPStime != gd->time)
{
// set GPS ready flag
setState(STATE_GPS_READY);
logger.setGpsDateTime(gd->date, gd->time);
logger.log(PID_GPS_DATE, gd->date);
logger.log(PID_GPS_TIME, gd->time);
logger.log(PID_GPS_ALTITUDE, gd->alt);
logger.log(PID_GPS_SPEED, gd->speed);
logger.log(PID_GPS_SAT_COUNT, gd->sat);
logger.logFloat(PID_GPS_LATITUDE, gd->lat);
logger.logFloat(PID_GPS_LONGITUDE, gd->lng);
lastGPStime = gd->time;
}
}
void logAdapterMetrics()
{
logger.log(PID_DEVICE_TEMP, readChipTemperature());
}
void loop()
{
// do nothing here because we're using FreeRTOS tasks to handle the loops
}
void Task1Loop(void *pvParameters)
{
Serial.print("GPS logger running on core ");
Serial.println(xPortGetCoreID());
unsigned long currentMillis;
for (;;)
{
currentMillis = millis();
if (currentMillis - _lastMillis >= 1000)
{
_lastMillis = currentMillis;
DEBUG_PRINT("[GPS] Logging");
logGPSData();
logAdapterMetrics();
}
logger.loop();
}
}
void Task2Loop(void *pvParameters)
{
Serial.print("Data recorder running on core ");
Serial.println(xPortGetCoreID());
unsigned long currentMillis;
char *message;
for (;;)
{
recorder.loop();
currentMillis = millis();
if (queue != 0 && xQueueReceive(queue, &message, portMAX_DELAY) == pdTRUE)
{
pinMode(PIN_LED, HIGH);
recorder.send(message);
free(message);
pinMode(PIN_LED, LOW);
}
}
}
void Task3Loop(void *pvParameters)
{
Serial.print("OBD-II logger running on core ");
Serial.println(xPortGetCoreID());
unsigned long currentMillis;
for (;;)
{
currentMillis = millis();
if (currentMillis - _lastMillis >= 1000)
{
_lastMillis = currentMillis;
DEBUG_PRINT("[OBD-II] Logging");
// log OBD data
logObd2();
}
}
}
void logObd2()
{
if (!obdConnected)
{
Serial.print("[OBD-II] Connecting");
if (obd.init())
{
Serial.println("OK");
obdConnected = true;
}
else
{
Serial.println();
delay(1000);
}
return;
}
int value;
if (obd.readPID(PID_RPM, value))
{
logger.log(PID_RPM | 0x100, value);
}
if (obd.readPID(PID_SPEED, value))
{
logger.log(PID_SPEED | 0x100, value);
}
if (obd.readPID(PID_DISTANCE, value))
{
logger.log(PID_DISTANCE | 0x100, value);
}
logger.log(PID_BATTERY_VOLTAGE, obd.getVoltage());
if (obd.errors > 2)
{
Serial.println("OBD disconnected");
obdConnected = false;
obd.reset();
}
}