This commit is contained in:
theo.canaud 2022-06-13 15:22:05 +02:00
commit c0e70cec4e
23 changed files with 741 additions and 60 deletions

16
.vscode/settings.json vendored
View file

@ -1,6 +1,20 @@
{
"C_Cpp.errorSquiggles": "Disabled",
"files.associations": {
"time.h": "c"
"time.h": "c",
"b2hd.h": "c",
"main.h": "c",
"stdio.h": "c",
"string.h": "c",
"initialparameters.h": "c",
"filegestion.h": "c",
"power.h": "c",
"getarray.h": "c",
"math.h": "c",
"limits": "c",
"*.tcc": "c",
"type_traits": "c",
"simulateflux.h": "c",
"pthread.h": "c"
}
}

Binary file not shown.

View file

@ -1,28 +0,0 @@
#include "b2hd.h"
typedef struct values
{
int allValues[1000][8];
}values;
values getArray(){
values tab;
int b=1;
char *token;
char buff[256];
fscanf(stdin, "%s", buff)
token = strtok(buff , ",");
int i = 0 ;
int j = 0 ;
while(token!=NULL){
t[j][i] = atoi(token);
token = strtok(NULL,",");
i++;
if(i==8){
j++;
i=0;
}
}
return tab;
}

5
Code-C/.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,5 @@
{
"files.associations": {
"power.h": "c"
}
}

7
Code-C/Makefile Normal file
View file

@ -0,0 +1,7 @@
CC = gcc
all:
$(CC) fileGestion.c getArray.c power.c queue.c simulateFlux.c main.c -lm -lpthread -o main
# $(CC) queue.c simulateFlux.c main.c -lm -lpthread -o main
./main < ../02400031.TXT

View file

