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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. using namespace std;
  21. /**
  22. * Class used for communicating with robot over serial
  23. *
  24. * @brief Communication class with robot
  25. *
  26. */
  27. class ComRobot {
  28. public:
  29. /**
  30. * Constructor
  31. */
  32. ComRobot() {}
  33. /**
  34. * Destructor
  35. */
  36. virtual ~ComRobot() {}
  37. /**
  38. * Open serial link with robot
  39. * @return File descriptor
  40. * @throw std::runtime_error if it fails
  41. */
  42. int Open();
  43. /**
  44. * Close serial link
  45. * @return Success if above 0, failure if below 0
  46. */
  47. int Close();
  48. /**
  49. * Get a message from robot
  50. * @return Message currently received
  51. * @attention A message object is created (new) when receiving data from robot. You MUST remember to destroy is (delete) after use
  52. * @attention Read method is blocking until a message is received
  53. * @warning Read is not thread safe : Do not call it in multiple tasks simultaneously
  54. */
  55. Message* Read();
  56. /**
  57. * Send a message to robot
  58. * @param msg Message to send to robot
  59. * @return 1 if success, 0 otherwise
  60. * @attention Message is destroyed (delete) after being sent. You do not need to delete it yourself
  61. * @attention Write is blocking until message is written into buffer (linux side)
  62. * @warning Write is not thread save : check that multiple tasks can't access this method simultaneously
  63. */
  64. int Write(Message* msg);
  65. /**
  66. * Function called at beginning of Read method
  67. * Use it to do some synchronization (call of mutex, for example)
  68. */
  69. virtual void Read_Pre() {}
  70. /**
  71. * Function called at end of Read method
  72. * Use it to do some synchronization (call of mutex, for example)
  73. */
  74. virtual void Read_Post() {}
  75. /**
  76. * Function called at beginning of Write method
  77. * Use it to do some synchronization (call of mutex, for example)
  78. */
  79. virtual void Write_Pre() {}
  80. /**
  81. * Function called at end of Write method
  82. * Use it to do some synchronization (call of mutex, for example)
  83. */
  84. virtual void Write_Post() {}
  85. static Message *Ping();
  86. protected:
  87. /**
  88. * Serial link file descriptor
  89. */
  90. int fd;
  91. /**
  92. * Convert an array of char to float
  93. * @param bytes Array of char, containing a binary image of a float
  94. * @return Float value
  95. */
  96. float CharToFloat(unsigned char *bytes);
  97. /**
  98. * Convert an array of char to boolean
  99. * @param bytes Array of char, containing a binary image of a boolean
  100. * @return Boolean value
  101. */
  102. bool CharToBool(unsigned char *bytes);
  103. /**
  104. * Convert an array of char to integer
  105. * @param bytes Array of char, containing a binary image of an integer
  106. * @return Integer value
  107. */
  108. unsigned int CharToInt(unsigned char *bytes);
  109. /**
  110. * Convert an array of char to its message representation (when receiving data from stm32)
  111. * @param bytes Array of char
  112. * @return Message corresponding to received array of char
  113. */
  114. Message* CharToMessage(unsigned char *bytes);
  115. /**
  116. * Convert a message to its array of char representation (for sending command to stm32)
  117. * @param msg Message to be sent to robot
  118. * @param buffer Array of char, image of message to send
  119. */
  120. void MessageToChar(Message *msg, unsigned char *buffer);
  121. };
  122. #endif /* __COMROBOT_H__ */