1
0
Fork 0
mirror of https://github.com/RobotechLille/cdf2018-principal synced 2024-05-21 22:26:13 +02:00

Rattrapage de session abandonnée Arduino

This commit is contained in:
Geoffrey Frogeye 2018-02-16 21:35:05 +01:00
parent 45151e7b1a
commit 89d3bf21cd
3 changed files with 45 additions and 1 deletions

View file

@ -25,6 +25,7 @@ void configureArduino()
{
// Connection au port série
printf("Connexion à %s... ", ARDUINO_PORTNAME);
fflush(stdout);
arduino = open(ARDUINO_PORTNAME, O_RDWR | O_NOCTTY | O_NDELAY);
if (arduino < 0) {
printf("Échec !\n");
@ -93,6 +94,16 @@ void onA2CD_ERR()
printf("Erreur reçue : %c (%2x)\n", s.code, s.code);
}
void setPret()
{
pret = true;
}
void doNothing()
{
}
void configureCA()
{
configureArduino();
@ -102,6 +113,36 @@ void configureCA()
pthread_mutex_init(&sSendCA, NULL);
pthread_create(&tReaderAC, NULL, TaskReaderAC, NULL);
printf("Attente de réponse de l'Arduino... ");
fflush(stdout);
struct timespec tim;
tim.tv_sec = 0;
tim.tv_nsec = 100000000L;
// Dans le cas où on aurait laissé l'Arduino en attente de donnée,
// on envoie des pings en boucle jusqu'à ce qu'il nous réponde.
pret = false;
registerRxHandler(C2AD_PING, setPret);
while (!pret) {
sendCA(C2AD_PING, NULL, 0);
nanosleep(&tim, NULL);
}
registerRxHandler(C2AD_PING, doNothing);
// Dans le cas où les données de ping complèteraient une commande de déplacement,
// on envoie un STOP en préventif. Ça permet aussi d'attendre que les PING
// en trop aient été absorbés
pret = false;
registerRxHandler(C2AD_STOP, setPret);
sendCA(C2AD_STOP, NULL, 0);
while (!pret) {
nanosleep(&tim, NULL);
}
registerRxHandler(C2AD_STOP, NULL);
registerRxHandler(C2AD_PING, NULL);
printf("OK !\n");
registerRxHandler(A2CD_ERR, onA2CD_ERR);
}

View file

@ -4,6 +4,7 @@
#include <sys/ioctl.h>
#include <pthread.h>
#include <termios.h> // baudrates
#include <stdbool.h>
#include "ACsignals.h"
@ -17,6 +18,7 @@ pthread_t tReaderAC;
typedef void (*rxHandler)(void);
rxHandler rxHandlersAC[256];
bool pret;
void registerRxHandler(unsigned char code, rxHandler handler); // À utiliser après configureCA();
void sendByteCA(unsigned char data); // Privé

View file

@ -8,13 +8,14 @@ void onC2AD_STOP()
{
// On considère que l'arrêt se fait très rapidement pour ne pas
// avoir à attendre le signal de retour de C2AD_STOP
registerRxHandler(C2AD_STOP, NULL);
}
void stop()
{
printf("→ Arrêt\n");
sendCA(C2AD_STOP, NULL, 0);
registerRxHandler(C2AD_STOP, onC2AD_STOP);
sendCA(C2AD_STOP, NULL, 0);
}
// Inspiré de https://stackoverflow.com/a/1760819