@ -1,5 +1,9 @@
#include "b2hd.h"
int b2hd ()
/**
* @brief allow to transform all binary hex data send by the Vegetal Signal captor in a .TXT file into decimal values in a .csv
*
*/
void b2hd()
{
int i;
int indice[6];
@ -8,15 +12,15 @@ int b2hd ()
int32_t values[8];
uint32_t valbin[8];
quartet value;
while (fread(&buff, 26, 1, stdin)) {
fprintf(stdout , "%ld,", millis());
fprintf(stdout , "%d,", millis());
for (int i = 1; i < 9; i++){
/*buff[25] = '\0';*/
if (strncmp(buff, "#################\n", (size_t)18) == 0) {
if (!(fread(&buff2, 18, 1, stdin))) {
fprintf(stderr, "Erreur lesture après ###...#");
return(1);
break;
} else {
/*buff2[17] = '\0';*/
strncpy(buff, &buff[18], 8);
@ -29,7 +33,7 @@ int b2hd ()
value.octet4 = 0;
/*memcpy(&values[i], &value, sizeof(quartet));*/
valbin[i] = buff[3*i+1]*256*256*256 + buff[3*i+2]*256*256 + buff[3*i+3]*256;
memcpy(&values[i], &valbin[i], sizeof(int32_t));
memcpy(&values[i], &valbin[i], sizeof(uint32_t));
if(i<8){
fprintf(stdout, "%d,", values[i]/256);
}
@ -40,5 +44,5 @@ int b2hd ()
}
}
int main(int argc , char** argv){
b2hd();
b2hd();
}

View file

@ -1,16 +1,24 @@
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
/**
* @brief struct used to stock binary 32 bits data from the captor
*
*/
typedef struct {
uint8_t octet1;
uint8_t octet2;
uint8_t octet3;
uint8_t octet4;
} quartet;
int64_t millis()
{
struct timespec now;
timespec_get(&now, TIME_UTC);
return ((int64_t) now.tv_sec) * 1000 + ((int64_t) now.tv_nsec) / 1000000;
}
}

40
Code-C/fileGestion.c Normal file
View file

@ -0,0 +1,40 @@
#include "fileGestion.h"
/**
* @brief function that delete nRow lign in the beginning of the file rawData.csv . This function is necessary to not deal with the same ligns over and over
*
* @param nRow number of lign in the beginning of the file rawData.csv that has to be delete
*/
void clearRawData(int nRow){
char buffer[256];
FILE *f = fopen("newFile.csv","w+");
FILE *g = fopen("rawData.csv","r");
for(int i = 0; i < nRow; i++){ //first the program read the first nRow ligns of the csv file but do nothing
fgets(buffer , sizeof buffer , g);
}
while(1){ //then, till the end of the csv file it copy the lign to a new csv : newFile.csv
if(!fgets(buffer,sizeof buffer , g)) break;
fprintf(f,"%s",buffer);
}
remove("rawData.csv"); rename("newFile.csv", "rawData.csv"); //finally we remove the original file and rename the new one to replace rawData.csv
fclose(f); fclose(g);
}
/**
* @brief use to write the file powerData.csv that contaign all power calculous results
*
* @param powerArray
* @param nCol size of the power array, correspond to the number of captor used
*/
void writePowerData(double powerArray[], int nCol){
FILE *f = fopen("powerData.csv","a+");
for(int i = 0 ; i < nCol ; i++){
if( i < nCol-1){
fprintf(f, "%f , ", powerArray[i]);
} else {
fprintf(f, "%f\n", powerArray[i]);
}
}
fclose(f);
}

8
Code-C/fileGestion.h Normal file
View file

@ -0,0 +1,8 @@
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
void clearRawData(int N);
void writePowerData(double a[], int N);

89
Code-C/getArray.c Normal file
View file

@ -0,0 +1,89 @@
#include "getArray.h"
#include "fileGestion.h"
//#include <string.h>
long **get(int N, int M) /* Allocate the array */
{
/* Check if allocation succeeded. (check for NULL pointer) */
int i;
long **array;
array = (long **) malloc(N*sizeof(long *));
for(i = 0 ; i < N ; i++)
array[i] = (long *) malloc( M*sizeof(long) );
return array;
}
void fillArrayWithRawData(char *rawDataFileName,long** p, int N, int M) {
int i, j;
char *buffer;
size_t bufsize = 200;
buffer = (char *)malloc(bufsize * sizeof(char));
char* token;
FILE *f = fopen(rawDataFileName,"r");
for(i = 0 ; i < N ; i++){
if (!getline(&buffer, &bufsize, f)) break; // condition d'arret de la boucle si fichier fini
//printf("buffer : %s token : ",buffer);
j = 0;
while((token = strsep(&buffer,",")) != NULL){ // séparation valeur par virgule initiale : csv
//printf(token);
p[i][j] = atoi(token);
//printf("%d,", p[i][j]);
//printf("%d, " , p[i][j]);
j++;
}
//printf("\n\n");
}
fclose(f);
}
/**
* @brief print all the element of a bidimensionnal array p of shape : N x M
*/
void printArrayData(long** p, int N, int M) {
int i, j;
for(i = 0 ; i < N ; i++){
printf("line n°%d : " , i);
for(int j = 0 ; j < M ; j++){
printf("%d , " , p[i][j]);
if(j==(M-1)) printf("\n");
}
}
}
/**
* @brief verify if all the element of an array are not NULL, return
*
* @param p
* @param N
* @param M
* @return int
*/
bool checkArrayFullyFill(long **p, int N , int M){
for(int i = 0 ; i < N ; i++){
if(p[i][0] == '\0'){ return false; }
}
return true;
}
void freeArray(long **p, int N) {
int i;
for(i = 0 ; i < N ; i++)
free(p[i]);
free(p);
}
long **getRawDataArray(char* rawDataFileName , int N , int M){
long **p;
p = get(N, M);
fillArrayWithRawData(rawDataFileName,p ,N, M);
//printf("before test\n");
if(checkArrayFullyFill(p,N,M)==0){
//printf("after test 0\n");
clearRawData(N);
return p;
}
else{
//printf("after test 1\n");
return NULL;
}
}

11
Code-C/getArray.h Normal file
View file

@ -0,0 +1,11 @@
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <inttypes.h>
long **getRawDataArray(char *rawDataFileName, int N , int M);
void printArrayData(long** p, int N, int M);
void freeArray(long **p, int N);
bool checkArrayFullyFill(long **p, int N , int M);

View file

@ -0,0 +1,15 @@
#include <stdbool.h>
#include "queue.h"
extern bool rawDataWriteFlag , stopFlag;
extern int nRow;
extern int nCol;
extern double freqEch;
extern double period;
extern double invTimeBandWidth;
extern int selectionCaptors[];
extern int sizeSelectionArray;
extern Pqueue firstRawDataQueue;

BIN
Code-C/main Executable file

Binary file not shown.

51
Code-C/main.c Normal file
View file

@ -0,0 +1,51 @@
#include <pthread.h>
#include <stdbool.h>
#include "simulateFlux.h"
#include "power.h"
#include "initialParameters.h"
#include "queue.h"
bool rawDataWriteFlag = 0, stopFlag = 0;
int nRow = 100000;
int nCol = 9;
double freqEch = 250;
Pqueue firstRawDataQueue;
int selectionCaptors[] = {1,2,3,4,5,6,7,8};
int sizeSelectionArray = 8;
int cptData = 0;
int cptFile = 1;
double period = 0;
double invTimeBandWidth = 0;
void *threadCalcul(void *vargp){
printf("start thread calcul\n");
Pqueue rawDataQueue = firstRawDataQueue;
while(queueGetNextE(rawDataQueue) != NULL){
printf("wile calcul\n");
//pthread_mutex_lock(&mutex);
power(queueGetTabChar(rawDataQueue),nRow,nCol,period,invTimeBandWidth);
/*remove(queueGetTabChar(rawDataQueue));
rawDataQueue = queueRmFrstE(rawDataQueue);*/
//pthread_mutex_unlock(&mutex);
}
}
int main(int argc , char** argv){
period = 1 / freqEch;
invTimeBandWidth = 1 /(nRow * period);
firstRawDataQueue = queueCreateEmpty(); // change this for create empty
pthread_t rawData;
pthread_create(&rawData , NULL, simulateFlux, (void *)&rawData);
//pthread_t calcul;
//pthread_create(&calcul , NULL, threadCalcul, (void *)&calcul);
pthread_exit(NULL);
}

33
Code-C/power.c Normal file
View file

@ -0,0 +1,33 @@
#include "power.h"
#include "getArray.h"
#include "fileGestion.h"
#include "initialParameters.h"
#include "queue.h"
void powerCalculation(long **p, double powerArray[] , int N, int M , double period , double invTimeBandwidth){
for(int i = 0; i < M-1; i++){
int j = 0;
powerArray[i] = 0;
while(j < N-1){
double aire = ( pow(p[j][i+1],2) + pow(p[j+1][i+1],2) ) / 2 * period;
//printf("aire [%d,%d] : %f\n",j,i,aire);
powerArray[i] += aire;
j++;
}
powerArray[i] *= invTimeBandwidth;
//printf("%f\n", powerArray[i]);
}
}
bool power(char* rawDataFileName,int N , int M, double periode , double invTimeBandwidth){
long **p = getRawDataArray(rawDataFileName,N, M);
double pw[8];
if(p !=NULL){
powerCalculation(p,pw,N,M,periode,invTimeBandwidth);
writePowerData(pw,8);
freeArray(p,N);
return true;
}
else{
return false;
}
}

5
Code-C/power.h Normal file
View file

@ -0,0 +1,5 @@
#include <math.h>
#include <stdbool.h>
bool power(char* rawDataFileName,int N , int M, double periode , double invTimeBandwidth);
void *threadCalcul(void *vargp);

219
Code-C/queue.c Normal file
View file

@ -0,0 +1,219 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "queue.h"
/**
* @brief struct queue struct used for queueing string name
*
* @param charLen lenght of tabChar pointeur
* @param tabChar char array pointer
* @param pNextE point next element
*/
struct queue {
int charLen;
char* tabChar;
Pqueue pNextE;
};
typedef struct queue *Pqueue;
/**
* @brief create an empty element of queue
*
* @return Pqueue
*/
Pqueue queueCreateEmpty(){
Pqueue new = (Pqueue) malloc(sizeof(struct queue));
assert(new);
new->charLen = 0;
new->tabChar = NULL;
new->pNextE = NULL;
return new;
}
/************ SETTER ************/
/**
* @brief set char size array
*
* @param elem targeted element
* @param _charLen size of char array
*/
void queueSetCharLen(Pqueue elem, int _charLen){
assert(elem);
elem->charLen = _charLen;
}
/**
* @brief set the char array in the element
*
* @param elem targeted element
* @param _charLen char array size
* @param _tabChar pointer to static char array
*/
void queueSetTabChar(Pqueue elem, int _charLen, const char *_tabChar){
assert(elem);
assert(_tabChar);
elem->charLen = _charLen;
elem->tabChar = calloc(_charLen , sizeof(char));
for(int i = 0; i < _charLen; i++){
elem->tabChar[i] = _tabChar[i];
}
}
/**
* @brief set next pqueue element
*
* @param elem target element
* @param next next element
*/
void queueSetNextE(Pqueue elem , Pqueue next){
assert(elem);
assert(next);
elem -> pNextE = next;
}
/************ GETTER ************/
/**
* @brief switch to the next element
*
* @param elem current element
* @return Pqueue next element
*/
Pqueue queueGetNextE(Pqueue elem){
assert(elem);
return elem->pNextE;
}
/**
* @brief get the size char array
*
* @param elem targeted element
* @return int
*/
int queueGetCharLen(Pqueue elem){
assert(elem);
return elem->charLen;
}
/**
* @brief get the pointer of char array
*
* @param elem targeted elemnt
* @return char*
*/
char * queueGetTabChar(Pqueue elem){
assert(elem);
return elem->tabChar;
}
/************ ************/
/**
* @brief Create Element of queue
*
* @param lenChar size of char array that will be created
* @return Pqueue new element created
*/
Pqueue queueCreateE(int lenChar, const char* _tabChar){
Pqueue new = (Pqueue) malloc(sizeof(struct queue));
assert(new);
queueSetTabChar(new, lenChar, _tabChar);
new->pNextE = NULL;
return new;
}
/**
* @brief remove and free the last element of queue
*
* @param elem current element
* @return Pqueue current element
*/
Pqueue queueRmLastE(Pqueue elem){
assert(elem);
Pqueue tmp = elem, previous = NULL;
while(elem->pNextE != NULL){
previous = tmp;
tmp = queueGetNextE(tmp);
}
free(tmp->tabChar);
free(tmp);
previous->pNextE = NULL;
return elem;
}
/**
* @brief remove and free the first element of queue
*
* @param elem target to remove (the element need to be the first)
* @return Pqueue the second element after remove first
*/
Pqueue queueRmFrstE(Pqueue elem){
assert(elem);
Pqueue tmp = elem->pNextE;
free(elem->tabChar);
free(elem);
return tmp;
}
/**
* @brief delete the first value and return the next value
*
* @param elem
* @return Pqueue
*/
Pqueue queueNextDelFrst(Pqueue elem){
assert(elem);
Pqueue tmp = elem->pNextE;
queueRmFrstE(elem);
return tmp;
}
/**
* @brief add in the end of queue element with string parameter
*
* @param elem Pqueue will be added at the end of queue
* @param str the string will be bind at the element
* @param len the lenght of char array
* @return Pqueue pointer to the last element
*/
void queueAddLastQ(Pqueue elem, const char* str, int len){
assert(elem);
assert(str);
Pqueue next = elem, previous = NULL;
while(next->pNextE != NULL){
previous = next;
next = queueGetNextE(next);
}
previous = next;
next = queueCreateE(len,str);
queueSetNextE(previous,next);
}
/**
* @brief Print targeted element
*
* @param elem targeted element
*/
void queuePrintE(Pqueue elem){
Pqueue next = queueGetNextE(elem);
printf("\nFile Name : %s \n(size of the file name : %d)\n",elem -> tabChar , elem -> charLen );
if(next != NULL){
printf("Next File : %s\n", next ->tabChar);
}
else{
printf("No nextFile existing (null)\n");
}
}
void queuePrintWholeQ(Pqueue firstE){
queuePrintE(firstE);
Pqueue elem = firstE;
while(queueGetNextE(elem) != NULL){
elem = queueGetNextE(elem);
queuePrintE(elem);
}
}

27
Code-C/queue.h Normal file
View file

@ -0,0 +1,27 @@
typedef struct queue *Pqueue;
//Constructors
Pqueue queueCreateE(int lenChar, const char* _tabChar);
Pqueue queueCreateEmpty();
//Getters
Pqueue queueGetNextE(Pqueue elem);
int queueGetCharLen(Pqueue elem);
char* queueGetTabChar(Pqueue elem);
//Setters
void queueSetCharLen(Pqueue elem, int _charLen);
void queueSetTabChar(Pqueue elem, int _charLen, const char *_tabChar);
void queueSetNextE(Pqueue elem , Pqueue next);
void queueAddLastQ(Pqueue elem, const char* str, int len);
//Removers
Pqueue queueRmLastE(Pqueue elem);
Pqueue queueRmFrstE(Pqueue elem);
Pqueue queueNextDelFrst(Pqueue elem);
//print function
void queuePrintE(Pqueue elem);
void queuePrintWholeQ(Pqueue firstE);

6
Code-C/rmRawData.sh Normal file
View file

@ -0,0 +1,6 @@
#!/bin/bash
cd ../RawDataFiles
for i in *
do
rm $i
done

134
Code-C/simulateFlux.c Normal file
View file

@ -0,0 +1,134 @@
#include "simulateFlux.h"
#include "initialParameters.h"
#include "queue.h"
char* convertIntegerToChar(int N)
{
// Count digits in number N
int m = N;
int digit = 0;
while (m) {
digit++;
m /= 10;
}
char* arr;
char arr1[digit];
arr = (char*)malloc(digit);
int index = 0;
while (N) {
arr1[++index] = N % 10 + '0';
N /= 10;
}
int i;
for (i = 0; i < index; i++) {
arr[i] = arr1[index - i];
}
arr[i] = '\0';
return (char*)arr;
}
char *createNewRawDataFileName(){
char *fileName = "../RawDataFiles/RawData";
char *extension = ".csv\0";
char *fileNumber = convertIntegerToChar(cptFile);
char fileNameNumber[strlen(fileName)+strlen(fileNumber)];
char *fullFileName = malloc((strlen(fileNameNumber)+strlen(extension)) * sizeof(char*));
strcpy( fileNameNumber, fileName );
strcat( fileNameNumber, fileNumber );
strcpy( fullFileName, fileNameNumber );
strcat( fullFileName, extension );
return fullFileName;
}
int intInArray(int number , int *array , int N){
for(int i = 0 ; i < N ; i++){
if(array[i] == number) return 0;
}
return -1;
}
int maxInArray(int *array , int N){
int max = 0;
for(int i = 0 ; i < N ; i++){
if(array[i]>max) max = array[i];
}
return max;
}
int64_t millis()
{
struct timespec now;
timespec_get(&now, TIME_UTC);
return ((int64_t) now.tv_sec) * 1000 + ((int64_t) now.tv_nsec) / 1000000;
}
bool writeOneRawData(FILE *rawDataFile){
char buff[26];
char buff2[18];
int32_t values[8];
uint32_t valbin[8];
quartet value;
if(fread(&buff, 26, 1, stdin)) {
fprintf(rawDataFile , "%d,", millis());
if (strncmp(buff, "#################\n", (size_t)18) == 0) {
if (!(fread(&buff2, 18, 1, stdin))) {
fprintf(stderr, "Erreur lecture après ###...#");
return 2;
} else {
strncpy(buff, &buff[18], 8);
strncpy(&buff[8], buff2, 18);
}
}
int maxCApteurNb = maxInArray(selectionCaptors,sizeSelectionArray);
for (int i = 1; i < 9; i++){
if(intInArray(i,selectionCaptors,sizeSelectionArray)==0){
value.octet1 = buff[3*i+1];
value.octet2 = buff[3*i+2];
value.octet3 = buff[3*i+3];
value.octet4 = 0;
valbin[i] = buff[3*i+1]*256*256*256 + buff[3*i+2]*256*256 + buff[3*i+3]*256;
memcpy(&values[i], &valbin[i], sizeof(uint32_t));
if(i==maxCApteurNb){
fprintf(rawDataFile, "%d\n", values[i]/256);
}
else{
fprintf(rawDataFile, "%d,", values[i]/256);
}
}
}
cptData++;
//sleep(0.004); //simul la freq ech
return true;
}
else {
return false;
}
}
void *simulateFlux(void *vargp){
printf("start thread simul\n");
char *fileName = createNewRawDataFileName();
FILE *rawDataFile = fopen(fileName,"w+");
while(writeOneRawData(rawDataFile)){
if(cptData == nRow){
printf("in if\n");
cptData = 0;
cptFile++;
//create struct here
queueAddLastQ(firstRawDataQueue , fileName , strlen(fileName));
//prepare next file now
fileName = createNewRawDataFileName();
FILE *rawDataFile = fopen(fileName,"w+");
}
}
}

21
Code-C/simulateFlux.h Normal file
View file

@ -0,0 +1,21 @@
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <time.h>
typedef struct {
uint8_t octet1;
uint8_t octet2;
uint8_t octet3;
uint8_t octet4;
} quartet;
extern int cptData;
extern int cptFile;
char *convertIntegerToChar(int N);
bool writeOneRawData(FILE *f);
void *simulateFlux(void *vargp);

View file

@ -1,5 +1,5 @@
import numpy as np
#import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
import struct
@ -9,7 +9,7 @@ import struct
AllRes = [[],[],[],[],[],[],[],[]]
link = "02400007.TXT"
link = "02400031.TXT"
f = open(link , "rb")#ouvertuture du fichier txt traiter
@ -47,7 +47,7 @@ while True:
f.close()#fermeturedu fichier txt
"""fig, (ax1, ax2) = plt.subplots(2, 1) #Création de la figure à 2 plots
fig, (ax1, ax2) = plt.subplots(2, 1) #Création de la figure à 2 plots
Fe = 250000 #Fréquence d'échantillonage
tstep = 1 / Fe #Time spacing
@ -70,8 +70,8 @@ sp = np.fft.fft(y,Fe)
f = np.fft.fftfreq(Fe,tstep) #axe des abscisses: fréquence
#Définition des courbes des plots
ax1.plot(t,y)
ax1.plot(t,y) # print y(t)
ax2.plot(f,abs(sp))
#Lancement de l'affichage du plot
plt.show()"""
plt.show()

View file

@ -1,26 +1,38 @@
import math
import numpy as np
# import matplotlib.pyplot as plt
import struct
import matplotlib.pyplot as plt
import csv
#"C:\Users\quent\OneDrive\Bureau\ENSC\TransD\Framboisier\02400001.TXT"
#! /usr/bin/env python3
# -*- coding: UTF-8 -*-
link = "test.txt"
f = open(link , "rb")#ouvertuture du fichier txt traiter
taille = 5
freq = 250000
taille = 100000
freq = 250
periode = 1 / freq
temps = periode * taille
resultat = np.zeros(taille)
for i in range(taille):
resultat[i] = f.readline() #on sait que chaque ligne corespond à une mesure
f.close()
resultat = np.zeros([taille,8])
j = 0
# opening the CSV file
with open('rawData.csv', mode ='r') as file:
# reading the CSV file
csvFile = csv.reader(file, delimiter=",")
# displaying the contents of the CSV file
for lines in csvFile:
line = np.zeros(8)
for i in range(8):
line[i] = lines[i+1]
resultat[j] = line
j+=1
if(j == taille) : break
#print(resultat.shape)
resultat*=resultat
aire = 0
for i in range(taille-1):
aire += (resultat[i]+resultat[i+1]) / 2 * periode
f = open("puissance.txt" ,"a")
f.writelines(str(aire)+"\n")
f.close()
for i in range(8):
aire = 0
flag = False
for j in range(taille-1):
aire += (resultat[j,i]+resultat[j+1,i]) / 2 * periode
if(aire!=0 and flag == False):
flag = True
print(i , j , resultat[j,i] , resultat[j+1,i], aire)
res = aire/temps
print("power c°" + str(i+1) + " : " + str(res))