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

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