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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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_ACK,
  33. MESSAGE_ANSWER_NACK,
  34. MESSAGE_ANSWER_ROBOT_TIMEOUT,
  35. MESSAGE_ANSWER_ROBOT_UNKNOWN_COMMAND,
  36. MESSAGE_ANSWER_ROBOT_ERROR,
  37. MESSAGE_ANSWER_COM_ERROR,
  38. // messages for serial communication with robot
  39. MESSAGE_ROBOT_COM_OPEN,
  40. MESSAGE_ROBOT_COM_CLOSE,
  41. // Messages specific to server
  42. MESSAGE_MONITOR_LOST,
  43. // Messages for camera
  44. MESSAGE_CAM_OPEN,
  45. MESSAGE_CAM_CLOSE,
  46. MESSAGE_CAM_ASK_ARENA,
  47. MESSAGE_CAM_ARENA_CONFIRM,
  48. MESSAGE_CAM_ARENA_INFIRM,
  49. MESSAGE_CAM_POSITION_COMPUTE_START,
  50. MESSAGE_CAM_POSITION_COMPUTE_STOP,
  51. MESSAGE_CAM_POSITION,
  52. MESSAGE_CAM_IMAGE,
  53. // Messages for robot
  54. MESSAGE_ROBOT_PING,
  55. MESSAGE_ROBOT_RESET,
  56. MESSAGE_ROBOT_START_WITH_WD,
  57. MESSAGE_ROBOT_START_WITHOUT_WD,
  58. MESSAGE_ROBOT_RELOAD_WD,
  59. MESSAGE_ROBOT_MOVE,
  60. MESSAGE_ROBOT_TURN,
  61. MESSAGE_ROBOT_GO_FORWARD,
  62. MESSAGE_ROBOT_GO_BACKWARD,
  63. MESSAGE_ROBOT_GO_LEFT,
  64. MESSAGE_ROBOT_GO_RIGHT,
  65. MESSAGE_ROBOT_STOP,
  66. MESSAGE_ROBOT_POWEROFF,
  67. MESSAGE_ROBOT_BATTERY_GET,
  68. MESSAGE_ROBOT_BATTERY_LEVEL,
  69. MESSAGE_ROBOT_STATE_GET,
  70. MESSAGE_ROBOT_STATE_NOT_BUSY,
  71. MESSAGE_ROBOT_STATE_BUSY
  72. } MessageID;
  73. typedef enum {
  74. BATTERY_UNKNOWN=-1,
  75. BATTERY_EMPTY=0,
  76. BATTERY_LOW,
  77. BATTERY_FULL
  78. } BatteryLevel;
  79. using namespace std;
  80. /**
  81. * Base class for messaging
  82. *
  83. * @brief Base class for messaging
  84. *
  85. */
  86. class Message {
  87. public:
  88. /**
  89. * Create a new, empty message
  90. */
  91. Message();
  92. /**
  93. * Create a new, empty message
  94. */
  95. Message(MessageID id);
  96. /**
  97. * Destroy message
  98. */
  99. virtual ~Message();
  100. /**
  101. * Translate content of message into a string that can be displayed
  102. * @return A string describing message contents
  103. */
  104. virtual string ToString();
  105. /**
  106. * Allocate a new mesage and copy contents of current message
  107. * @return A message, copy of current
  108. */
  109. virtual Message* Copy();
  110. /**
  111. * Compare message ID
  112. * @param id Id to compare message to
  113. * @return true if id is equal to message id, false otherwise
  114. */
  115. bool CompareID(MessageID id) {
  116. return (this->messageID == id) ? true:false;
  117. }
  118. /**
  119. * Get message ID
  120. * @return Current message ID
  121. */
  122. MessageID GetID() {
  123. return messageID;
  124. }
  125. /**
  126. * Set message ID
  127. * @param id Message ID
  128. */
  129. virtual void SetID(MessageID id);
  130. /**
  131. * Comparison operator
  132. * @param msg Message to be compared
  133. * @return true if message are equal, false otherwise
  134. */
  135. virtual bool operator==(const Message& msg) {
  136. return (messageID == msg.messageID);
  137. }
  138. /**
  139. * Difference operator
  140. * @param msg Message to be compared
  141. * @return true if message are different, false otherwise
  142. */
  143. virtual bool operator!=(const Message& msg) {
  144. return !(messageID == msg.messageID);
  145. }
  146. protected:
  147. /**
  148. * Message identifier (@see MessageID)
  149. */
  150. MessageID messageID;
  151. /**
  152. * Verify if message ID is compatible with current message type
  153. * @param id Message ID
  154. * @return true, if message ID is acceptable, false otherwise
  155. */
  156. virtual bool CheckID(MessageID id);
  157. };
  158. /**
  159. * Message class for holding float value, based on Message class
  160. *
  161. * @brief Float message class
  162. *
  163. */
  164. class MessageInt : public Message {
  165. public:
  166. /**
  167. * Create a new, empty float message
  168. */
  169. MessageInt();
  170. /**
  171. * Create a new float message, with given ID and value
  172. * @param id Message ID
  173. * @param val Message value
  174. * @throw std::runtime_error if message ID is incompatible with float data
  175. */
  176. MessageInt(MessageID id, int val);
  177. /**
  178. * Set message ID
  179. * @param id Message ID
  180. * @throw std::runtime_error if message ID is incompatible with float data
  181. */
  182. void SetID(MessageID id);
  183. /**
  184. * Get message value (int)
  185. * @return int value
  186. */
  187. int GetValue() {
  188. return value;
  189. }
  190. /**
  191. * Set message value (int)
  192. * @param val int value to store in message
  193. */
  194. void SetValue(int val) {
  195. this->value = val;
  196. }
  197. /**
  198. * Translate content of message into a string that can be displayed
  199. * @return A string describing message contents
  200. */
  201. string ToString();
  202. /**
  203. * Allocate a new mesage and copy contents of current message
  204. * @return A message, copy of current
  205. */
  206. Message* Copy();
  207. /**
  208. * Comparison operator
  209. * @param msg Message to be compared
  210. * @return true if message are equal, false otherwise
  211. */
  212. virtual bool operator==(const MessageInt& msg) {
  213. return ((messageID == msg.messageID) && (value == msg.value));
  214. }
  215. /**
  216. * Difference operator
  217. * @param msg Message to be compared
  218. * @return true if message are different, false otherwise
  219. */
  220. virtual bool operator!=(const MessageInt& msg) {
  221. return !((messageID == msg.messageID) && (value == msg.value));
  222. }
  223. protected:
  224. /**
  225. * Message integer value
  226. */
  227. int value;
  228. /**
  229. * Verify if message ID is compatible with current message type
  230. * @param id Message ID
  231. * @return true, if message ID is acceptable, false otherwise
  232. */
  233. bool CheckID(MessageID id);
  234. };
  235. /**
  236. * Message class for holding string value, based on Message class
  237. *
  238. * @brief String message class
  239. *
  240. */
  241. class MessageString : public Message {
  242. public:
  243. /**
  244. * Create a new, empty string message
  245. */
  246. MessageString();
  247. /**
  248. * Create a new string message, with given ID and string
  249. * @param id Message ID
  250. * @param s Message string
  251. * @throw std::runtime_error if message ID is incompatible with string data
  252. */
  253. MessageString(MessageID id, string s);
  254. /**
  255. * Set message ID
  256. * @param id Message ID
  257. * @throw std::runtime_error if message ID is incompatible with string data
  258. */
  259. void SetID(MessageID id);
  260. /**
  261. * Get message string
  262. * @return String
  263. */
  264. string GetString() {
  265. return s;
  266. }
  267. /**
  268. * Set message string
  269. * @param s String to store in message
  270. */
  271. void SetString(string s) {
  272. this->s = s;
  273. }
  274. /**
  275. * Translate content of message into a string that can be displayed
  276. * @return A string describing message contents
  277. */
  278. string ToString();
  279. /**
  280. * Allocate a new message and copy contents of current message
  281. * @return A message, copy of current
  282. */
  283. Message* Copy();
  284. /**
  285. * Comparison operator
  286. * @param msg Message to be compared
  287. * @return true if message are equal, false otherwise
  288. */
  289. virtual bool operator==(const MessageString& msg) {
  290. return ((messageID == msg.messageID) && (s == msg.s));
  291. }
  292. /**
  293. * Difference operator
  294. * @param msg Message to be compared
  295. * @return true if message are different, false otherwise
  296. */
  297. virtual bool operator!=(const MessageString& msg) {
  298. return !((messageID == msg.messageID) && (s == msg.s));
  299. }
  300. protected:
  301. /**
  302. * Message content
  303. */
  304. string s;
  305. /**
  306. * Verify if message ID is compatible with current message type
  307. * @param id Message ID
  308. * @return true, if message ID is acceptable, false otherwise
  309. */
  310. bool CheckID(MessageID id);
  311. };
  312. /**
  313. * Message class for holding image, based on Message class
  314. *
  315. * @brief Image message class
  316. *
  317. */
  318. class MessageImg : public Message {
  319. public:
  320. /**
  321. * Create a new, empty image message
  322. */
  323. MessageImg();
  324. /**
  325. * Create a new image message, with given ID and boolean value
  326. * @param id Message ID
  327. * @param image Pointer to image
  328. * @throw std::runtime_error if message ID is incompatible with image message
  329. */
  330. MessageImg(MessageID id, Img* image);
  331. /**
  332. * Destroy Image message
  333. */
  334. virtual ~MessageImg();
  335. /**
  336. * Set message ID
  337. * @param id Message ID
  338. * @throw std::runtime_error if message ID is incompatible withimage message
  339. */
  340. void SetID(MessageID id);
  341. /**
  342. * Get message image
  343. * @return Pointer to image
  344. */
  345. Img* GetImage() {
  346. return image;
  347. }
  348. /**
  349. * Set message image
  350. * @param image Pointer to image object
  351. */
  352. void SetImage(Img* image);
  353. /**
  354. * Translate content of message into a string that can be displayed
  355. * @return A string describing message contents
  356. */
  357. string ToString();
  358. /**
  359. * Allocate a new message and copy contents of current message
  360. * @return A message, copy of current
  361. */
  362. Message* Copy();
  363. protected:
  364. /**
  365. * Message image
  366. */
  367. Img* image;
  368. /**
  369. * Verify if message ID is compatible with current message type
  370. * @param id Message ID
  371. * @return true, if message ID is acceptable, false otherwise
  372. */
  373. bool CheckID(MessageID id);
  374. };
  375. /**
  376. * Message class for holding position, based on Message class
  377. *
  378. * @brief Position message class
  379. *
  380. */
  381. class MessagePosition : public Message {
  382. public:
  383. /**
  384. * Create a new, empty image message
  385. */
  386. MessagePosition();
  387. /**
  388. * Create a new image message, with given ID and boolean value
  389. * @param id Message ID
  390. * @param pos Position
  391. * @throw std::runtime_error if message ID is incompatible with image message
  392. */
  393. MessagePosition(MessageID id, Position& pos);
  394. /**
  395. * Set message ID
  396. * @param id Message ID
  397. * @throw std::runtime_error if message ID is incompatible withimage message
  398. */
  399. void SetID(MessageID id);
  400. /**
  401. * Get message image
  402. * @return Pointer to image
  403. */
  404. Position GetPosition() {
  405. return pos;
  406. }
  407. /**
  408. * Set message image
  409. * @param image Pointer to image object
  410. */
  411. void SetPosition(Position& pos);
  412. /**
  413. * Translate content of message into a string that can be displayed
  414. * @return A string describing message contents
  415. */
  416. string ToString();
  417. /**
  418. * Allocate a new message and copy contents of current message
  419. * @return A message, copy of current
  420. */
  421. Message* Copy();
  422. protected:
  423. /**
  424. * Message position
  425. */
  426. Position pos;
  427. /**
  428. * Verify if message ID is compatible with current message type
  429. * @param id Message ID
  430. * @return true, if message ID is acceptable, false otherwise
  431. */
  432. bool CheckID(MessageID id);
  433. };
  434. /**
  435. * Message class for holding battery level, based on Message class
  436. *
  437. * @brief Position message class
  438. *
  439. */
  440. class MessageBattery : public Message {
  441. public:
  442. /**
  443. * Create a new, empty image message
  444. */
  445. MessageBattery();
  446. /**
  447. * Create a new image message, with given ID and boolean value
  448. * @param id Message ID
  449. * @param image Pointer to image
  450. * @throw std::runtime_error if message ID is incompatible with image message
  451. */
  452. MessageBattery(MessageID id, BatteryLevel level);
  453. /**
  454. * Set message ID
  455. * @param id Message ID
  456. * @throw std::runtime_error if message ID is incompatible withimage message
  457. */
  458. void SetID(MessageID id);
  459. /**
  460. * Get message image
  461. * @return Pointer to image
  462. */
  463. BatteryLevel GetLevel() {
  464. return level;
  465. }
  466. /**
  467. * Set message image
  468. * @param image Pointer to image object
  469. */
  470. void SetLevel(BatteryLevel level);
  471. /**
  472. * Translate content of message into a string that can be displayed
  473. * @return A string describing message contents
  474. */
  475. string ToString();
  476. /**
  477. * Allocate a new message and copy contents of current message
  478. * @return A message, copy of current
  479. */
  480. Message* Copy();
  481. protected:
  482. /**
  483. * Message position
  484. */
  485. BatteryLevel level;
  486. /**
  487. * Verify if message ID is compatible with current message type
  488. * @param id Message ID
  489. * @return true, if message ID is acceptable, false otherwise
  490. */
  491. bool CheckID(MessageID id);
  492. };
  493. #endif /* __MESSAGES_H__ */