Message.h
Go to the documentation of this file.
1 
47 #ifndef CLEARPATH_MESSAGE_H
48 #define CLEARPATH_MESSAGE_H
49 
50 #include <iostream>
51 #include <cstdlib>
52 #include <stdint.h>
53 
55 
56 
57 namespace clearpath
58 {
59 
60  class MessageException : public Exception
61  {
62  public:
63  enum errors
64  {
67  };
68 
69  public:
70  enum errors type;
71 
72  MessageException(const char *msg, enum errors ex_type = ERROR_BASE);
73  };
74 
75  class Message
76  {
77  public:
78  static const size_t MAX_MSG_LENGTH = 256;
79 
80 
81  protected:
82  static const size_t CRC_LENGTH = 2;
83  static const uint16_t CRC_INIT_VAL = 0xFFFF;
84 
85  static const size_t HEADER_LENGTH = 12;
86 
87  // Offsets of fields within data
89  {
90  SOH_OFST = 0,
95  FLAGS_OFST = 8,
97  STX_OFST = 11,
98  PAYLOAD_OFST
99  };
100 
101  uint8_t data[MAX_MSG_LENGTH];
102  // Total length (incl. full header & checksum)
103  size_t total_len;
104 
105  // Whether this Message has ever been sent by the Transport()
106  // (Updated by Transport::send())
107  bool is_sent;
108 
109  friend class Transport; // Allow Transport to read data and total_len directly
110 
111  public:
112  static const size_t MIN_MSG_LENGTH = HEADER_LENGTH + CRC_LENGTH;
113  static const uint8_t SOH = 0xAA;
114  static const uint8_t STX = 0x55;
115 
116  protected:
117  size_t crcOffset()
118  {
119  return total_len - CRC_LENGTH;
120  };
121 
122  void setLength(uint8_t len);
123 
124  void setVersion(uint8_t version);
125 
126  void setTimestamp(uint32_t timestamp);
127 
128  void setFlags(uint8_t flags);
129 
130  void setType(uint16_t type);
131 
132  uint8_t *getPayloadPointer(size_t offset = 0);
133 
134  void setPayload(void *buf, size_t buf_size);
135 
136  void setPayloadLength(uint8_t len);
137 
138  void makeValid();
139 
140  public:
141  Message();
142 
143  Message(void *input, size_t msg_len);
144 
145  Message(const Message &other);
146 
147  Message(uint16_t type, uint8_t *payload, size_t payload_len,
148  uint32_t timestamp = 0, uint8_t flags = 0, uint8_t version = 0);
149 
150  virtual ~Message();
151 
152  void send();
153 
154  uint8_t getLength(); // as reported by packet length field.
155  uint8_t getLengthComp();
156 
157  uint8_t getVersion();
158 
159  uint32_t getTimestamp();
160 
161  uint8_t getFlags();
162 
163  uint16_t getType();
164 
165  uint16_t getChecksum();
166 
168  {
169  return total_len - HEADER_LENGTH - CRC_LENGTH;
170  }
171 
172  size_t getPayload(void *buf, size_t max_size);
173 
174  size_t getTotalLength()
175  {
176  return total_len;
177  }
178 
179  size_t toBytes(void *buf, size_t buf_size);
180 
181  bool isValid(char *whyNot = NULL, size_t strLen = 0);
182 
183  bool isCommand()
184  {
185  return getType() < 0x4000;
186  }
187 
188  bool isRequest()
189  {
190  return (getType() >= 0x4000) && (getType() < 0x8000);
191  }
192 
193  bool isData()
194  {
195  return (getType() >= 0x8000) && (getType() < 0xC000);
196  }
197 
198  virtual std::ostream &printMessage(std::ostream &stream = std::cout);
199 
200  void printRaw(std::ostream &stream = std::cout);
201 
202 
203  static Message *factory(void *input, size_t msg_len);
204 
205  static Message *popNext();
206 
207  static Message *waitNext(double timeout = 0.0);
208 
209  }; // class Message
210 
212  {
213  /*
214  * Set commands
215  */
224  SET_TURN_SETPT = 0x0205,
225  SET_MAX_SPEED = 0x0210,
226  SET_MAX_ACCEL = 0x0211,
230  SET_GPIO_OUTPUT = 0x0302,
232 
233  /*
234  * Command commands
235  */
239 
240  /*
241  * Request commands
242  */
243  REQUEST_ECHO = 0x4000,
265  REQUEST_ORIENT = 0x4600,
267  REQUEST_ACCEL = 0x4602,
268  REQUEST_6AXIS = 0x4603,
270  REQUEST_ENCODER = 0x4800,
272 
273  /*
274  * Data commands
275  */
276  DATA_ECHO = 0x8000,
289  DATA_TURN_SETPT = 0x8205,
290  DATA_MAX_SPEED = 0x8210,
291  DATA_MAX_ACCEL = 0x8211,
292  DATA_GEAR_SETPT = 0x8212,
299  DATA_ORIENT = 0x8600,
300  DATA_ROT_RATE = 0x8601,
301  DATA_ACCEL = 0x8602,
302  DATA_6AXIS = 0x8603,
305  DATA_ENCODER = 0x8800,
310  DATA_ORIENT_RAW = 0xA113,
311  DATA_GYRO_RAW = 0xA114,
312  DATA_ACCEL_RAW = 0xA115,
314  }; // enum MessageTypes
315 
316 }; // namespace clearpath
317 
318 std::ostream &operator<<(std::ostream &stream, clearpath::Message &msg);
319 
320 #endif // CLEARPATH_MESSAGE_H
data
std::ostream & operator<<(std::ostream &stream, clearpath::Message &msg)
Definition: Message.cpp:498
size_t crcOffset()
Definition: Message.h:117
size_t total_len
Definition: Message.h:103
MessageException(const char *msg, enum errors ex_type=ERROR_BASE)
Definition: Message.cpp:66
size_t getTotalLength()
Definition: Message.h:174
MessageTypes
Definition: Message.h:211
const ros::Time & getTimestamp(const T &t)
size_t getPayloadLength()
Definition: Message.h:167
bool isRequest()
Definition: Message.h:188
bool isCommand()
Definition: Message.h:183
bool isValid(const trajectory_msgs::JointTrajectoryPoint &point, const unsigned int joint_dim)


husky_base
Author(s): Mike Purvis , Paul Bovbel
autogenerated on Fri Oct 2 2020 03:40:07