123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784 |
- /*
- * 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 22
- #define PRIORITY_TRECEIVEFROMMON 25
- #define PRIORITY_TSTARTROBOT 20
- #define PRIORITY_TCAMERA 21
- #define PRIORITY_TWATCHDOG 35
- #define PRIORITY_TBATTERY 20
-
- /*
- * 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 existe for ComMonitor::Write !
- *
- * 6- When you want to write something in terminal, use cout and terminate with endl and flush
- *
- * 7- Good luck !
- */
-
- /**
- * @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_watchdog, NULL)) {
- cerr << "Error mutex create: " << strerror(-err) << endl << flush;
- exit(EXIT_FAILURE);
- }
- if (err = rt_mutex_create(&mutex_etatCamera, NULL)) {
- cerr << "Error mutex create: " << strerror(-err) << endl << flush;
- exit(EXIT_FAILURE);
- }
- cout << "Mutexes created successfully" << endl << flush;
-
- /**************************************************************************************/
- /* Semaphors 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_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_startWD, NULL, 0, S_FIFO)) {
- cerr << "Error semaphore create: " << strerror(-err) << endl << flush;
- exit(EXIT_FAILURE);
- }
- if (err = rt_sem_create(&sem_startCAM, 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_battery, "th_battery", 0, PRIORITY_TBATTERY, 0)) {
- cerr << "Error task create: " << strerror(-err) << endl << flush;
- exit(EXIT_FAILURE);
- }
- if (err = rt_task_create(&th_robot, "th_robot", 0, PRIORITY_TMOVE, 0)) {
- cerr << "Error task create: " << strerror(-err) << endl << flush;
- exit(EXIT_FAILURE);
- }
- if (err = rt_task_create(&th_watchdog, "th_watchdog", 0, PRIORITY_TWATCHDOG, 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_startCamera, "th_startCamera", 0, PRIORITY_TCAMERA, 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*)*50, Q_UNLIMITED, Q_FIFO)) < 0) {
- cerr << "Error msg queue create: " << strerror(-err) << endl << flush;
- exit(EXIT_FAILURE);
- }
- cout << "Queues created successfully" << 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_robot, (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::SendBatteryTask, this)) {
- cerr << "Error task start: " << strerror(-err) << endl << flush;
- exit(EXIT_FAILURE);
- }
- if (err = rt_task_start(&th_startCamera, (void(*)(void*)) & Tasks::StartCamera, this)) {
- cerr << "Error task start: " << strerror(-err) << endl << flush;
- exit(EXIT_FAILURE);
- }
- if (err = rt_task_start(&th_watchdog, (void(*)(void*)) & Tasks::ReloadWatchdogTask, 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);
- }
-
- cout << "Tasks launched" << endl << flush;
- }
-
- /**
- * @brief Arrêt des tâches
- */
- void Tasks::Stop() {
- camera.Close();
- 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;
- 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 receiveFromMon starts here */
- /**************************************************************************************/
- while(1){
- 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)) {
- cout << "On est dans le message monitor lost" << endl << flush;
-
- rt_mutex_acquire(&mutex_robot, TM_INFINITE);
- int cpMove = MESSAGE_ROBOT_STOP;
- robot.Write(new Message((MessageID)cpMove));
- rt_mutex_release(&mutex_robot);
- robot.Close();
- monitor.Close();
- rt_mutex_acquire(&mutex_move, TM_INFINITE);
- move = MESSAGE_ROBOT_STOP;
- rt_mutex_release(&mutex_move);
- rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE);
- robotStarted = 0;
- rt_mutex_release(&mutex_robotStarted);
- rt_mutex_acquire(&mutex_watchdog, TM_INFINITE);
- watchdog = 0;
- rt_mutex_release(&mutex_watchdog);
- rt_mutex_acquire(&mutex_etatCamera, TM_INFINITE);
- etatCamera.arena = Arena();
- etatCamera.askArena = 0;
- etatCamera.cameraStarted = 0;
- etatCamera.getImg = 0;
- etatCamera.getPos = 0;
- rt_mutex_release(&mutex_etatCamera);
- camera.Close();
- /* if (err = rt_task_delete(&th_server)) {
- cerr << "Error task delete: " << strerror(-err) << endl << flush;
- //exit(EXIT_FAILURE);
- } */
- 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_start(&th_server, (void(*)(void*)) & Tasks::ServerTask, this)) {
- cerr << "Error task start: " << strerror(-err) << endl << flush;
- exit(EXIT_FAILURE);
- }
- rt_sem_v(&sem_barrier);
- Kill_StartWD();
- break;
- } 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_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);
- cout << "On est dans le message mouvement" << endl << flush;
- }
- else if (msgRcv->CompareID(MESSAGE_ROBOT_START_WITH_WD)){
- rt_mutex_acquire(&mutex_watchdog, TM_INFINITE);
- watchdog = 1;
- rt_mutex_release(&mutex_watchdog);
- rt_sem_v(&sem_startRobot);
- cout << "On a start le robot avec Watchdog" << endl << flush;
- }
- else if (msgRcv->CompareID(MESSAGE_CAM_OPEN)){
- rt_sem_v(&sem_startCAM);
- }
- else if (msgRcv->CompareID(MESSAGE_CAM_CLOSE)){
- camera.Close();
- rt_mutex_acquire(&mutex_etatCamera, TM_INFINITE);
- etatCamera.cameraStarted = 0;
- etatCamera.arena = Arena();
- etatCamera.getImg = 0;
- etatCamera.getPos = 0;
- etatCamera.askArena = 0;
- rt_mutex_release(&mutex_etatCamera);
- }
- else if (msgRcv->CompareID(MESSAGE_CAM_ASK_ARENA)){
- rt_mutex_acquire(&mutex_etatCamera, TM_INFINITE);
- etatCamera.askArena = 1;
- etatCamera.getImg = 0;
- rt_mutex_release(&mutex_etatCamera);
- }
- else if (msgRcv->CompareID(MESSAGE_CAM_ARENA_CONFIRM)){
- rt_mutex_acquire(&mutex_etatCamera, TM_INFINITE);
- etatCamera.askArena = 0;
- etatCamera.getImg = 1;
- rt_mutex_release(&mutex_etatCamera);
- }
- else if (msgRcv->CompareID(MESSAGE_CAM_ARENA_INFIRM)){
- rt_mutex_acquire(&mutex_etatCamera, TM_INFINITE);
- etatCamera.arena = Arena();
- etatCamera.askArena = 0;
- etatCamera.getImg = 1;
- rt_mutex_release(&mutex_etatCamera);
- }
- else if (msgRcv->CompareID(MESSAGE_CAM_POSITION_COMPUTE_START)){
- rt_mutex_acquire(&mutex_etatCamera, TM_INFINITE);
- etatCamera.getPos = 1;
- rt_mutex_release(&mutex_etatCamera);
- }
- else if (msgRcv->CompareID(MESSAGE_CAM_POSITION_COMPUTE_STOP)){
- rt_mutex_acquire(&mutex_etatCamera, TM_INFINITE);
- etatCamera.getPos = 0;
- rt_mutex_release(&mutex_etatCamera);
- }
- delete(msgRcv); // mus 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 {
- msgSend = new Message(MESSAGE_ANSWER_ACK);
- }
- WriteInQueue(&q_messageToMon, msgSend); // msgSend will be deleted by sendToMon
- }
- }
-
- /**
- * @brief Thread starting the communication with the robot.
- */
- void Tasks::StartRobotTask(void *arg) {
- int etatWD;
- 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 (";
- rt_mutex_acquire(&mutex_robot, TM_INFINITE);
- rt_mutex_acquire(&mutex_watchdog, TM_INFINITE);
- etatWD = watchdog;
- rt_mutex_release(&mutex_watchdog);
- cout << "Watchdog = " << etatWD << endl << flush;
- if (etatWD == 0){
- cout << "Dans le if without WD" << etatWD << endl << flush;
- msgSend = 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);
- }
- }
- else{
- cout << "Dans le if with WD" << etatWD << endl << flush;
- msgSend = robot.Write(robot.StartWithWD());
- 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_startWD);
- }
- }
- }
- }
-
- /**
- * @brief Thread handling control of the robot.
- */
- void Tasks::MoveTask(void *arg) {
- int rs;
- int cpMove;
- int previousMove = -1;
- int cpt = 0;
-
-
- Message * response;
- 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) {
- if (!(previousMove == move)){
- rt_mutex_acquire(&mutex_move, TM_INFINITE);
- cpMove = move;
- rt_mutex_release(&mutex_move);
-
- cout << " move: " << cpMove;
-
- rt_mutex_acquire(&mutex_robot, TM_INFINITE);
- response = robot.Write(new Message((MessageID)cpMove));
- rt_mutex_release(&mutex_robot);
-
- if (response->CompareID(MESSAGE_ANSWER_ACK)){
- cpt = 0;
- }
- else{
- cpt++;
- if (cpt == 3){
- Message * respToMon = new Message(MESSAGE_ANSWER_COM_ERROR);
- WriteInQueue(&q_messageToMon,respToMon);
- rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE);
- robotStarted = 0;
- rt_mutex_release(&mutex_robotStarted);
- rt_mutex_acquire(&mutex_watchdog, TM_INFINITE);
- watchdog = 0;
- rt_mutex_release(&mutex_watchdog);
- Kill_StartWD();
- cpt = 0;
- }
- }
- previousMove = cpMove;
- }
- }
- cout << endl << flush;
- }
- }
-
- /**
- * @brief Kills and restart the thread watchdog
- */
- void Tasks::Kill_StartWD(){
- int err;
- if (err = rt_task_delete(&th_watchdog)) {
- cerr << "Error task delete: " << strerror(-err) << endl << flush;
- exit(EXIT_FAILURE);
- }
- if (err = rt_task_create(&th_watchdog, "th_watchdog", 0, PRIORITY_TWATCHDOG, 0)) {
- cerr << "Error task create: " << strerror(-err) << endl << flush;
- exit(EXIT_FAILURE);
- }
- if (err = rt_task_start(&th_watchdog, (void(*)(void*)) & Tasks::ReloadWatchdogTask, this)) {
- cerr << "Error task start: " << strerror(-err) << endl << flush;
- exit(EXIT_FAILURE);
- }
- rt_sem_broadcast(&sem_barrier);
- }
-
- /**
- * 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;
- }
-
- void Tasks::SendBatteryTask(void *arg){
- int rs;
- Message * responseBattery;
-
- 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, 500000000);
-
- while(1){
- rt_task_wait_period(NULL);
- rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE);
- rs = robotStarted;
- rt_mutex_release(&mutex_robotStarted);
- cout << "Periodic battery update before rs" << endl << flush;
- if (rs){
- cout << "Periodic battery update" << endl << flush;
-
- rt_mutex_acquire(&mutex_robot, TM_INFINITE);
- responseBattery = robot.Write(new Message(MESSAGE_ROBOT_BATTERY_GET));
- rt_mutex_release(&mutex_robot);
-
- cout << "Periodic battery update before comparing message" << endl << flush;
- if (responseBattery->CompareID(MESSAGE_ROBOT_BATTERY_LEVEL)){
- WriteInQueue(&q_messageToMon,responseBattery);
- }
- }
- cout << endl << flush;
- }
- }
-
- /**
- * @brief Sends the reload message to the Robot if started with watchdog
- */
- void Tasks::ReloadWatchdogTask(void *arg) {
- int rs;
- // Synchronization barrier (waiting that all tasks are starting)
- rt_sem_p(&sem_barrier, TM_INFINITE);
-
- //We wait for the signal startWd
- rt_sem_p(&sem_startWD, TM_INFINITE);
- cout << "thread WD started" << endl << flush;
- /**************************************************************************************/
- /* The task starts here */
- /**************************************************************************************/
- rt_task_set_periodic(NULL, TM_NOW, 1000000000);
-
- while(1){
- rt_task_wait_period(NULL);
- rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE);
- rs = robotStarted;
- rt_mutex_release(&mutex_robotStarted);
- if (rs){
- cout << "Periodic watchdog reload" << endl << flush;
-
- rt_mutex_acquire(&mutex_robot, TM_INFINITE);
- robot.Write(new Message(MESSAGE_ROBOT_RELOAD_WD));
- rt_mutex_release(&mutex_robot);
- }
- }
-
- }
-
- /**
- * @brief Starts the Camera and the aquisition of images
- */
- void Tasks::StartCamera(void * arg){
- bool resp;
-
- // Synchronization barrier (waiting that all tasks are starting)
- rt_sem_p(&sem_barrier, TM_INFINITE);
-
- while(1){
- //We wait for the signal startCam
- rt_sem_p(&sem_startCAM, TM_INFINITE);
- cout << "thread StartCam started" << endl << flush;
- Message * msg;
- resp = camera.Open();
- if (resp){
- cout << "thread StartCam ok" << endl << flush;
- msg = new Message(MESSAGE_ANSWER_ACK);
- rt_mutex_acquire(&mutex_etatCamera, TM_INFINITE);
- etatCamera.cameraStarted = 1;
- etatCamera.getImg = 1;
- rt_mutex_release(&mutex_etatCamera);
- }
- else{
- msg = new Message(MESSAGE_ANSWER_NACK);
- }
- WriteInQueue(&q_messageToMon,msg);
- }
- }
-
- /**
- * @brief The nominal task for the Camera
- */
- void Tasks::CameraTask(void * arg){
- struct etatCamera_t ec;
- // Synchronization barrier (waiting that all tasks are starting)
- rt_sem_p(&sem_barrier, TM_INFINITE);
-
- cout << "thread Camera started" << endl << flush;
- /**************************************************************************************/
- /* The task starts here */
- /**************************************************************************************/
- rt_task_set_periodic(NULL, TM_NOW, 100000000);
-
- while(1){
- rt_task_wait_period(NULL);
- rt_mutex_acquire(&mutex_etatCamera, TM_INFINITE);
- ec = etatCamera;
- rt_mutex_release(&mutex_etatCamera);
-
- if (ec.cameraStarted == 1){
- if (ec.getImg == 1){
- Img img = camera.Grab();
- if (ec.getPos == 1){
- std::list<Position> pos;
- pos = img.SearchRobot(ec.arena);
- if (pos.empty()){
- //Atention j'ai modifié img.h, classe Position attribut center dans le constructeur
- pos.push_front(Position());
- }
- else{
- img.DrawAllRobots(pos);
- }
- MessagePosition* msgPos = new MessagePosition(MESSAGE_CAM_POSITION, pos.front());
- WriteInQueue(&q_messageToMon,msgPos);
- }
- MessageImg * msgImg;
- msgImg = new MessageImg(MESSAGE_CAM_IMAGE, &img);
- //WriteInQueue(&q_messageToMon,msgImg);
- monitor.Write(msgImg);
- }
- else if (ec.askArena == 1){
- Img img = camera.Grab();
- ec.arena = img.SearchArena();
- if (!ec.arena.IsEmpty()){
- rt_mutex_acquire(&mutex_etatCamera, TM_INFINITE);
- etatCamera.arena = ec.arena;
- etatCamera.askArena = 0;
- rt_mutex_release(&mutex_etatCamera);
- img.DrawArena(ec.arena);
- MessageImg * msgImg;
- msgImg = new MessageImg(MESSAGE_CAM_IMAGE, &img);
- monitor.Write(msgImg);
- //WriteInQueue(&q_messageToMon,msgImg);
- }
- }
- }
- }
- }
|