-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobals.c
More file actions
150 lines (128 loc) · 6.51 KB
/
globals.c
File metadata and controls
150 lines (128 loc) · 6.51 KB
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
/*
@file globals.c
@brief Shared variables and types to be used with this project (implemented for XMC4700 and DAVE)
@version 1.0
@date 2020-02-20
@author Rene Santeler @ MCI 2020/21
*/
#include <DAVE.h>
#include <math.h>
#include <globals.h>
/* SYSTEM VARIABLEs */
volatile uint8_t main_trigger = 0; // Trigger for main loop. Is set every time by Adc_Measurement_Handler (used in main and measure)
/* MEASUREMENTs */
volatile uint32_t measurementCounter = 0; // Count of executed measurements
volatile uint8_t monitorSensorIdx = 0;
// Sensor 1 Front
int_buffer_t s1_buf_0raw [S_BUF_SIZE] = { 0 }; // all elements 0
float_buffer_t s1_buf_1filter[S_BUF_SIZE] = { 0.0 };
float_buffer_t s1_buf_2conv [S_BUF_SIZE] = { 0.0 };
#define S1_FILENAME_CURLEN 6
char s1_filename_cal[STR_SPEC_MAXLEN] = "S1.CAL"; // Note: File extension must be 3 characters long or an error will occur (fatfs lib?)
volatile sensor sensor1 = {
.index = 0,
.name = "Front",
.adcChannel = &ADC_MEASUREMENT_Channel_A,
.bufIdx = 0,
.bufMaxIdx = S_BUF_SIZE-1,
.bufRaw = (int_buffer_t*)&s1_buf_0raw,
.bufFilter = (float_buffer_t*)&s1_buf_1filter,
.bufConv = (float_buffer_t*)&s1_buf_2conv,
.originPoint = 0,
.operatingPoint = 0,
.errorOccured = 0,
.errorThreshold = 3900, // Raw value above this threshold will be considered invalid ( errorOccured=1 ). The stored value will be linear interpolated on the last Filter values.
.avgFilterInterval = 5,
.avgFilterSum = 0.0,
.fitFilename = s1_filename_cal,
.fitFilename_curLen = S1_FILENAME_CURLEN,
.fitOrder = 2,
.fitCoefficients[0] = 0,
.fitCoefficients[1] = 0,
.fitCoefficients[2] = 0,
.fitCoefficients[3] = 0
};
// Sensor 2 Rear
int_buffer_t s2_buf_0raw [S_BUF_SIZE] = { 0 }; // all elements 0
float_buffer_t s2_buf_1filter[S_BUF_SIZE] = { 0.0 };
float_buffer_t s2_buf_2conv [S_BUF_SIZE] = { 0.0 };
#define S2_FILENAME_CURLEN 6
char s2_filename_cal[STR_SPEC_MAXLEN] = "S2.CAL"; // Note: File extension must be 3 characters long or an error will occur (fatfs lib?)
volatile sensor sensor2 = {
.index = 1,
.name = "Rear",
.adcChannel = &ADC_MEASUREMENT_Channel_B,
.bufIdx = 0,
.bufMaxIdx = S_BUF_SIZE-1,
.bufRaw = (int_buffer_t*)&s2_buf_0raw,
.bufFilter = (float_buffer_t*)&s2_buf_1filter,
.bufConv = (float_buffer_t*)&s2_buf_2conv,
.originPoint = 0,
.operatingPoint = 0,
.errorOccured = 0,
.errorThreshold = 3900, // Raw value above this threshold will be considered invalid ( errorOccured=1 ). The stored value will be linear interpolated on the last Filter values.
.avgFilterInterval = 5,
.avgFilterSum = 0.0,
.fitFilename = s2_filename_cal,
.fitFilename_curLen = S2_FILENAME_CURLEN,
.fitOrder = 2,
.fitCoefficients[0] = 0,
.fitCoefficients[1] = 0,
.fitCoefficients[2] = 0,
.fitCoefficients[3] = 0
};
// Array of all sensor objects to be used in measurement handler
volatile sensor* sensors[SENSORS_SIZE] = {&sensor1, &sensor2};
/* LOG FIFO */
char filename_rec[FILENAME_REC_MAXLEN] = "log.csv";
uint8_t filename_rec_curLength = 8;
// Also see FIFO_BLOCK_SIZE, FIFO_BLOCKS and FIFO_WRITEBUFIDX_BITS -> defined in header file
volatile uint8_t volatile * volatile fifo_buf = NULL; // The pointer to the FIFO buffer itself. Will be allocated by malloc at start of log
volatile uint16_t fifo_writeBufIdx = 0; // The index inside the fifo_buf where the next value will be written to by the measurement handler
volatile uint8_t fifo_writeBlock = 0; // The current block (multiple of BLOCK_SIZE) inside the fifo_buf the measurement handler is writing to
volatile uint8_t fifo_recordBlock = 0; // The current block (multiple of BLOCK_SIZE) inside the fifo_buf that shall be written to SD-Card (as soon as finBlock==logBlock)
volatile uint8_t fifo_finBlock[FIFO_BLOCKS] = {0}; // An array with an element for every Block in fifo_buf. Each corresponding element represents if a block is ready to recorded
///* MENU AND USER INTERFACE */
sdStates sdState = sdNone;
volatile measureModes measureMode = measureModeMonitoring;
///* FUNCTIONS ---------------------------------------------------------------------------------------------------------------------------- */
void delay_ms(uint32_t ms){
uint32_t targetMicroSec = SYSTIMER_GetTime() + (ms*1000);
while(targetMicroSec > SYSTIMER_GetTime())
__NOP(); // do nothing
}
float poly_calc (float c_x, float* f_Coefficients, uint8_t order){
/// Calculate the result of an polynomial based on the x value, the coefficients and the order (1=linear(2 coefficients used), 2=square(3 coefficients used)...)
/// NOTE: A specialized version (inline and only for sensor struct's) of this code is implemented in measure.c because inline functions must be static to avoid compiler error (https://stackoverflow.com/questions/28606847/global-inline-function-between-two-c-files)
// Temporary sum and result value, marked to be stored in a register to enhance speed (multiple successive access!). Use first coefficient (constant) as init value.
register float result = f_Coefficients[0];
// Calculate every term and add it to the result
for(uint8_t i = 1; i < order+1; i++)
result += f_Coefficients[i] * powf(c_x, i);
// Return result
return result;
}
float_buffer_t measure_movAvgFilter_clean(sensor* sens, uint16_t filterInterval, uint8_t compFilterOrder){
/// Implementation of an moving average filter on an ring-buffer. This version is used to reevaluate the sum variable if the filter order is changed or the buffer is not 0'd when the filter starts.
/// It can also be used to calculate the sum over the filter while ignoring zeroed out values (decrease order/divider with every 0 found during sum).
/// Note: This resets the sum and adds all elements between the oldest and newest entry to it before dividing.
/// Return the filtered value (only needed if the value is processes async of buffers - see dashboard display code)
// Get index of oldest element, which shall be removed (current index minus filter order with roll-over check)
int32_t oldIdx = sens->bufIdx - sens->avgFilterInterval;
if(oldIdx < 0) oldIdx += sens->bufMaxIdx;
// Subtract oldest element and add newest to sum
sens->avgFilterSum = 0;
for(int i = sens->bufIdx; i != oldIdx; i--){
if(i<0) i += sens->bufMaxIdx+1;
sens->avgFilterSum += sens->bufRaw[i];
}
// Calculate average and return it
if(compFilterOrder != 0)
sens->bufFilter[sens->bufIdx] = sens->avgFilterSum / compFilterOrder;
else if(filterInterval != 0)
sens->bufFilter[sens->bufIdx] = sens->avgFilterSum / filterInterval;
else
sens->bufFilter[sens->bufIdx] = 0;
// Return result
return sens->bufFilter[sens->bufIdx];
}