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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. Camera camera = Camera(sm,10);
  58. int robotStarted = 0;
  59. int move = MESSAGE_ROBOT_STOP;
  60. struct etatCamera_t{
  61. int cameraStarted = 0;
  62. int getImg = 0;
  63. int askArena = 0;
  64. Arena arena = Arena();
  65. int getPos = 0;
  66. };
  67. struct etatCamera_t etatCamera;
  68. int watchdog = 0;
  69. /**********************************************************************/
  70. /* Tasks */
  71. /**********************************************************************/
  72. RT_TASK th_server;
  73. RT_TASK th_sendToMon;
  74. RT_TASK th_receiveFromMon;
  75. RT_TASK th_openComRobot;
  76. RT_TASK th_startRobot;
  77. RT_TASK th_battery;
  78. RT_TASK th_robot;
  79. RT_TASK th_watchdog;
  80. RT_TASK th_camera;
  81. RT_TASK th_startCamera;
  82. /**********************************************************************/
  83. /* Mutex */
  84. /**********************************************************************/
  85. RT_MUTEX mutex_monitor;
  86. RT_MUTEX mutex_robot;
  87. RT_MUTEX mutex_robotStarted;
  88. RT_MUTEX mutex_move;
  89. RT_MUTEX mutex_watchdog;
  90. RT_MUTEX mutex_etatCamera;
  91. /**********************************************************************/
  92. /* Semaphores */
  93. /**********************************************************************/
  94. RT_SEM sem_barrier;
  95. RT_SEM sem_openComRobot;
  96. RT_SEM sem_serverOk;
  97. RT_SEM sem_startRobot;
  98. RT_SEM sem_startWD;
  99. RT_SEM sem_startCAM;
  100. /**********************************************************************/
  101. /* Message queues */
  102. /**********************************************************************/
  103. int MSG_QUEUE_SIZE;
  104. RT_QUEUE q_messageToMon;
  105. /**********************************************************************/
  106. /* Tasks' functions */
  107. /**********************************************************************/
  108. /**
  109. * @brief Thread handling server communication with the monitor.
  110. */
  111. void ServerTask(void *arg);
  112. /**
  113. * @brief Thread sending data to monitor.
  114. */
  115. void SendToMonTask(void *arg);
  116. /**
  117. * @brief Thread receiving data from monitor.
  118. */
  119. void ReceiveFromMonTask(void *arg);
  120. /**
  121. * @brief Thread opening communication with the robot.
  122. */
  123. void OpenComRobot(void *arg);
  124. /**
  125. * @brief Thread starting the communication with the robot.
  126. */
  127. void StartRobotTask(void *arg);
  128. /**
  129. * @brief Thread handling control of the robot.
  130. */
  131. void MoveTask(void *arg);
  132. /**
  133. * @brief Thread to get battery from robot and send it to the monitor (every 500ms)
  134. */
  135. void SendBatteryTask(void *arg);
  136. /**
  137. * @brief Sends the reload message to the Robot if started with watchdog
  138. */
  139. void ReloadWatchdogTask(void *arg);
  140. /**
  141. * @brief Kills and restart the thread watchdog
  142. */
  143. void Kill_StartWD();
  144. /**
  145. * @brief Starts the Camera and the aquisition of images
  146. */
  147. void StartCamera(void * arg);
  148. /**
  149. * @brief The nominal task for the Camera
  150. */
  151. void CameraTask(void * arg);
  152. /**********************************************************************/
  153. /* Queue services */
  154. /**********************************************************************/
  155. /**
  156. * Write a message in a given queue
  157. * @param queue Queue identifier
  158. * @param msg Message to be stored
  159. */
  160. void WriteInQueue(RT_QUEUE *queue, Message *msg);
  161. /**
  162. * Read a message from a given queue, block if empty
  163. * @param queue Queue identifier
  164. * @return Message read
  165. */
  166. Message *ReadInQueue(RT_QUEUE *queue);
  167. };
  168. #endif // __TASKS_H__