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.cpp 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. #include "tasks.h"
  18. #include <stdexcept>
  19. // Déclaration des priorités des taches
  20. #define PRIORITY_TSERVER 30
  21. #define PRIORITY_TOPENCOMROBOT 20
  22. #define PRIORITY_TMOVE 20
  23. #define PRIORITY_TSENDTOMON 22
  24. #define PRIORITY_TRECEIVEFROMMON 25
  25. #define PRIORITY_TSTARTROBOT 20
  26. #define PRIORITY_TCAMERA 21
  27. #define PRIORITY_TBATTERY 31
  28. /*
  29. * Some remarks:
  30. * 1- This program is mostly a template. It shows you how to create tasks, semaphore
  31. * message queues, mutex ... and how to use them
  32. *
  33. * 2- semDumber is, as name say, useless. Its goal is only to show you how to use semaphore
  34. *
  35. * 3- Data flow is probably not optimal
  36. *
  37. * 4- Take into account that ComRobot::Write will block your task when serial buffer is full,
  38. * time for internal buffer to flush
  39. *
  40. * 5- Same behavior existe for ComMonitor::Write !
  41. *
  42. * 6- When you want to write something in terminal, use cout and terminate with endl and flush
  43. *
  44. * 7- Good luck !
  45. */
  46. /**
  47. * @brief Initialisation des structures de l'application (tâches, mutex,
  48. * semaphore, etc.)
  49. */
  50. void Tasks::Init() {
  51. int status;
  52. int err;
  53. /**************************************************************************************/
  54. /* Mutex creation */
  55. /**************************************************************************************/
  56. if (err = rt_mutex_create(&mutex_monitor, NULL)) {
  57. cerr << "Error mutex create: " << strerror(-err) << endl << flush;
  58. exit(EXIT_FAILURE);
  59. }
  60. if (err = rt_mutex_create(&mutex_robot, NULL)) {
  61. cerr << "Error mutex create: " << strerror(-err) << endl << flush;
  62. exit(EXIT_FAILURE);
  63. }
  64. if (err = rt_mutex_create(&mutex_robotStarted, NULL)) {
  65. cerr << "Error mutex create: " << strerror(-err) << endl << flush;
  66. exit(EXIT_FAILURE);
  67. }
  68. if (err = rt_mutex_create(&mutex_move, NULL)) {
  69. cerr << "Error mutex create: " << strerror(-err) << endl << flush;
  70. exit(EXIT_FAILURE);
  71. }
  72. cout << "Mutexes created successfully" << endl << flush;
  73. /**************************************************************************************/
  74. /* Semaphors creation */
  75. /**************************************************************************************/
  76. if (err = rt_sem_create(&sem_barrier, NULL, 0, S_FIFO)) {
  77. cerr << "Error semaphore create: " << strerror(-err) << endl << flush;
  78. exit(EXIT_FAILURE);
  79. }
  80. if (err = rt_sem_create(&sem_openComRobot, NULL, 0, S_FIFO)) {
  81. cerr << "Error semaphore create: " << strerror(-err) << endl << flush;
  82. exit(EXIT_FAILURE);
  83. }
  84. if (err = rt_sem_create(&sem_serverOk, NULL, 0, S_FIFO)) {
  85. cerr << "Error semaphore create: " << strerror(-err) << endl << flush;
  86. exit(EXIT_FAILURE);
  87. }
  88. if (err = rt_sem_create(&sem_startRobot, NULL, 0, S_FIFO)) {
  89. cerr << "Error semaphore create: " << strerror(-err) << endl << flush;
  90. exit(EXIT_FAILURE);
  91. }
  92. cout << "Semaphores created successfully" << endl << flush;
  93. /**************************************************************************************/
  94. /* Tasks creation */
  95. /**************************************************************************************/
  96. if (err = rt_task_create(&th_server, "th_server", 0, PRIORITY_TSERVER, 0)) {
  97. cerr << "Error task create: " << strerror(-err) << endl << flush;
  98. exit(EXIT_FAILURE);
  99. }
  100. if (err = rt_task_create(&th_sendToMon, "th_sendToMon", 0, PRIORITY_TSENDTOMON, 0)) {
  101. cerr << "Error task create: " << strerror(-err) << endl << flush;
  102. exit(EXIT_FAILURE);
  103. }
  104. if (err = rt_task_create(&th_receiveFromMon, "th_receiveFromMon", 0, PRIORITY_TRECEIVEFROMMON, 0)) {
  105. cerr << "Error task create: " << strerror(-err) << endl << flush;
  106. exit(EXIT_FAILURE);
  107. }
  108. if (err = rt_task_create(&th_openComRobot, "th_openComRobot", 0, PRIORITY_TOPENCOMROBOT, 0)) {
  109. cerr << "Error task create: " << strerror(-err) << endl << flush;
  110. exit(EXIT_FAILURE);
  111. }
  112. if (err = rt_task_create(&th_startRobot, "th_startRobot", 0, PRIORITY_TSTARTROBOT, 0)) {
  113. cerr << "Error task create: " << strerror(-err) << endl << flush;
  114. exit(EXIT_FAILURE);
  115. }
  116. if (err = rt_task_create(&th_move, "th_move", 0, PRIORITY_TMOVE, 0)) {
  117. cerr << "Error task create: " << strerror(-err) << endl << flush;
  118. exit(EXIT_FAILURE);
  119. }
  120. if (err = rt_task_create(&th_getBattery, "th_getBattery", 0, PRIORITY_TBATTERY, 0)) {
  121. cerr << "Error task create: " << strerror(-err) << endl << flush;
  122. exit(EXIT_FAILURE);
  123. }
  124. cout << "Tasks created successfully" << endl << flush;
  125. /**************************************************************************************/
  126. /* Message queues creation */
  127. /**************************************************************************************/
  128. if ((err = rt_queue_create(&q_messageToMon, "q_messageToMon", sizeof (Message*)*50, Q_UNLIMITED, Q_FIFO)) < 0) {
  129. cerr << "Error msg queue create: " << strerror(-err) << endl << flush;
  130. exit(EXIT_FAILURE);
  131. }
  132. cout << "Queues created successfully" << endl << flush;
  133. }
  134. /**
  135. * @brief Démarrage des tâches
  136. */
  137. void Tasks::Run() {
  138. rt_task_set_priority(NULL, T_LOPRIO);
  139. int err;
  140. if (err = rt_task_start(&th_server, (void(*)(void*)) & Tasks::ServerTask, this)) {
  141. cerr << "Error task start: " << strerror(-err) << endl << flush;
  142. exit(EXIT_FAILURE);
  143. }
  144. if (err = rt_task_start(&th_sendToMon, (void(*)(void*)) & Tasks::SendToMonTask, this)) {
  145. cerr << "Error task start: " << strerror(-err) << endl << flush;
  146. exit(EXIT_FAILURE);
  147. }
  148. if (err = rt_task_start(&th_receiveFromMon, (void(*)(void*)) & Tasks::ReceiveFromMonTask, this)) {
  149. cerr << "Error task start: " << strerror(-err) << endl << flush;
  150. exit(EXIT_FAILURE);
  151. }
  152. if (err = rt_task_start(&th_openComRobot, (void(*)(void*)) & Tasks::OpenComRobot, this)) {
  153. cerr << "Error task start: " << strerror(-err) << endl << flush;
  154. exit(EXIT_FAILURE);
  155. }
  156. if (err = rt_task_start(&th_startRobot, (void(*)(void*)) & Tasks::StartRobotTask, this)) {
  157. cerr << "Error task start: " << strerror(-err) << endl << flush;
  158. exit(EXIT_FAILURE);
  159. }
  160. if (err = rt_task_start(&th_move, (void(*)(void*)) & Tasks::MoveTask, this)) {
  161. cerr << "Error task start: " << strerror(-err) << endl << flush;
  162. exit(EXIT_FAILURE);
  163. }
  164. if (err = rt_task_start(&th_getBattery, (void(*)(void*)) & Tasks::ReadBattery, this)) {
  165. cerr << "Error task start: " << strerror(-err) << endl << flush;
  166. exit(EXIT_FAILURE);
  167. }
  168. cout << "Tasks launched" << endl << flush;
  169. }
  170. /**
  171. * @brief Arrêt des tâches
  172. */
  173. void Tasks::Stop() {
  174. monitor.Close();
  175. robot.Close();
  176. }
  177. /**
  178. */
  179. void Tasks::Join() {
  180. cout << "Tasks synchronized" << endl << flush;
  181. rt_sem_broadcast(&sem_barrier);
  182. pause();
  183. }
  184. /**
  185. * @brief Thread handling server communication with the monitor.
  186. */
  187. void Tasks::ServerTask(void *arg) {
  188. int status;
  189. cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
  190. // Synchronization barrier (waiting that all tasks are started)
  191. rt_sem_p(&sem_barrier, TM_INFINITE);
  192. /**************************************************************************************/
  193. /* The task server starts here */
  194. /**************************************************************************************/
  195. rt_mutex_acquire(&mutex_monitor, TM_INFINITE);
  196. status = monitor.Open(SERVER_PORT);
  197. rt_mutex_release(&mutex_monitor);
  198. cout << "Open server on port " << (SERVER_PORT) << " (" << status << ")" << endl;
  199. if (status < 0) throw std::runtime_error {
  200. "Unable to start server on port " + std::to_string(SERVER_PORT)
  201. };
  202. monitor.AcceptClient(); // Wait the monitor client
  203. cout << "Rock'n'Roll baby, client accepted!" << endl << flush;
  204. rt_sem_broadcast(&sem_serverOk);
  205. }
  206. /**
  207. * @brief Thread sending data to monitor.
  208. */
  209. void Tasks::SendToMonTask(void* arg) {
  210. Message *msg;
  211. cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
  212. // Synchronization barrier (waiting that all tasks are starting)
  213. rt_sem_p(&sem_barrier, TM_INFINITE);
  214. /**************************************************************************************/
  215. /* The task sendToMon starts here */
  216. /**************************************************************************************/
  217. rt_sem_p(&sem_serverOk, TM_INFINITE);
  218. while (1) {
  219. cout << "wait msg to send" << endl << flush;
  220. msg = ReadInQueue(&q_messageToMon);
  221. cout << "Send msg to mon: " << msg->ToString() << endl << flush;
  222. rt_mutex_acquire(&mutex_monitor, TM_INFINITE);
  223. monitor.Write(msg); // The message is deleted with the Write
  224. rt_mutex_release(&mutex_monitor);
  225. }
  226. }
  227. /**
  228. * @brief Thread receiving data from monitor.
  229. */
  230. void Tasks::ReceiveFromMonTask(void *arg) {
  231. Message *msgRcv;
  232. cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
  233. // Synchronization barrier (waiting that all tasks are starting)
  234. rt_sem_p(&sem_barrier, TM_INFINITE);
  235. /**************************************************************************************/
  236. /* The task receiveFromMon starts here */
  237. /**************************************************************************************/
  238. rt_sem_p(&sem_serverOk, TM_INFINITE);
  239. cout << "Received message from monitor activated" << endl << flush;
  240. while (1) {
  241. msgRcv = monitor.Read();
  242. cout << "Rcv <= " << msgRcv->ToString() << endl << flush;
  243. if (msgRcv->CompareID(MESSAGE_MONITOR_LOST)) {
  244. delete(msgRcv);
  245. exit(-1);
  246. } else if (msgRcv->CompareID(MESSAGE_ROBOT_COM_OPEN)) {
  247. rt_sem_v(&sem_openComRobot);
  248. } else if (msgRcv->CompareID(MESSAGE_ROBOT_START_WITHOUT_WD)) {
  249. rt_sem_v(&sem_startRobot);
  250. } else if (msgRcv->CompareID(MESSAGE_ROBOT_GO_FORWARD) ||
  251. msgRcv->CompareID(MESSAGE_ROBOT_GO_BACKWARD) ||
  252. msgRcv->CompareID(MESSAGE_ROBOT_GO_LEFT) ||
  253. msgRcv->CompareID(MESSAGE_ROBOT_GO_RIGHT) ||
  254. msgRcv->CompareID(MESSAGE_ROBOT_STOP)) {
  255. rt_mutex_acquire(&mutex_move, TM_INFINITE);
  256. move = msgRcv->GetID();
  257. rt_mutex_release(&mutex_move);
  258. }
  259. delete(msgRcv); // mus be deleted manually, no consumer
  260. }
  261. }
  262. /**
  263. * @brief Thread opening communication with the robot.
  264. */
  265. void Tasks::OpenComRobot(void *arg) {
  266. int status;
  267. int err;
  268. cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
  269. // Synchronization barrier (waiting that all tasks are starting)
  270. rt_sem_p(&sem_barrier, TM_INFINITE);
  271. /**************************************************************************************/
  272. /* The task openComRobot starts here */
  273. /**************************************************************************************/
  274. while (1) {
  275. rt_sem_p(&sem_openComRobot, TM_INFINITE);
  276. cout << "Open serial com (";
  277. rt_mutex_acquire(&mutex_robot, TM_INFINITE);
  278. status = robot.Open();
  279. rt_mutex_release(&mutex_robot);
  280. cout << status;
  281. cout << ")" << endl << flush;
  282. Message * msgSend;
  283. if (status < 0) {
  284. msgSend = new Message(MESSAGE_ANSWER_NACK);
  285. } else {
  286. msgSend = new Message(MESSAGE_ANSWER_ACK);
  287. }
  288. WriteInQueue(&q_messageToMon, msgSend); // msgSend will be deleted by sendToMon
  289. }
  290. }
  291. /**
  292. * @brief Thread starting the communication with the robot.
  293. */
  294. void Tasks::StartRobotTask(void *arg) {
  295. cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
  296. // Synchronization barrier (waiting that all tasks are starting)
  297. rt_sem_p(&sem_barrier, TM_INFINITE);
  298. /**************************************************************************************/
  299. /* The task startRobot starts here */
  300. /**************************************************************************************/
  301. while (1) {
  302. Message * msgSend;
  303. rt_sem_p(&sem_startRobot, TM_INFINITE);
  304. cout << "Start robot without watchdog (";
  305. rt_mutex_acquire(&mutex_robot, TM_INFINITE);
  306. msgSend = robot.Write(robot.StartWithoutWD());
  307. rt_mutex_release(&mutex_robot);
  308. cout << msgSend->GetID();
  309. cout << ")" << endl;
  310. cout << "Movement answer: " << msgSend->ToString() << endl << flush;
  311. WriteInQueue(&q_messageToMon, msgSend); // msgSend will be deleted by sendToMon
  312. if (msgSend->GetID() == MESSAGE_ANSWER_ACK) {
  313. rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE);
  314. robotStarted = 1;
  315. rt_mutex_release(&mutex_robotStarted);
  316. }
  317. }
  318. }
  319. /**
  320. * @brief Thread handling control of the robot.
  321. */
  322. void Tasks::MoveTask(void *arg) {
  323. int rs;
  324. int previousMove = MESSAGE_ROBOT_GO_FORWARD;
  325. int cpMove;
  326. cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
  327. // Synchronization barrier (waiting that all tasks are starting)
  328. rt_sem_p(&sem_barrier, TM_INFINITE);
  329. /**************************************************************************************/
  330. /* The task starts here */
  331. /**************************************************************************************/
  332. rt_task_set_periodic(NULL, TM_NOW, 100000000);
  333. while (1) {
  334. rt_task_wait_period(NULL);
  335. cout << "Periodic movement update";
  336. rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE);
  337. rs = robotStarted;
  338. rt_mutex_release(&mutex_robotStarted);
  339. if (rs == 1) {
  340. rt_mutex_acquire(&mutex_move, TM_INFINITE);
  341. cpMove = move;
  342. rt_mutex_release(&mutex_move);
  343. if (cpMove != previousMove) {
  344. cout << " move: " << cpMove;
  345. rt_mutex_acquire(&mutex_robot, TM_INFINITE);
  346. robot.Write(new Message((MessageID)cpMove));
  347. rt_mutex_release(&mutex_robot);
  348. previousMove = cpMove;
  349. }
  350. }
  351. cout << endl << flush;
  352. }
  353. }
  354. /**
  355. * Write a message in a given queue
  356. * @param queue Queue identifier
  357. * @param msg Message to be stored
  358. */
  359. void Tasks::WriteInQueue(RT_QUEUE *queue, Message *msg) {
  360. int err;
  361. if ((err = rt_queue_write(queue, (const void *) &msg, sizeof ((const void *) &msg), Q_NORMAL)) < 0) {
  362. cerr << "Write in queue failed: " << strerror(-err) << endl << flush;
  363. throw std::runtime_error{"Error in write in queue"};
  364. }
  365. }
  366. /**
  367. * Read a message from a given queue, block if empty
  368. * @param queue Queue identifier
  369. * @return Message read
  370. */
  371. Message *Tasks::ReadInQueue(RT_QUEUE *queue) {
  372. int err;
  373. Message *msg;
  374. if ((err = rt_queue_read(queue, &msg, sizeof ((void*) &msg), TM_INFINITE)) < 0) {
  375. cout << "Read in queue failed: " << strerror(-err) << endl << flush;
  376. throw std::runtime_error{"Error in read in queue"};
  377. }/** else {
  378. cout << "@msg :" << msg << endl << flush;
  379. } /**/
  380. return msg;
  381. }
  382. void Tasks::ReadBattery(void *arg){
  383. cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
  384. // Synchronization barrier (waiting that all tasks are starting)
  385. rt_sem_p(&sem_barrier, TM_INFINITE);
  386. /**************************************************************************************/
  387. /* The task starts here */
  388. /**************************************************************************************/
  389. rt_task_set_periodic(NULL, TM_NOW, 500000000);
  390. int rs;
  391. while (1) {
  392. rt_task_wait_period(NULL);
  393. cout << "Periodic battery get lvl \n";
  394. rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE);
  395. rs = robotStarted;
  396. rt_mutex_release(&mutex_robotStarted);
  397. if (rs == 1) {
  398. rt_mutex_acquire(&mutex_robot, TM_INFINITE);
  399. Message *msg = robot.Write( robot.GetBattery() ) ;
  400. rt_mutex_release(&mutex_robot);
  401. if (msg->CompareID((MessageID)MESSAGE_ROBOT_BATTERY_LEVEL)) {
  402. rt_mutex_acquire(&mutex_monitor, TM_INFINITE);
  403. monitor.Write(msg);
  404. rt_mutex_release(&mutex_monitor);
  405. }
  406. }
  407. cout << endl << flush;
  408. }
  409. }