-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord_sdp.cpp
380 lines (318 loc) · 10.7 KB
/
record_sdp.cpp
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
/*
* sdptasks.c
*
* Created on: Sep 9, 2021
* Author: Danylo Ulianych
*/
#include <M5Core2.h>
#include <math.h>
#include <unistd.h>
#include <string.h>
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "bsp_log.h"
#include "onlinemean.h"
#include "SDPSensors.h"
#include "sdcard.h"
#include "record.h"
#include "board.h"
#define READ_SENSORS_PRIORITY 3
#define WRITE_TO_SDCARD_PRIORITY 2
#define WATCHDOG_UPDATE_MS 1000L
#define LCD_HEADER_UPDATE_MS 5000L
#define SD_WAIT_UNTIL_RESTART_MS 10L
#define SDP_WATCHDOG_MAX_FAILS 3
#define ESP32_SDPSENSOR_DEBUG 0
/**
* Buffer recordings to 512 bytes,
* which corresponds to the sector
* size of an SD card.
* One record occupies 8+2 bytes.
*/
#define RECORDS_BUFFER_SIZE 52
static esp_timer_handle_t sdp_timer;
static TaskHandle_t read_sensor_task_handle;
static TaskHandle_t write_task_handle;
static QueueHandle_t xQueueSDP;
static QueueHandle_t xQueueSDPErrors;
static uint64_t records_received = 0;
static const char *TAG = "sdptask";
SDPSensor sdp(0x25);
static void sdptask_read_sensor();
static void sdptask_write();
static void sdp_timer_callback(void* arg);
static void xqueue_check_errors();
static FILE* open_fileSDP();
static void sdp_timer_callback(void* arg)
{
xTaskNotifyGive(read_sensor_task_handle);
}
/**
* Check the error code status of the last read measurement.
* If enough errors occurred, a software reset is issued.
*
* Usage:
* esp_err_t err = sdp.readContinuousRaw(&diff_pressure);
* sdp.watchdogCheck(err);
*
* @param status - the error code of the last measurement
*/
bool sdp_watchdog_check(bool success) {
static int failsCount = 0;
if (success) {
failsCount = 0;
} else {
failsCount++;
}
if (failsCount >= SDP_WATCHDOG_MAX_FAILS) {
BSP_LOGW(TAG, "SDPWatchdog barked");
bool restarted = false;
do {
restarted = sdp.stopContinuous();
if (restarted) {
restarted = sdp.startContinuous(false);
}
} while (!restarted);
BSP_LOGI(TAG, "SDP restarted");
return true;
}
return false;
}
/**
* Writes SDP sensor info to the SD card.
*
* Must be called after both the SD card
* and the sensor are initialized.
*/
static esp_err_t write_sdpinfo() {
char fpath[128];
snprintf(fpath, sizeof(fpath), "%s/SENSOR.TXT", sdcard_get_record_dir());
FILE *file = fopen(fpath, "w");
if (file == NULL) {
return ESP_ERR_NOT_FOUND;
}
uint32_t model_number, range_pa;
switch(sdp.getModel()) {
case SDP31_500:
model_number = 31;
range_pa = 500;
break;
case SDP32_125:
model_number = 32;
range_pa = 125;
break;
case SDP800_500:
model_number = 800;
range_pa = 500;
break;
case SDP810_500:
model_number = 810;
range_pa = 500;
break;
case SDP801_500:
model_number = 801;
range_pa = 500;
break;
case SDP811_500:
model_number = 811;
range_pa = 500;
break;
case SDP800_125:
model_number = 800;
range_pa = 125;
break;
case SDP810_125:
model_number = 810;
range_pa = 125;
break;
default:
model_number = 0;
range_pa = 0;
break;
}
uint32_t product_id;
uint64_t serial;
sdp.readProductID(&product_id, &serial);
BSP_LOGI(TAG, "Initialized SDP%d %dPa sensor", model_number, range_pa);
uint16_t pressure_scale = sdp.getPressureScale();
fprintf(file, "Model number: %u\n", model_number);
fprintf(file, "Range Pa: %u\n", range_pa);
fprintf(file, "Pressure scale: %u\n", pressure_scale);
fprintf(file, "Product ID: 0x%08X\n", product_id);
fprintf(file, "Serial: 0x%016llX\n", serial);
fclose(file);
BSP_LOGI(TAG, "Wrote sensor info to '%s'", fpath);
return ESP_OK;
}
static void sdptask_read_sensor() {
bool success;
SDPRecord sdp_record; // diff pressure
const Board_t *board = board_get();
const int8_t is_bmp_same_wire = board->bmp_gpio.sda == board->sdp_gpio.sda
|| board->bmp_gpio.scl == board->sdp_gpio.scl;
const esp_err_t err_fail = ESP_FAIL;
while (1) {
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
sdp_record.time = esp_timer_get_time();
success = sdp.readMeasurement(&sdp_record.diff_pressure_raw, NULL, NULL);
if (success) {
xQueueSend(xQueueSDP, &sdp_record, portMAX_DELAY);
} else {
xQueueSend(xQueueSDPErrors, &err_fail, 0);
// Set the LED error flag ON
board->led_set_error();
}
if (is_bmp_same_wire) {
record_bmp_read();
}
sdp_watchdog_check(success);
}
}
static FILE* open_fileSDP() {
static int trial = 0;
char fpath[128];
snprintf(fpath, sizeof(fpath), "%s/SDP-%03d.BIN", sdcard_get_record_dir(), trial++);
FILE *file = fopen(fpath, "w");
int64_t t_last = esp_timer_get_time();
while (file == NULL) {
int64_t t_curr = esp_timer_get_time();
if (t_curr - t_last > SD_WAIT_UNTIL_RESTART_MS * 1000L) {
SD.end();
while (!SD.begin(TFCARD_CS_PIN, SPI, 40000000)) delay(10);
BSP_LOGW(TAG, "SD restarted");
t_last = t_curr;
}
vTaskDelay(pdMS_TO_TICKS(10));
file = fopen(fpath, "w");
}
BSP_LOGI(TAG, "Opened %s", fpath);
return file;
}
static void xqueue_check_errors() {
static uint64_t errors_cnt = 0;
esp_err_t err;
const uint64_t n_total = records_received + uxQueueMessagesWaiting(xQueueSDP);
while (uxQueueMessagesWaiting(xQueueSDPErrors)) {
if (xQueueReceive(xQueueSDPErrors, &err, pdMS_TO_TICKS(10)) == pdTRUE) {
errors_cnt++;
BSP_LOGW(TAG, "[%llu / %llu] SDP sensor read failed: %s",
errors_cnt, n_total, esp_err_to_name(err));
}
}
}
static void update_lcd_header(long int fsize) {
static int64_t last_update = 0;
if (esp_timer_get_time() - last_update > LCD_HEADER_UPDATE_MS * 1000L) {
int16_t x = M5.lcd.getCursorX();
int16_t y = M5.lcd.getCursorY();
M5.Lcd.setCursor(0, 0);
M5.Lcd.setTextColor(LIGHTGREY);
M5.Lcd.setTextSize(2);
M5.lcd.fillRect(0, 0, 320, 16, BLACK);
M5.Lcd.printf("# %llu %lld Kb", records_received, fsize >> 10);
M5.Lcd.setCursor(x, y);
last_update = esp_timer_get_time();
}
}
static void sdptask_write() {
SDPRecord sdp_records[RECORDS_BUFFER_SIZE];
uint32_t rid; // diff pressure record id
UBaseType_t messages_cnt_max = 0;
OnlineMean dt_mean;
int64_t wd_update, dt, dt_min = INT64_MAX, dt_max = 0;
int16_t dp_max = 0;
size_t wcnt;
OnlineMean_Init(&dt_mean);
const Board_t *board = board_get();
FILE *fileSDP = open_fileSDP();
const TickType_t sleep_ticks = pdMS_TO_TICKS(10);
while (1) {
int8_t print_update = 0;
OnlineMean_Reset(&dt_mean);
wd_update = esp_timer_get_time();
do { /* fwrite until the queue length is not enough */
if (esp_timer_get_time() - wd_update > WATCHDOG_UPDATE_MS * 1000L) {
vTaskDelay(sleep_ticks);
wd_update = esp_timer_get_time();
}
if (messages_cnt_max < uxQueueMessagesWaiting(xQueueSDP)) {
messages_cnt_max = uxQueueMessagesWaiting(xQueueSDP);
print_update = 1;
}
for (rid = 0; rid < RECORDS_BUFFER_SIZE; rid++) {
while (xQueueReceive(xQueueSDP, sdp_records + rid, pdMS_TO_TICKS(20)) != pdTRUE) {
xqueue_check_errors();
}
records_received++;
xqueue_check_errors();
if (sdp_records[rid].diff_pressure_raw > dp_max) {
dp_max = sdp_records[rid].diff_pressure_raw;
}
if (rid > 0) {
dt = sdp_records[rid].time - sdp_records[rid-1].time;
OnlineMean_Update(&dt_mean, dt);
if (dt > dt_max) {
dt_max = dt;
print_update = 1;
}
if (dt < dt_min) {
dt_min = dt;
print_update = 1;
}
}
}
wcnt = fwrite(sdp_records, sizeof(SDPRecord), RECORDS_BUFFER_SIZE, fileSDP);
int fflush_res = fflush(fileSDP);
int fsync_res = fsync(fileno(fileSDP));
while (!(wcnt == RECORDS_BUFFER_SIZE && fflush_res == 0 && fsync_res == 0)) {
BSP_LOGE(TAG, "fwrite SDP failed");
board->led_set_error();
fclose(fileSDP);
fileSDP = open_fileSDP();
wcnt = fwrite(sdp_records, sizeof(SDPRecord), RECORDS_BUFFER_SIZE, fileSDP);
fflush_res = fflush(fileSDP);
fsync_res = fsync(fileno(fileSDP));
}
} while (uxQueueMessagesWaiting(xQueueSDP) >= RECORDS_BUFFER_SIZE);
if (print_update || ESP32_SDPSENSOR_DEBUG) {
BSP_LOGI(TAG, "[q %u] [r %.0f %lld %lld] [dp %d]",
messages_cnt_max,
OnlineMean_GetMean(&dt_mean), dt_min, dt_max,
dp_max);
}
update_lcd_header(ftell(fileSDP));
vTaskDelay(sleep_ticks);
}
}
void record_sdp_start() {
const Board_t *board = board_get();
while (!sdp.stopContinuous());
while (sdp.begin() == 0);
while (!sdp.startContinuous(false));
write_sdpinfo();
int16_t temperature;
if (sdp.readMeasurement(NULL, &temperature, NULL)) {
BSP_LOGI(TAG, "SDP sensor t° %.1f", temperature / 200.0f);
}
const esp_timer_create_args_t sdp_timer_args = {
.callback = &sdp_timer_callback,
.name = "sdp_timer"
};
ESP_ERROR_CHECK(esp_timer_create(&sdp_timer_args, &sdp_timer));
xQueueSDP = xQueueCreate( 5000 * RECORDS_BUFFER_SIZE, sizeof(SDPRecord) );
xQueueSDPErrors = xQueueCreate( 100, sizeof(esp_err_t) );
xTaskCreatePinnedToCore((TaskFunction_t) sdptask_read_sensor, "sdp_read", 2048, NULL, READ_SENSORS_PRIORITY, &read_sensor_task_handle, PRO_CPU_NUM);
xTaskCreatePinnedToCore((TaskFunction_t) sdptask_write, "sdp_write", 8192, NULL, WRITE_TO_SDCARD_PRIORITY, &write_task_handle, APP_CPU_NUM);
// let SDP & BMP tasks start
vTaskDelay(pdMS_TO_TICKS(1000));
ESP_ERROR_CHECK(esp_timer_start_periodic(sdp_timer, SDPSENSOR_SAMPLE_PERIOUD_US));
BSP_LOGI(TAG, "Started a periodic timer");
BSP_LOGI(TAG, "SDP tasks started");
}
void record_sdp_stop() {
vTaskDelete(read_sensor_task_handle);
vTaskDelete(write_task_handle);
BSP_LOGW(TAG, "SDP tasks stopped");
}