Formula Student Electronics & Software
The code for the embedded software
Loading...
Searching...
No Matches
logging.cpp
Go to the documentation of this file.
1#include "logging.h"
2#include <iostream>
3#include <string>
4
7
8extern FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> can1;
10File myFile;
11
12
13constexpr size_t CANBUFSIZE = 100;
15volatile unsigned int lbuf_head = 0;
16volatile unsigned int lbuf_tail = 0;
17int t = 0;
18
19char file[16] = "Test1.csv";
20
21int count = 1;
22int count2 = 1;
23
24void getTimeStamp(LogEntry* logEntry)
25{
26 // read rtc (64bit, number of 32,768 kHz crystal periods)
27 uint64_t periods;
28 uint32_t hi1 = SNVS_HPRTCMR, lo1 = SNVS_HPRTCLR;
29 while (true)
30 {
31 uint32_t hi2 = SNVS_HPRTCMR, lo2 = SNVS_HPRTCLR;
32 if (lo1 == lo2 && hi1 == hi2)
33 {
34 periods = (uint64_t)hi2 << 32 | lo2;
35 break;
36 }
37 hi1 = hi2;
38 lo1 = lo2;
39 }
40
41 // calculate seconds and milliseconds
42 uint32_t ms = (1000 * (periods % 32768)) / 32768;
43 time_t sec = periods / 32768;
44
45 tm t = *gmtime(&sec); // calculate calendar data
46 logEntry->second = t.tm_sec;
47 logEntry->minute = t.tm_min;
48 logEntry->hour = t.tm_hour;
49 logEntry->day = t.tm_mday;
50 logEntry->month = t.tm_mon + 1;
51 logEntry->year = t.tm_year + 1900;
52 logEntry->millisecond = ms;
53}
54
55
57
58 //SPI.setCS(PIN_SD_CS);
59 //SPI.setMISO(PIN_SPI_MISO);
60 //SPI.setMOSI(PIN_SPI_MOSI);
61 //SPI.setSCK(PIN_SPI_SCK);
62 // Setup pinout
63 pinMode(PIN_SPI_MOSI, OUTPUT);
64 pinMode(PIN_SPI_MISO, INPUT);
65 pinMode(PIN_SPI_SCK, OUTPUT);
66 // Disable SPI devices
67 pinMode(PIN_SD_CS, OUTPUT);
68 digitalWrite(PIN_SD_CS, HIGH);
69
70
71 // Setup serial
72 Serial.begin(9600);
73
74 // Setup SD card
75 if (!SD.begin(BUILTIN_SDCARD)) {
76 Serial.println("initialization failed!");
77 return;
78 }
79
80 while(SD.exists(file)) {
81 snprintf(file, sizeof(file), "Test%d.csv", count);
82 count++;
83 }
84
85 myFile_setup = SD.open(file, FILE_WRITE);
86 myFile_setup.printf("TimeStamp, Current, Voltage, MinTmp, MaxTmp, AvgTmp, APPS1, APPS2, Brake, RPM, Motor Current, BAMO Temp, Motor Temp, Torque, battery Voltage \n");
87 myFile_setup.close();
88
89}
90void Logging::write_to_file(int current, int voltage, int mintmp, int maxtmp, int avgtmp, int apps1, int apps2, int brake, int rpm, int I_actual, int powerStageTmp, int motorTmp, int Torque, int motor_voltage, int battery_voltage) {
91
92 //Serial.print("Starting to write...");
93
94 myFile = SD.open(file, FILE_WRITE);
95
96 //getTimeStamp(entry);
97
98 //myFile.printf("%d-%02d-%02d %02d:%02d:%02d.%03u \n", entry->year, entry->month, entry->day, entry->hour, entry->minute, entry->second, entry->millisecond);
99
100
101 myFile.printf("%d, ", t);
102
103 myFile.printf("%d, ",current);
104
105 myFile.printf("%d, ",voltage);
106
107 myFile.printf("%d, ",mintmp);
108
109 myFile.printf("%d, ",maxtmp);
110
111 myFile.printf("%d, ",avgtmp);
112
113 myFile.printf("%d, ",apps1);
114
115 myFile.printf("%d, ",apps2);
116
117 myFile.printf("%d, ",brake);
118
119 myFile.printf("%d, ",rpm);
120
121 myFile.printf("%d, ",I_actual);
122
123 myFile.printf("%d, ",powerStageTmp);
124
125 myFile.printf("%d, ",motorTmp);
126
127 myFile.printf("%d, ",Torque);
128
129 myFile.printf("%d, ",motor_voltage);
130
131 myFile.printf("%d \n", battery_voltage);
132
133 myFile.close();
134
136}
int voltage
Definition main.cpp:60
int I_actual
Definition main.cpp:70
int avgtmp
Definition main.cpp:63
int apps2
Definition main.cpp:65
int battery_voltage
Definition main.cpp:78
int motor_voltage
Definition main.cpp:77
int brake
Definition main.cpp:66
int current
Definition main.cpp:59
int mintmp
Definition main.cpp:61
int maxtmp
Definition main.cpp:62
int apps1
Definition main.cpp:64
int rpm
Definition display.cpp:11
void write_to_file(int current, int voltage, int mintmp, int maxtmp, int avgtmp, int apps1, int apps2, int brake, int rpm, int I_actual, int powerStageTmp, int motorTmp, int Torque, int motor_voltage, int battery_voltage)
Definition logging.cpp:90
void setup_log()
Definition logging.cpp:56
LogEntry * entry
Definition logging.cpp:6
int count2
Definition logging.cpp:22
void getTimeStamp(LogEntry *logEntry)
Definition logging.cpp:24
volatile unsigned int lbuf_head
Definition logging.cpp:15
LogEntry lbuf[CANBUFSIZE]
Definition logging.cpp:14
File myFile
Definition logging.cpp:10
File myFile_setup
Definition logging.cpp:9
volatile unsigned int lbuf_tail
Definition logging.cpp:16
int t
Definition logging.cpp:17
char file[16]
Definition logging.cpp:19
constexpr size_t CANBUFSIZE
Definition logging.cpp:13
int count
Definition logging.cpp:21
LogEntry globalLogEntry
Definition logging.cpp:5
FlexCAN_T4< CAN1, RX_SIZE_256, TX_SIZE_16 > can1
Definition main.cpp:87
#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 LOGGING_PERIOD
Definition logging.h:23
Definition logging.h:27
int day
Definition logging.h:30
int month
Definition logging.h:29
int second
Definition logging.h:33
uint32_t millisecond
Definition logging.h:34
int minute
Definition logging.h:32
int year
Definition logging.h:28
int hour
Definition logging.h:31