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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. if ((err = rt_queue_create(&q_messageComRobot, "q_messageComRobot", sizeof (Message*)*50, Q_UNLIMITED, Q_FIFO)) < 0) {
  133. cerr << "Error messageComRobot queue create: " << strerror(-err) << endl << flush;
  134. exit(EXIT_FAILURE);
  135. }
  136. if ((err = rt_queue_create(&q_messageControlRobot, "q_messageControlRobot", sizeof (Message*)*50, Q_UNLIMITED, Q_FIFO)) < 0) {
  137. cerr << "Error messageControlRobot queue create: " << strerror(-err) << endl << flush;
  138. exit(EXIT_FAILURE);
  139. }
  140. if ((err = rt_queue_create(&q_messageControlCam, "q_messageControlCam", sizeof (Message*)*50, Q_UNLIMITED, Q_FIFO)) < 0) {
  141. cerr << "Error messageControlCam queue create: " << strerror(-err) << endl << flush;
  142. exit(EXIT_FAILURE);
  143. }
  144. cout << "Queues created successfully" << endl << flush;
  145. }
  146. /**
  147. * @brief Démarrage des tâches
  148. */
  149. void Tasks::Run() {
  150. rt_task_set_priority(NULL, T_LOPRIO);
  151. int err;
  152. if (err = rt_task_start(&th_server, (void(*)(void*)) & Tasks::ServerTask, this)) {
  153. cerr << "Error task start: " << strerror(-err) << endl << flush;
  154. exit(EXIT_FAILURE);
  155. }
  156. if (err = rt_task_start(&th_sendToMon, (void(*)(void*)) & Tasks::SendToMonTask, this)) {
  157. cerr << "Error task start: " << strerror(-err) << endl << flush;
  158. exit(EXIT_FAILURE);
  159. }
  160. if (err = rt_task_start(&th_receiveFromMon, (void(*)(void*)) & Tasks::ReceiveFromMonTask, this)) {
  161. cerr << "Error task start: " << strerror(-err) << endl << flush;
  162. exit(EXIT_FAILURE);
  163. }
  164. if (err = rt_task_start(&th_openComRobot, (void(*)(void*)) & Tasks::OpenComRobot, this)) {
  165. cerr << "Error task start: " << strerror(-err) << endl << flush;
  166. exit(EXIT_FAILURE);
  167. }
  168. if (err = rt_task_start(&th_startRobot, (void(*)(void*)) & Tasks::StartRobotTask, this)) {
  169. cerr << "Error task start: " << strerror(-err) << endl << flush;
  170. exit(EXIT_FAILURE);
  171. }
  172. if (err = rt_task_start(&th_move, (void(*)(void*)) & Tasks::MoveTask, this)) {
  173. cerr << "Error task start: " << strerror(-err) << endl << flush;
  174. exit(EXIT_FAILURE);
  175. }
  176. if (err = rt_task_start(&th_getBattery, (void(*)(void*)) & Tasks::ReadBattery, this)) {
  177. cerr << "Error task start: " << strerror(-err) << endl << flush;
  178. exit(EXIT_FAILURE);
  179. }
  180. cout << "Tasks launched" << endl << flush;
  181. }
  182. /**
  183. * @brief Arrêt des tâches
  184. */
  185. void Tasks::Stop() {
  186. monitor.Close();
  187. robot.Close();
  188. }
  189. /**
  190. */
  191. void Tasks::Join() {
  192. cout << "Tasks synchronized" << endl << flush;
  193. rt_sem_broadcast(&sem_barrier);
  194. pause();
  195. }
  196. /**
  197. * @brief Thread handling server communication with the monitor.
  198. */
  199. void Tasks::ServerTask(void *arg) {
  200. int status;
  201. cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
  202. // Synchronization barrier (waiting that all tasks are started)
  203. rt_sem_p(&sem_barrier, TM_INFINITE);
  204. /**************************************************************************************/
  205. /* The task server starts here */
  206. /**************************************************************************************/
  207. rt_mutex_acquire(&mutex_monitor, TM_INFINITE);
  208. status = monitor.Open(SERVER_PORT);
  209. rt_mutex_release(&mutex_monitor);
  210. cout << "Open server on port " << (SERVER_PORT) << " (" << status << ")" << endl;
  211. if (status < 0) throw std::runtime_error {
  212. "Unable to start server on port " + std::to_string(SERVER_PORT)
  213. };
  214. monitor.AcceptClient(); // Wait the monitor client
  215. cout << "Rock'n'Roll baby, client accepted!" << endl << flush;
  216. rt_sem_broadcast(&sem_serverOk);
  217. }
  218. /**
  219. * @brief Thread sending data to monitor.
  220. */
  221. void Tasks::SendToMonTask(void* arg) {
  222. Message *msg;
  223. cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
  224. // Synchronization barrier (waiting that all tasks are starting)
  225. rt_sem_p(&sem_barrier, TM_INFINITE);
  226. /**************************************************************************************/
  227. /* The task sendToMon starts here */
  228. /**************************************************************************************/
  229. rt_sem_p(&sem_serverOk, TM_INFINITE);
  230. while (1) {
  231. cout << "wait msg to send" << endl << flush;
  232. msg = ReadInQueue(&q_messageToMon);
  233. cout << "Send msg to mon: " << msg->ToString() << endl << flush;
  234. rt_mutex_acquire(&mutex_monitor, TM_INFINITE);
  235. monitor.Write(msg); // The message is deleted with the Write
  236. rt_mutex_release(&mutex_monitor);
  237. }
  238. }
  239. /**
  240. * @brief Thread receiving data from monitor.
  241. */
  242. void Tasks::ReceiveFromMonTask(void *arg) {
  243. Message *msgRcv;
  244. cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
  245. // Synchronization barrier (waiting that all tasks are starting)
  246. rt_sem_p(&sem_barrier, TM_INFINITE);
  247. /**************************************************************************************/
  248. /* The task receiveFromMon starts here */
  249. /**************************************************************************************/
  250. while(1){
  251. rt_sem_p(&sem_serverOk, TM_INFINITE);
  252. cout << "Received message from monitor activated" << endl << flush;
  253. while (1) {
  254. msgRcv = monitor.Read();
  255. cout << "Rcv <= " << msgRcv->ToString() << endl << flush;
  256. if (msgRcv->CompareID(MESSAGE_MONITOR_LOST)) {
  257. delete(msgRcv);
  258. WriteInQueue(&q_messageComRobot, new Message(MESSAGE_ROBOT_COM_CLOSE));
  259. WriteInQueue(&q_messageControlRobot, new Message(MESSAGE_ROBOT_RESET));
  260. WriteInQueue(&q_messageControlCam, new Message(MESSAGE_CAM_CLOSE));
  261. rt_sem_v(&sem_restartServer, TM_INFINITE); // A VERIFIER
  262. break;
  263. } else if (msgRcv->CompareID(MESSAGE_ROBOT_COM_OPEN) || msgRcv->CompareID(MESSAGE_ROBOT_COM_CLOSE)) {
  264. WriteInQueue(&q_messageComRobot, msgRcv);
  265. //rt_sem_v(&sem_openComRobot);
  266. } else if (msgRcv->CompareID(MESSAGE_ROBOT_START_WITHOUT_WD) || msgRcv->CompareID(MESSAGE_ROBOT_START_WITH_WD)) {
  267. WriteInQueue(&q_messageControlRobot, msgRcv);
  268. //rt_sem_v(&sem_startRobot);
  269. }
  270. else if (msgRcv->CompareID(MESSAGE_ROBOT_GO_FORWARD) ||
  271. msgRcv->CompareID(MESSAGE_ROBOT_GO_BACKWARD) ||
  272. msgRcv->CompareID(MESSAGE_ROBOT_GO_LEFT) ||
  273. msgRcv->CompareID(MESSAGE_ROBOT_GO_RIGHT) ||
  274. msgRcv->CompareID(MESSAGE_ROBOT_STOP)) {
  275. rt_mutex_acquire(&mutex_move, TM_INFINITE);
  276. move = msgRcv->GetID();
  277. rt_mutex_release(&mutex_move);
  278. }
  279. else if (msgRcv->CompareID(MESSAGE_CAM_OPEN) ||
  280. msgRcv->CompareID(MESSAGE_CAM_CLOSE) ||
  281. msgRcv->CompareID(MESSAGE_CAM_ASK_ARENA) ||
  282. msgRcv->CompareID(MESSAGE_CAM_ARENA_CONFIRM) ||
  283. msgRcv->CompareID(MESSAGE_CAM_ARENA_INFIRM) ||
  284. msgRcv->CompareID(MESSAGE_CAM_POSITION_COMPUTE_START) ||
  285. msgRcv->CompareID(MESSAGE_CAM_POSITION_COMPUTE_STOP) ||
  286. )
  287. {
  288. WriteInQueue(&q_messageControlCam, msgRcv);
  289. }
  290. delete(msgRcv); // mus be deleted manually, no consumer
  291. }
  292. }
  293. }
  294. /**
  295. * @brief Thread opening communication with the robot.
  296. */
  297. void Tasks::OpenComRobot(void *arg) {
  298. int status;
  299. int err;
  300. cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
  301. // Synchronization barrier (waiting that all tasks are starting)
  302. rt_sem_p(&sem_barrier, TM_INFINITE);
  303. /**************************************************************************************/
  304. /* The task openComRobot starts here */
  305. /**************************************************************************************/
  306. while (1) {
  307. rt_sem_p(&sem_openComRobot, TM_INFINITE);
  308. cout << "Open serial com (";
  309. rt_mutex_acquire(&mutex_robot, TM_INFINITE);
  310. status = robot.Open();
  311. rt_mutex_release(&mutex_robot);
  312. cout << status;
  313. cout << ")" << endl << flush;
  314. Message * msgSend;
  315. if (status < 0) {
  316. msgSend = new Message(MESSAGE_ANSWER_NACK);
  317. } else {
  318. msgSend = new Message(MESSAGE_ANSWER_ACK);
  319. }
  320. WriteInQueue(&q_messageToMon, msgSend); // msgSend will be deleted by sendToMon
  321. }
  322. }
  323. /**
  324. * @brief Thread starting the communication with the robot.
  325. */
  326. void Tasks::StartRobotTask(void *arg) {
  327. cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
  328. // Synchronization barrier (waiting that all tasks are starting)
  329. rt_sem_p(&sem_barrier, TM_INFINITE);
  330. /**************************************************************************************/
  331. /* The task startRobot starts here */
  332. /**************************************************************************************/
  333. while (1) {
  334. Message * tmp;
  335. Message * msgSend;
  336. tmp = ReadInQueue(&q_messageControlRobot);
  337. if (tmp -> CompareID(MESSAGE_ROBOT_START_WITHOUT_WD)){
  338. cout << "Start robot without watchdog (";
  339. rt_mutex_acquire(&mutex_robot, TM_INFINITE);
  340. msgSend = robot.Write(robot.StartWithoutWD());
  341. rt_mutex_release(&mutex_robot);
  342. cout << msgSend->GetID();
  343. cout << ")" << endl;
  344. cout << "Movement answer: " << msgSend->ToString() << endl << flush;
  345. WriteInQueue(&q_messageToMon, msgSend); // msgSend will be deleted by sendToMon
  346. if (msgSend->GetID() == MESSAGE_ANSWER_ACK) {
  347. rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE);
  348. robotStarted = 1;
  349. rt_mutex_release(&mutex_robotStarted);
  350. }
  351. } else if (tmp -> CompareID(MESSAGE_ROBOT_START_WITH_WD)){
  352. cout << "Start robot with watchdog (";
  353. rt_mutex_acquire(&mutex_robot, TM_INFINITE);
  354. msgSend = robot.Write(robot.StartWithWD());
  355. rt_mutex_release(&mutex_robot);
  356. cout << msgSend->GetID();
  357. cout << ")" << endl;
  358. cout << "Movement answer: " << msgSend->ToString() << endl << flush;
  359. WriteInQueue(&q_messageToMon, msgSend); // msgSend will be deleted by sendToMon
  360. if (msgSend->GetID() == MESSAGE_ANSWER_ACK) {
  361. rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE);
  362. robotStarted = 1;
  363. rt_mutex_release(&mutex_robotStarted);
  364. rt_task_set_periodic(&th_watchDog,TM_NOW,1000000000);
  365. }
  366. } else if (tmp -> CompareID(MESSAGE_ROBOT_RESET)){
  367. rt_task_set_periodic(&th_watchDog,TM_NOW,0);
  368. cout << "Stopping Robot (";
  369. rt_mutex_acquire(&mutex_robot, TM_INFINITE);
  370. msgSend = robot.Write(new Message(MESSAGE_ROBOT_RESET));
  371. rt_mutex_release(&mutex_robot);
  372. cout << msgSend->GetID();
  373. cout << ")" << endl;
  374. cout << "Movement answer: " << msgSend->ToString() << endl << flush;
  375. WriteInQueue(&q_messageToMon, msgSend); // msgSend will be deleted by sendToMon
  376. rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE);
  377. robotStarted = 0;
  378. rt_mutex_release(&mutex_robotStarted);
  379. }
  380. }
  381. }
  382. void Tasks::WatchDog(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. rt_task_set_periodic(NULL, TM_NOW, 0);
  387. while (1) {
  388. rt_task_wait_period(NULL);
  389. Message * msgSend;
  390. rt_mutex_acquire(&mutex_robot, TM_INFINITE);
  391. msgSend = robot.ReloadWD();
  392. rt_mutex_release(&mutex_robot);
  393. }
  394. }
  395. /**
  396. * @brief Thread handling control of the robot.
  397. */
  398. void Tasks::MoveTask(void *arg) {
  399. int rs;
  400. int previousMove = MESSAGE_ROBOT_GO_FORWARD;
  401. int cpMove;
  402. cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
  403. // Synchronization barrier (waiting that all tasks are starting)
  404. rt_sem_p(&sem_barrier, TM_INFINITE);
  405. /**************************************************************************************/
  406. /* The task starts here */
  407. /**************************************************************************************/
  408. rt_task_set_periodic(NULL, TM_NOW, 100000000);
  409. while (1) {
  410. rt_task_wait_period(NULL);
  411. cout << "Periodic movement update";
  412. rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE);
  413. rs = robotStarted;
  414. rt_mutex_release(&mutex_robotStarted);
  415. if (rs == 1) {
  416. rt_mutex_acquire(&mutex_move, TM_INFINITE);
  417. cpMove = move;
  418. rt_mutex_release(&mutex_move);
  419. if (cpMove != previousMove) {
  420. cout << " move: " << cpMove;
  421. rt_mutex_acquire(&mutex_robot, TM_INFINITE);
  422. robot.Write(new Message((MessageID)cpMove));
  423. rt_mutex_release(&mutex_robot);
  424. previousMove = cpMove;
  425. }
  426. }
  427. cout << endl << flush;
  428. }
  429. }
  430. /**
  431. * Write a message in a given queue
  432. * @param queue Queue identifier
  433. * @param msg Message to be stored
  434. */
  435. void Tasks::WriteInQueue(RT_QUEUE *queue, Message *msg) {
  436. int err;
  437. if ((err = rt_queue_write(queue, (const void *) &msg, sizeof ((const void *) &msg), Q_NORMAL)) < 0) {
  438. cerr << "Write in queue failed: " << strerror(-err) << endl << flush;
  439. throw std::runtime_error{"Error in write in queue"};
  440. }
  441. }
  442. /**
  443. * Read a message from a given queue, block if empty
  444. * @param queue Queue identifier
  445. * @return Message read
  446. */
  447. Message *Tasks::ReadInQueue(RT_QUEUE *queue) {
  448. int err;
  449. Message *msg;
  450. if ((err = rt_queue_read(queue, &msg, sizeof ((void*) &msg), TM_INFINITE)) < 0) {
  451. cout << "Read in queue failed: " << strerror(-err) << endl << flush;
  452. throw std::runtime_error{"Error in read in queue"};
  453. }/** else {
  454. cout << "@msg :" << msg << endl << flush;
  455. } /**/
  456. return msg;
  457. }
  458. void Tasks::ReadBattery(void *arg){
  459. cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
  460. // Synchronization barrier (waiting that all tasks are starting)
  461. rt_sem_p(&sem_barrier, TM_INFINITE);
  462. /**************************************************************************************/
  463. /* The task starts here */
  464. /**************************************************************************************/
  465. rt_task_set_periodic(NULL, TM_NOW, 500000000);
  466. int rs;
  467. while (1) {
  468. rt_task_wait_period(NULL);
  469. cout << "Periodic battery get lvl \n";
  470. rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE);
  471. rs = robotStarted;
  472. rt_mutex_release(&mutex_robotStarted);
  473. if (rs == 1) {
  474. rt_mutex_acquire(&mutex_robot, TM_INFINITE);
  475. Message *msg = robot.Write( robot.GetBattery() ) ;
  476. rt_mutex_release(&mutex_robot);
  477. if (msg->CompareID((MessageID)MESSAGE_ROBOT_BATTERY_LEVEL)) {
  478. rt_mutex_acquire(&mutex_monitor, TM_INFINITE);
  479. monitor.Write(msg);
  480. rt_mutex_release(&mutex_monitor);
  481. }
  482. }
  483. cout << endl << flush;
  484. }
  485. }