diff --git a/arduino/serial.c b/arduino/AC.c similarity index 57% rename from arduino/serial.c rename to arduino/AC.c index 386b5d4..1be04a3 100644 --- a/arduino/serial.c +++ b/arduino/AC.c @@ -1,12 +1,13 @@ -#include -#include -#include -#include +#include "AC.h" -#include "serial.h" -#include "ACsignals.h" +void registerRxHandler(unsigned char code, rxHandler handler) +{ + rxHandlers[code] = handler; +} -ISR(USART0_UDRE_vect) { + +ISR(USART0_UDRE_vect) +{ // When a transmit is ready to be done again TaskHandle_t holder = xSemaphoreGetMutexHolder(sSendAC); if (holder != NULL) { @@ -14,14 +15,16 @@ ISR(USART0_UDRE_vect) { } } -void sendByteAC(unsigned char data) { +void sendByteAC(unsigned char data) +{ while (!bit_is_set(UCSR0A, UDRE0)) { vTaskSuspend(xSemaphoreGetMutexHolder(sSendAC)); } UDR0 = data; } -void sendAC(unsigned char code, void* data, size_t size) { +void sendAC(unsigned char code, void* data, size_t size) +{ xSemaphoreTake(sSendAC, 0); sendByteAC(code); unsigned char* p = data; @@ -31,31 +34,48 @@ void sendAC(unsigned char code, void* data, size_t size) { xSemaphoreGive(sSendAC); } -ISR(USART0_RX_vect) { +ISR(USART0_RX_vect) +{ // When a character is received vTaskResume(tReaderAC); } - -unsigned char readAC() { +unsigned char readByteAC() +{ while (!bit_is_set(UCSR0A, RXC0)) { vTaskSuspend(tReaderAC); } return UDR0; } -void TaskReaderAC(void *pvParameters) { - (void) pvParameters; - unsigned char code; - for (;;) { - code = readAC(); - - char* sending = "Bonjour ! Comment va ?"; - sendAC('@', sending, 22); +void readAC(void* data, size_t size) +{ + unsigned char* p = data; + for (int i = 0; i < size; i++) { + *p = readByteAC(); + p++; } } -void configureAC() { +void TaskReaderAC(void* pvParameters) +{ + (void)pvParameters; + unsigned char code; + for (;;) { + code = readByteAC(); + + rxHandler handler = rxHandlers[code]; + if (handler != NULL) { + handler(); + } else { + struct A2CI_ERRs err = { ERR_UNKNOWN_CODE }; + sendAC(A2CD_ERR, &err, sizeof(err)); + } + } +} + +void configureAC() +{ /* Set baud rate */ UBRR0 = AC_PRESCALER; @@ -68,7 +88,9 @@ void configureAC() { /* Set 8 bits character and 1 stop bit */ UCSR0C = (1 << UCSZ01 | 1 << UCSZ00); + for (int i = 0; i < 256; i++) { + rxHandlers[i] = NULL; + } sSendAC = xSemaphoreCreateMutex(); xTaskCreate(TaskReaderAC, "TaskReaderAC", 128, NULL, 2, &tReaderAC); } - diff --git a/arduino/AC.h b/arduino/AC.h new file mode 100644 index 0000000..bcc6c32 --- /dev/null +++ b/arduino/AC.h @@ -0,0 +1,30 @@ +#ifndef __SERIAL_H_ +#define __SERIAL_H_ + +#include +#include +#include +#include +#include +#include +#include +#include "ACsignals.h" + +#define CPU_FREQ 16000000UL +#define AC_PRESCALER CPU_FREQ / (AC_BAUDRATE << 4) - 1 + +TaskHandle_t tReaderAC; +SemaphoreHandle_t sSendAC; + +typedef void (*rxHandler)(void); +rxHandler rxHandlers[256]; + +void registerRxHandler(unsigned char code, rxHandler handler); +void sendByteAC(unsigned char data); +void sendAC(unsigned char code, void* data, size_t size); +unsigned char readByteAC(); // À utiliser uniquement depuis un rxHandler +void readAC(void* data, size_t size); // À utiliser uniquement depuis un rxHandler +void TaskReaderAC(void* pvParameters); +void configureAC(); + +#endif diff --git a/arduino/ACsignals.h b/arduino/ACsignals.h index 0b95384..b537d3d 100644 --- a/arduino/ACsignals.h +++ b/arduino/ACsignals.h @@ -1,5 +1,6 @@ -// Définition des signaux échagés entre l'Arduino et le chef -// +/* + * Définition des signaux échagés entre l'Arduino et le chef + */ #ifndef __ACSIGNALS_H_ #define __ACSIGNALS_H_ @@ -21,17 +22,41 @@ // Chef → Arduino // Pour le debug -#define C2AD_PING 0 +#define C2AD_PING 'P' +struct C2AD_PINGs { +}; + // Arrête tous les actionneurs -#define C2AD_STOP 1 +#define C2AD_STOP 'S' +struct C2ADD_STOPs { +}; + // Donne une destination -#define C2AD_GOTO 2 -// Donne une rotation -#define C2AD_ROTATE 3 +#define C2AD_GOTO 'G' +struct C2AD_GOTOs { + float x; + float y; + float o; // Peut-être NaN ou autre valeur spécifique si indifférent +}; // Arduino → Chef -// Envoie la position actuelle -#define A2CI_POS +// Erreur quelconque +#define A2CD_ERR 'E' +struct A2CI_ERRs { + unsigned char code; +}; + +#define ERR_UNKNOWN_CODE 'C' + +// Envoie les infos de debug +#define A2CI_DBG 'D' +struct A2CI_DBGs { + float x; + float y; + float o; + // ... +}; + #endif diff --git a/arduino/Makefile b/arduino/Makefile index ac76a72..1646056 100644 --- a/arduino/Makefile +++ b/arduino/Makefile @@ -29,7 +29,7 @@ FORMAT = ihex # Target file name (without extension). TARGET = principal -OBJS = serial +OBJS = AC position movement # Custom NAME = $(TARGET).c diff --git a/arduino/movement.c b/arduino/movement.c new file mode 100644 index 0000000..e8b6534 --- /dev/null +++ b/arduino/movement.c @@ -0,0 +1,51 @@ +#include "movement.h" +#include "AC.h" + +void TaskMovement(void *pvParameters) { + (void) pvParameters; + TickType_t xLastWakeTime; + TickType_t xFrequency = 100 / portTICK_PERIOD_MS; + + vTaskSuspend(tMovement); // Mettre en veille jusqu'à l'arrivée de la prochaine instruction + xLastWakeTime = xTaskGetTickCount(); + for (;;) { + + // TODO Ici ira le code qui changera les valeurs des moteurs + vTaskDelayUntil(&xLastWakeTime, 1000 / portTICK_PERIOD_MS); + + if (true) { // Arrivé à destination + sendAC(movement, NULL, 0); // On rapporte au chef qu'on a terminé l'action en cours + // Mettre en brake + + // Envoi du message de bonne réception + vTaskSuspend(tMovement); // Mettre en veille jusqu'à l'arrivée de la prochaine instruction + xLastWakeTime = xTaskGetTickCount(); + } else { + vTaskDelayUntil(&xLastWakeTime, xFrequency); + } + } +} + +void onC2AD_STOP() { + movement = C2AD_STOP; + vTaskResume(tMovement); +} + +void onC2AD_GOTO() { + movement = C2AD_GOTO; + readAC(&destination, sizeof(struct C2AD_GOTOs)); + vTaskResume(tMovement); +} + + +void configureMovement() { + // TODO Configuration des pins + + movement = C2AD_STOP; + + registerRxHandler(C2AD_STOP, onC2AD_STOP); + registerRxHandler(C2AD_GOTO, onC2AD_GOTO); + + xTaskCreate(TaskMovement, "Movement", 128, NULL, 2, &tMovement);; +} + diff --git a/arduino/movement.h b/arduino/movement.h new file mode 100644 index 0000000..41b64b9 --- /dev/null +++ b/arduino/movement.h @@ -0,0 +1,23 @@ +/* + * Outils assurant le déplacement du robot + */ + +#ifndef __MOVEMENT_H_ +#define __MOVEMENT_H_ + +#include +#include +#include "stdbool.h" +#include "ACsignals.h" + +// TODO Définition des pins + +TaskHandle_t tMovement; + +unsigned char movement; +struct C2AD_GOTOs destination; + +void TaskMovement(); +void configureMovement(); + +#endif diff --git a/arduino/position.c b/arduino/position.c new file mode 100644 index 0000000..a67b83b --- /dev/null +++ b/arduino/position.c @@ -0,0 +1,21 @@ +#include "position.h" +#include "AC.h" + +void TaskPosition(void *pvParameters) { + (void) pvParameters; + TickType_t xLastWakeTime; + TickType_t xFrequency = 100 / portTICK_PERIOD_MS; + + vTaskSuspend(tPosition); // TODO Dummy + xLastWakeTime = xTaskGetTickCount(); + for (;;) { + vTaskDelayUntil(&xLastWakeTime, xFrequency); + } +} + + + +void configurePosition() { + xTaskCreate(TaskPosition, "Position", 128, NULL, 2, &tPosition);; +} + diff --git a/arduino/position.h b/arduino/position.h new file mode 100644 index 0000000..febaf07 --- /dev/null +++ b/arduino/position.h @@ -0,0 +1,22 @@ +/* + * Outils assurant la connaissance de la postion du robot + */ + +#ifndef __POSITION_H_ +#define __POSITION_H_ + +#include +#include + +struct position { + float x; + float y; + float o; +}; + +TaskHandle_t tPosition; + +void TaskPosition(); +void configurePosition(); + +#endif diff --git a/arduino/principal.c b/arduino/principal.c index 48bf23e..4da5670 100644 --- a/arduino/principal.c +++ b/arduino/principal.c @@ -3,7 +3,9 @@ #include #include -#include "serial.h" +#include "AC.h" +#include "position.h" +#include "movement.h" unsigned char speed = 200; @@ -23,9 +25,12 @@ void TaskBlink(void *pvParameters) { int main(void) { configureAC(); - sei(); + configureMovement(); + configurePosition(); xTaskCreate(TaskBlink, "Blink", 128, NULL, 2, NULL); + sei(); + vTaskStartScheduler(); return 0; } diff --git a/arduino/serial.h b/arduino/serial.h deleted file mode 100644 index 970b9aa..0000000 --- a/arduino/serial.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef __SERIAL_H_ -#define __SERIAL_H_ - -#include -#include -#include - -#define CPU_FREQ 16000000UL -#define AC_PRESCALER CPU_FREQ / (AC_BAUDRATE << 4) - 1 - -TaskHandle_t tReaderAC; -SemaphoreHandle_t sSendAC; - -void sendByteAC(unsigned char data); -void sendAC(unsigned char code, void* data, size_t size); -unsigned char readAC(); -void TaskReaderAC(void *pvParameters); -void configureAC(); - -#endif