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

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