mirror of
https://github.com/RobotechLille/cdf2018-principal
synced 2024-12-22 05:30:37 +01:00
Arduino↔Chef: Ajout côté chef & corrections
This commit is contained in:
parent
0a9009d0c3
commit
45151e7b1a
62
arduino/AC.c
62
arduino/AC.c
|
@ -1,35 +1,37 @@
|
|||
#include "AC.h"
|
||||
|
||||
void registerRxHandler(unsigned char code, rxHandler handler)
|
||||
void registerRxHandlerAC(unsigned char code, rxHandler handler)
|
||||
{
|
||||
rxHandlers[code] = handler;
|
||||
rxHandlersAC[code] = handler;
|
||||
}
|
||||
|
||||
|
||||
ISR(USART0_UDRE_vect)
|
||||
{
|
||||
// When a transmit is ready to be done again
|
||||
TaskHandle_t holder = xSemaphoreGetMutexHolder(sSendAC);
|
||||
if (holder != NULL) {
|
||||
vTaskResume(holder);
|
||||
UDR0 = *toSendAC;
|
||||
toSendAC++;
|
||||
toSendSizeAC--;
|
||||
if (toSendSizeAC <= 0) {
|
||||
UCSR0B &= ~(1 << UDRIE0);
|
||||
TaskHandle_t holder = xSemaphoreGetMutexHolder(sSendAC);
|
||||
if (holder != NULL) {
|
||||
vTaskNotifyGiveFromISR(holder, NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
xSemaphoreTake(sSendAC, 0);
|
||||
sendByteAC(code);
|
||||
unsigned char* p = data;
|
||||
for (int i = 0; i < size; i++) {
|
||||
sendByteAC(*p++);
|
||||
xSemaphoreTake(sSendAC, portMAX_DELAY);
|
||||
toSendAC = &code;
|
||||
toSendSizeAC = sizeof(code);
|
||||
UCSR0B |= (1 << UDRIE0);
|
||||
ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
|
||||
if (size > 0) {
|
||||
toSendAC = data;
|
||||
toSendSizeAC = size;
|
||||
UCSR0B |= (1 << UDRIE0);
|
||||
ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
|
||||
}
|
||||
xSemaphoreGive(sSendAC);
|
||||
}
|
||||
|
@ -37,15 +39,16 @@ void sendAC(unsigned char code, void* data, size_t size)
|
|||
ISR(USART0_RX_vect)
|
||||
{
|
||||
// When a character is received
|
||||
vTaskResume(tReaderAC);
|
||||
vTaskNotifyGiveFromISR(tReaderAC, NULL);
|
||||
}
|
||||
|
||||
unsigned char readByteAC()
|
||||
{
|
||||
while (!bit_is_set(UCSR0A, RXC0)) {
|
||||
vTaskSuspend(tReaderAC);
|
||||
ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
|
||||
}
|
||||
return UDR0;
|
||||
unsigned char c = UDR0;
|
||||
return c;
|
||||
}
|
||||
|
||||
void readAC(void* data, size_t size)
|
||||
|
@ -64,16 +67,21 @@ void TaskReaderAC(void* pvParameters)
|
|||
for (;;) {
|
||||
code = readByteAC();
|
||||
|
||||
rxHandler handler = rxHandlers[code];
|
||||
rxHandler handler = rxHandlersAC[code];
|
||||
if (handler != NULL) {
|
||||
handler();
|
||||
} else {
|
||||
struct A2CI_ERRs err = { ERR_UNKNOWN_CODE };
|
||||
struct A2CD_ERRs err = { ERR_UNKNOWN_CODE };
|
||||
sendAC(A2CD_ERR, &err, sizeof(err));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void onC2AD_PING()
|
||||
{
|
||||
sendAC(C2AD_PING, NULL, 0);
|
||||
}
|
||||
|
||||
void configureAC()
|
||||
{
|
||||
/* Set baud rate */
|
||||
|
@ -83,14 +91,16 @@ void configureAC()
|
|||
UCSR0A &= ~(1 << U2X0);
|
||||
|
||||
/* Enable transmitter & receiver with interrupts */
|
||||
UCSR0B = (1 << RXCIE0 | 1 << UDRIE0 | 1 << TXEN0 | 1 << RXEN0);
|
||||
UCSR0B = (1 << RXCIE0 | 1 << TXEN0 | 1 << RXEN0);
|
||||
|
||||
/* Set 8 bits character and 1 stop bit */
|
||||
UCSR0C = (1 << UCSZ01 | 1 << UCSZ00);
|
||||
|
||||
for (int i = 0; i < 256; i++) {
|
||||
rxHandlers[i] = NULL;
|
||||
rxHandlersAC[i] = NULL;
|
||||
}
|
||||
sSendAC = xSemaphoreCreateMutex();
|
||||
xTaskCreate(TaskReaderAC, "TaskReaderAC", 128, NULL, 2, &tReaderAC);
|
||||
|
||||
registerRxHandlerAC(C2AD_PING, onC2AD_PING);
|
||||
}
|
||||
|
|
13
arduino/AC.h
13
arduino/AC.h
|
@ -2,8 +2,8 @@
|
|||
* Définition des fonctions utilisées pour échager entre l'Arduino et le chef
|
||||
*/
|
||||
|
||||
#ifndef __SERIAL_H_
|
||||
#define __SERIAL_H_
|
||||
#ifndef __AC_H_
|
||||
#define __AC_H_
|
||||
|
||||
#include <FreeRTOS.h>
|
||||
#include <avr/interrupt.h>
|
||||
|
@ -19,16 +19,17 @@
|
|||
|
||||
TaskHandle_t tReaderAC;
|
||||
SemaphoreHandle_t sSendAC;
|
||||
unsigned char* toSendAC;
|
||||
size_t toSendSizeAC;
|
||||
|
||||
typedef void (*rxHandler)(void);
|
||||
rxHandler rxHandlers[256];
|
||||
rxHandler rxHandlersAC[256];
|
||||
|
||||
void registerRxHandler(unsigned char code, rxHandler handler);
|
||||
void sendByteAC(unsigned char data);
|
||||
void registerRxHandlerAC(unsigned char code, rxHandler handler); // À utiliser après configureAC();
|
||||
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 TaskReaderAC(void* pvParameters); // Privé
|
||||
void configureAC();
|
||||
|
||||
#endif
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
#define AC_BAUDRATE 9600UL
|
||||
|
||||
// Structures used everywhere
|
||||
struct position {
|
||||
struct __attribute__ ((packed)) position {
|
||||
float x;
|
||||
float y;
|
||||
float o;
|
||||
|
@ -30,13 +30,9 @@ struct position {
|
|||
|
||||
// Pour le debug
|
||||
#define C2AD_PING 'P'
|
||||
struct C2AD_PINGs {
|
||||
};
|
||||
|
||||
// Arrête tous les actionneurs
|
||||
#define C2AD_STOP 'S'
|
||||
struct C2ADD_STOPs {
|
||||
};
|
||||
|
||||
// Donne une destination
|
||||
#define C2AD_GOTO 'G'
|
||||
|
@ -47,15 +43,16 @@ struct C2ADD_STOPs {
|
|||
|
||||
// Erreur quelconque
|
||||
#define A2CD_ERR 'E'
|
||||
struct A2CI_ERRs {
|
||||
struct __attribute__ ((packed)) A2CD_ERRs {
|
||||
unsigned char code;
|
||||
};
|
||||
|
||||
#define ERR_UNKNOWN_CODE 'C'
|
||||
#define ERR_UNKNOWN_CODE_FPGA 'c'
|
||||
|
||||
// Envoie les infos de debug
|
||||
#define A2CI_DBG 'D'
|
||||
struct A2CI_DBGs {
|
||||
struct __attribute__ ((packed)) A2CI_DBGs {
|
||||
struct position actuel;
|
||||
struct position destination;
|
||||
unsigned char movement;
|
||||
|
|
99
arduino/AF.c
Normal file
99
arduino/AF.c
Normal file
|
@ -0,0 +1,99 @@
|
|||
#include "AF.h"
|
||||
#include "AC.h"
|
||||
|
||||
void registerRxHandlerAF(unsigned char code, rxHandler handler)
|
||||
{
|
||||
rxHandlersAF[code] = handler;
|
||||
}
|
||||
|
||||
ISR(USART1_UDRE_vect)
|
||||
{
|
||||
// When a transmit is ready to be done again
|
||||
UDR1 = *toSendAF;
|
||||
toSendAF++;
|
||||
toSendSizeAF--;
|
||||
if (toSendSizeAF <= 0) {
|
||||
UCSR1B &= ~(1 << UDRIE0);
|
||||
TaskHandle_t holder = xSemaphoreGetMutexHolder(sSendAF);
|
||||
if (holder != NULL) {
|
||||
vTaskNotifyGiveFromISR(holder, NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void sendAF(unsigned char code, void* data, size_t size)
|
||||
{
|
||||
xSemaphoreTake(sSendAF, portMAX_DELAY);
|
||||
toSendAF = &code;
|
||||
toSendSizeAF = sizeof(code);
|
||||
UCSR1B |= (1 << UDRIE0);
|
||||
ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
|
||||
if (size > 0) {
|
||||
toSendAF = data;
|
||||
toSendSizeAF = size;
|
||||
UCSR1B |= (1 << UDRIE0);
|
||||
ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
|
||||
}
|
||||
xSemaphoreGive(sSendAF);
|
||||
}
|
||||
|
||||
ISR(USART1_RX_vect)
|
||||
{
|
||||
// When a character is received
|
||||
vTaskNotifyGiveFromISR(tReaderAF, NULL);
|
||||
}
|
||||
|
||||
unsigned char readByteAF()
|
||||
{
|
||||
while (!bit_is_set(UCSR1A, RXC1)) {
|
||||
ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
|
||||
}
|
||||
return UDR1;
|
||||
}
|
||||
|
||||
void readAF(void* data, size_t size)
|
||||
{
|
||||
unsigned char* p = data;
|
||||
for (int i = 0; i < size; i++) {
|
||||
*p = readByteAF();
|
||||
p++;
|
||||
}
|
||||
}
|
||||
|
||||
void TaskReaderAF(void* pvParameters)
|
||||
{
|
||||
(void)pvParameters;
|
||||
unsigned char code;
|
||||
for (;;) {
|
||||
code = readByteAF();
|
||||
|
||||
rxHandler handler = rxHandlersAF[code];
|
||||
if (handler != NULL) {
|
||||
handler();
|
||||
} else {
|
||||
struct A2CD_ERRs err = { ERR_UNKNOWN_CODE_FPGA };
|
||||
sendAC(A2CD_ERR, &err, sizeof(err));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void configureAF()
|
||||
{
|
||||
/* Set baud rate */
|
||||
UBRR1 = AF_PRESCALER;
|
||||
|
||||
/* Set off UART baud doubler */
|
||||
UCSR1A &= ~(1 << U2X1);
|
||||
|
||||
/* Enable transmitter & receiver with interrupts */
|
||||
UCSR1B = (1 << RXCIE1 | 1 << TXEN1 | 1 << RXEN1);
|
||||
|
||||
/* Set 8 bits character and 1 stop bit */
|
||||
UCSR1C = (1 << UCSZ11 | 1 << UCSZ10);
|
||||
|
||||
for (int i = 0; i < 256; i++) {
|
||||
rxHandlersAF[i] = NULL;
|
||||
}
|
||||
sSendAF = xSemaphoreCreateMutex();
|
||||
xTaskCreate(TaskReaderAF, "TaskReaderAF", 128, NULL, 2, &tReaderAF);
|
||||
}
|
35
arduino/AF.h
Normal file
35
arduino/AF.h
Normal file
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Définition des fonctions utilisées pour échager entre l'Arduino et le chef
|
||||
*/
|
||||
|
||||
#ifndef __AF_H_
|
||||
#define __AF_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 "AFsignals.h"
|
||||
|
||||
#define CPU_FREQ 16000000UL
|
||||
#define AF_PRESCALER CPU_FREQ / (AF_BAUDRATE << 4) - 1
|
||||
|
||||
TaskHandle_t tReaderAF;
|
||||
SemaphoreHandle_t sSendAF;
|
||||
unsigned char* toSendAF;
|
||||
size_t toSendSizeAF;
|
||||
|
||||
typedef void (*rxHandler)(void);
|
||||
rxHandler rxHandlersAF[256];
|
||||
|
||||
void registerRxHandlerAF(unsigned char code, rxHandler handler); // À utiliser après configureAF();
|
||||
void sendAF(unsigned char code, void* data, size_t size);
|
||||
unsigned char readByteAF(); // À utiliser uniquement depuis un rxHandler
|
||||
void readAF(void* data, size_t size); // À utiliser uniquement depuis un rxHandler
|
||||
void TaskReaderAF(void* pvParameters); // Privé
|
||||
void configureAF();
|
||||
|
||||
#endif
|
67
arduino/AFsignals.h
Normal file
67
arduino/AFsignals.h
Normal file
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* Définition des signaux échagés entre l'Arduino et le chef
|
||||
*/
|
||||
|
||||
#ifndef __AFSIGNALS_H_
|
||||
#define __AFSIGNALS_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define AF_BAUDRATE 9600UL
|
||||
|
||||
// Structures used everywhere
|
||||
|
||||
// D: Direct
|
||||
// Sens naturel : Envoi de donnée
|
||||
// Sens inverse : Accusé de réception
|
||||
|
||||
// I: Indirect
|
||||
// Sens inverse : Demande de donnée
|
||||
// Sens naturel : Envoi de donnée
|
||||
|
||||
// T: Trigger
|
||||
// Sens inverse : Paramètrage du trigger
|
||||
// Sens naturel : Envoi de trigger
|
||||
|
||||
// Arduino → FPGA
|
||||
|
||||
// Pour le debug
|
||||
#define A2FD_PING 'P'
|
||||
|
||||
// Réinitialise la valeur des codeuses
|
||||
#define A2FD_RESETCODER 'R'
|
||||
|
||||
// FPGA → Arduino
|
||||
|
||||
// Erreur quelconque
|
||||
#define F2AD_ERR 'E'
|
||||
struct __attribute__ ((packed)) F2AD_ERRs {
|
||||
unsigned char code;
|
||||
};
|
||||
|
||||
#define ERR_UNKNOWN_CODE 'C'
|
||||
|
||||
// Récupère les valeur des encodeurs
|
||||
#define F2AI_CODER 'D'
|
||||
struct __attribute__ ((packed)) F2AI_CODERs {
|
||||
int16_t left;
|
||||
int16_t right;
|
||||
};
|
||||
|
||||
// Récupère les valeur des capteurs de distance
|
||||
#define F2AI_CAPT 'C'
|
||||
struct __attribute__ ((packed)) F2AI_CAPTs {
|
||||
uint16_t front;
|
||||
uint16_t back;
|
||||
};
|
||||
|
||||
// Récupère les valeur des capteurs de distance (trigger au supérieur)
|
||||
#define F2AT_CAPT 'c'
|
||||
// /!\ Structure de la requête de trigger (A→F). Les données seront envoyées avec F2AI_CAPT
|
||||
struct __attribute__ ((packed)) F2AT_CAPTs {
|
||||
uint16_t front;
|
||||
uint16_t back;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
|
@ -85,13 +85,6 @@
|
|||
#define INCLUDE_xQueueGetMutexHolder 1
|
||||
// -- done
|
||||
|
||||
// JF: added
|
||||
#define configUSE_TIMERS 1
|
||||
#define configTIMER_TASK_PRIORITY 3
|
||||
#define configTIMER_QUEUE_LENGTH 10
|
||||
#define configTIMER_TASK_STACK_DEPTH configMINIMAL_STACK_SIZE
|
||||
// -- done
|
||||
|
||||
#define configUSE_PREEMPTION 1
|
||||
#define configUSE_IDLE_HOOK 0
|
||||
#define configUSE_TICK_HOOK 0
|
||||
|
|
|
@ -29,7 +29,7 @@ FORMAT = ihex
|
|||
|
||||
# Target file name (without extension).
|
||||
TARGET = principal
|
||||
OBJS = AC position movement debug
|
||||
OBJS = AC AF position movement debug
|
||||
|
||||
# Custom
|
||||
NAME = $(TARGET).c
|
||||
|
@ -78,7 +78,7 @@ LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
|
|||
# Type: avrdude -c ?
|
||||
# to get a full listing.
|
||||
#
|
||||
AVRDUDE_PROGRAMMER = stk500
|
||||
AVRDUDE_PROGRAMMER = wiring
|
||||
|
||||
AVRDUDE_PORT = /dev/ttyACM0 # programmer connected to serial device
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
void TaskDebug(void *pvParameters) {
|
||||
(void) pvParameters;
|
||||
for (;;) {
|
||||
vTaskSuspend(tDebug);
|
||||
ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
|
||||
|
||||
// Copie des valeurs à envoyer en debug
|
||||
memcpy((void*) &debug.actuel, (const void*) &actuel, (unsigned long) sizeof(actuel));
|
||||
|
@ -21,12 +21,12 @@ void TaskDebug(void *pvParameters) {
|
|||
}
|
||||
|
||||
void onA2CI_DBG() {
|
||||
vTaskResume(tDebug);
|
||||
vTaskNotifyGiveFromISR(tDebug, NULL);
|
||||
}
|
||||
|
||||
|
||||
void configureDebug() {
|
||||
registerRxHandler(A2CI_DBG, onA2CI_DBG);
|
||||
registerRxHandlerAC(A2CI_DBG, onA2CI_DBG);
|
||||
xTaskCreate(TaskDebug, "Debug", 128, NULL, 10, &tDebug);;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include "movement.h"
|
||||
#include "position.h"
|
||||
#include "AC.h"
|
||||
|
||||
void TaskMovement(void *pvParameters) {
|
||||
|
@ -6,19 +7,25 @@ void TaskMovement(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
|
||||
ulTaskNotifyTake(pdFALSE, portMAX_DELAY); // 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 (movement == C2AD_GOTO) {
|
||||
actuel.x = destination.x;
|
||||
actuel.y = destination.y;
|
||||
actuel.o = destination.o;
|
||||
}
|
||||
|
||||
if (true) { // Arrivé à destination
|
||||
sendAC(movement, NULL, 0); // On rapporte au chef qu'on a terminé l'action en cours
|
||||
// Mettre en brake
|
||||
// TODO Mettre en brake
|
||||
movement = C2AD_STOP;
|
||||
|
||||
// Envoi du message de bonne réception
|
||||
vTaskSuspend(tMovement); // Mettre en veille jusqu'à l'arrivée de la prochaine instruction
|
||||
ulTaskNotifyTake(pdFALSE, portMAX_DELAY); // Mettre en veille jusqu'à l'arrivée de la prochaine instruction
|
||||
xLastWakeTime = xTaskGetTickCount();
|
||||
} else {
|
||||
vTaskDelayUntil(&xLastWakeTime, xFrequency);
|
||||
|
@ -28,13 +35,13 @@ void TaskMovement(void *pvParameters) {
|
|||
|
||||
void onC2AD_STOP() {
|
||||
movement = C2AD_STOP;
|
||||
vTaskResume(tMovement);
|
||||
vTaskNotifyGiveFromISR(tMovement, NULL);
|
||||
}
|
||||
|
||||
void onC2AD_GOTO() {
|
||||
movement = C2AD_GOTO;
|
||||
readAC(&destination, sizeof(struct C2AD_GOTOs));
|
||||
vTaskResume(tMovement);
|
||||
vTaskNotifyGiveFromISR(tMovement, NULL);
|
||||
}
|
||||
|
||||
|
||||
|
@ -43,8 +50,8 @@ void configureMovement() {
|
|||
|
||||
movement = C2AD_STOP;
|
||||
|
||||
registerRxHandler(C2AD_STOP, onC2AD_STOP);
|
||||
registerRxHandler(C2AD_GOTO, onC2AD_GOTO);
|
||||
registerRxHandlerAC(C2AD_STOP, onC2AD_STOP);
|
||||
registerRxHandlerAC(C2AD_GOTO, onC2AD_GOTO);
|
||||
|
||||
xTaskCreate(TaskMovement, "Movement", 128, NULL, 2, &tMovement);;
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ void TaskPosition(void *pvParameters) {
|
|||
TickType_t xLastWakeTime;
|
||||
TickType_t xFrequency = 100 / portTICK_PERIOD_MS;
|
||||
|
||||
vTaskSuspend(tPosition); // TODO Dummy
|
||||
ulTaskNotifyTake(pdFALSE, portMAX_DELAY); // TODO Dummy
|
||||
xLastWakeTime = xTaskGetTickCount();
|
||||
for (;;) {
|
||||
vTaskDelayUntil(&xLastWakeTime, xFrequency);
|
||||
|
@ -16,6 +16,10 @@ void TaskPosition(void *pvParameters) {
|
|||
|
||||
|
||||
void configurePosition() {
|
||||
actuel.x = 0;
|
||||
actuel.y = 0;
|
||||
actuel.o = 90;
|
||||
|
||||
xTaskCreate(TaskPosition, "Position", 128, NULL, 2, &tPosition);;
|
||||
}
|
||||
|
||||
|
|
|
@ -4,16 +4,15 @@
|
|||
#include <task.h>
|
||||
|
||||
#include "AC.h"
|
||||
#include "AF.h"
|
||||
#include "position.h"
|
||||
#include "movement.h"
|
||||
#include "debug.h"
|
||||
|
||||
unsigned char speed = 200;
|
||||
|
||||
void TaskBlink(void *pvParameters) {
|
||||
(void) pvParameters;
|
||||
TickType_t xLastWakeTime;
|
||||
TickType_t xFrequency = speed / portTICK_PERIOD_MS;
|
||||
TickType_t xFrequency = 200 / portTICK_PERIOD_MS;
|
||||
|
||||
DDRB = 0xFF;
|
||||
|
||||
|
@ -26,6 +25,7 @@ void TaskBlink(void *pvParameters) {
|
|||
|
||||
int main(void) {
|
||||
configureAC(); // Doit rester en premier :)
|
||||
configureAF(); // Doit rester en premier :)
|
||||
configureMovement();
|
||||
configurePosition();
|
||||
configureDebug();
|
||||
|
|
|
@ -5,7 +5,7 @@ CC=gcc
|
|||
# Bibliothèques
|
||||
LIBS=
|
||||
## Drapeaux pour le linker
|
||||
LDFLAGS=
|
||||
LDFLAGS=-lpthread
|
||||
## Drapeaux pour le compilateur
|
||||
CFLAGS=
|
||||
## Générateurs de drapeaux pour les bibliothèques
|
||||
|
@ -37,7 +37,7 @@ endif
|
|||
default: bin/testpin bin/premier
|
||||
|
||||
# Binaires (dont il faut spécifier les objets explicitement)
|
||||
bin/premier: obj/common.o obj/serial.o
|
||||
bin/premier: obj/CA.o obj/debug.o obj/movement.o
|
||||
bin/testPin: obj/testPin.o
|
||||
$(CC) $(LDFLAGS) $^ -lwiringPi -o $@
|
||||
|
||||
|
|
178
chef/src/CA.c
Normal file
178
chef/src/CA.c
Normal file
|
@ -0,0 +1,178 @@
|
|||
#include "CA.h"
|
||||
#include <fcntl.h> // O_*
|
||||
#include <stdio.h> // printf...
|
||||
#include <stdlib.h> // stuff
|
||||
#include <string.h> // memcpy
|
||||
#include <strings.h> // bzero
|
||||
#include <unistd.h> // read(), write()...
|
||||
|
||||
void printData(void* data, size_t size)
|
||||
{
|
||||
printf(" ");
|
||||
unsigned char* p = data;
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
if (*p >= ' ' && *p <= '~') {
|
||||
printf(" %c", *p);
|
||||
} else {
|
||||
printf(" %02x", *p);
|
||||
}
|
||||
p++;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void configureArduino()
|
||||
{
|
||||
// Connection au port série
|
||||
printf("Connexion à %s... ", ARDUINO_PORTNAME);
|
||||
arduino = open(ARDUINO_PORTNAME, O_RDWR | O_NOCTTY | O_NDELAY);
|
||||
if (arduino < 0) {
|
||||
printf("Échec !\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Configuration du port série
|
||||
struct termios cfg;
|
||||
bzero(&cfg, sizeof(cfg));
|
||||
cfg.c_cflag = CLOCAL | CREAD | CA_BAUDRATE | CS8;
|
||||
cfg.c_iflag = 0;
|
||||
cfg.c_oflag = 0;
|
||||
cfg.c_lflag = 0; /* set input mode (non-canonical, no echo,...) */
|
||||
cfg.c_cc[VTIME] = 0; /* inter-character timer unused */
|
||||
cfg.c_cc[VMIN] = 1; /* blocking read until 1 char received */
|
||||
if (tcsetattr(arduino, TCSANOW, &cfg) < 0) {
|
||||
perror("serialConfig.tcsetattr");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
sleep(1);
|
||||
|
||||
// Flush
|
||||
unsigned char trash[1024];
|
||||
read(arduino, &trash, sizeof(trash));
|
||||
|
||||
printf("OK!\n");
|
||||
}
|
||||
|
||||
void deconfigureArduino()
|
||||
{
|
||||
close(arduino);
|
||||
printf("Déconnecté\n");
|
||||
}
|
||||
|
||||
void registerRxHandler(unsigned char code, rxHandler handler)
|
||||
{
|
||||
rxHandlersAC[code] = handler;
|
||||
}
|
||||
|
||||
void* TaskReaderAC(void* pdata)
|
||||
{
|
||||
(void)pdata;
|
||||
unsigned char code;
|
||||
for (;;) {
|
||||
code = readByteCA();
|
||||
|
||||
#ifdef PRINTRAWDATA
|
||||
printf("↓");
|
||||
printData(&code, sizeof(code));
|
||||
#endif
|
||||
rxHandler handler = rxHandlersAC[code];
|
||||
if (handler != NULL) {
|
||||
handler();
|
||||
} else {
|
||||
printf("Code inconnu: %x (%c)\n", code, code);
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void onA2CD_ERR()
|
||||
{
|
||||
struct A2CD_ERRs s;
|
||||
readCA(&s, sizeof(struct A2CD_ERRs));
|
||||
printf("Erreur reçue : %c (%2x)\n", s.code, s.code);
|
||||
}
|
||||
|
||||
void configureCA()
|
||||
{
|
||||
configureArduino();
|
||||
for (int i = 0; i < 256; i++) {
|
||||
rxHandlersAC[i] = NULL;
|
||||
}
|
||||
|
||||
pthread_mutex_init(&sSendCA, NULL);
|
||||
pthread_create(&tReaderAC, NULL, TaskReaderAC, NULL);
|
||||
registerRxHandler(A2CD_ERR, onA2CD_ERR);
|
||||
}
|
||||
|
||||
void deconfigureCA()
|
||||
{
|
||||
deconfigureArduino();
|
||||
}
|
||||
|
||||
void sendByteCA(unsigned char data)
|
||||
{
|
||||
write(arduino, &data, sizeof(data));
|
||||
|
||||
#ifdef PRINTRAWDATA
|
||||
printf("↑");
|
||||
printData(&data, sizeof(data));
|
||||
#endif
|
||||
}
|
||||
|
||||
void sendCA(unsigned char code, void* data, size_t size)
|
||||
{
|
||||
pthread_mutex_lock(&sSendCA);
|
||||
|
||||
sendByteCA(code);
|
||||
if (size > 0) {
|
||||
unsigned char* p = data;
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
write(arduino, p, sizeof(unsigned char));
|
||||
p++;
|
||||
}
|
||||
// Envoyer plus d'un octet d'un coup curieusement il aime pas ça du tout
|
||||
}
|
||||
pthread_mutex_unlock(&sSendCA);
|
||||
|
||||
#ifdef PRINTRAWDATA
|
||||
if (size > 0) {
|
||||
printf("↑");
|
||||
printData(data, size);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
unsigned char readByteCA()
|
||||
{
|
||||
unsigned char c;
|
||||
|
||||
while (read(arduino, &c, sizeof(c)) < 1) {
|
||||
sleep(0);
|
||||
}
|
||||
return c;
|
||||
|
||||
#ifdef PRINTRAWDATA
|
||||
printf("↓");
|
||||
printData(&c, sizeof(c));
|
||||
#endif
|
||||
}
|
||||
|
||||
void readCA(void* data, size_t size)
|
||||
{
|
||||
size_t remaining = size;
|
||||
int justRead;
|
||||
char* p = data;
|
||||
do {
|
||||
justRead = read(arduino, p, remaining);
|
||||
if (justRead > 0) {
|
||||
p += justRead;
|
||||
remaining -= justRead;
|
||||
}
|
||||
} while (remaining > 0);
|
||||
|
||||
#ifdef PRINTRAWDATA
|
||||
printf("↓");
|
||||
printData(data, size);
|
||||
#endif
|
||||
}
|
29
chef/src/CA.h
Normal file
29
chef/src/CA.h
Normal file
|
@ -0,0 +1,29 @@
|
|||
#ifndef __AC_H_
|
||||
#define __AC_H_
|
||||
|
||||
#include <sys/ioctl.h>
|
||||
#include <pthread.h>
|
||||
#include <termios.h> // baudrates
|
||||
|
||||
#include "ACsignals.h"
|
||||
|
||||
#define ARDUINO_PORTNAME "/dev/ttyACM0"
|
||||
#define CA_BAUDRATE B9600
|
||||
// #define PRINTRAWDATA
|
||||
|
||||
int arduino;
|
||||
pthread_mutex_t sSendCA;
|
||||
pthread_t tReaderAC;
|
||||
|
||||
typedef void (*rxHandler)(void);
|
||||
rxHandler rxHandlersAC[256];
|
||||
|
||||
void registerRxHandler(unsigned char code, rxHandler handler); // À utiliser après configureCA();
|
||||
void sendByteCA(unsigned char data); // Privé
|
||||
void sendCA(unsigned char code, void* data, size_t size);
|
||||
unsigned char readByteCA(); // À utiliser uniquement depuis un rxHandler
|
||||
void readCA(void* data, size_t size); // À utiliser uniquement depuis un rxHandler
|
||||
void configureCA();
|
||||
void deconfigureCA();
|
||||
|
||||
#endif
|
|
@ -1 +0,0 @@
|
|||
#include "common.h"
|
|
@ -1,10 +0,0 @@
|
|||
#ifndef __COMMON_H_
|
||||
#define __COMMON_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define SIGNAL_AVANCER 42
|
||||
|
||||
#endif
|
||||
|
52
chef/src/debug.c
Normal file
52
chef/src/debug.c
Normal file
|
@ -0,0 +1,52 @@
|
|||
#include "debug.h"
|
||||
#include <stdio.h>
|
||||
#include <unistd.h> // sleep
|
||||
|
||||
void* TaskDebug(void* pdata)
|
||||
{
|
||||
(void)pdata;
|
||||
struct timespec tim;
|
||||
/* tim.tv_sec = 0; */
|
||||
/* tim.tv_nsec = 100000000L; */
|
||||
tim.tv_sec = 1;
|
||||
tim.tv_nsec = 0;
|
||||
|
||||
for (;;) {
|
||||
nanosleep(&tim, NULL);
|
||||
sendCA(A2CI_DBG, NULL, 0);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void printDebugInfos(struct A2CI_DBGs* debug)
|
||||
{
|
||||
printf("← Position actuelle (%f; %f) (%f°)", debug->actuel.x, debug->actuel.y, debug->actuel.o);
|
||||
printf(", destination : ");
|
||||
if (debug->movement == C2AD_STOP) {
|
||||
printf("aucune\n");
|
||||
} else {
|
||||
printf("(%f; %f) (%f°)\n", debug->destination.x, debug->destination.y, debug->destination.o);
|
||||
}
|
||||
}
|
||||
|
||||
void onA2CI_DBG()
|
||||
{
|
||||
readCA(&debug, sizeof(struct A2CI_DBGs));
|
||||
printDebugInfos(&debug);
|
||||
}
|
||||
|
||||
void configureDebug()
|
||||
{
|
||||
registerRxHandler(A2CI_DBG, onA2CI_DBG);
|
||||
#ifdef REGULARREPORTS
|
||||
pthread_create(&tDebug, NULL, TaskDebug, NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
void deconfigureDebug()
|
||||
{
|
||||
#ifdef REGULARREPORTS
|
||||
pthread_cancel(tDebug);
|
||||
#endif
|
||||
}
|
16
chef/src/debug.h
Normal file
16
chef/src/debug.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
#ifndef __DEBUG_H_
|
||||
#define __DEBUG_H_
|
||||
|
||||
#include <pthread.h>
|
||||
#include "CA.h"
|
||||
#define REGULARREPORTS
|
||||
|
||||
struct A2CI_DBGs debug;
|
||||
pthread_t tDebug;
|
||||
|
||||
void* TaskDebug(void *pdata);
|
||||
void onA2CI_DBG();
|
||||
void configureDebug();
|
||||
void deconfigureDebug();
|
||||
|
||||
#endif
|
55
chef/src/movement.c
Normal file
55
chef/src/movement.c
Normal file
|
@ -0,0 +1,55 @@
|
|||
#include "movement.h"
|
||||
#include "CA.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
void stop()
|
||||
{
|
||||
printf("→ Arrêt\n");
|
||||
sendCA(C2AD_STOP, NULL, 0);
|
||||
registerRxHandler(C2AD_STOP, onC2AD_STOP);
|
||||
}
|
||||
|
||||
// Inspiré de https://stackoverflow.com/a/1760819
|
||||
pthread_mutex_t reponseMutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
pthread_cond_t reponseCond = PTHREAD_COND_INITIALIZER;
|
||||
bool attenteReponse = false;
|
||||
|
||||
void onC2AD_GOTO()
|
||||
{
|
||||
pthread_mutex_lock(&reponseMutex);
|
||||
attenteReponse = false;
|
||||
pthread_cond_signal(&reponseCond);
|
||||
pthread_mutex_unlock(&reponseMutex);
|
||||
}
|
||||
|
||||
void aller(struct position* pos)
|
||||
{
|
||||
printf("→ Déplacement vers (%f; %f) (%f°)\n", pos->x, pos->y, pos->o);
|
||||
|
||||
if (attenteReponse) {
|
||||
printf("Déjà en déplacement !\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
attenteReponse = true;
|
||||
|
||||
registerRxHandler(C2AD_GOTO, onC2AD_GOTO);
|
||||
sendCA(C2AD_GOTO, (struct C2AD_GOTOs*)pos, sizeof(struct C2AD_GOTOs));
|
||||
|
||||
pthread_mutex_lock(&reponseMutex);
|
||||
while (attenteReponse) {
|
||||
pthread_cond_wait(&reponseCond, &reponseMutex);
|
||||
}
|
||||
pthread_mutex_unlock(&reponseMutex);
|
||||
|
||||
registerRxHandler(C2AD_GOTO, NULL);
|
||||
|
||||
printf("← Arrivé à destination\n");
|
||||
}
|
9
chef/src/movement.h
Normal file
9
chef/src/movement.h
Normal file
|
@ -0,0 +1,9 @@
|
|||
#ifndef __MOVEMENT_H_
|
||||
#define __MOVEMENT_H_
|
||||
|
||||
#include "CA.h"
|
||||
|
||||
void stop();
|
||||
void aller(struct position* pos);
|
||||
|
||||
#endif
|
|
@ -1,15 +1,59 @@
|
|||
#include "premier.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h> // random seed
|
||||
#include <pthread.h>
|
||||
#include <unistd.h> // sleep
|
||||
|
||||
#include "serial.h"
|
||||
#include "CA.h"
|
||||
#include "movement.h"
|
||||
#include "debug.h"
|
||||
|
||||
#define TEMPSMAX 10
|
||||
|
||||
void* TaskParcours(void *pdata)
|
||||
{
|
||||
(void) pdata;
|
||||
|
||||
struct position pos;
|
||||
for (;;) {
|
||||
pos.x = (int) (rand()*200.0/RAND_MAX);
|
||||
pos.y = (int) (rand()*100.0/RAND_MAX);
|
||||
pos.o = (int) (rand()*360.0/RAND_MAX);
|
||||
aller(&pos);
|
||||
sleep(1);
|
||||
stop();
|
||||
sleep(2);
|
||||
}
|
||||
|
||||
printf("Fin du parcours\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
unsigned char g;
|
||||
|
||||
printf("Démarrage...\n");
|
||||
configureCA();
|
||||
sendByteCA('c');
|
||||
while ((g = readByteCA())) {
|
||||
printf("%c\n", g);
|
||||
}
|
||||
configureDebug();
|
||||
srand(time(NULL));
|
||||
|
||||
/* printf("Synchronisation avec le Raspberry Pi\n"); // TODO */
|
||||
|
||||
/* printf("En attente de la tirette...\n"); // TODO */
|
||||
printf("C'est parti !\n");
|
||||
|
||||
pthread_t tParcours;
|
||||
pthread_create(&tParcours, NULL, TaskParcours, NULL);
|
||||
|
||||
sleep(TEMPSMAX);
|
||||
|
||||
printf("Fin des %d secondes\n", TEMPSMAX);
|
||||
/* pthread_cancel(tParcours); */
|
||||
|
||||
|
||||
/* stop(); */
|
||||
/* */
|
||||
deconfigureDebug();
|
||||
deconfigureCA();
|
||||
return 0;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
#ifndef __PREMIER_H_
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#endif
|
|
@ -1,58 +0,0 @@
|
|||
#include "serial.h"
|
||||
#include <stdio.h> // printf...
|
||||
#include <stdlib.h> // stuff
|
||||
#include <unistd.h> // read(), write()...
|
||||
#include <fcntl.h> // O_*
|
||||
#include <strings.h> // bzero
|
||||
|
||||
#include "common.h"
|
||||
|
||||
void configureCA()
|
||||
{
|
||||
// Connection au port série
|
||||
printf("Connexion à %s... ", ARDUINO_PORTNAME);
|
||||
arduino = open(ARDUINO_PORTNAME, O_RDWR | O_NOCTTY | O_NDELAY);
|
||||
if (arduino < 0) {
|
||||
printf("Échec !\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Configuration du port série
|
||||
struct termios cfg;
|
||||
bzero(&cfg, sizeof(cfg));
|
||||
cfg.c_cflag = CLOCAL | CREAD | CA_BAUDRATE | CS8;
|
||||
cfg.c_iflag = 0;
|
||||
cfg.c_oflag = 0;
|
||||
cfg.c_lflag = 0; /* set input mode (non-canonical, no echo,...) */
|
||||
cfg.c_cc[VTIME] = 0; /* inter-character timer unused */
|
||||
cfg.c_cc[VMIN] = 1; /* blocking read until 1 char received */
|
||||
if (tcsetattr(arduino, TCSANOW, &cfg) < 0) {
|
||||
perror("serialConfig.tcsetattr");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
sleep(1);
|
||||
|
||||
printf("OK!\n");
|
||||
}
|
||||
|
||||
void deconfigureCA() {
|
||||
close(arduino);
|
||||
}
|
||||
|
||||
void sendByteCA(unsigned char data) {
|
||||
write(arduino, &data, sizeof(data));
|
||||
}
|
||||
|
||||
void sendCA(unsigned char code, void* data, size_t size)
|
||||
{
|
||||
sendByteCA(code);
|
||||
write(arduino, data, size);
|
||||
}
|
||||
|
||||
unsigned char readByteCA() {
|
||||
unsigned char c;
|
||||
while (read(arduino, &c, sizeof(c)) < 1) {
|
||||
}
|
||||
return c;
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
#ifndef __SERIAL_H_
|
||||
#define __SERIAL_H_
|
||||
|
||||
#include <sys/ioctl.h>
|
||||
#include <termios.h> // baudrates
|
||||
|
||||
#include "ACsignals.h"
|
||||
|
||||
#define ARDUINO_PORTNAME "/dev/ttyACM0"
|
||||
#define CA_BAUDRATE B9600
|
||||
|
||||
int arduino;
|
||||
|
||||
void configureCA();
|
||||
void deconfigureCA();
|
||||
void sendByteCA(unsigned char data);
|
||||
unsigned char readByteCA();
|
||||
|
||||
#endif
|
Loading…
Reference in a new issue