From ad736b1a0ee04ef052de173c2773c8cabffd5043 Mon Sep 17 00:00:00 2001 From: Geoffrey Frogeye Date: Sun, 29 Apr 2018 09:38:49 +0200 Subject: [PATCH] Revamped debug interface --- chef/Makefile | 7 ++-- chef/src/debug.c | 96 +++++++++++++++++++++++++++++++++------------ chef/src/debug.h | 21 +++++++--- chef/src/local.c | 25 +++++++----- chef/src/position.c | 21 ++++++++-- chef/src/position.h | 12 +----- chef/src/premier.c | 28 +++++++------ 7 files changed, 142 insertions(+), 68 deletions(-) diff --git a/chef/Makefile b/chef/Makefile index af9aa89..0a9e5b3 100644 --- a/chef/Makefile +++ b/chef/Makefile @@ -25,7 +25,7 @@ CFLAGS += -Wall -Wextra -pedantic -g -DDEBUG # Génération des fichiers éxecutables bin/%: obj/%.o - $(CC) $(LDFLAGS_CUSTOM) $^ -o $@ + $(CXX) $(LDFLAGS_CUSTOM) $^ -o $@ # On enlève les symboles inutiles pour gagner en temps de chargement de l'éxecutable ifeq ($(DEBUG),no) strip $@ @@ -34,13 +34,14 @@ endif # RÈGLES DE COMPILATION # Règle éxecutée par défaut (quand on fait juste `make`) -default: bin/testpin bin/premier bin/local +default: bin/testpin bin/premier bin/local bin/testI2c bin/testLCD # Binaires (dont il faut spécifier les objets explicitement) bin/premier: obj/CF.o obj/movement.o obj/debug.o obj/position.o bin/testPin: obj/testPin.o +bin/testI2c: obj/testI2c.o obj/i2c.o obj/srf08.o obj/lcd.o -bin/local: obj/local.o obj/CF.o obj/position.o +bin/local: obj/local.o obj/debug.o $(CC) -lpthread $^ -o $@ # Génération des fichiers objets diff --git a/chef/src/debug.c b/chef/src/debug.c index 6c4e501..2bda9a5 100644 --- a/chef/src/debug.c +++ b/chef/src/debug.c @@ -1,38 +1,64 @@ -#include "debug.h" -#include -#include // sleep -#include -#include #include +#include +#include +#include #include +#include +#include // sleep -#include "position.h" +#include "debug.h" +// Variables globales +pthread_t tDebug; + +struct debugArg* listeDebugArgs = NULL; + +FILE* debugFd; void* TaskDebug(void* pdata) { (void)pdata; + + clock_t debugStart; + debugStart = clock(); + struct timespec tim; // 100 ms tim.tv_sec = 0; tim.tv_nsec = 100000000L; - /* tim.tv_sec = 1; */ - /* tim.tv_nsec = 0; */ - char line[1024]; - clock_t t; + fprintf(debugFd, "\n"); for (;;) { + clock_t t = clock() - debugStart; + fprintf(debugFd, "%ld", t); - // Calculating time index - t = clock() - debugStart; + struct debugArg* arg = listeDebugArgs; + while (arg != NULL) { + switch (arg->type) { + case d: + fprintf(debugFd, ",%d", *((int*)arg->var)); + break; + case ld: + fprintf(debugFd, ",%ld", *((long int*)arg->var)); + break; + case f: + fprintf(debugFd, ",%f", *((float*)arg->var)); + break; + case lf: + fprintf(debugFd, ",%f", *((double*)arg->var)); + break; + case s: + fprintf(debugFd, ",%s", *((char**)arg->var)); + break; + default: + fprintf(debugFd, ",?"); + break; + } - // Generating line - sprintf(line, "%ld,%d,%ld,%ld\n", t, nbCalcPos, lCodTot, rCodTot); + arg = arg->next; + } + fprintf(debugFd, "\n"); - // Writing - write(debugFd, line, strlen(line)); - - // Sleeping nanosleep(&tim, NULL); } @@ -41,7 +67,6 @@ void* TaskDebug(void* pdata) void configureDebug() { - debugStart = clock(); // Génération du nom de fichier char path[256]; @@ -50,20 +75,41 @@ void configureDebug() sprintf(path, "log/%ld.csv", startTime); // Open file - debugFd = open(path, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); - if (debugFd < 0) { - fprintf(stderr, "Impossible d'ouvrir le fichier '%s', debug désactivé.\n", path); + debugFd = fopen(path, "w"); + if (debugFd == NULL) { + perror("fopen debug file"); return; } - char header[] = "time,nbCalcPos,lCodTot,rCodTot\n"; - write(debugFd, header, strlen(header)); + fprintf(debugFd, "time"); +} +void registerDebugVar(char* name, enum debugArgTypes type, void* var) +{ + fprintf(debugFd, ",%s", name); + + struct debugArg* arg = NULL; + struct debugArg** addrArg = &listeDebugArgs; + while (*addrArg != NULL) { + addrArg = &((*addrArg)->next); + } + + arg = malloc(sizeof(struct debugArg)); + arg->type = type; + arg->var = var; + arg->next = NULL; + + *addrArg = arg; +} + +void startDebug() +{ pthread_create(&tDebug, NULL, TaskDebug, NULL); } void deconfigureDebug() { pthread_cancel(tDebug); - close(debugFd); + fclose(debugFd); + // TODO Vider la liste des arguments } diff --git a/chef/src/debug.h b/chef/src/debug.h index b5c26bb..a133795 100644 --- a/chef/src/debug.h +++ b/chef/src/debug.h @@ -4,13 +4,24 @@ #include #include -clock_t debugStart; -int debugFd; -pthread_t tDebug; +// Structures +enum debugArgTypes {d, f, ld, lf, s}; -void* TaskDebug(void *pdata); -void configureDebug(); +struct debugArg { + enum debugArgTypes type; + void* var; + struct debugArg* next; +}; + + +// Public +void configureDebug(); // Avant tous les configure +void registerDebugVar(char* name, enum debugArgTypes type, void* var); +void startDebug(); // Après tous les configure void deconfigureDebug(); +// Private +void* TaskDebug(void *pdata); + #endif diff --git a/chef/src/local.c b/chef/src/local.c index f85ca66..fe4ec74 100644 --- a/chef/src/local.c +++ b/chef/src/local.c @@ -3,27 +3,34 @@ #include #include // sleep -#include "CF.h" -#include "position.h" +#include "debug.h" -#define TEMPSMAX 10 +unsigned long int canard = 42; +double banane = 63; +char* artichaut = "Torticoli"; int main() { printf("Démarrage...\n"); - configureCF(); - configurePosition(); srand(time(NULL)); - printf("C'est parti !\n"); + configureDebug(); + registerDebugVar("canard", ld, &canard); + registerDebugVar("banane", lf, &banane); + registerDebugVar("artichaut", s, &artichaut); + startDebug(); - sleep(TEMPSMAX); + for (int i = 0; i < 2; i++) { + printf("21 %d\n", i); + canard += 3; + banane /= 2; + sleep(1); + } - printf("Fin des %d secondes\n", TEMPSMAX); + deconfigureDebug(); - deconfigureCF(); return EXIT_SUCCESS; } diff --git a/chef/src/position.c b/chef/src/position.c index 2392fd0..1a87006 100644 --- a/chef/src/position.c +++ b/chef/src/position.c @@ -2,9 +2,22 @@ * Fonctions de calcul de la position du robot */ -#include "position.h" #include +#include "debug.h" +#include "position.h" + +// Globales +struct position actuel; +struct F2CI_CODERs deltaCoders; +pthread_mutex_t posPolling; +pthread_t tPosition; + +// Globales +unsigned int nbCalcPos; +long lCodTot, rCodTot; + + void* TaskPosition(void* pData) { (void)pData; @@ -29,7 +42,6 @@ void* TaskPosition(void* pData) nbCalcPos++; lCodTot += deltaCoders.dL; rCodTot += deltaCoders.dR; - } return NULL; @@ -43,8 +55,11 @@ void onF2CI_CODER() void configurePosition() { - pthread_create(&tPosition, NULL, TaskPosition, NULL); registerRxHandler(F2CI_CODER, onF2CI_CODER); + registerDebugVar("lCodTot", ld, &lCodTot); + registerDebugVar("rCodTot", ld, &rCodTot); + registerDebugVar("nbCalcPos", d, &nbCalcPos); + pthread_create(&tPosition, NULL, TaskPosition, NULL); } void deconfigurePosition() diff --git a/chef/src/position.h b/chef/src/position.h index 9966cab..55b8045 100644 --- a/chef/src/position.h +++ b/chef/src/position.h @@ -9,23 +9,13 @@ #include +// Structures struct __attribute__ ((packed)) position { float x; float y; float o; }; -struct position actuel; -struct F2CI_CODERs deltaCoders; - -// Debug -unsigned int nbCalcPos; -long lCodTot, rCodTot; - -// -pthread_mutex_t posPolling; -pthread_t tPosition; - // Fonctions void configurePosition(); void deconfigurePosition(); diff --git a/chef/src/premier.c b/chef/src/premier.c index 52d349f..8f4c9e2 100644 --- a/chef/src/premier.c +++ b/chef/src/premier.c @@ -33,7 +33,7 @@ void* TaskParcours(void *pdata) #define RAMP_TIME 100 #define MAX_VIT MOT_MAX_V - for (;;) { + /* for (;;) { */ // ↗ for (int i = 0; i < RAMP_TIME; i++) { float p = (float) i / (float) RAMP_TIME; @@ -43,15 +43,15 @@ void* TaskParcours(void *pdata) changerMoteurs(MOT_MAX_V, MOT_MAX_V); // ↑ sleep(2); - // ↘ - for (int i = 0; i < RAMP_TIME; i++) { - float p = (float) i / (float) RAMP_TIME; - p = 1 - p; - changerMoteurs(p * MOT_MAX_V, p * MOT_MAX_V); - nanosleep(&tim, NULL); - } - sleep(5); - } + /* // ↘ */ + /* for (int i = 0; i < RAMP_TIME; i++) { */ + /* float p = (float) i / (float) RAMP_TIME; */ + /* p = 1 - p; */ + /* changerMoteurs(p * MOT_MAX_V, p * MOT_MAX_V); */ + /* nanosleep(&tim, NULL); */ + /* } */ + /* sleep(5); */ + /* } */ printf("Fin du parcours\n"); return NULL; @@ -61,10 +61,11 @@ int main() { printf("Démarrage...\n"); - configureCF(); configureDebug(); + configureCF(); configureMovement(); configurePosition(); + startDebug(); srand(time(NULL)); /* printf("En attente de la tirette...\n"); // TODO */ @@ -78,12 +79,15 @@ int main() printf("Fin des %d secondes\n", TEMPSMAX); /* pthread_cancel(tParcours); */ + for (;;) { + + } /* stop(); */ deconfigureMovement(); deconfigurePosition(); - deconfigureDebug(); deconfigureCF(); + deconfigureDebug(); return EXIT_SUCCESS; }