1
0
Fork 0
mirror of https://github.com/RobotechLille/cdf2018-principal synced 2024-11-14 12:26:06 +01:00

A↔C: Développement côté Arduino

This commit is contained in:
Geoffrey Frogeye 2018-02-14 18:07:10 +01:00
parent fc6b9e3317
commit d795e2d906
10 changed files with 233 additions and 54 deletions

View file

@ -1,12 +1,13 @@
#include <avr/interrupt.h> #include "AC.h"
#include <avr/io.h>
#include <FreeRTOS.h>
#include <queue.h>
#include "serial.h" void registerRxHandler(unsigned char code, rxHandler handler)
#include "ACsignals.h" {
rxHandlers[code] = handler;
}
ISR(USART0_UDRE_vect) {
ISR(USART0_UDRE_vect)
{
// When a transmit is ready to be done again // When a transmit is ready to be done again
TaskHandle_t holder = xSemaphoreGetMutexHolder(sSendAC); TaskHandle_t holder = xSemaphoreGetMutexHolder(sSendAC);
if (holder != NULL) { 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)) { while (!bit_is_set(UCSR0A, UDRE0)) {
vTaskSuspend(xSemaphoreGetMutexHolder(sSendAC)); vTaskSuspend(xSemaphoreGetMutexHolder(sSendAC));
} }
UDR0 = data; 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); xSemaphoreTake(sSendAC, 0);
sendByteAC(code); sendByteAC(code);
unsigned char* p = data; unsigned char* p = data;
@ -31,31 +34,48 @@ void sendAC(unsigned char code, void* data, size_t size) {
xSemaphoreGive(sSendAC); xSemaphoreGive(sSendAC);
} }
ISR(USART0_RX_vect) { ISR(USART0_RX_vect)
{
// When a character is received // When a character is received
vTaskResume(tReaderAC); vTaskResume(tReaderAC);
} }
unsigned char readByteAC()
unsigned char readAC() { {
while (!bit_is_set(UCSR0A, RXC0)) { while (!bit_is_set(UCSR0A, RXC0)) {
vTaskSuspend(tReaderAC); vTaskSuspend(tReaderAC);
} }
return UDR0; return UDR0;
} }
void TaskReaderAC(void *pvParameters) { void readAC(void* data, size_t size)
(void) pvParameters; {
unsigned char code; unsigned char* p = data;
for (;;) { for (int i = 0; i < size; i++) {
code = readAC(); *p = readByteAC();
p++;
char* sending = "Bonjour ! Comment va ?";
sendAC('@', sending, 22);
} }
} }
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 */ /* Set baud rate */
UBRR0 = AC_PRESCALER; UBRR0 = AC_PRESCALER;
@ -68,7 +88,9 @@ void configureAC() {
/* Set 8 bits character and 1 stop bit */ /* Set 8 bits character and 1 stop bit */
UCSR0C = (1 << UCSZ01 | 1 << UCSZ00); UCSR0C = (1 << UCSZ01 | 1 << UCSZ00);
for (int i = 0; i < 256; i++) {
rxHandlers[i] = NULL;
}
sSendAC = xSemaphoreCreateMutex(); sSendAC = xSemaphoreCreateMutex();
xTaskCreate(TaskReaderAC, "TaskReaderAC", 128, NULL, 2, &tReaderAC); xTaskCreate(TaskReaderAC, "TaskReaderAC", 128, NULL, 2, &tReaderAC);
} }

30
arduino/AC.h Normal file
View file

@ -0,0 +1,30 @@
#ifndef __SERIAL_H_
#define __SERIAL_H_
#include <FreeRTOS.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <queue.h>
#include <semphr.h>
#include <stdlib.h>
#include <task.h>
#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

View file

@ -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_ #ifndef __ACSIGNALS_H_
#define __ACSIGNALS_H_ #define __ACSIGNALS_H_
@ -21,17 +22,41 @@
// Chef → Arduino // Chef → Arduino
// Pour le debug // Pour le debug
#define C2AD_PING 0 #define C2AD_PING 'P'
struct C2AD_PINGs {
};
// Arrête tous les actionneurs // Arrête tous les actionneurs
#define C2AD_STOP 1 #define C2AD_STOP 'S'
struct C2ADD_STOPs {
};
// Donne une destination // Donne une destination
#define C2AD_GOTO 2 #define C2AD_GOTO 'G'
// Donne une rotation struct C2AD_GOTOs {
#define C2AD_ROTATE 3 float x;
float y;
float o; // Peut-être NaN ou autre valeur spécifique si indifférent
};
// Arduino → Chef // Arduino → Chef
// Envoie la position actuelle // Erreur quelconque
#define A2CI_POS #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 #endif

View file

@ -29,7 +29,7 @@ FORMAT = ihex
# Target file name (without extension). # Target file name (without extension).
TARGET = principal TARGET = principal
OBJS = serial OBJS = AC position movement
# Custom # Custom
NAME = $(TARGET).c NAME = $(TARGET).c

51
arduino/movement.c Normal file
View file

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

23
arduino/movement.h Normal file
View file

@ -0,0 +1,23 @@
/*
* Outils assurant le déplacement du robot
*/
#ifndef __MOVEMENT_H_
#define __MOVEMENT_H_
#include <FreeRTOS.h>
#include <task.h>
#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

21
arduino/position.c Normal file
View file

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

22
arduino/position.h Normal file
View file

@ -0,0 +1,22 @@
/*
* Outils assurant la connaissance de la postion du robot
*/
#ifndef __POSITION_H_
#define __POSITION_H_
#include <FreeRTOS.h>
#include <task.h>
struct position {
float x;
float y;
float o;
};
TaskHandle_t tPosition;
void TaskPosition();
void configurePosition();
#endif

View file

@ -3,7 +3,9 @@
#include <FreeRTOS.h> #include <FreeRTOS.h>
#include <task.h> #include <task.h>
#include "serial.h" #include "AC.h"
#include "position.h"
#include "movement.h"
unsigned char speed = 200; unsigned char speed = 200;
@ -23,9 +25,12 @@ void TaskBlink(void *pvParameters) {
int main(void) { int main(void) {
configureAC(); configureAC();
sei(); configureMovement();
configurePosition();
xTaskCreate(TaskBlink, "Blink", 128, NULL, 2, NULL); xTaskCreate(TaskBlink, "Blink", 128, NULL, 2, NULL);
sei();
vTaskStartScheduler(); vTaskStartScheduler();
return 0; return 0;
} }

View file

@ -1,20 +0,0 @@
#ifndef __SERIAL_H_
#define __SERIAL_H_
#include <semphr.h>
#include <stdlib.h>
#include <task.h>
#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