Formula Student Electronics & Software
The code for the embedded software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
write_data.cpp
Go to the documentation of this file.
1
6#include <CSVFile.h>
7#include <SdFat.h>
8
9#include "can.h"
10#include "debug.h"
11#include "elapsedMillis.h"
12#include "write_data.h"
13
14extern FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> can1;
15
16extern CAN_message_t Nact_filtered;
17extern CAN_message_t Vout_msg;
18extern CAN_message_t Iq_cmd_msg;
19extern CAN_message_t Iq_actual_msg;
20extern CAN_message_t Mout_msg;
21extern CAN_message_t I_lim_inuse_msg;
22extern CAN_message_t I_actual_filtered_msg;
23extern CAN_message_t Tpeak_msg;
24extern CAN_message_t Imax_peak_msg;
25extern CAN_message_t I_con_eff_msg;
26
27extern int soc;
28extern int current;
29extern int packVoltage;
30extern int Nact;
31extern int Vout;
32extern int Iq_cmd;
33extern int Iq_actual;
34extern int Mout;
35extern int I_lim_inuse;
36extern int I_actual_filtered;
37extern int Tpeak;
38extern int Imax_peak;
39extern int I_con_eff;
40extern int motorTemp;
41extern int powerStageTemp;
42
43SdFat sd;
44CSVFile csv;
45
46int t = 0;
47
48void setup_csv() {
49 // Setup pinout
50 pinMode(PIN_SPI_MOSI, OUTPUT);
51 pinMode(PIN_SPI_MISO, INPUT);
52 pinMode(PIN_SPI_SCK, OUTPUT);
53 // Disable SPI devices
54 pinMode(PIN_SD_CS, OUTPUT);
55 digitalWrite(PIN_SD_CS, HIGH);
56
57#if PIN_OTHER_DEVICE_CS > 0
58 pinMode(PIN_OTHER_DEVICE_CS, OUTPUT);
59 digitalWrite(PIN_OTHER_DEVICE_CS, HIGH);
60#endif // PIN_OTHER_DEVICE_CS > 0
61
62 // Setup serial
63 Serial.begin(9600);
64 while (!Serial) { /* wait for Leonardo */
65 }
66 // Setup SD card
67 if (!sd.begin(PIN_SD_CS, SD_CARD_SPEED)) {
68 Serial.println("SD card begin error");
69 return;
70 }
71
72 can1.write(Nact_filtered);
73 can1.write(Vout_msg);
74 can1.write(Iq_cmd_msg);
75 can1.write(Iq_actual_msg);
76 can1.write(Mout_msg);
77 can1.write(I_lim_inuse_msg);
79 can1.write(Tpeak_msg);
80 can1.write(Imax_peak_msg);
81 can1.write(I_con_eff_msg);
82}
83
84void initSdFile(char* filename) {
85 if (sd.exists(filename) && !sd.remove(filename)) {
86 Serial.println("Failed init remove file");
87 return;
88 }
89 // Important note!
90 // You should use flag O_RDWR even if you use CSV File
91 // only for writting.
92 if (!csv.open(filename, O_WRONLY | O_APPEND | O_CREAT)) {
93 Serial.println("Failed open file");
94 }
95}
96
97void write() {
98 // Data in CSV file is stored in lines.
99 // Each line have some (or zero) fields.
100 // First you should add line and next
101 // add fields. After you can add next line.
102
103 // Each line is ended by end line character '\n',
104 // (UNIX style - without '\r').
105 // You shouldn't use "println" method (and similars).
106 // The fields are separated by delimiter ';'.
107 // You can change this character in source file.
108 // Your CSV file shouldn't contain this characters.
109
110 // Important note!
111 // You should use flag O_RDWR for initialize CSV File even if you use CSV File
112 // only for writting.
113
114 initSdFile("dataLogging.csv");
115
116 // At the begin of file we don't need
117 // add new line.
118
119 // 2. Number field with non-fixed size.
120 // Use this field if you don't need
121 // edit field's value later.
122 // Support only positive integers.
123 // It is function designed for write
124 // line numbers.
125
126 csv.addField(t);
127
128 // N act (filt) - 0xA8
129 csv.addField(Nact);
130
131 // Vout - 0x8A
132 csv.addField(Vout);
133
134 // Iq cmd - 0x26
135 csv.addField(Iq_cmd);
136
137 // Iq actual - 0x27
138 csv.addField(Iq_actual);
139
140 // M out - 0xA0
141 csv.addField(Mout);
142
143 // I lim inuse - 0x48
144 csv.addField(I_lim_inuse);
145
146 // I act (filt) - 0x5F
147 csv.addField(I_actual_filtered);
148
149 // T-peak - 0xF0
150 csv.addField(Tpeak);
151
152 // Imax pk - 0xC4
153 csv.addField(Imax_peak);
154
155 // I con eff - 0xC5
156 csv.addField(I_con_eff);
157
158 // T-motor - 0x49
159 csv.addField(motorTemp);
160
161 // T-igbt - 0x4A
162 csv.addField(powerStageTemp);
163
164 // SoC
165 csv.addField(soc);
166
167 // V bat
168 csv.addField(packVoltage);
169
170 // I bat
171 csv.addField(current);
172
173 csv.addLine();
174
175 // We don't add empty line at the end of file.
176 // CSV file shouldn't end by '\n' char.
177
178 // Don't forget close the file.
179 csv.close();
180
181 t++;
182
183 // After this operations your CSV file should look like this
184 // ('\0' is null character):
185
186 /*0;65535;3444\n
187 */
188}
#define SD_CARD_SPEED
Definition logging.h:19
#define PIN_SPI_MISO
Definition logging.h:10
#define PIN_SPI_MOSI
Definition logging.h:9
#define PIN_SD_CS
Definition logging.h:11
#define PIN_OTHER_DEVICE_CS
Definition logging.h:15
int Nact
Definition can.cpp:33
CAN_message_t Iq_cmd_msg
CAN_message_t Imax_peak_msg
int Iq_actual
Definition can.cpp:39
int packVoltage
Definition display.cpp:22
int powerStageTemp
Definition main.cpp:71
CAN_message_t Tpeak_msg
CSVFile csv
CAN_message_t I_con_eff_msg
int Mout
Definition can.cpp:32
int motorTemp
Definition main.cpp:72
int Iq_cmd
Definition can.cpp:36
CAN_message_t Iq_actual_msg
CAN_message_t Vout_msg
int soc
Definition display.cpp:12
int Imax_peak
Definition can.cpp:37
CAN_message_t I_actual_filtered_msg
SdFat sd
CAN_message_t I_lim_inuse_msg
void setup_csv()
void write()
CAN_message_t Mout_msg
int current
Definition main.cpp:59
int Vout
Definition can.cpp:34
int I_actual_filtered
Definition can.cpp:41
int t
int I_lim_inuse
Definition can.cpp:40
CAN_message_t Nact_filtered
FlexCAN_T4< CAN1, RX_SIZE_256, TX_SIZE_16 > can1
Definition main.cpp:87
void initSdFile(char *filename)
int I_con_eff
Definition can.cpp:38
int Tpeak
Definition can.cpp:35