Projet_RT/software/raspberry/superviseur-robot/superviseur/src/main.cpp

193 lines
5.3 KiB
C++
Raw Normal View History

2018-08-27 16:39:49 +02:00
/*
* File: main.c
* Author: pehladik
*
* Created on 23 décembre 2017, 19:45
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <alchemy/task.h>
#include <alchemy/timer.h>
#include <alchemy/mutex.h>
#include <alchemy/sem.h>
#include <alchemy/queue.h>
#include "functions.h"
2018-08-27 16:39:49 +02:00
// Déclaration des taches
RT_TASK th_server;
RT_TASK th_sendToMon;
RT_TASK th_receiveFromMon;
RT_TASK th_openComRobot;
RT_TASK th_startRobot;
RT_TASK th_move;
// Déclaration des priorités des taches
int PRIORITY_TSERVER = 30;
int PRIORITY_TOPENCOMROBOT = 20;
int PRIORITY_TMOVE = 10;
int PRIORITY_TSENDTOMON = 25;
int PRIORITY_TRECEIVEFROMMON = 22;
int PRIORITY_TSTARTROBOT = 20;
RT_MUTEX mutex_robotStarted;
RT_MUTEX mutex_move;
// Déclaration des sémaphores
RT_SEM sem_barrier;
RT_SEM sem_openComRobot;
RT_SEM sem_serverOk;
RT_SEM sem_startRobot;
// Déclaration des files de message
RT_QUEUE q_messageToMon;
int MSG_QUEUE_SIZE = 10;
// Déclaration des ressources partagées
int etatCommMoniteur = 1;
int robotStarted = 0;
char robotMove = DMB_STOP_MOVE;
2018-08-27 16:39:49 +02:00
/**
* \fn void initStruct(void)
* \brief Initialisation des structures de l'application (tâches, mutex,
* semaphore, etc.)
*/
void initStruct(void);
/**
* \fn void startTasks(void)
* \brief Démarrage des tâches
*/
void startTasks(void);
/**
* \fn void deleteTasks(void)
* \brief Arrêt des tâches
*/
void deleteTasks(void);
int main(int argc, char **argv) {
int err;
//Lock the memory to avoid memory swapping for this program
mlockall(MCL_CURRENT | MCL_FUTURE);
printf("#################################\n");
printf("# DE STIJL PROJECT #\n");
printf("#################################\n");
initStruct();
startTasks();
rt_sem_broadcast(&sem_barrier);
pause();
deleteTasks();
return 0;
}
void initStruct(void) {
2018-08-27 16:39:49 +02:00
int err;
/* Creation des mutex */
if (err = rt_mutex_create(&mutex_robotStarted, NULL)) {
printf("Error mutex create: %d %s\n", err, strerror(-err));
2018-08-27 16:39:49 +02:00
exit(EXIT_FAILURE);
}
if (err = rt_mutex_create(&mutex_move, NULL)) {
printf("Error mutex create: %s\n", strerror(-err));
exit(EXIT_FAILURE);
}
/* Creation du semaphore */
if (err = rt_sem_create(&sem_barrier, "truc", 0, S_FIFO)) {
printf("Error semaphore create 1: %d %s\n", err, strerror(-err));
2018-08-27 16:39:49 +02:00
exit(EXIT_FAILURE);
}
if (err = rt_sem_create(&sem_openComRobot, NULL, 0, S_FIFO)) {
printf("Error semaphore create 2: %s\n", strerror(-err));
2018-08-27 16:39:49 +02:00
exit(EXIT_FAILURE);
}
if (err = rt_sem_create(&sem_serverOk, NULL, 0, S_FIFO)) {
printf("Error semaphore create 3: %s\n", strerror(-err));
2018-08-27 16:39:49 +02:00
exit(EXIT_FAILURE);
}
if (err = rt_sem_create(&sem_startRobot, NULL, 0, S_FIFO)) {
printf("Error semaphore create 4: %s\n", strerror(-err));
2018-08-27 16:39:49 +02:00
exit(EXIT_FAILURE);
}
/* Creation des taches */
if (err = rt_task_create(&th_server, "th_server", 0, PRIORITY_TSERVER, 0)) {
printf("Error task create: %s\n", strerror(-err));
exit(EXIT_FAILURE);
}
if (err = rt_task_create(&th_receiveFromMon, "th_receiveFromMon", 0, PRIORITY_TRECEIVEFROMMON, 0)) {
printf("Error task create: %s\n", strerror(-err));
exit(EXIT_FAILURE);
}
if (err = rt_task_create(&th_sendToMon, "th_sendToMon", 0, PRIORITY_TSENDTOMON, 0)) {
printf("Error task create: %s\n", strerror(-err));
exit(EXIT_FAILURE);
}
if (err = rt_task_create(&th_openComRobot, "th_openComRobot", 0, PRIORITY_TOPENCOMROBOT, 0)) {
printf("Error task create: %s\n", strerror(-err));
exit(EXIT_FAILURE);
}
if (err = rt_task_create(&th_startRobot, "th_startRobot", 0, PRIORITY_TSTARTROBOT, 0)) {
printf("Error task create: %s\n", strerror(-err));
exit(EXIT_FAILURE);
}
if (err = rt_task_create(&th_move, "th_move", 0, PRIORITY_TMOVE, 0)) {
printf("Error task create: %s\n", strerror(-err));
exit(EXIT_FAILURE);
}
/* Creation des files de messages */
if (err = rt_queue_create(&q_messageToMon, "toto", MSG_QUEUE_SIZE * sizeof (MessageToRobot), MSG_QUEUE_SIZE, Q_FIFO)) {
printf("Error msg queue create: %s\n", strerror(-err));
exit(EXIT_FAILURE);
}
}
void startTasks() {
int err;
2018-11-07 11:32:19 +01:00
2018-08-27 16:39:49 +02:00
if (err = rt_task_start(&th_startRobot, &f_startRobot, NULL)) {
printf("Error task start: %s\n", strerror(-err));
exit(EXIT_FAILURE);
}
if (err = rt_task_start(&th_receiveFromMon, &f_receiveFromMon, NULL)) {
printf("Error task start: %s\n", strerror(-err));
exit(EXIT_FAILURE);
}
if (err = rt_task_start(&th_sendToMon, &f_sendToMon, NULL)) {
printf("Error task start: %s\n", strerror(-err));
exit(EXIT_FAILURE);
}
if (err = rt_task_start(&th_openComRobot, &f_openComRobot, NULL)) {
printf("Error task start: %s\n", strerror(-err));
exit(EXIT_FAILURE);
}
if (err = rt_task_start(&th_move, &f_move, NULL)) {
printf("Error task start: %s\n", strerror(-err));
exit(EXIT_FAILURE);
}
if (err = rt_task_start(&th_server, &f_server, NULL)) {
printf("Error task start: %s\n", strerror(-err));
exit(EXIT_FAILURE);
}
}
void deleteTasks() {
rt_task_delete(&th_server);
rt_task_delete(&th_openComRobot);
rt_task_delete(&th_move);
}