No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tasks.h 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (C) 2018 dimercur
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef __TASKS_H__
  18. #define __TASKS_H__
  19. #include <unistd.h>
  20. #include <iostream>
  21. #include <sys/mman.h>
  22. #include <alchemy/task.h>
  23. #include <alchemy/timer.h>
  24. #include <alchemy/mutex.h>
  25. #include <alchemy/sem.h>
  26. #include <alchemy/queue.h>
  27. #include "messages.h"
  28. #include "commonitor.h"
  29. #include "comrobot.h"
  30. #include "camera.h"
  31. #include "img.h"
  32. using namespace std;
  33. class Tasks {
  34. public:
  35. /**
  36. * @brief Initializes main structures (semaphores, tasks, mutex, etc.)
  37. */
  38. void Init();
  39. /**
  40. * @brief Starts tasks
  41. */
  42. void Run();
  43. /**
  44. * @brief Stops tasks
  45. */
  46. void Stop();
  47. /**
  48. * @brief Suspends main thread
  49. */
  50. void Join();
  51. private:
  52. /**********************************************************************/
  53. /* Shared data */
  54. /**********************************************************************/
  55. ComMonitor monitor;
  56. ComRobot robot;
  57. int robotStarted = 0;
  58. int move = MESSAGE_ROBOT_STOP;
  59. /**********************************************************************/
  60. /* Tasks */
  61. /**********************************************************************/
  62. RT_TASK th_server;
  63. RT_TASK th_sendToMon;
  64. RT_TASK th_receiveFromMon;
  65. RT_TASK th_openComRobot;
  66. RT_TASK th_startRobot;
  67. RT_TASK th_move;
  68. RT_TASK th_getBattery;
  69. RT_TASK th_watchDog;
  70. RT_TASK th_controlCamera;
  71. RT_TASK th_picture;
  72. /**********************************************************************/
  73. /* Mutex */
  74. /**********************************************************************/
  75. RT_MUTEX mutex_monitor;
  76. RT_MUTEX mutex_robot;
  77. RT_MUTEX mutex_robotStarted;
  78. RT_MUTEX mutex_move;
  79. RT_MUTEX mutex_camera;
  80. RT_MUTEX mutex_controlStruct;
  81. /**********************************************************************/
  82. /* Semaphores */
  83. /**********************************************************************/
  84. RT_SEM sem_barrier;
  85. RT_SEM sem_openComRobot;
  86. RT_SEM sem_serverOk;
  87. RT_SEM sem_startRobot;
  88. RT_SEM sem_watchdogStart;
  89. RT_SEM sem_restartServer;
  90. RT_SEM sem_robotStopped;
  91. /**********************************************************************/
  92. /* Message queues */
  93. /**********************************************************************/
  94. int MSG_QUEUE_SIZE;
  95. RT_QUEUE q_messageToMon;
  96. RT_QUEUE q_messageComRobot;
  97. RT_QUEUE q_messageControlRobot;
  98. RT_QUEUE q_messageControlCam;
  99. /**********************************************************************/
  100. /* Tasks' functions */
  101. /**********************************************************************/
  102. /**
  103. * @brief Thread handling server communication with the monitor.
  104. */
  105. void ServerTask(void *arg);
  106. /**
  107. * @brief Thread sending data to monitor.
  108. */
  109. void SendToMonTask(void *arg);
  110. /**
  111. * @brief Thread receiving data from monitor.
  112. */
  113. void ReceiveFromMonTask(void *arg);
  114. /**
  115. * @brief Thread opening communication with the robot.
  116. */
  117. void OpenComRobot(void *arg);
  118. /**
  119. * @brief Thread starting the communication with the robot.
  120. */
  121. void StartRobotTask(void *arg);
  122. /**
  123. * @brief Thread handling control of the robot.
  124. */
  125. void MoveTask(void *arg);
  126. /**********************************************************************/
  127. /* Queue services */
  128. /**********************************************************************/
  129. /**
  130. * Write a message in a given queue
  131. * @param queue Queue identifier
  132. * @param msg Message to be stored
  133. */
  134. void WriteInQueue(RT_QUEUE *queue, Message *msg);
  135. /**
  136. * Read a message from a given queue, block if empty
  137. * @param queue Queue identifier
  138. * @return Message read
  139. */
  140. Message *ReadInQueue(RT_QUEUE *queue);
  141. /**
  142. * Read the battery level of the robot
  143. * @return Nothing
  144. */
  145. void ReadBattery(void *arg);
  146. /**
  147. * Sends periodically a message to the robot
  148. * @return Nothing
  149. */
  150. void WatchDog(void *arg);
  151. };
  152. #endif // __TASKS_H__