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.

comrobot.h 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 __COMROBOT_H__
  18. #define __COMROBOT_H__
  19. #include "messages.h"
  20. #include <string>
  21. using namespace std;
  22. /**
  23. * Class used for communicating with robot over serial
  24. *
  25. * @brief Communication class with robot
  26. *
  27. */
  28. class ComRobot {
  29. public:
  30. /**
  31. * Constructor
  32. */
  33. ComRobot() {
  34. }
  35. /**
  36. * Destructor
  37. */
  38. virtual ~ComRobot() {
  39. }
  40. /**
  41. * Open serial link with robot
  42. * @return File descriptor
  43. * @throw std::runtime_error if it fails
  44. */
  45. int Open();
  46. /**
  47. * Open serial link with robot
  48. * @param usart Filename of usart to open
  49. * @return File descriptor
  50. * @throw std::runtime_error if it fails
  51. */
  52. int Open(string usart);
  53. /**
  54. * Close serial link
  55. * @return Success if above 0, failure if below 0
  56. */
  57. int Close();
  58. /**
  59. * Send a message to robot
  60. * @param msg Message to send to robot
  61. * @return A message containing either an answer (Ack/Nak/Timeout/Error) or a value (battery level, robot state) depending of the command
  62. * @attention Input message is destroyed (delete) after being sent. You do not need to delete it yourself
  63. * @attention Write produce an answer message. You have to dispose it (delete) when you have finished using it
  64. * @attention Write is blocking until message is written into buffer (linux side)
  65. * @warning Write is not thread save : check that multiple tasks can't access this method simultaneously
  66. */
  67. Message *Write(Message* msg);
  68. /**
  69. * Function called at beginning of Write method
  70. * Use it to do some synchronization (call of mutex, for example)
  71. */
  72. virtual void Write_Pre() {
  73. }
  74. /**
  75. * Function called at end of Write method
  76. * Use it to do some synchronization (call of mutex, for example)
  77. */
  78. virtual void Write_Post() {
  79. }
  80. Message *SendCommand(Message* msg, MessageID answerID, int maxRetries);
  81. static Message *Ping() {
  82. return new Message(MESSAGE_ROBOT_PING);
  83. }
  84. static Message *Reset() {
  85. return new Message(MESSAGE_ROBOT_RESET);
  86. }
  87. static Message *PowerOff() {
  88. return new Message(MESSAGE_ROBOT_POWEROFF);
  89. }
  90. static Message *StartWithWD() {
  91. return new Message(MESSAGE_ROBOT_START_WITH_WD);
  92. }
  93. static Message *StartWithoutWD() {
  94. return new Message(MESSAGE_ROBOT_START_WITHOUT_WD);
  95. }
  96. static Message *ReloadWD() {
  97. return new Message(MESSAGE_ROBOT_RELOAD_WD);
  98. }
  99. static Message *Move(int length) {
  100. return new MessageInt(MESSAGE_ROBOT_MOVE, length);
  101. }
  102. static Message *Turn(int angle) {
  103. return new MessageInt(MESSAGE_ROBOT_TURN, angle);
  104. }
  105. static Message *Stop() {
  106. return new Message(MESSAGE_ROBOT_STOP);
  107. }
  108. static Message *GoForward() {
  109. return new Message(MESSAGE_ROBOT_GO_FORWARD);
  110. }
  111. static Message *GoBackward() {
  112. return new Message(MESSAGE_ROBOT_GO_BACKWARD);
  113. }
  114. static Message *GoLeft() {
  115. return new Message(MESSAGE_ROBOT_GO_LEFT);
  116. }
  117. static Message *GoRight() {
  118. return new Message(MESSAGE_ROBOT_GO_RIGHT);
  119. }
  120. static Message *GetBattery() {
  121. return new Message(MESSAGE_ROBOT_BATTERY_GET);
  122. }
  123. static Message *GetState() {
  124. return new Message(MESSAGE_ROBOT_STATE_GET);
  125. }
  126. protected:
  127. /**
  128. * Serial link file descriptor
  129. */
  130. int fd;
  131. /**
  132. * Get an answer from robot
  133. * @return String containing answer from robot
  134. * @attention Read method is blocking until a message is received (timeout of 500 ms)
  135. * @warning Read is not thread safe : Do not call it in multiple tasks simultaneously
  136. */
  137. string Read();
  138. /**
  139. * Convert a string to its message representation (when receiving data from robot)
  140. * @param s String from robot containing answer
  141. * @return Message corresponding to received array of char
  142. */
  143. Message* StringToMessage(string s);
  144. /**
  145. * Convert a message to its string representation (for sending command to robot)
  146. * @param msg Message to be sent to robot
  147. * @return String containing command to robot
  148. */
  149. string MessageToString(Message *msg);
  150. /**
  151. * Add a checksum and carriage return to a command string
  152. * @param[in,out] s String containing command for robot, without ending char (carriage return)
  153. */
  154. void AddChecksum(string &s);
  155. /**
  156. * Verify if checksum of an incoming answer from robot is valid,
  157. * then remove checksum from incoming answer (if checksum is ok)
  158. * @param[in,out] s String containing incoming answer from robot
  159. * @return true is checksum is valid, false otherwise.
  160. */
  161. bool VerifyChecksum(string &s);
  162. };
  163. #endif /* __COMROBOT_H__ */