-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.ino
538 lines (493 loc) · 12.1 KB
/
logging.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
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
527
528
529
530
531
532
533
534
535
536
537
538
/*-------------------------
management of data logging in controller (to SD)
and transfer to computer (via USB)
-------------------------*/
// rounding accuracy
int nPositions = 6;
// for SD card initialization
const uint8_t chipSelect = SS;
// file system object
SdFat sd;
// log file object
SdFile file;
char paramStoreFile[11] = "params.txt";
char paramStoreFile2[11] = "lghtps.txt";
// temporary variables for parameter reading from storage file
float resArr[numChambers + 2][6] = {0};
float line[6] = {0};
char cs[6];
/*-----------------------
logging setup
-----------------------*/
void loggingSetup()
{
setSyncProvider(RTC.get); // the function to get the time from the RTC
if(timeStatus() != timeSet)
sendError(F("Unable to sync with the RTC"));
// else
// Serial.println(F("RTC has set the system time"));
SDavail = initSD();
loadParameters();
}
/*-----------------------
logging via serial connection
-----------------------*/
void loggingEvent()
{
logToSerial();
logToSD();
}
/*-----------------------
send current data to computer via serial
-----------------------*/
void logToSerial()
{
// send message type
Serial.print("DATA;");
// send intensity values
for(int j=0; j<numChambers; j++)
{
for(int i=0; i < numLeds; i++)
{
Serial.print(odValues[j][i],nPositions);
if(!((j == numChambers-1) && (i == numLeds-1)))
Serial.print(sep);
}
}
Serial.print(';');
// send background intensity values
for(int i=0; i<numChambers; i++)
{
Serial.print(odValues[i][5],nPositions);
if(i<numChambers-1) Serial.print(sep);
}
Serial.print(';');
// send temperature values
for(int i=0; i<numChambers; i++)
{
Serial.print(temperatureInLiquid[i]);
if(i<numChambers-1) Serial.print(sep);
}
Serial.print(';');
// send light values
for(int i=0; i<numChambers; i++)
{
Serial.print(lightBrightness[i]);
if(i<numChambers-1) Serial.print(sep);
}
Serial.print(';');
//send min light values
for(int i=0; i<numChambers; i++)
{
Serial.print(minLightBrightness[i]);
if(i<numChambers-1) Serial.print(sep);
}
Serial.print(';');
//send max light values
for(int i=0; i<numChambers; i++)
{
Serial.print(maxLightBrightness[i]);
if(i<numChambers-1) Serial.print(sep);
}
Serial.print(';');
Serial.print(sensorSamplingTime);
Serial.print(sep);
Serial.print(curr_t);
Serial.print(sep);
Serial.println(BIOREACTOR_MODE);
}
/*-----------------------
write current data to file on SD, if available
-----------------------*/
void logToSD()
{
// check if SD logging is available
if(!SDavail | !SDlogging)
return;
//log measured values
if (!file.open(fileName, O_CREAT | O_APPEND | O_WRITE))
{
sendError(F("logToSD:error SD file.open"));
return;
}
// send message type
file.print("DATA;");
// send intensity values
for(int j=0; j<numChambers; j++)
{
for(int i=0; i < numLeds; i++)
{
file.print(odValues[j][i],nPositions);
if(!((j == numChambers-1) && (i == numLeds-1)))
file.print(sep);
}
}
file.print(';');
// send background intensity values
for(int i=0; i<numChambers; i++)
{
file.print(odValues[i][5],nPositions);
if(i<numChambers-1) file.print(sep);
}
file.print(';');
// send temperature values
for(int i=0; i<numChambers; i++)
{
file.print(temperatureInLiquid[i]);
if(i<numChambers-1) file.print(sep);
}
file.print(';');
// send light values
for(int i=0; i<numChambers; i++)
{
file.print(lightBrightness[i]);
if(i<numChambers-1) file.print(sep);
}
file.print(';');
//send min light values
for(int i=0; i<numChambers; i++)
{
file.print(minLightBrightness[i]);
if(i<numChambers-1) file.print(sep);
}
file.print(';');
//send max light values
for(int i=0; i<numChambers; i++)
{
file.print(maxLightBrightness[i]);
if(i<numChambers-1) file.print(sep);
}
file.print(';');
file.print(sensorSamplingTime);
file.print(sep);
file.print(curr_t);
file.print(sep);
file.println(BIOREACTOR_MODE);
if (!file.sync() || file.getWriteError()) {
sendError(F("SD write error"));
}
file.close();
}
/*-----------------------
announce reference values used for OD calculation
-----------------------*/
void sendReferenceValues()
{
Serial.print("REF;");
for(int j=0; j<numChambers; j++)
{
for(int i=0; i < numLeds; i++)
{
Serial.print(refValues[j][i]);
Serial.print(sep);
}
}
Serial.println();
}
/*-----------------------
announce dynamic light profile values
-----------------------*/
void sendLightProfile(int chamber)
{
Serial.print(F("LP;"));
for(int i=0;i<lightProfileLength;i++)
{
Serial.print(brightnessValue[chamber][i]);
Serial.print(sep);
Serial.print(brightnessDuration[chamber][i]);
Serial.print(sep);
}
Serial.println();
}
/*-----------------------
announce current NinjaPBR mode
-----------------------*/
void sendMode()
{
Serial.print(F("MD;"));
Serial.print(BIOREACTOR_MODE);
Serial.println(sep);
}
/*-----------------------
Initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
breadboards. Use SPI_FULL_SPEED for better performance.
-----------------------*/
boolean initSD()
{
boolean res = sd.begin(chipSelect, SPI_HALF_SPEED);
if(!res)
sendMessage("could not init SD");
return(res);
}
/*-----------------------
create new log file name with consecutive numbering
-----------------------*/
boolean findLogName(boolean newFile)
{
boolean res = true;
// init SD card for logging
const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
// Find an unused file name.
if (BASE_NAME_SIZE > 6) {
sendError("FILE_BASE_NAME too long");
}
char lastExisting[13];
while (sd.exists(fileName)) {
strcpy(lastExisting, fileName);
if (fileName[BASE_NAME_SIZE + 1] != '9') {
fileName[BASE_NAME_SIZE + 1]++;
} else if (fileName[BASE_NAME_SIZE] != '9') {
fileName[BASE_NAME_SIZE + 1] = '0';
fileName[BASE_NAME_SIZE]++;
} else {
sendError("Can't create file name");
res = false;
}
}
if(!newFile)
strcpy(fileName, lastExisting);
return(res);
}
/*-----------------------
remove last valid log file from SD card
-----------------------*/
boolean rmLastValidLog()
{
// jump to last valid log file
findLogName(false);
//remove it
if(sd.exists(fileName))
sd.remove(fileName);
else
sendError(F("Error removing SD Log File"));
strcpy(fileName, FILE_BASE_NAME "00.csv");
// jump to last valid log file to use
findLogName(false);
sendMessage(F("removed SD Log File"));
sendMessage(fileName);
}
/*-----------------------
test writing to the specified log file
-----------------------*/
boolean testLogFile()
{
if (!file.open(fileName, O_CREAT | O_WRITE)) {
sendError(F("TestLog: error SD file.open"));
return(false);
}
else
{
file.close();
String s = "using SD log" + String(fileName);
sendMessage(s);
return(true);
}
}
/*-----------------------
tell computer about something
-----------------------*/
void sendMessage(String msg)
{
Serial.print(F("MSG;"));
Serial.println(msg);
}
/*-----------------------
tell computer about error
-----------------------*/
void sendError(String msg)
{
Serial.print(F("ERROR;"));
Serial.println(msg);
}
/*-----------------------
send the current data log to the computer
-----------------------*/
size_t n;
void sendLogData()
{
if(!SDavail)
{
sendMessage(F("SD card not initialized, no LOG available"));
return;
}
sendMessage(fileName);
if (!file.open(fileName, O_READ)) sendError(F("open LOG failed"));
while ((n = file.fgets(buffer, line_buffer_size)) > 0)
{
// Print line number.
Serial.print(buffer);
}
if (!file.close()) sendError(F("closing LOG failed"));
}
/*-------------------
load last used box parameters in case of restart
------------------- */
boolean loadParameters()
{
if(!SDavail)
{
sendMessage(F("SD card not initialized"));
return(false);
}
/*------------------------
load reference values and light parameters
------------------------*/
// open input file
ifstream sdin(paramStoreFile);
// check for open error
if (!sdin.is_open()) {
sendError(F("Can not open parameter file"));
}
// read until input fails
for(int i=0; i<numChambers + 2; i++)
{
// Assume EOF if fail.
if (sdin.fail())
break;
// Get commas and numbers.
sdin >> line[0] >> cs[0] >> line[1]>> cs[1] >> line[2] >> cs[2] >> line[3]>> cs[3] >> line[4] >> cs[4] >> line[5] >> cs[5];
// Skip CR/LF.
sdin.skipWhite();
if (sdin.fail())
sendError(F("Bad input in parameter file"));
// error in line if not commas
// if (cs[j] != sep)
// sendError(F("Comma"));
// print in six character wide columns
for(int j=0; j<6; j++)
resArr[i][j] = line[j];
}
// brightness values for constant light modes
for(int i=0; i<3; i++)
{
minLightBrightness[i] = resArr[0][i];
maxLightBrightness[i] = resArr[0][i+3];
}
// set last reference values
for(int i=0; i<numChambers; i++)
{
for(int j=0; j<6; j++)
{
refValues[i][j] = resArr[i+1][j];
}
}
// sampling frequency
sensorSamplingTime = resArr[numChambers + 1][0];
// current position in the light cycle of the dynamic light program
for(int j=0; j<3; j++)
lightProfileIdx[j] = resArr[numChambers + 1][j+1];
// check last state of SD logging. if was active and SD is available, set to active again
if(SDavail)
{
SDlogging = (boolean) resArr[numChambers + 1][4];
}
/*-------------------------
load dynamic light programs
--------------------------*/
// open input file
ifstream sdin2(paramStoreFile2);
// check for open error
if (!sdin2.is_open()) {
sendError(F("Can not open dynamic light program file"));
}
// read until input fails
for(int i=0; i<lightProfileLength; i++)
{
// Assume EOF if fail.
if (sdin2.fail())
break;
// Get commas and numbers.
sdin2 >> line[0] >> cs[0] >> line[1]>> cs[1] >> line[2] >> cs[2] >> line[3]>> cs[3] >> line[4] >> cs[4] >> line[5] >> cs[5];
// Skip CR/LF.
sdin2.skipWhite();
if (sdin2.fail())
sendError(F("Bad input in dynamic light file"));
for(int j=0; j<3; j++)
{
brightnessValue[j][i] = line[j];
brightnessDuration[j][i] = line[j+3];
}
}
return(true);
}
/*-------------------
persists current box parameters in case of restart
------------------- */
boolean saveParameters()
{
if(!SDavail)
{
sendError(F("SD card not initialized"));
return(false);
}
/*-------------------
saving parameters and reference values
--------------------*/
if (!file.open(paramStoreFile, O_CREAT | O_WRITE))
{
sendError(F("error writing params to file"));
return(false);
}
for(int i=0; i<3; i++)
{
file.print(minLightBrightness[i]);
file.print(sep);
}
for(int i=0; i<3; i++)
{
file.print(maxLightBrightness[i]);
file.print(sep);
}
file.println();
for(int i=0; i<numChambers; i++)
{
for(int j=0; j<6; j++)
{
file.print(refValues[i][j]);
file.print(sep);
}
file.println();
}
file.print(sensorSamplingTime);
file.print(sep);
for(int j = 0; j < 3; j++)
{
file.print(lightProfileIdx[j]);
file.print(sep);
}
// save SD logging state
file.print((int)SDlogging);
file.println(F(",-1,"));
file.print(F("NinjaPBR Parameters, "));
file.println(curr_t);
// Force data to SD and update the directory entry to avoid data loss.
if (!file.sync() || file.getWriteError())
{
sendError("SD write error");
}
file.close();
/*-------------------
saving light programs
--------------------*/
if (!file.open(paramStoreFile2, O_CREAT | O_WRITE))
{
sendError(F("error writing light programs to file"));
return(false);
}
for(int i=0; i<lightProfileLength; i++)
{
for(int j=0; j<3; j++)
{
file.print(brightnessValue[j][i]);
file.print(sep);
}
for(int j=0; j<3; j++)
{
file.print(brightnessDuration[j][i]);
file.print(sep);
}
file.println();
}
file.close();
return(true);
}