-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSdCard.cpp
168 lines (147 loc) · 4.87 KB
/
SdCard.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
//
//
//
#include "SdCard.h"
#include "Config.h"
#include "SD.h"
#include "SPI.h"
#include "RPM.h"
#include "RxLinkQuality.h"
#include "Power.h"
#include "Telemetry.h"
#include "Temperature.h"
#include <TimeLib.h>
#include "MCU6050.h"
#include "ErrorHandling.h"
// Private Variables
bool sdCardLoggingActive = false;
bool fileExists = false;
byte csPin = 0;
File SdFile;
// Public Functions
void _sd_SetUp() {
delay(100);
#if defined (__MK20DX256__)
csPin = 14;
#elif defined (__MK64FX512__)
csPin = BUILTIN_SDCARD;
#else
#error "SD Card not supported, use Teensy v3.2 or v3.5";
#endif
if (!SD.begin(csPin)) { // CS Pin 14
// TODO - Send Telemetry Error instead - persistent
Serial.println("SD Card initialization failed!");
sdCardLoggingActive = false;
}
else {
sdCardLoggingActive = true;
char myFileName[13];
// New Method
// Format Date for Filename
char fileNamePrefix[9];
char fileNameSuffix[5] = ".csv";
char stryear[5], strmonth[3], strday[3];
sprintf(stryear, "%04d", year());
sprintf(strmonth, "%02d", month());
sprintf(strday, "%02d", day());
strcpy(fileNamePrefix, stryear);
strcat(fileNamePrefix, strmonth);
strcat(fileNamePrefix, strday);
sprintf(myFileName, "%s%s", fileNamePrefix, fileNameSuffix);
// Open the filename
fileExists = SD.exists(myFileName);
SdFile = SD.open(myFileName, FILE_WRITE);
delay(1);
sd_WriteLogHeader();
}
}
void _sd_WriteLogDate() {
// if the file opened okay, write to it:
if (SdFile && sdCardLoggingActive) {
String text = "";
// Format data and time
char strmonth[3], strday[3], strhour[3], strminute[3], strsecond[3];
sprintf(strmonth, "%02d", month());
sprintf(strday, "%02d", day());
sprintf(strhour, "%02d", hour());
sprintf(strminute, "%02d", minute());
sprintf(strsecond, "%02d", second());
text.concat(year()); text.concat("-"); text.concat(strmonth); text.concat("-"); text.concat(strday);
text.concat(","); text.concat(strhour); text.concat(":"); text.concat(strminute); text.concat(":"); text.concat(strsecond);
text.concat("."); text.concat(millis());
// Error Data
text.concat(","); text.concat(_error);
text.concat(","); text.concat(_error1);
// RPM Data
text.concat(","); text.concat(_inFlight);
text.concat(","); text.concat(_mainRPMSensorDetectedRPM);
text.concat(","); text.concat(_clutchRPMSensorDetectedRPM);
text.concat(","); text.concat(_headRPMCalculatedRPM);
// SBUS Quality Data
text.concat(","); text.concat(_totalFrames);
text.concat(","); text.concat(_lostFramesPercentage100Result);
text.concat(","); text.concat(_overallE2EQuality);
text.concat(","); text.concat(_badFramesPercentage100Result);
text.concat(","); text.concat(_channelsMaxHoldMillis100Resul);
text.concat(","); text.concat(_channel16chFrameSyncSuccessRate);
text.concat(","); text.concat(_wave1);
text.concat(","); text.concat(_wave2);
// Power / Charging Data
text.concat(","); text.concat(_recVoltage);
text.concat(","); text.concat(_becVoltage);
text.concat(","); text.concat(_becDischargeLoopAmps);
text.concat(","); text.concat(_becTemp);
text.concat(","); text.concat(_batteryVoltage);
text.concat(","); text.concat(_batteryDischargeLoopAmps);
text.concat(","); text.concat(_batteryDischargeTotalMAH);
float cells = cell[0] + cell[1];
text.concat(","); text.concat(cells);
text.concat(","); text.concat(cell[0]);
text.concat(","); text.concat(cell[1]);
text.concat(","); text.concat(_teensyVoltage);
// Temperature Data
text.concat(","); text.concat(_ambientTemp);
text.concat(","); text.concat(_canopyTemp);
text.concat(","); text.concat(_engineTemp);
// Vibration Data
text.concat(","); text.concat(_AccX);
text.concat(","); text.concat(_AccY);
text.concat(","); text.concat(_AccZ);
text.concat(","); text.concat(_AccTmp);
text.concat(","); text.concat(_GyrX);
text.concat(","); text.concat(_GyrY);
text.concat(","); text.concat(_GyrZ);
text.concat(","); text.concat(_vibrationStatus);
#ifdef DEBUG_SD_CARD
Serial.println(text);
#endif // DEBUG_SD_CARD
SdFile.println(text);
// Just flush the data, never close the file:
SdFile.flush();
}
else {
// if the file didn't open, print an error:
// TODO - Send Telemetry Error instead
Serial.println("error writting to SD Card");
}
}
// Private Functions
void sd_WriteLogHeader() {
if (SdFile) {
String text = "Date,Time,ERR,ErrD,InFlight,MAIN,CLUT,HEAD,TFCK,LFP,E2EQ,BFP,MCHM,16FS,Wav1,Wav2,RecV,BecV,BecA,BecT,BatV,BatA,BmAH,CELS,Cel1,Cel2,TeeV,AmbT,CanT,EngT,AccX,AccY,AccZ,AccTmp,GyrX,GyrY,GyrZ,VibS";
#ifdef DEBUG_SD_CARD
Serial.println(text);
#endif // DEBUG_SD_CARD
if (!fileExists) {
SdFile.println(text);
Serial.println("Header written to SD Card");
// Just flush the data, never close the file:
SdFile.flush();
}
}
else {
// if the file didn't open, print an error:
// TODO - Send Telemetry Error instead
Serial.println("error writting to SD Card");
}
}