939 lines
36 KiB
C++
939 lines
36 KiB
C++
/*
|
|
* Copyright (C) 2018 dimercur
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "tasks.h"
|
|
#include <stdexcept>
|
|
|
|
// Déclaration des priorités des taches
|
|
#define PRIORITY_TSERVER 30
|
|
#define PRIORITY_TOPENCOMROBOT 20
|
|
#define PRIORITY_TMOVE 20
|
|
#define PRIORITY_TSENDTOMON 23
|
|
#define PRIORITY_TRECEIVEFROMMON 25
|
|
#define PRIORITY_TSTARTROBOT 20
|
|
#define PRIORITY_TCAMERA 21
|
|
#define PRIORITY_TBATTERY 19
|
|
#define PRIORITY_TSTARTCAMERA 22
|
|
#define PRIORITY_TROBOTRELOADMESSAGE 25
|
|
|
|
/*
|
|
* Some remarks:
|
|
* 1- This program is mostly a template. It shows you how to create tasks, semaphore
|
|
* message queues, mutex ... and how to use them
|
|
*
|
|
* 2- semDumber is, as name say, useless. Its goal is only to show you how to use semaphore
|
|
*
|
|
* 3- Data flow is probably not optimal
|
|
*
|
|
* 4- Take into account that ComRobot::Write will block your task when serial buffer is full,
|
|
* time for internal buffer to flush
|
|
*
|
|
* 5- Same behavior exists for ComMonitor::Write !
|
|
*
|
|
* 6- When you want to write something in terminal, use cout and terminate with endl and flush
|
|
*
|
|
* 7- Good luck ! Thanks
|
|
*/
|
|
|
|
/**
|
|
* @brief Initialisation des structures de l'application (tâches, mutex,
|
|
* semaphore, etc.)
|
|
*/
|
|
|
|
void Tasks::Init() {
|
|
int status;
|
|
int err;
|
|
|
|
/**************************************************************************************/
|
|
/* Mutex creation */
|
|
/**************************************************************************************/
|
|
if (err = rt_mutex_create(&mutex_monitor, NULL)) {
|
|
cerr << "Error mutex create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_mutex_create(&mutex_robot, NULL)) {
|
|
cerr << "Error mutex create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_mutex_create(&mutex_robotStarted, NULL)) {
|
|
cerr << "Error mutex create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_mutex_create(&mutex_move, NULL)) {
|
|
cerr << "Error mutex create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_mutex_create(&mutex_compteurRobotLoss, NULL)) {
|
|
cerr << "Error mutex create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
if (err = rt_mutex_create(&mutex_cameraStarted, NULL)) {
|
|
cerr << "Error mutex create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_mutex_create(&mutex_robot_pos, NULL)) {
|
|
cerr << "Error mutex create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_mutex_create(&mutex_search_arena, NULL)) {
|
|
cerr << "Error mutex create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_mutex_create(&mutex_camera, NULL)) {
|
|
cerr << "Error mutex create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_mutex_create(&mutex_battery, NULL)) {
|
|
cerr << "Error mutex create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_mutex_create(&mutex_image, NULL)) {
|
|
cerr << "Error mutex create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_mutex_create(&mutex_answerSync, NULL)) {
|
|
cerr << "Error mutex create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
cout << "Mutexes created successfully" << endl << flush;
|
|
|
|
/**************************************************************************************/
|
|
/* Semaphores creation */
|
|
/**************************************************************************************/
|
|
if (err = rt_sem_create(&sem_barrier, NULL, 0, S_FIFO)) {
|
|
cerr << "Error semaphore create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_sem_create(&sem_openComRobot, NULL, 0, S_FIFO)) {
|
|
cerr << "Error semaphore create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_sem_create(&sem_connexionEstablished, NULL, 0, S_FIFO)) {
|
|
cerr << "Error semaphore create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_sem_create(&sem_serverOk, NULL, 0, S_FIFO)) {
|
|
cerr << "Error semaphore create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_sem_create(&sem_startRobot, NULL, 0, S_FIFO)) {
|
|
cerr << "Error semaphore create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_sem_create(&sem_startRobotWithWatchdog, NULL, 0, S_FIFO)) {
|
|
cerr << "Error semaphore create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_sem_create(&sem_startCamera, NULL, 0, S_FIFO)) {
|
|
cerr << "Error semaphore create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_sem_create(&sem_reloadMessages, NULL, 0, S_FIFO)) {
|
|
cerr << "Error semaphore create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
cout << "Semaphores created successfully" << endl << flush;
|
|
|
|
/**************************************************************************************/
|
|
/* Tasks creation */
|
|
/**************************************************************************************/
|
|
if (err = rt_task_create(&th_server, "th_server", 0, PRIORITY_TSERVER, 0)) {
|
|
cerr << "Error task create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_task_create(&th_sendToMon, "th_sendToMon", 0, PRIORITY_TSENDTOMON, 0)) {
|
|
cerr << "Error task create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_task_create(&th_receiveFromMon, "th_receiveFromMon", 0, PRIORITY_TRECEIVEFROMMON, 0)) {
|
|
cerr << "Error task create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_task_create(&th_openComRobot, "th_openComRobot", 0, PRIORITY_TOPENCOMROBOT, 0)) {
|
|
cerr << "Error task create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_task_create(&th_startRobot, "th_startRobot", 0, PRIORITY_TSTARTROBOT, 0)) {
|
|
cerr << "Error task create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_task_create(&th_startCamera, "th_startCamera", 0, PRIORITY_TSTARTCAMERA, 0)) {
|
|
cerr << "Error task create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_task_create(&th_move, "th_move", 0, PRIORITY_TMOVE, 0)) {
|
|
cerr << "Error task create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_task_create(&th_battery, "th_battery", 0, PRIORITY_TBATTERY, 0)) {
|
|
cerr << "Error task create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_task_create(&th_camera, "th_camera", 0, PRIORITY_TCAMERA, 0)) {
|
|
cerr << "Error task create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_task_create(&th_startRobotWithWatchdog, "th_startRobotWithWatchdog", 0, PRIORITY_TSTARTROBOT, 0)) {
|
|
cerr << "Error task create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_task_create(&th_robotReloadMessage, "th_robotReloadMessage", 0, PRIORITY_TROBOTRELOADMESSAGE, 0)) {
|
|
cerr << "Error task create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
cout << "Tasks created successfully" << endl << flush;
|
|
|
|
/**************************************************************************************/
|
|
/* Message queues creation */
|
|
/**************************************************************************************/
|
|
if ((err = rt_queue_create(&q_messageToMon, "q_messageToMon", sizeof (Message*)*100, Q_UNLIMITED, Q_FIFO)) < 0) {
|
|
cerr << "Error msg queue create: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
cout << "Queues created successfully" << endl << flush;
|
|
|
|
camera = new Camera(sm,5);
|
|
cout << "New camera ! " << endl << flush;
|
|
}
|
|
|
|
/**
|
|
* @brief Démarrage des tâches
|
|
*/
|
|
void Tasks::Run() {
|
|
rt_task_set_priority(NULL, T_LOPRIO);
|
|
int err;
|
|
|
|
if (err = rt_task_start(&th_server, (void(*)(void*)) & Tasks::ServerTask, this)) {
|
|
cerr << "Error task start: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_task_start(&th_sendToMon, (void(*)(void*)) & Tasks::SendToMonTask, this)) {
|
|
cerr << "Error task start: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_task_start(&th_receiveFromMon, (void(*)(void*)) & Tasks::ReceiveFromMonTask, this)) {
|
|
cerr << "Error task start: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_task_start(&th_openComRobot, (void(*)(void*)) & Tasks::OpenComRobot, this)) {
|
|
cerr << "Error task start: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_task_start(&th_startRobot, (void(*)(void*)) & Tasks::StartRobotTask, this)) {
|
|
cerr << "Error task start: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_task_start(&th_move, (void(*)(void*)) & Tasks::MoveTask, this)) {
|
|
cerr << "Error task start: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_task_start(&th_battery, (void(*)(void*)) & Tasks::BatteryTask, this)) {
|
|
cerr << "Error task start: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_task_start(&th_camera, (void(*)(void*)) & Tasks::CameraTask, this)) {
|
|
cerr << "Error task start: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_task_start(&th_startCamera, (void(*)(void*)) & Tasks::StartCameraTask, this)) {
|
|
cerr << "Error task start: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_task_start(&th_robotReloadMessage, (void(*)(void*)) & Tasks::RobotReloadMessage, this)) {
|
|
cerr << "Error task start: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (err = rt_task_start(&th_startRobotWithWatchdog, (void(*)(void*)) & Tasks::StartRobotWithWatchDogTask, this)) {
|
|
cerr << "Error task start: " << strerror(-err) << endl << flush;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
cout << "Tasks launched" << endl << flush;
|
|
}
|
|
|
|
/**
|
|
* @brief Arrêt des tâches
|
|
*/
|
|
void Tasks::Stop() {
|
|
monitor.Close();
|
|
robot.Close();
|
|
}
|
|
/**
|
|
*/
|
|
void Tasks::Join() {
|
|
cout << "Tasks synchronized" << endl << flush;
|
|
rt_sem_broadcast(&sem_barrier);
|
|
pause();
|
|
}
|
|
|
|
/**
|
|
* @brief Thread handling server communication with the monitor.
|
|
*/
|
|
void Tasks::ServerTask(void *arg) {
|
|
int status;
|
|
|
|
cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
|
|
// Synchronization barrier (waiting that all tasks are started)
|
|
rt_sem_p(&sem_barrier, TM_INFINITE);
|
|
|
|
/**************************************************************************************/
|
|
/* The task server starts here */
|
|
/**************************************************************************************/
|
|
rt_mutex_acquire(&mutex_monitor, TM_INFINITE);
|
|
status = monitor.Open(SERVER_PORT);
|
|
rt_mutex_release(&mutex_monitor);
|
|
|
|
cout << "Open server on port " << (SERVER_PORT) << " (" << status << ")" << endl;
|
|
|
|
if (status < 0) throw std::runtime_error {
|
|
"Unable to start server on port " + std::to_string(SERVER_PORT)
|
|
};
|
|
monitor.AcceptClient(); // Wait the monitor client
|
|
cout << "Rock'n'Roll baby, client accepted!" << endl << flush;
|
|
rt_sem_broadcast(&sem_serverOk);
|
|
}
|
|
|
|
/**
|
|
* @brief Thread sending data to monitor.
|
|
*/
|
|
void Tasks::SendToMonTask(void* arg) {
|
|
Message *msg;
|
|
|
|
cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
|
|
// Synchronization barrier (waiting that all tasks are starting)
|
|
rt_sem_p(&sem_barrier, TM_INFINITE);
|
|
|
|
/**************************************************************************************/
|
|
/* The task sendToMon starts here */
|
|
/**************************************************************************************/
|
|
rt_sem_p(&sem_serverOk, TM_INFINITE);
|
|
|
|
while (1) {
|
|
cout << "wait msg to send" << endl << flush;
|
|
msg = ReadInQueue(&q_messageToMon);
|
|
cout << "Send msg to mon: " << msg->ToString() << endl << flush;
|
|
rt_mutex_acquire(&mutex_monitor, TM_INFINITE);
|
|
monitor.Write(msg); // The message is deleted with the Write
|
|
rt_mutex_release(&mutex_monitor);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Thread receiving data from monitor.
|
|
*/
|
|
void Tasks::ReceiveFromMonTask(void *arg) {
|
|
Message *msgRcv;
|
|
|
|
cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
|
|
// Synchronization barrier (waiting that all tasks are starting)
|
|
rt_sem_p(&sem_barrier, TM_INFINITE);
|
|
|
|
/**************************************************************************************/
|
|
/* The task receiveFromMon starts here */
|
|
/**************************************************************************************/
|
|
rt_sem_p(&sem_serverOk, TM_INFINITE);
|
|
cout << "Received message from monitor activated" << endl << flush;
|
|
|
|
while (1) {
|
|
msgRcv = monitor.Read();
|
|
cout << "Rcv <= " << msgRcv->ToString() << endl << flush;
|
|
|
|
if (msgRcv->CompareID(MESSAGE_MONITOR_LOST)) {
|
|
delete(msgRcv);
|
|
exit(-1);
|
|
} else if (msgRcv->CompareID(MESSAGE_ROBOT_COM_OPEN)) {
|
|
|
|
rt_sem_v(&sem_openComRobot);
|
|
|
|
} else if (msgRcv->CompareID(MESSAGE_ROBOT_START_WITHOUT_WD)) {
|
|
|
|
rt_sem_v(&sem_startRobot);
|
|
|
|
} else if (msgRcv->CompareID(MESSAGE_ROBOT_START_WITH_WD)) {
|
|
|
|
rt_sem_v(&sem_startRobotWithWatchdog);
|
|
|
|
} else if (msgRcv->CompareID(MESSAGE_ROBOT_GO_FORWARD) ||
|
|
msgRcv->CompareID(MESSAGE_ROBOT_GO_BACKWARD) ||
|
|
msgRcv->CompareID(MESSAGE_ROBOT_GO_LEFT) ||
|
|
msgRcv->CompareID(MESSAGE_ROBOT_GO_RIGHT) ||
|
|
msgRcv->CompareID(MESSAGE_ROBOT_STOP)) {
|
|
|
|
rt_mutex_acquire(&mutex_move, TM_INFINITE);
|
|
move = msgRcv->GetID();
|
|
rt_mutex_release(&mutex_move);
|
|
|
|
} else if (msgRcv->CompareID(MESSAGE_CAM_OPEN)){ // Ouverture de la caméra
|
|
|
|
rt_sem_v(&sem_startCamera);
|
|
|
|
} else if (msgRcv->CompareID(MESSAGE_CAM_CLOSE)){ // Fermeture de la caméra
|
|
|
|
rt_sem_v(&sem_startCamera);
|
|
|
|
}else if(msgRcv->CompareID(MESSAGE_CAM_ASK_ARENA)){ // Demande de recherche de l'arène
|
|
|
|
cout << "Enabling search arena" << endl << flush;
|
|
|
|
rt_mutex_acquire(&mutex_search_arena, TM_INFINITE);
|
|
searchArena = 1; // Activation du flag de recherche de l'arène
|
|
rt_mutex_release(&mutex_search_arena);
|
|
|
|
cout << "Search arena started " << endl << flush;
|
|
|
|
} else if(msgRcv->CompareID(MESSAGE_CAM_ARENA_CONFIRM) || // Réponse du moniteur sur la confirmation de l'arène
|
|
msgRcv->CompareID(MESSAGE_CAM_ARENA_INFIRM)){
|
|
|
|
rt_mutex_acquire(&mutex_answer_arena,TM_INFINITE);
|
|
arenaConfirm = msgRcv->CompareID(MESSAGE_CAM_ARENA_CONFIRM)? 1:0;
|
|
//cout << "Statut de l'arene " << arenaConfirm << endl << flush;
|
|
rt_mutex_release(&mutex_answer_arena);
|
|
|
|
//rt_sem_v(&sem_answerSync);
|
|
rt_mutex_acquire(&mutex_answer_arena,TM_INFINITE);
|
|
answerSync = 1;
|
|
rt_mutex_release(&mutex_answer_arena);
|
|
|
|
}else if(msgRcv->CompareID(MESSAGE_CAM_POSITION_COMPUTE_START)){ // Recherche de la position du robot
|
|
|
|
rt_mutex_acquire(&mutex_robot_pos, TM_INFINITE);
|
|
robotPos = 1;
|
|
rt_mutex_release(&mutex_robot_pos);
|
|
|
|
cout << "Searching robot position " << endl << flush;
|
|
|
|
}else if(msgRcv->CompareID(MESSAGE_CAM_POSITION_COMPUTE_STOP)){ // Arret de la recherche du robot
|
|
|
|
rt_mutex_acquire(&mutex_robot_pos, TM_INFINITE);
|
|
robotPos = 0; //
|
|
rt_mutex_release(&mutex_robot_pos);
|
|
cout << "Robot Pos false" << endl << flush;
|
|
|
|
}else if(msgRcv->CompareID(MESSAGE_MONITOR_LOST)){ // Perte de connexion avec le moniteur
|
|
cout << "MONITOR LOST" << endl << flush;
|
|
|
|
rt_mutex_acquire(&mutex_robot, TM_INFINITE);
|
|
robot.Close();
|
|
rt_mutex_release(&mutex_robot);
|
|
|
|
rt_mutex_acquire(&mutex_image, TM_INFINITE);
|
|
getImage = 0;
|
|
rt_mutex_release(&mutex_image);
|
|
|
|
rt_mutex_acquire(&mutex_camera, TM_INFINITE);
|
|
camera->Close();
|
|
rt_mutex_release(&mutex_camera);
|
|
|
|
rt_mutex_acquire(&mutex_cameraStarted, TM_INFINITE);
|
|
cameraStarted = 0;
|
|
rt_mutex_release(&mutex_cameraStarted);
|
|
|
|
|
|
|
|
rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE);
|
|
robotStarted = 0;
|
|
rt_mutex_release(&mutex_robotStarted);
|
|
|
|
}
|
|
delete(msgRcv); // must be deleted manually, no consumer
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Thread opening communication with the robot.
|
|
*/
|
|
void Tasks::OpenComRobot(void *arg) {
|
|
int status;
|
|
int err;
|
|
|
|
cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
|
|
// Synchronization barrier (waiting that all tasks are starting)
|
|
rt_sem_p(&sem_barrier, TM_INFINITE);
|
|
|
|
/**************************************************************************************/
|
|
/* The task openComRobot starts here */
|
|
/**************************************************************************************/
|
|
while (1) {
|
|
rt_sem_p(&sem_openComRobot, TM_INFINITE);
|
|
cout << "Open serial com (";
|
|
rt_mutex_acquire(&mutex_robot, TM_INFINITE);
|
|
status = robot.Open();
|
|
rt_mutex_release(&mutex_robot);
|
|
cout << status;
|
|
cout << ")" << endl << flush;
|
|
|
|
Message * msgSend;
|
|
if (status < 0) {
|
|
msgSend = new Message(MESSAGE_ANSWER_NACK);
|
|
} else {
|
|
rt_sem_v(&sem_connexionEstablished);
|
|
msgSend = new Message(MESSAGE_ANSWER_ACK);
|
|
}
|
|
|
|
cout << "Open serial com (";
|
|
WriteInQueue(&q_messageToMon, msgSend); // msgSend will be deleted by sendToMon
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Thread starting the communication with the robot.
|
|
*/
|
|
void Tasks::StartRobotTask(void *arg) {
|
|
cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
|
|
// Synchronization barrier (waiting that all tasks are starting)
|
|
rt_sem_p(&sem_barrier, TM_INFINITE);
|
|
|
|
/**************************************************************************************/
|
|
/* The task startRobot starts here */
|
|
/**************************************************************************************/
|
|
while (1) {
|
|
|
|
Message * msgSend;
|
|
rt_sem_p(&sem_startRobot, TM_INFINITE);
|
|
cout << "Start robot without watchdog (";
|
|
rt_mutex_acquire(&mutex_robot, TM_INFINITE);
|
|
msgSend = parseMessage(robot.Write(robot.StartWithoutWD()));
|
|
rt_mutex_release(&mutex_robot);
|
|
cout << msgSend->GetID();
|
|
cout << ")" << endl;
|
|
|
|
cout << "Movement answer: " << msgSend->ToString() << endl << flush;
|
|
WriteInQueue(&q_messageToMon, msgSend); // msgSend will be deleted by sendToMon
|
|
|
|
if (msgSend->GetID() == MESSAGE_ANSWER_ACK) {
|
|
rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE);
|
|
robotStarted = 1;
|
|
rt_mutex_release(&mutex_robotStarted);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Thread starting the communication with the robot with watchdog.
|
|
*/
|
|
|
|
// TODO : refactor with the previous : if (watchdog) {call with; start the watchdog messages tasks} else {call without}
|
|
void Tasks::StartRobotWithWatchDogTask(void *arg) {
|
|
cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
|
|
// Synchronization barrier (waiting that all tasks are starting)
|
|
rt_sem_p(&sem_barrier, TM_INFINITE);
|
|
|
|
/**************************************************************************************/
|
|
/* The task startRobot starts here */
|
|
/**************************************************************************************/
|
|
while (1) {
|
|
|
|
Message * msgSend;
|
|
rt_sem_p(&sem_startRobotWithWatchdog, TM_INFINITE);
|
|
cout << "Start robot with watchdog (";
|
|
rt_mutex_acquire(&mutex_robot, TM_INFINITE);
|
|
msgSend = parseMessage(robot.Write(robot.StartWithWD())); // TODO : convert Message MessageID
|
|
rt_mutex_release(&mutex_robot);
|
|
cout << msgSend->GetID();
|
|
cout << ")" << endl;
|
|
|
|
cout << "Movement answer: " << msgSend->ToString() << endl << flush;
|
|
WriteInQueue(&q_messageToMon, msgSend); // msgSend will be deleted by sendToMon
|
|
|
|
if (msgSend->GetID() == MESSAGE_ANSWER_ACK) {
|
|
rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE);
|
|
robotStarted = 1;
|
|
rt_mutex_release(&mutex_robotStarted);
|
|
}
|
|
|
|
rt_sem_v(&sem_reloadMessages);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Thread sending a message every 500 ms to the robot to prevent it from turning off.
|
|
*/
|
|
|
|
void Tasks::RobotReloadMessage(void *arg) {
|
|
int status;
|
|
|
|
cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
|
|
|
|
// Synchronization barrier (waiting that all tasks are starting)
|
|
rt_sem_p(&sem_barrier, TM_INFINITE);
|
|
rt_sem_p(&sem_reloadMessages, TM_INFINITE);
|
|
|
|
|
|
cout << "Starting to monitor packet losses " << endl << flush;
|
|
rt_task_set_periodic(NULL, TM_NOW, 1000*1000*1000);
|
|
while(1) {
|
|
rt_task_wait_period(NULL);
|
|
|
|
rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE);
|
|
int tempRobotStarted = robotStarted ;
|
|
rt_mutex_release(&mutex_robotStarted);
|
|
|
|
if (tempRobotStarted != 0) {
|
|
rt_mutex_acquire(&mutex_robot, TM_INFINITE);
|
|
status = parseMessage(robot.Write(robot.ReloadWD()))->GetID();
|
|
rt_mutex_release(&mutex_robot);
|
|
// send info to monitor
|
|
if (status < 0) {
|
|
WriteInQueue(&q_messageToMon, new Message(MESSAGE_ANSWER_NACK));
|
|
} else {
|
|
WriteInQueue(&q_messageToMon, new Message(MESSAGE_ANSWER_ACK));
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief Thread starting the communication with the robot.
|
|
*/
|
|
void Tasks::StartCameraTask(void *arg) {
|
|
cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
|
|
// Synchronization barrier (waiting that all tasks are starting)
|
|
rt_sem_p(&sem_barrier, TM_INFINITE);
|
|
|
|
/**************************************************************************************/
|
|
/* The task startRobot starts here */
|
|
/**************************************************************************************/
|
|
while (1) {
|
|
|
|
Message * msg;
|
|
bool status;
|
|
rt_sem_p(&sem_startCamera, TM_INFINITE);
|
|
cout << "Camera changed state " << endl << flush;
|
|
|
|
rt_mutex_acquire(&mutex_cameraStarted, TM_INFINITE);
|
|
int cameraStart = cameraStarted;
|
|
rt_mutex_release(&mutex_cameraStarted);
|
|
rt_mutex_acquire(&mutex_camera, TM_INFINITE);
|
|
if(!cameraStart){
|
|
|
|
status = camera->Open();
|
|
cout << "Camera Opened" << endl << flush;
|
|
|
|
rt_mutex_acquire(&mutex_image, TM_INFINITE);
|
|
getImage = 1;
|
|
rt_mutex_release(&mutex_image);
|
|
cameraStart = 1;
|
|
|
|
}else{
|
|
|
|
camera->Close();
|
|
status = true; // camera->Close() does not return a status
|
|
cout << "Camera Closed" << endl << flush;
|
|
|
|
cameraStart = 0;
|
|
rt_mutex_acquire(&mutex_image, TM_INFINITE);
|
|
getImage = 0;
|
|
rt_mutex_release(&mutex_image);
|
|
|
|
|
|
}
|
|
rt_mutex_release(&mutex_camera);
|
|
|
|
if(!status){
|
|
|
|
msg = new Message(MESSAGE_ANSWER_NACK);
|
|
cout << msg->ToString() << endl << flush;
|
|
cout << "Camera failed to start" << endl << flush;
|
|
|
|
}else{
|
|
|
|
msg = new Message(MESSAGE_ANSWER_ACK);
|
|
cout << msg->ToString() << endl << flush;
|
|
rt_mutex_acquire(&mutex_cameraStarted, TM_INFINITE);
|
|
cameraStarted = cameraStart;
|
|
rt_mutex_release(&mutex_cameraStarted);
|
|
cout << "Camera started successfully !" << endl <<flush;
|
|
}
|
|
|
|
WriteInQueue(&q_messageToMon, msg);
|
|
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Thread handling control of the robot.
|
|
*/
|
|
void Tasks::MoveTask(void *arg) {
|
|
int rs;
|
|
int cpMove;
|
|
|
|
cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
|
|
// Synchronization barrier (waiting that all tasks are starting)
|
|
rt_sem_p(&sem_barrier, TM_INFINITE);
|
|
|
|
/**************************************************************************************/
|
|
/* The task starts here */
|
|
/**************************************************************************************/
|
|
rt_task_set_periodic(NULL, TM_NOW, 100000000);
|
|
|
|
while (1) {
|
|
rt_task_wait_period(NULL);
|
|
//cout << "Periodic movement update";
|
|
rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE);
|
|
rs = robotStarted;
|
|
rt_mutex_release(&mutex_robotStarted);
|
|
if (rs == 1) {
|
|
rt_mutex_acquire(&mutex_move, TM_INFINITE);
|
|
cpMove = move;
|
|
rt_mutex_release(&mutex_move);
|
|
|
|
cout << " move: " << cpMove << endl << flush;
|
|
|
|
rt_mutex_acquire(&mutex_robot, TM_INFINITE);
|
|
parseMessage(robot.Write(new Message((MessageID)cpMove)));
|
|
rt_mutex_release(&mutex_robot);
|
|
}
|
|
//cout << endl << flush;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief task in charge of the battery status.
|
|
*/
|
|
void Tasks::BatteryTask(void *arg) {
|
|
int rs;
|
|
int cpMove;
|
|
|
|
cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
|
|
// Synchronization barrier (waiting that all tasks are starting)
|
|
rt_sem_p(&sem_barrier, TM_INFINITE);
|
|
|
|
/**************************************************************************************/
|
|
/* The task starts here */
|
|
/**************************************************************************************/
|
|
rt_task_set_periodic(NULL, TM_NOW, 500*1000*1000);
|
|
|
|
while (1) {
|
|
rt_task_wait_period(NULL);
|
|
rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE);
|
|
rs = robotStarted;
|
|
rt_mutex_release(&mutex_robotStarted);
|
|
|
|
if (rs == 1) {
|
|
rt_mutex_acquire(&mutex_robot, TM_INFINITE);
|
|
Message* batteryReply = parseMessage(robot.Write(robot.GetBattery()));
|
|
rt_mutex_release(&mutex_robot);
|
|
WriteInQueue(&q_messageToMon, batteryReply);
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief task in charge of managing the camera.
|
|
*/
|
|
void Tasks::CameraTask(void *arg){
|
|
int cam;
|
|
Img* image; // Image qui sera envoyée au moniteur
|
|
Arena arenaSaved,arena ; // arenaSaved : Arène sauvegardée par le moniteur, arena: Arène temporaire pour le traitement
|
|
std::list<Position> robotPosition; // Liste des positions de robot à envoyer
|
|
MessageImg* imgToSend;
|
|
|
|
cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
|
|
// Synchronization barrier (waiting that all tasks are starting)
|
|
rt_sem_p(&sem_barrier, TM_INFINITE);
|
|
rt_task_set_periodic(NULL, TM_NOW, 100*1000*1000); // Periode de 100 ms
|
|
|
|
while(1){
|
|
rt_task_wait_period(NULL);
|
|
rt_mutex_acquire(&mutex_cameraStarted, TM_INFINITE);
|
|
cam = cameraStarted;
|
|
rt_mutex_release(&mutex_cameraStarted);
|
|
|
|
if(!cam){
|
|
continue;
|
|
}
|
|
rt_mutex_acquire(&mutex_image, TM_INFINITE);
|
|
int statusGetImage = getImage;
|
|
rt_mutex_release(&mutex_image);
|
|
if(statusGetImage){
|
|
// Lancer le traitement périodique
|
|
//cout << "Traitement de l'image" << endl << flush ;
|
|
rt_mutex_acquire(&mutex_camera, TM_INFINITE);
|
|
|
|
Img img = camera->Grab();
|
|
image = img.Copy();
|
|
rt_mutex_release(&mutex_camera);
|
|
rt_mutex_acquire(&mutex_search_arena, TM_INFINITE);
|
|
arena = image->SearchArena(); // Mise a jour de l'arene tout le temps pour la position du robot mais draw si search arena = true
|
|
if(searchArena){
|
|
statusGetImage = 0;
|
|
if(arena.IsEmpty()){
|
|
cout << "Arena is empty" << endl << flush;
|
|
// envoi d'un no ack au moniteur
|
|
WriteInQueue(&q_messageToMon, new Message(MESSAGE_ANSWER_NACK));
|
|
|
|
}else{
|
|
cout << "Draw Arena" << endl << flush;
|
|
image->DrawArena(arena);
|
|
// envoi de l'arene pour vérif
|
|
}
|
|
}else {
|
|
image->DrawArena(arenaSaved);
|
|
}
|
|
|
|
rt_mutex_acquire(&mutex_robot_pos, TM_INFINITE);
|
|
if(robotPos){
|
|
// chopper le robot
|
|
cout << "Searching robot positions" << endl << flush ;
|
|
robotPosition = image->SearchRobot(arena);
|
|
|
|
//Traitement du robot
|
|
if(!robotPosition.empty()){ // Robot position != null
|
|
image->DrawAllRobots(robotPosition);
|
|
}
|
|
for (auto position : robotPosition)
|
|
{
|
|
WriteInQueue(&q_messageToMon, new MessagePosition(MESSAGE_CAM_POSITION,position)); // Envoi de toutes les positions
|
|
}
|
|
}
|
|
rt_mutex_release(&mutex_robot_pos);
|
|
imgToSend = new MessageImg(MESSAGE_CAM_IMAGE,image);
|
|
WriteInQueue(&q_messageToMon, imgToSend);
|
|
|
|
}
|
|
|
|
if(searchArena){
|
|
rt_mutex_acquire(&mutex_answerSync, TM_INFINITE); // Possibilité de réduire les conditions inutiles avec des sémaphores
|
|
if(answerSync){
|
|
|
|
rt_mutex_acquire(&mutex_answer_arena, TM_INFINITE);
|
|
if(arenaConfirm){
|
|
// Arena OK
|
|
cout << "Arena Saved" << endl << flush;
|
|
arenaSaved = arena;
|
|
}else{
|
|
// Arena not OK
|
|
cout << "Arena Dumped" << endl << flush;
|
|
// Rien ?
|
|
}
|
|
statusGetImage = 1;
|
|
searchArena = 0;
|
|
answerSync = 0;
|
|
rt_mutex_release(&mutex_answer_arena);
|
|
}
|
|
|
|
rt_mutex_release(&mutex_answerSync);
|
|
}
|
|
|
|
rt_mutex_release(&mutex_search_arena);
|
|
rt_mutex_acquire(&mutex_image, TM_INFINITE);
|
|
getImage = statusGetImage;
|
|
rt_mutex_release(&mutex_image);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief verifie si la connexion avec le robot est toujours opérationnel
|
|
*/
|
|
Message* Tasks::parseMessage(Message* msg){
|
|
int state;
|
|
|
|
rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE);
|
|
int tempRobotStarted = robotStarted ;
|
|
rt_mutex_release(&mutex_robotStarted);
|
|
|
|
if (tempRobotStarted != 0) {
|
|
|
|
if (msg->CompareID(MESSAGE_ANSWER_NACK) ||msg->CompareID(MESSAGE_ANSWER_ROBOT_TIMEOUT) ||msg->CompareID(MESSAGE_ANSWER_ROBOT_UNKNOWN_COMMAND) ||msg->CompareID(MESSAGE_ANSWER_ROBOT_ERROR) ||msg->CompareID(MESSAGE_ANSWER_COM_ERROR)){
|
|
// increment
|
|
rt_mutex_acquire(&mutex_compteurRobotLoss, TM_INFINITE);
|
|
compteurRobotLoss ++;
|
|
cout << "Current loss value " << compteurRobotLoss << endl << flush;
|
|
rt_mutex_release(&mutex_compteurRobotLoss);
|
|
} else {
|
|
|
|
// reset
|
|
rt_mutex_acquire(&mutex_compteurRobotLoss, TM_INFINITE);
|
|
if (compteurRobotLoss > 0){
|
|
cout << "Loss value reset" << endl << flush;
|
|
}
|
|
compteurRobotLoss = 0;
|
|
rt_mutex_release(&mutex_compteurRobotLoss);
|
|
}
|
|
|
|
rt_mutex_acquire(&mutex_compteurRobotLoss, TM_INFINITE);
|
|
if(compteurRobotLoss >= 3){
|
|
// send info to monitor
|
|
WriteInQueue(&q_messageToMon, new Message(MESSAGE_ANSWER_COM_ERROR)); // TODO : c'est probablement faux mais on a suivi la doc *sig*
|
|
cout << "================================== " << endl << flush;
|
|
cout << "|| Lost communication with robot || " << endl << flush;
|
|
cout << "================================== " << endl << flush;
|
|
|
|
// close robot
|
|
rt_mutex_acquire(&mutex_robot, TM_INFINITE);
|
|
state = robot.Close();
|
|
rt_mutex_release(&mutex_robot);
|
|
|
|
rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE);
|
|
robotStarted = 0;
|
|
rt_mutex_release(&mutex_robotStarted);
|
|
|
|
rt_mutex_acquire(&mutex_compteurRobotLoss, TM_INFINITE);
|
|
compteurRobotLoss = 0;
|
|
rt_mutex_release(&mutex_compteurRobotLoss);
|
|
|
|
|
|
// send info to monitor
|
|
if (state < 0) {
|
|
WriteInQueue(&q_messageToMon, new Message(MESSAGE_ANSWER_NACK));
|
|
} else {
|
|
WriteInQueue(&q_messageToMon, new Message(MESSAGE_ANSWER_ACK));
|
|
}
|
|
}
|
|
rt_mutex_release(&mutex_compteurRobotLoss);
|
|
}
|
|
return msg;
|
|
}
|
|
|
|
/**
|
|
* Write a message in a given queue
|
|
* @param queue Queue identifier
|
|
* @param msg Message to be stored
|
|
*/
|
|
void Tasks::WriteInQueue(RT_QUEUE *queue, Message *msg) {
|
|
int err;
|
|
if ((err = rt_queue_write(queue, (const void *) &msg, sizeof ((const void *) &msg), Q_NORMAL)) < 0) {
|
|
cerr << "Write in queue failed: " << strerror(-err) << endl << flush;
|
|
throw std::runtime_error{"Error in write in queue"};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Read a message from a given queue, block if empty
|
|
* @param queue Queue identifier
|
|
* @return Message read
|
|
*/
|
|
Message *Tasks::ReadInQueue(RT_QUEUE *queue) {
|
|
int err;
|
|
Message *msg;
|
|
|
|
if ((err = rt_queue_read(queue, &msg, sizeof ((void*) &msg), TM_INFINITE)) < 0) {
|
|
cout << "Read in queue failed: " << strerror(-err) << endl << flush;
|
|
throw std::runtime_error{"Error in read in queue"};
|
|
}/** else {
|
|
cout << "@msg :" << msg << endl << flush;
|
|
} /**/
|
|
|
|
return msg;
|
|
}
|
|
|
|
|
|
|