Init commit
- Moved compilation to cmake - Added library - TODO: testing std_dev logic
This commit is contained in:
parent
aefed3bd1f
commit
00d4a5937d
3
.gitignore
vendored
3
.gitignore
vendored
@ -52,3 +52,6 @@ Module.symvers
|
|||||||
Mkfile.old
|
Mkfile.old
|
||||||
dkms.conf
|
dkms.conf
|
||||||
|
|
||||||
|
/.vscode
|
||||||
|
|
||||||
|
/build/*
|
||||||
13
CMakeLists.txt
Normal file
13
CMakeLists.txt
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
|
||||||
|
# Set the project name
|
||||||
|
project(EmbeddedLibraryProject)
|
||||||
|
|
||||||
|
# Add the executable
|
||||||
|
add_executable(main main.c)
|
||||||
|
|
||||||
|
# Add the libraries
|
||||||
|
add_library(dataAcquisition libraries/dataAcquisition.c)
|
||||||
|
|
||||||
|
# Link the libraries to the executable
|
||||||
|
target_link_libraries(main dataAcquisition m)
|
||||||
169
libraries/dataAcquisition.c
Normal file
169
libraries/dataAcquisition.c
Normal file
@ -0,0 +1,169 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
// Variable definition
|
||||||
|
|
||||||
|
static float **readings;
|
||||||
|
|
||||||
|
int sensorsNumber;
|
||||||
|
int slidingWindowSize;
|
||||||
|
void initializeReadings(int numSensors, int windowSize) {
|
||||||
|
readings = (float **)malloc(numSensors * sizeof(float *));
|
||||||
|
if (readings == NULL) {
|
||||||
|
perror("Failed to allocate memory for readings");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < numSensors; i++) {
|
||||||
|
readings[i] = (float *)calloc(windowSize, sizeof(float));
|
||||||
|
if (readings[i] == NULL) {
|
||||||
|
perror("Failed to allocate memory for sensor readings");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Assign sensorsNumber and slidingWindowSize
|
||||||
|
setSensorsNumber(numSensors);
|
||||||
|
setSlidingWindowSize(windowSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
void freeReadings() {
|
||||||
|
for (int i = 0; i < sensorsNumber; i++) {
|
||||||
|
free(readings[i]);
|
||||||
|
}
|
||||||
|
free(readings);
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int sensorsNumber;
|
||||||
|
int slidingWindowSize;
|
||||||
|
} dataAcquisition;
|
||||||
|
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
// Get the number of sensors
|
||||||
|
int getSensorsNumber() {
|
||||||
|
return sensorsNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the sliding window size
|
||||||
|
int getSlidingWindowSize() {
|
||||||
|
return slidingWindowSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the number of sensors
|
||||||
|
void setSensorsNumber(int number) {
|
||||||
|
sensorsNumber = number;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the sliding window size
|
||||||
|
void setSlidingWindowSize(int size) {
|
||||||
|
slidingWindowSize = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Control on the fullness of the sliding window
|
||||||
|
bool isFull(int sensorIndex) {
|
||||||
|
for (int i = 0; i < slidingWindowSize; i++) {
|
||||||
|
if (readings[sensorIndex][i] == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add a reading to the readings array
|
||||||
|
void addReading(float value, int sensorIndex) {
|
||||||
|
if (isFull(sensorIndex)) {
|
||||||
|
for (int i = 0; i < slidingWindowSize - 1; i++) {
|
||||||
|
readings[sensorIndex][i] = readings[sensorIndex][i + 1];
|
||||||
|
}
|
||||||
|
readings[sensorIndex][slidingWindowSize - 1] = value;
|
||||||
|
} else {
|
||||||
|
for (int i = 0; i < slidingWindowSize; i++) {
|
||||||
|
if (readings[sensorIndex][i] == 0) {
|
||||||
|
readings[sensorIndex][i] = value;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
float getAverageOnSensor(int sensorIndex) {
|
||||||
|
if(isFull(sensorIndex) == false){
|
||||||
|
printf("The sliding window is not full\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
float sum = 0;
|
||||||
|
for (int i = 0; i < slidingWindowSize; i++) {
|
||||||
|
sum += readings[sensorIndex][i];
|
||||||
|
}
|
||||||
|
return sum / slidingWindowSize;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
float getAverageOnAllSensors() {
|
||||||
|
float sum = 0;
|
||||||
|
for (int i = 0; i < sensorsNumber; i++) {
|
||||||
|
sum += getAverageOnSensor(i);
|
||||||
|
}
|
||||||
|
return sum / sensorsNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
float getOverallAverage() {
|
||||||
|
float sum = 0;
|
||||||
|
int totalReadings = 0;
|
||||||
|
for (int i = 0; i < sensorsNumber; i++) {
|
||||||
|
if (!isFull(i)) {
|
||||||
|
printf("The sliding window for sensor %d is not full\n", i);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
for (int j = 0; j < slidingWindowSize; j++) {
|
||||||
|
sum += readings[i][j];
|
||||||
|
totalReadings++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sum / totalReadings;
|
||||||
|
}
|
||||||
|
|
||||||
|
float getStandardDeviationOnSensor(int sensorIndex) {
|
||||||
|
if(isFull(sensorIndex) == false){
|
||||||
|
printf("The sliding window is not full\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
float sum = 0;
|
||||||
|
float average = getAverageOnSensor(sensorIndex);
|
||||||
|
for (int i = 0; i < slidingWindowSize; i++) {
|
||||||
|
sum += pow(readings[sensorIndex][i] - average, 2);
|
||||||
|
}
|
||||||
|
return sqrt(sum / slidingWindowSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
float getStandardDeviationOnAllSensors() {
|
||||||
|
float sum = 0;
|
||||||
|
for (int i = 0; i < sensorsNumber; i++) {
|
||||||
|
sum += getStandardDeviationOnSensor(i);
|
||||||
|
}
|
||||||
|
return sum / sensorsNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
float getOverallStandardDeviation() {
|
||||||
|
float sum = 0;
|
||||||
|
int totalReadings = 0;
|
||||||
|
for (int i = 0; i < sensorsNumber; i++) {
|
||||||
|
if (!isFull(i)) {
|
||||||
|
printf("The sliding window for sensor %d is not full\n", i);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
for (int j = 0; j < slidingWindowSize; j++) {
|
||||||
|
sum += pow(readings[i][j] - getOverallAverage(), 2);
|
||||||
|
totalReadings++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sqrt(sum / totalReadings);
|
||||||
|
}
|
||||||
36
libraries/dataAcquisition.h
Normal file
36
libraries/dataAcquisition.h
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#ifndef DATA_ACQUISITION_H
|
||||||
|
#define DATA_ACQUISITION_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
// Struttura per la gestione dei dati di acquisizione
|
||||||
|
typedef struct {
|
||||||
|
int sensorsNumber;
|
||||||
|
int slidingWindowSize;
|
||||||
|
} dataAcquisition;
|
||||||
|
|
||||||
|
// Funzioni per la gestione dei dati di acquisizione
|
||||||
|
void initializeReadings();
|
||||||
|
void freeReadings();
|
||||||
|
|
||||||
|
// Funzioni getter
|
||||||
|
int getSensorsNumber();
|
||||||
|
int getSlidingWindowSize();
|
||||||
|
bool isFull(int sensorIndex);
|
||||||
|
|
||||||
|
// Funzioni setter
|
||||||
|
void addReading(float value, int sensorIndex);
|
||||||
|
void setSensorsNumber(int number);
|
||||||
|
void setSlidingWindowSize(int size);
|
||||||
|
|
||||||
|
float getAverageOnSensor(int sensorIndex);
|
||||||
|
float getAverageOnAllSensors();
|
||||||
|
float getOverallAverage();
|
||||||
|
|
||||||
|
float getStandardDeviationOnSensor(int sensorIndex);
|
||||||
|
float getStandardDeviationOnAllSensors();
|
||||||
|
float getStandardDeviationOnAllSensors();
|
||||||
|
|
||||||
|
#endif // DATA_ACQUISITION_H
|
||||||
34
main.c
Normal file
34
main.c
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "libraries/dataAcquisition.c"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
initializeReadings(5, 100);
|
||||||
|
|
||||||
|
printf("Sensors Number: %d\n", getSensorsNumber());
|
||||||
|
addReading(10, 0);
|
||||||
|
printf("Reading: %f\n", readings[0][0]);
|
||||||
|
addReading(20, 1);
|
||||||
|
printf("Reading: %f\n", readings[1][0]);
|
||||||
|
addReading(30, 2);
|
||||||
|
printf("Reading: %f\n", readings[2][0]);
|
||||||
|
addReading(40, 3);
|
||||||
|
printf("Reading: %f\n", readings[3][0]);
|
||||||
|
addReading(50, 4);
|
||||||
|
printf("Reading: %f\n", readings[4][0]);
|
||||||
|
getOverallAverage();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
while(1){
|
||||||
|
addReading(10, 0);
|
||||||
|
addReading(20, 1);
|
||||||
|
addReading(30, 2);
|
||||||
|
addReading(40, 3);
|
||||||
|
addReading(50, 4);
|
||||||
|
printf("Average All: %f\n", getOverallAverage());
|
||||||
|
printf("Standard Deviation All: %f\n", getStandardDeviationOnAllSensors());
|
||||||
|
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user