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.

messages.h 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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 __MESSAGES_H__
  18. #define __MESSAGES_H__
  19. #include <string>
  20. #include "img.h"
  21. /**
  22. * Message ID defined for system communication
  23. *
  24. * @brief List of available message ID
  25. *
  26. */
  27. typedef enum {
  28. //Generic messages
  29. MESSAGE_EMPTY = 0,
  30. MESSAGE_LOG,
  31. // Message containing answer (after robot command, or for monitor)
  32. MESSAGE_ANSWER,
  33. // messages for serial communication with robot
  34. MESSAGE_OPEN_COM,
  35. MESSAGE_CLOSE_COM,
  36. // Messages specific to server
  37. MESSAGE_MONITOR_LOST,
  38. // Messages for camera
  39. MESSAGE_CAM_OPEN,
  40. MESSAGE_CAM_CLOSE,
  41. MESSAGE_ASK_ARENA,
  42. MESSAGE_ARENA_CONFIRM,
  43. MESSAGE_ARENA_INFIRM,
  44. MESSAGE_COMPUTE_POSITION,
  45. MESSAGE_STOP_COMPUTE_POSITION,
  46. MESSAGE_POSITION,
  47. MESSAGE_IMAGE,
  48. // Messages for robot
  49. MESSAGE_ROBOT_PING,
  50. MESSAGE_ROBOT_RESET,
  51. MESSAGE_ROBOT_START_WITH_WD,
  52. MESSAGE_ROBOT_START_WITHOUT_WD,
  53. MESSAGE_ROBOT_RELOAD_WD,
  54. MESSAGE_ROBOT_MOVE,
  55. MESSAGE_ROBOT_TURN,
  56. MESSAGE_ROBOT_GO_FORWARD,
  57. MESSAGE_ROBOT_GO_BACK,
  58. MESSAGE_ROBOT_GO_LEFT,
  59. MESSAGE_ROBOT_GO_RIGHT,
  60. MESSAGE_ROBOT_STOP,
  61. MESSAGE_ROBOT_POWEROFF,
  62. MESSAGE_ROBOT_GET_BATTERY,
  63. MESSAGE_ROBOT_BATTERY_LEVEL,
  64. MESSAGE_ROBOT_GET_STATE,
  65. MESSAGE_ROBOT_CURRENT_STATE
  66. } MessageID;
  67. typedef enum {
  68. ANSWER_ACK=0,
  69. ANSWER_NACK,
  70. ANSWER_LOST_ROBOT,
  71. ANSWER_ROBOT_TIMEOUT,
  72. ANSWER_ROBOT_UNKNOWN_COMMAND,
  73. ANSWER_ROBOT_ERROR,
  74. ANSWER_ROBOT_CHECKSUM
  75. } AnswerID;
  76. typedef enum {
  77. BATTERY_UNKNOWN=-1,
  78. BATTERY_EMPTY=0,
  79. BATTERY_LOW,
  80. BATTERY_FULL
  81. } BatteryLevel;
  82. typedef enum {
  83. ROBOT_NOT_BUSY=0,
  84. ROBOT_BUSY
  85. } RobotState;
  86. using namespace std;
  87. /**
  88. * Base class for messaging
  89. *
  90. * @brief Base class for messaging
  91. *
  92. */
  93. class Message {
  94. public:
  95. /**
  96. * Create a new, empty message
  97. */
  98. Message();
  99. /**
  100. * Create a new, empty message
  101. */
  102. Message(MessageID id);
  103. /**
  104. * Destroy message
  105. */
  106. virtual ~Message();
  107. /**
  108. * Translate content of message into a string that can be displayed
  109. * @return A string describing message contents
  110. */
  111. virtual string ToString();
  112. /**
  113. * Allocate a new mesage and copy contents of current message
  114. * @return A message, copy of current
  115. */
  116. virtual Message* Copy();
  117. /**
  118. * Get message ID
  119. * @return Current message ID
  120. */
  121. MessageID GetID() {
  122. return messageID;
  123. }
  124. /**
  125. * Set message ID
  126. * @param id Message ID
  127. */
  128. virtual void SetID(MessageID id);
  129. /**
  130. * Comparison operator
  131. * @param msg Message to be compared
  132. * @return true if message are equal, false otherwise
  133. */
  134. virtual bool operator==(const Message& msg) {
  135. return (messageID == msg.messageID);
  136. }
  137. /**
  138. * Difference operator
  139. * @param msg Message to be compared
  140. * @return true if message are different, false otherwise
  141. */
  142. virtual bool operator!=(const Message& msg) {
  143. return !(messageID == msg.messageID);
  144. }
  145. protected:
  146. /**
  147. * Message identifier (@see MessageID)
  148. */
  149. MessageID messageID;
  150. /**
  151. * Verify if message ID is compatible with current message type
  152. * @param id Message ID
  153. * @return true, if message ID is acceptable, false otherwise
  154. */
  155. virtual bool CheckID(MessageID id);
  156. };
  157. /**
  158. * Message class for holding float value, based on Message class
  159. *
  160. * @brief Float message class
  161. *
  162. */
  163. class MessageInt : public Message {
  164. public:
  165. /**
  166. * Create a new, empty float message
  167. */
  168. MessageInt();
  169. /**
  170. * Create a new float message, with given ID and value
  171. * @param id Message ID
  172. * @param val Message value
  173. * @throw std::runtime_error if message ID is incompatible with float data
  174. */
  175. MessageInt(MessageID id, int val);
  176. /**
  177. * Set message ID
  178. * @param id Message ID
  179. * @throw std::runtime_error if message ID is incompatible with float data
  180. */
  181. void SetID(MessageID id);
  182. /**
  183. * Get message value (int)
  184. * @return int value
  185. */
  186. int GetValue() {
  187. return value;
  188. }
  189. /**
  190. * Set message value (int)
  191. * @param val int value to store in message
  192. */
  193. void SetValue(int val) {
  194. this->value = val;
  195. }
  196. /**
  197. * Translate content of message into a string that can be displayed
  198. * @return A string describing message contents
  199. */
  200. string ToString();
  201. /**
  202. * Allocate a new mesage and copy contents of current message
  203. * @return A message, copy of current
  204. */
  205. Message* Copy();
  206. /**
  207. * Comparison operator
  208. * @param msg Message to be compared
  209. * @return true if message are equal, false otherwise
  210. */
  211. virtual bool operator==(const MessageInt& msg) {
  212. return ((messageID == msg.messageID) && (value == msg.value));
  213. }
  214. /**
  215. * Difference operator
  216. * @param msg Message to be compared
  217. * @return true if message are different, false otherwise
  218. */
  219. virtual bool operator!=(const MessageInt& msg) {
  220. return !((messageID == msg.messageID) && (value == msg.value));
  221. }
  222. protected:
  223. /**
  224. * Message integer value
  225. */
  226. int value;
  227. /**
  228. * Verify if message ID is compatible with current message type
  229. * @param id Message ID
  230. * @return true, if message ID is acceptable, false otherwise
  231. */
  232. bool CheckID(MessageID id);
  233. };
  234. /**
  235. * Message class for holding string value, based on Message class
  236. *
  237. * @brief String message class
  238. *
  239. */
  240. class MessageString : public Message {
  241. public:
  242. /**
  243. * Create a new, empty string message
  244. */
  245. MessageString();
  246. /**
  247. * Create a new string message, with given ID and string
  248. * @param id Message ID
  249. * @param s Message string
  250. * @throw std::runtime_error if message ID is incompatible with string data
  251. */
  252. MessageString(MessageID id, string s);
  253. /**
  254. * Set message ID
  255. * @param id Message ID
  256. * @throw std::runtime_error if message ID is incompatible with string data
  257. */
  258. void SetID(MessageID id);
  259. /**
  260. * Get message string
  261. * @return String
  262. */
  263. string GetString() {
  264. return s;
  265. }
  266. /**
  267. * Set message string
  268. * @param s String to store in message
  269. */
  270. void SetString(string s) {
  271. this->s = s;
  272. }
  273. /**
  274. * Translate content of message into a string that can be displayed
  275. * @return A string describing message contents
  276. */
  277. string ToString();
  278. /**
  279. * Allocate a new message and copy contents of current message
  280. * @return A message, copy of current
  281. */
  282. Message* Copy();
  283. /**
  284. * Comparison operator
  285. * @param msg Message to be compared
  286. * @return true if message are equal, false otherwise
  287. */
  288. virtual bool operator==(const MessageString& msg) {
  289. return ((messageID == msg.messageID) && (s == msg.s));
  290. }
  291. /**
  292. * Difference operator
  293. * @param msg Message to be compared
  294. * @return true if message are different, false otherwise
  295. */
  296. virtual bool operator!=(const MessageString& msg) {
  297. return !((messageID == msg.messageID) && (s == msg.s));
  298. }
  299. protected:
  300. /**
  301. * Message content
  302. */
  303. string s;
  304. /**
  305. * Verify if message ID is compatible with current message type
  306. * @param id Message ID
  307. * @return true, if message ID is acceptable, false otherwise
  308. */
  309. bool CheckID(MessageID id);
  310. };
  311. /**
  312. * Message class for holding image, based on Message class
  313. *
  314. * @brief Image message class
  315. *
  316. */
  317. class MessageImg : public Message {
  318. public:
  319. /**
  320. * Create a new, empty image message
  321. */
  322. MessageImg();
  323. /**
  324. * Create a new image message, with given ID and boolean value
  325. * @param id Message ID
  326. * @param image Pointer to image
  327. * @throw std::runtime_error if message ID is incompatible with image message
  328. */
  329. MessageImg(MessageID id, Img* image);
  330. /**
  331. * Destroy Image message
  332. */
  333. virtual ~MessageImg();
  334. /**
  335. * Set message ID
  336. * @param id Message ID
  337. * @throw std::runtime_error if message ID is incompatible withimage message
  338. */
  339. void SetID(MessageID id);
  340. /**
  341. * Get message image
  342. * @return Pointer to image
  343. */
  344. Img* GetImage() {
  345. return image;
  346. }
  347. /**
  348. * Set message image
  349. * @param image Pointer to image object
  350. */
  351. void SetImage(Img* image);
  352. /**
  353. * Translate content of message into a string that can be displayed
  354. * @return A string describing message contents
  355. */
  356. string ToString();
  357. /**
  358. * Allocate a new message and copy contents of current message
  359. * @return A message, copy of current
  360. */
  361. Message* Copy();
  362. protected:
  363. /**
  364. * Message image
  365. */
  366. Img* image;
  367. /**
  368. * Verify if message ID is compatible with current message type
  369. * @param id Message ID
  370. * @return true, if message ID is acceptable, false otherwise
  371. */
  372. bool CheckID(MessageID id);
  373. };
  374. /**
  375. * Message class for holding position, based on Message class
  376. *
  377. * @brief Position message class
  378. *
  379. */
  380. class MessagePosition : public Message {
  381. public:
  382. /**
  383. * Create a new, empty image message
  384. */
  385. MessagePosition();
  386. /**
  387. * Create a new image message, with given ID and boolean value
  388. * @param id Message ID
  389. * @param pos Position
  390. * @throw std::runtime_error if message ID is incompatible with image message
  391. */
  392. MessagePosition(MessageID id, Position& pos);
  393. /**
  394. * Set message ID
  395. * @param id Message ID
  396. * @throw std::runtime_error if message ID is incompatible withimage message
  397. */
  398. void SetID(MessageID id);
  399. /**
  400. * Get message image
  401. * @return Pointer to image
  402. */
  403. Position GetPosition() {
  404. return pos;
  405. }
  406. /**
  407. * Set message image
  408. * @param image Pointer to image object
  409. */
  410. void SetPosition(Position& pos);
  411. /**
  412. * Translate content of message into a string that can be displayed
  413. * @return A string describing message contents
  414. */
  415. string ToString();
  416. /**
  417. * Allocate a new message and copy contents of current message
  418. * @return A message, copy of current
  419. */
  420. Message* Copy();
  421. protected:
  422. /**
  423. * Message position
  424. */
  425. Position pos;
  426. /**
  427. * Verify if message ID is compatible with current message type
  428. * @param id Message ID
  429. * @return true, if message ID is acceptable, false otherwise
  430. */
  431. bool CheckID(MessageID id);
  432. };
  433. /**
  434. * Message class for holding battery level, based on Message class
  435. *
  436. * @brief Position message class
  437. *
  438. */
  439. class MessageBattery : public Message {
  440. public:
  441. /**
  442. * Create a new, empty image message
  443. */
  444. MessageBattery();
  445. /**
  446. * Create a new image message, with given ID and boolean value
  447. * @param id Message ID
  448. * @param image Pointer to image
  449. * @throw std::runtime_error if message ID is incompatible with image message
  450. */
  451. MessageBattery(MessageID id, BatteryLevel level);
  452. /**
  453. * Set message ID
  454. * @param id Message ID
  455. * @throw std::runtime_error if message ID is incompatible withimage message
  456. */
  457. void SetID(MessageID id);
  458. /**
  459. * Get message image
  460. * @return Pointer to image
  461. */
  462. BatteryLevel GetLevel() {
  463. return level;
  464. }
  465. /**
  466. * Set message image
  467. * @param image Pointer to image object
  468. */
  469. void SetLevel(BatteryLevel level);
  470. /**
  471. * Translate content of message into a string that can be displayed
  472. * @return A string describing message contents
  473. */
  474. string ToString();
  475. /**
  476. * Allocate a new message and copy contents of current message
  477. * @return A message, copy of current
  478. */
  479. Message* Copy();
  480. protected:
  481. /**
  482. * Message position
  483. */
  484. BatteryLevel level;
  485. /**
  486. * Verify if message ID is compatible with current message type
  487. * @param id Message ID
  488. * @return true, if message ID is acceptable, false otherwise
  489. */
  490. bool CheckID(MessageID id);
  491. };
  492. /**
  493. * Message class for holding answer, based on Message class
  494. *
  495. * @brief Answer message class
  496. *
  497. */
  498. class MessageAnswer : public Message {
  499. public:
  500. /**
  501. * Create a new, empty image message
  502. */
  503. MessageAnswer();
  504. /**
  505. * Create a new image message, with given ID and boolean value
  506. * @param id Message ID
  507. * @param ans Answer ID
  508. * @throw std::runtime_error if message ID is incompatible with image message
  509. */
  510. MessageAnswer(MessageID id, AnswerID ans);
  511. /**
  512. * Set message ID
  513. * @param id Message ID
  514. * @throw std::runtime_error if message ID is incompatible withimage message
  515. */
  516. void SetID(MessageID id);
  517. /**
  518. * Get message image
  519. * @return Pointer to image
  520. */
  521. AnswerID GetAnswer() {
  522. return answer;
  523. }
  524. /**
  525. * Set message answer
  526. * @param ans Answer ID
  527. */
  528. void SetAnswer(AnswerID ans);
  529. /**
  530. * Translate content of message into a string that can be displayed
  531. * @return A string describing message contents
  532. */
  533. string ToString();
  534. /**
  535. * Allocate a new message and copy contents of current message
  536. * @return A message, copy of current
  537. */
  538. Message* Copy();
  539. protected:
  540. /**
  541. * Message answer
  542. */
  543. AnswerID answer;
  544. /**
  545. * Verify if message ID is compatible with current message type
  546. * @param id Message ID
  547. * @return true, if message ID is acceptable, false otherwise
  548. */
  549. bool CheckID(MessageID id);
  550. };
  551. /**
  552. * Message class for holding robot state, based on Message class
  553. *
  554. * @brief Answer message class
  555. *
  556. */
  557. class MessageState: public Message {
  558. public:
  559. /**
  560. * Create a new, empty image message
  561. */
  562. MessageState();
  563. /**
  564. * Create a new image message, with given ID and boolean value
  565. * @param id Message ID
  566. * @param image Pointer to image
  567. * @throw std::runtime_error if message ID is incompatible with image message
  568. */
  569. MessageState(MessageID id, RobotState state);
  570. /**
  571. * Set message ID
  572. * @param id Message ID
  573. * @throw std::runtime_error if message ID is incompatible withimage message
  574. */
  575. void SetID(MessageID id);
  576. /**
  577. * Get message image
  578. * @return Pointer to image
  579. */
  580. RobotState GetState() {
  581. return state;
  582. }
  583. /**
  584. * Set message image
  585. * @param image Pointer to image object
  586. */
  587. void SetState(RobotState state);
  588. /**
  589. * Translate content of message into a string that can be displayed
  590. * @return A string describing message contents
  591. */
  592. string ToString();
  593. /**
  594. * Allocate a new message and copy contents of current message
  595. * @return A message, copy of current
  596. */
  597. Message* Copy();
  598. protected:
  599. /**
  600. * Robot state
  601. */
  602. RobotState state;
  603. /**
  604. * Verify if message ID is compatible with current message type
  605. * @param id Message ID
  606. * @return true, if message ID is acceptable, false otherwise
  607. */
  608. bool CheckID(MessageID id);
  609. };
  610. #endif /* __MESSAGES_H__ */