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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 "comrobot.h"
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <termios.h>
  23. #include <string>
  24. #include <stdexcept>
  25. #ifdef __FOR_PC__
  26. #define USART_FILENAME "/dev/ttyUSB0"
  27. #else
  28. #define USART_FILENAME "/dev/ttyS0"
  29. #endif /* __FOR_PC__ */
  30. /*
  31. * Constants to be used for communicating with robot. Contains command tag
  32. */
  33. typedef enum {
  34. LABEL_ANGLE_POSITION = 'p',
  35. LABEL_ANGULAR_SPEED = 's',
  36. LABEL_BATTERY_LEVEL = 'b',
  37. LABEL_BETA_ANGLE = 'v',
  38. LABEL_USER_PRESENCE = 'u',
  39. LABEL_TORQUE = 'c',
  40. LABEL_EMERGENCY_STOP = 'a'
  41. } LabelRobot;
  42. /**
  43. * Open serial link with robot
  44. * @return File descriptor
  45. * @throw std::runtime_error if it fails
  46. */
  47. int ComRobot::Open() {
  48. fd = open(USART_FILENAME, O_RDWR | O_NOCTTY /*| O_NDELAY*/); //Open in blocking read/write mode
  49. if (fd == -1) {
  50. //ERROR - CAN'T OPEN SERIAL PORT
  51. throw std::runtime_error{"Error - Unable to open UART " + string(USART_FILENAME) + ". Ensure it is not in use by another application"};
  52. exit(EXIT_FAILURE);
  53. }
  54. //Configuration of the serial port 115 520 Bauds
  55. struct termios options;
  56. tcgetattr(fd, &options);
  57. options.c_cflag = B115200 | CS8 | CLOCAL | CREAD; //<Set baud rate
  58. options.c_iflag = IGNPAR; // ignores bytes with bad parity
  59. options.c_oflag = 0;
  60. options.c_lflag = 0;
  61. tcflush(fd, TCIFLUSH);
  62. tcsetattr(fd, TCSANOW, &options);
  63. return fd;
  64. }
  65. /**
  66. * Close serial link
  67. * @return Success if above 0, failure if below 0
  68. */
  69. int ComRobot::Close() {
  70. return close(fd);
  71. }
  72. /**
  73. * Get a message from robot
  74. * @return Message currently received
  75. * @attention A message object is created (new) when receiving data from robot. You MUST remember to destroy is (delete) after use
  76. * @attention Read method is blocking until a message is received
  77. * @warning Read is not thread safe : Do not call it in multiple tasks simultaneously
  78. */
  79. Message* ComRobot::Read() {
  80. int rxLength;
  81. unsigned char rxBuffer[6];
  82. unsigned char receivedChar;
  83. bool messageComplete = false;
  84. Message *msg;
  85. unsigned int i;
  86. /* Call pre method for read */
  87. Read_Pre();
  88. /* a message is composed of 7 bytes.
  89. the byte 0 should always be '<'
  90. the byte 1 should be an ascii char that is the label. It define what the data represent
  91. the bytes 2 to 5 are the float value
  92. the byte 6 should always be a '\n'
  93. */
  94. while (messageComplete == false) {
  95. rxLength = read(this->fd, (void*) &receivedChar, 1); //Filestream, buffer to store in, number of bytes to read (max)
  96. //printf ("W=%02X ", receivedChar);
  97. if (rxLength <= -1) {
  98. this->lostCom = true;
  99. printf("Warning: communication lost in ComStm32::Read\n");
  100. msg = new Message();
  101. return msg;
  102. } else if (rxLength == 0) {
  103. // nothing to do
  104. } else if (receivedChar == '<') { // start of frame received
  105. i = 0;
  106. do {
  107. rxLength = read(this->fd, (void*) &rxBuffer[i], 6 - i); //Filestream, buffer to store in, number of bytes to read (max)
  108. if (rxLength >= 0)
  109. i = i + rxLength;
  110. else {
  111. printf("Error while reading (%i)", rxLength);
  112. return NULL;
  113. }
  114. } while (i < 6);
  115. if (rxBuffer[5] == '\n') {
  116. messageComplete = true;
  117. }
  118. }
  119. }
  120. /* Treatment of received message */
  121. msg = CharToMessage(rxBuffer);
  122. /* Call Post method for read */
  123. Read_Post();
  124. return msg;
  125. }
  126. /**
  127. * Convert an array of char to its message representation (when receiving data from stm32)
  128. * @param bytes Array of char
  129. * @return Message corresponding to received array of char
  130. */
  131. Message* ComRobot::CharToMessage(unsigned char *bytes) {
  132. Message *msg = __null;
  133. MessageFloat *msgf;
  134. MessageBool *msgb;
  135. switch (bytes[0]) {
  136. case LABEL_ANGLE_POSITION:
  137. msgf = new MessageFloat();
  138. msgf->SetID(MESSAGE_ANGLE_POSITION);
  139. msgf->SetValue(CharToFloat(&bytes[1]));
  140. msg = (Message*) msgf;
  141. break;
  142. case LABEL_ANGULAR_SPEED:
  143. msgf = new MessageFloat();
  144. msgf->SetID(MESSAGE_ANGULAR_SPEED);
  145. msgf->SetValue(CharToFloat(&bytes[1]));
  146. msg = (Message*) msgf;
  147. break;
  148. case LABEL_BATTERY_LEVEL:
  149. msgf = new MessageFloat();
  150. msgf->SetID(MESSAGE_BATTERY);
  151. msgf->SetValue(CharToFloat(&bytes[1]));
  152. msg = (Message*) msgf;
  153. break;
  154. case LABEL_BETA_ANGLE:
  155. msgf = new MessageFloat();
  156. msgf->SetID(MESSAGE_BETA);
  157. msgf->SetValue(CharToFloat(&bytes[1]));
  158. msg = (Message*) msgf;
  159. break;
  160. case LABEL_USER_PRESENCE:
  161. msgb = new MessageBool();
  162. msgb->SetID(MESSAGE_USER_PRESENCE);
  163. msgb->SetState(CharToBool(&bytes[1]));
  164. msg = (Message*) msgb;
  165. break;
  166. default:
  167. printf("Unknown message received from robot (%i)\n", bytes[0]);
  168. fflush(stdout);
  169. msg = new Message();
  170. }
  171. if (msg == NULL) {
  172. printf("Message is null (%02X)\n", bytes[0]);
  173. fflush(stdout);
  174. msg = new Message();
  175. }
  176. return msg;
  177. }
  178. /**
  179. * Convert an array of char to float
  180. * @param bytes Array of char, containing a binary image of a float
  181. * @return Float value
  182. */
  183. float ComRobot::CharToFloat(unsigned char *bytes) {
  184. unsigned long value;
  185. union {
  186. unsigned char buffer[4];
  187. float f;
  188. } convert;
  189. convert.buffer[0] = bytes[0];
  190. convert.buffer[1] = bytes[1];
  191. convert.buffer[2] = bytes[2];
  192. convert.buffer[3] = bytes[3];
  193. //value = (bytes[3] << 24) | (bytes[2] << 16) | (bytes[1] << 8) | (bytes[0]);
  194. return convert.f;
  195. }
  196. /**
  197. * Convert an array of char to integer
  198. * @param bytes Array of char, containing a binary image of an integer
  199. * @return Integer value
  200. */
  201. unsigned int ComRobot::CharToInt(unsigned char *bytes) {
  202. unsigned long value;
  203. value = (bytes[3] << 24) | (bytes[2] << 16) | (bytes[1] << 8) | (bytes[0]);
  204. return (unsigned int) value;
  205. }
  206. /**
  207. * Convert an array of char to boolean
  208. * @param bytes Array of char, containing a binary image of a boolean
  209. * @return Boolean value
  210. */
  211. bool ComRobot::CharToBool(unsigned char *bytes) {
  212. unsigned long value;
  213. value = (bytes[3] << 24) | (bytes[2] << 16) | (bytes[1] << 8) | (bytes[0]);
  214. if (value == 0) return false;
  215. else return true;
  216. }
  217. /**
  218. * Send a message to robot
  219. * @param msg Message to send to robot
  220. * @return 1 if success, 0 otherwise
  221. * @attention Message is destroyed (delete) after being sent. You do not need to delete it yourself
  222. * @attention Write is blocking until message is written into buffer (linux side)
  223. * @warning Write is not thread save : check that multiple tasks can't access this method simultaneously
  224. */
  225. int ComRobot::Write(Message* msg) {
  226. unsigned char buffer[7];
  227. int ret_val = 0;
  228. MessageToChar(msg, buffer);
  229. Write_Pre();
  230. if (this->fd != -1) {
  231. int count = write(this->fd, &buffer[0], 7); //Filestream, bytes to write, number of bytes to write
  232. if (count < 0) {
  233. printf("Warning: UART TX error in ComStm32::Write\n");
  234. } else {
  235. ret_val = 1;
  236. }
  237. }
  238. // deallocation of msg
  239. delete(msg);
  240. Write_Post();
  241. return ret_val;
  242. }
  243. /**
  244. * Convert a message to its array of char representation (for sending command to stm32)
  245. * @param msg Message to be sent to robot
  246. * @param buffer Array of char, image of message to send
  247. */
  248. void ComRobot::MessageToChar(Message *msg, unsigned char *buffer) {
  249. float val_f;
  250. int val_i;
  251. unsigned char *b;
  252. buffer[0] = '<';
  253. buffer[6] = '\n';
  254. switch (msg->GetID()) {
  255. case MESSAGE_TORQUE:
  256. buffer[1] = LABEL_TORQUE;
  257. val_f = ((MessageFloat*) msg)->GetValue();
  258. b = (unsigned char *) &val_f;
  259. break;
  260. case MESSAGE_EMERGENCY_STOP:
  261. buffer[1] = LABEL_EMERGENCY_STOP;
  262. if (((MessageBool*) msg)->GetState())
  263. val_i = 1;
  264. else
  265. val_i = 0;
  266. b = (unsigned char *) &val_i;
  267. break;
  268. default:
  269. printf("Invalid message to send");
  270. val_i = 0;
  271. b = (unsigned char *) &val_i;
  272. }
  273. buffer[2] = b[0];
  274. buffer[3] = b[1];
  275. buffer[4] = b[2];
  276. buffer[5] = b[3];
  277. }