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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 "messages.h"
  18. #include <exception>
  19. #include <stdexcept>
  20. #include <string>
  21. /*
  22. * @brief Constants used with ToString method, for printing message id
  23. */
  24. const string MESSAGE_ID_STRING[] = {
  25. "Empty",
  26. "Log",
  27. "Answer [Acknowledge]",
  28. "Answer [Not Acknowledge]",
  29. "Answer [Command timeout]",
  30. "Answer [Command unknown]",
  31. "Answer [Command error]",
  32. "Answer [Communication error]",
  33. "Monitor connection lost",
  34. "Open serial com",
  35. "Close serial com",
  36. "Open camera",
  37. "Close camera",
  38. "Ask for arena",
  39. "Arena confirmed",
  40. "Arena infirmed",
  41. "Compute position",
  42. "Stop compute position",
  43. "Position",
  44. "Image",
  45. "Robot ping",
  46. "Robot reset",
  47. "Robot start with watchdog",
  48. "Robot start without watchdog",
  49. "Robot reload watchdog",
  50. "Robot move",
  51. "Robot turn",
  52. "Robot go forward",
  53. "Robot go backward",
  54. "Robot go left",
  55. "Robot go right",
  56. "Robot stop",
  57. "Robot poweroff",
  58. "Robot get battery",
  59. "Robot battery level",
  60. "Robot get state",
  61. "Robot current state",
  62. "Robot state [Not busy]",
  63. "Robot state [Busy]"
  64. };
  65. /*
  66. * @brief Constants used with ToString method, for printing answer id
  67. */
  68. const string ANSWER_ID_STRING[] = {
  69. "Acknowledge",
  70. "Not Acknowledge",
  71. "Robot lost",
  72. "Timeout error",
  73. "Unknown command",
  74. "Invalid or refused command",
  75. "Checksum error"
  76. };
  77. /**
  78. * Create a new, empty message
  79. */
  80. Message::Message() {
  81. this->messageID = MESSAGE_EMPTY;
  82. }
  83. /**
  84. * Create a new, empty message
  85. */
  86. Message::Message(MessageID id) {
  87. SetID(id);
  88. }
  89. /**
  90. * Destroy message
  91. */
  92. Message::~Message() {
  93. }
  94. /**
  95. * Set message ID
  96. * @param id Message ID
  97. */
  98. void Message::SetID(MessageID id) {
  99. if (CheckID(id)) {
  100. this->messageID = id;
  101. } else throw std::runtime_error {"Invalid message id for Message"};
  102. }
  103. /**
  104. * Translate content of message into a string that can be displayed
  105. * @return A string describing message contents
  106. */
  107. string Message::ToString() {
  108. if (CheckID(this->messageID))
  109. return "Message: \"" + MESSAGE_ID_STRING[this->messageID] + "\"";
  110. else
  111. return "Invalid message";
  112. }
  113. /**
  114. * Allocate a new mesage and copy contents of current message
  115. * @return A message, copy of current
  116. */
  117. Message* Message::Copy() {
  118. Message *msg = new Message(this->messageID);
  119. return msg;
  120. }
  121. /**
  122. * Check message ID
  123. * @return Current message ID
  124. */
  125. bool Message::CheckID(MessageID id) {
  126. if ((id == MESSAGE_CAM_IMAGE) ||
  127. (id == MESSAGE_CAM_POSITION) ||
  128. (id == MESSAGE_ROBOT_MOVE) ||
  129. (id == MESSAGE_ROBOT_TURN) ||
  130. (id == MESSAGE_LOG) ||
  131. (id == MESSAGE_ROBOT_BATTERY_LEVEL)) {
  132. return false;
  133. } else return true;
  134. }
  135. /* MessageInt */
  136. /**
  137. * Create a new, empty int message
  138. */
  139. MessageInt::MessageInt() {
  140. value = 0.0;
  141. }
  142. /**
  143. * Create a new int message, with given ID and value
  144. * @param id Message ID
  145. * @param val Message value
  146. * @throw std::runtime_error if message ID is incompatible with int data
  147. */
  148. MessageInt::MessageInt(MessageID id, int val) {
  149. MessageInt::SetID(id);
  150. value = val;
  151. }
  152. /**
  153. * Set message ID
  154. * @param id Message ID
  155. * @throw std::runtime_error if message ID is incompatible with float data
  156. */
  157. void MessageInt::SetID(MessageID id) {
  158. if (CheckID(id))
  159. messageID = id;
  160. else
  161. throw std::runtime_error {
  162. "Invalid message id for MessageInt"
  163. };
  164. }
  165. /**
  166. * Translate content of message into a string that can be displayed
  167. * @return A string describing message contents
  168. */
  169. string MessageInt::ToString() {
  170. if (CheckID(this->messageID))
  171. return "Message: \"" + MESSAGE_ID_STRING[this->messageID] + "\"\nValue: " + to_string(this->value);
  172. else
  173. return "Invalid message";
  174. }
  175. /**
  176. * Allocate a new message and copy contents of current message
  177. * @return A message, copy of current
  178. */
  179. Message* MessageInt::Copy() {
  180. return new MessageInt(this->messageID, this->value);
  181. }
  182. /**
  183. * Verify if message ID is compatible with current message type
  184. * @param id Message ID
  185. * @return true, if message ID is acceptable, false otherwise
  186. */
  187. bool MessageInt::CheckID(MessageID id) {
  188. if ((id != MESSAGE_ROBOT_TURN) &&
  189. (id != MESSAGE_ROBOT_MOVE)) {
  190. return false;
  191. } else return true;
  192. }
  193. /* class MessageString */
  194. /**
  195. * Create a new, empty string message
  196. */
  197. MessageString::MessageString() {
  198. s = string("");
  199. }
  200. /**
  201. * Create a new string message, with given ID and string
  202. * @param id Message ID
  203. * @param s Message string
  204. * @throw std::runtime_error if message ID is incompatible with string data
  205. */
  206. MessageString::MessageString(MessageID id, string s) {
  207. MessageString::SetID(id);
  208. this->s = s;
  209. }
  210. /**
  211. * Set message ID
  212. * @param id Message ID
  213. * @throw std::runtime_error if message ID is incompatible with string data
  214. */
  215. void MessageString::SetID(MessageID id) {
  216. if (CheckID(id))
  217. messageID = id;
  218. else
  219. throw std::runtime_error {
  220. "Invalid message id for MessageString"
  221. };
  222. }
  223. /**
  224. * Translate content of message into a string that can be displayed
  225. * @return A string describing message contents
  226. */
  227. string MessageString::ToString() {
  228. if (CheckID(this->messageID))
  229. return "Message: \"" + MESSAGE_ID_STRING[this->messageID] + "\"\nString: \"" + this->s + "\"";
  230. else
  231. return "Invalid message";
  232. }
  233. /**
  234. * Allocate a new message and copy contents of current message
  235. * @return A message, copy of current
  236. */
  237. Message* MessageString::Copy() {
  238. return new MessageString(this->messageID, this->s);
  239. }
  240. /**
  241. * Verify if message ID is compatible with current message type
  242. * @param id Message ID
  243. * @return true, if message ID is acceptable, false otherwise
  244. */
  245. bool MessageString::CheckID(MessageID id) {
  246. if ((id != MESSAGE_LOG)) {
  247. return false;
  248. } else return true;
  249. }
  250. /* class MessageImg */
  251. /**
  252. * Create a new, empty image message
  253. */
  254. MessageImg::MessageImg() {
  255. this->messageID = MESSAGE_CAM_IMAGE;
  256. image = NULL;
  257. }
  258. /**
  259. * Create a new image message, with given ID and image
  260. * @param id Message ID
  261. * @param image Image
  262. * @throw std::runtime_error if message ID is incompatible with image
  263. */
  264. MessageImg::MessageImg(MessageID id, Img *image) {
  265. MessageImg::SetID(id);
  266. MessageImg::SetImage(image);
  267. }
  268. /**
  269. * Destroy Image message
  270. */
  271. MessageImg::~MessageImg() {
  272. delete (this->image);
  273. }
  274. /**
  275. * Set message ID
  276. * @param id Message ID
  277. * @throw std::runtime_error if message ID is incompatible with image
  278. */
  279. void MessageImg::SetID(MessageID id) {
  280. if (CheckID(id))
  281. messageID = id;
  282. else
  283. throw std::runtime_error {
  284. "Invalid message id for MessageImg"
  285. };
  286. }
  287. /**
  288. * Set message image
  289. * @param image Reference to image object
  290. */
  291. void MessageImg::SetImage(Img* image) {
  292. //this->image = image->Copy();
  293. this->image = image;
  294. }
  295. /**
  296. * Translate content of message into a string that can be displayed
  297. * @return A string describing message contents
  298. */
  299. string MessageImg::ToString() {
  300. if (CheckID(this->messageID))
  301. return "Message: \"" + MESSAGE_ID_STRING[this->messageID] + "\"\n" + this->image->ToString();
  302. else
  303. return "Invalid message";
  304. }
  305. /**
  306. * Allocate a new message and copy contents of current message
  307. * @return A message, copy of current
  308. */
  309. Message* MessageImg::Copy() {
  310. return new MessageImg(this->messageID, this->image->Copy());
  311. }
  312. /**
  313. * Verify if message ID is compatible with current message type
  314. * @param id Message ID
  315. * @return true, if message ID is acceptable, false otherwise
  316. */
  317. bool MessageImg::CheckID(MessageID id) {
  318. if (id != MESSAGE_CAM_IMAGE) {
  319. return false;
  320. } else return true;
  321. }
  322. /* class MessageBattery */
  323. /**
  324. * Create a new, empty battery message
  325. */
  326. MessageBattery::MessageBattery() {
  327. this->messageID = MESSAGE_ROBOT_BATTERY_LEVEL;
  328. this->level = BATTERY_UNKNOWN;
  329. }
  330. /**
  331. * Create a new battery message, with given ID and battery level
  332. * @param id Message ID
  333. * @param level Battery level
  334. * @throw std::runtime_error if message ID is incompatible with battery
  335. */
  336. MessageBattery::MessageBattery(MessageID id, BatteryLevel level) {
  337. MessageBattery::SetID(id);
  338. MessageBattery::SetLevel(level);
  339. }
  340. /**
  341. * Set message ID
  342. * @param id Message ID
  343. * @throw std::runtime_error if message ID is incompatible with battery
  344. */
  345. void MessageBattery::SetID(MessageID id) {
  346. if (CheckID(id))
  347. messageID = id;
  348. else
  349. throw std::runtime_error {
  350. "Invalid message id for MessageBattery"
  351. };
  352. }
  353. /**
  354. * Set battery level
  355. * @param level Battery level
  356. */
  357. void MessageBattery::SetLevel(BatteryLevel level) {
  358. if ((level < BATTERY_UNKNOWN) || (level > BATTERY_FULL)) {
  359. throw std::runtime_error{
  360. "Invalid battery level for MessageBattery"};
  361. } else {
  362. this->level = level;
  363. }
  364. }
  365. /**
  366. * Translate content of message into a string that can be displayed
  367. * @return A string describing message contents
  368. */
  369. string MessageBattery::ToString() {
  370. string levelString;
  371. switch (this->level) {
  372. case BATTERY_UNKNOWN:
  373. levelString="Unknown";
  374. break;
  375. case BATTERY_EMPTY:
  376. levelString="Empty";
  377. break;
  378. case BATTERY_LOW:
  379. levelString="Low";
  380. break;
  381. case BATTERY_FULL:
  382. levelString="Full";
  383. break;
  384. default:
  385. levelString="Invalid";
  386. }
  387. if (CheckID(this->messageID))
  388. return "Message: \"" + MESSAGE_ID_STRING[this->messageID] + "\"\nBattery level: \"" + levelString + "\"";
  389. else
  390. return "Invalid message";
  391. }
  392. /**
  393. * Allocate a new message and copy contents of current message
  394. * @return A message, copy of current
  395. */
  396. Message* MessageBattery::Copy() {
  397. return new MessageBattery(this->messageID, this->level);
  398. }
  399. /**
  400. * Verify if message ID is compatible with current message type
  401. * @param id Message ID
  402. * @return true, if message ID is acceptable, false otherwise
  403. */
  404. bool MessageBattery::CheckID(MessageID id) {
  405. if ((id != MESSAGE_ROBOT_BATTERY_LEVEL)) {
  406. return false;
  407. } else return true;
  408. }
  409. /* class MessagePosition */
  410. /**
  411. * Create a new, empty position message
  412. */
  413. MessagePosition::MessagePosition() {
  414. this->messageID = MESSAGE_CAM_POSITION;
  415. this->pos.angle = 0.0;
  416. this->pos.robotId = 0;
  417. this->pos.center.x=0.0;
  418. this->pos.center.y=0.0;
  419. this->pos.direction.x=0.0;
  420. this->pos.direction.y=0.0;
  421. }
  422. /**
  423. * Create a new position message, with given ID and position
  424. * @param id Message ID
  425. * @param pos Message position
  426. * @throw std::runtime_error if message ID is incompatible with string data
  427. */
  428. MessagePosition::MessagePosition(MessageID id, Position& pos) {
  429. MessagePosition::SetID(id);
  430. MessagePosition::SetPosition(pos);
  431. }
  432. /**
  433. * Set message ID
  434. * @param id Message ID
  435. * @throw std::runtime_error if message ID is incompatible with string data
  436. */
  437. void MessagePosition::SetID(MessageID id) {
  438. if (CheckID(id))
  439. messageID = id;
  440. else
  441. throw std::runtime_error {
  442. "Invalid message id for MessagePosition"
  443. };
  444. }
  445. /**
  446. * Set position
  447. * @param pos Reference to position
  448. */
  449. void MessagePosition::SetPosition(Position& pos) {
  450. this->pos.angle = pos.angle;
  451. this->pos.robotId = pos.robotId;
  452. this->pos.center = pos.center;
  453. this->pos.direction = pos.direction;
  454. }
  455. /**
  456. * Translate content of message into a string that can be displayed
  457. * @return A string describing message contents
  458. */
  459. string MessagePosition::ToString() {
  460. if (CheckID(this->messageID))
  461. return "Message: \"" + MESSAGE_ID_STRING[this->messageID] + "\"\nPosition: \"" + to_string(this->pos.center.x) + ";" + to_string(this->pos.center.y) + "\"";
  462. else
  463. return "Invalid message";
  464. }
  465. /**
  466. * Allocate a new message and copy contents of current message
  467. * @return A message, copy of current
  468. */
  469. Message* MessagePosition::Copy() {
  470. return new MessagePosition(this->messageID, this->pos);
  471. }
  472. /**
  473. * Verify if message ID is compatible with current message type
  474. * @param id Message ID
  475. * @return true, if message ID is acceptable, false otherwise
  476. */
  477. bool MessagePosition::CheckID(MessageID id) {
  478. if ((id != MESSAGE_CAM_POSITION)) {
  479. return false;
  480. } else return true;
  481. }