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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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_battery;
  69. /**********************************************************************/
  70. /* Mutex */
  71. /**********************************************************************/
  72. RT_MUTEX mutex_monitor;
  73. RT_MUTEX mutex_robot;
  74. RT_MUTEX mutex_robotStarted;
  75. RT_MUTEX mutex_move;
  76. /**********************************************************************/
  77. /* Semaphores */
  78. /**********************************************************************/
  79. RT_SEM sem_barrier;
  80. RT_SEM sem_openComRobot;
  81. RT_SEM sem_serverOk;
  82. RT_SEM sem_startRobot;
  83. /**********************************************************************/
  84. /* Message queues */
  85. /**********************************************************************/
  86. int MSG_QUEUE_SIZE;
  87. RT_QUEUE q_messageToMon;
  88. /**********************************************************************/
  89. /* Tasks' functions */
  90. /**********************************************************************/
  91. /**
  92. * @brief Thread handling server communication with the monitor.
  93. */
  94. void ServerTask(void *arg);
  95. /**
  96. * @brief Thread sending data to monitor.
  97. */
  98. void SendToMonTask(void *arg);
  99. /**
  100. * @brief Thread receiving data from monitor.
  101. */
  102. void ReceiveFromMonTask(void *arg);
  103. /**
  104. * @brief Thread opening communication with the robot.
  105. */
  106. void OpenComRobot(void *arg);
  107. /**
  108. * @brief Thread starting the communication with the robot.
  109. */
  110. void StartRobotTask(void *arg);
  111. /**
  112. * @brief Thread handling control of the robot.
  113. */
  114. void MoveTask(void *arg);
  115. /**
  116. * @brief Thread to get battery from robot and send it to the monitor (every 500ms)
  117. */
  118. void SendBattery(void *arg);
  119. /**********************************************************************/
  120. /* Queue services */
  121. /**********************************************************************/
  122. /**
  123. * Write a message in a given queue
  124. * @param queue Queue identifier
  125. * @param msg Message to be stored
  126. */
  127. void WriteInQueue(RT_QUEUE *queue, Message *msg);
  128. /**
  129. * Read a message from a given queue, block if empty
  130. * @param queue Queue identifier
  131. * @return Message read
  132. */
  133. Message *ReadInQueue(RT_QUEUE *queue);
  134. };
  135. #endif // __TASKS_H__