1
0
Fork 0
mirror of https://github.com/RobotechLille/cdf2018-principal synced 2024-05-03 04:36:44 +00:00

Revamped debug interface

This commit is contained in:
Geoffrey Frogeye 2018-04-29 09:38:49 +02:00
parent b8f6d1e0bd
commit ad736b1a0e
7 changed files with 142 additions and 68 deletions

View file

@ -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

View file

@ -1,38 +1,64 @@
#include "debug.h"
#include <stdio.h>
#include <unistd.h> // sleep
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h> // 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
}

View file

@ -4,13 +4,24 @@
#include <pthread.h>
#include <time.h>
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

View file

@ -3,27 +3,34 @@
#include <pthread.h>
#include <unistd.h> // 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;
}

View file

@ -2,9 +2,22 @@
* Fonctions de calcul de la position du robot
*/
#include "position.h"
#include <stdio.h>
#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()

View file

@ -9,23 +9,13 @@
#include <pthread.h>
// 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();

View file

@ -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;
}