00001
00047 #ifndef CLEARPATH_MESSAGE_H
00048 #define CLEARPATH_MESSAGE_H
00049
00050 #include <iostream>
00051 #include <cstdlib>
00052 #include <stdint.h>
00053
00054 #include "husky_base/horizon_legacy/Exception.h"
00055
00056
00057 namespace clearpath
00058 {
00059
00060 class MessageException : public Exception
00061 {
00062 public:
00063 enum errors
00064 {
00065 ERROR_BASE = 0,
00066 INVALID_LENGTH
00067 };
00068
00069 public:
00070 enum errors type;
00071
00072 MessageException(const char *msg, enum errors ex_type = ERROR_BASE);
00073 };
00074
00075 class Message
00076 {
00077 public:
00078 static const size_t MAX_MSG_LENGTH = 256;
00079
00080
00081 protected:
00082 static const size_t CRC_LENGTH = 2;
00083 static const uint16_t CRC_INIT_VAL = 0xFFFF;
00084
00085 static const size_t HEADER_LENGTH = 12;
00086
00087
00088 enum dataOffsets
00089 {
00090 SOH_OFST = 0,
00091 LENGTH_OFST,
00092 LENGTH_COMP_OFST,
00093 VERSION_OFST,
00094 TIMESTAMP_OFST,
00095 FLAGS_OFST = 8,
00096 TYPE_OFST,
00097 STX_OFST = 11,
00098 PAYLOAD_OFST
00099 };
00100
00101 uint8_t data[MAX_MSG_LENGTH];
00102
00103 size_t total_len;
00104
00105
00106
00107 bool is_sent;
00108
00109 friend class Transport;
00110
00111 public:
00112 static const size_t MIN_MSG_LENGTH = HEADER_LENGTH + CRC_LENGTH;
00113 static const uint8_t SOH = 0xAA;
00114 static const uint8_t STX = 0x55;
00115
00116 protected:
00117 size_t crcOffset()
00118 {
00119 return total_len - CRC_LENGTH;
00120 };
00121
00122 void setLength(uint8_t len);
00123
00124 void setVersion(uint8_t version);
00125
00126 void setTimestamp(uint32_t timestamp);
00127
00128 void setFlags(uint8_t flags);
00129
00130 void setType(uint16_t type);
00131
00132 uint8_t *getPayloadPointer(size_t offset = 0);
00133
00134 void setPayload(void *buf, size_t buf_size);
00135
00136 void setPayloadLength(uint8_t len);
00137
00138 void makeValid();
00139
00140 public:
00141 Message();
00142
00143 Message(void *input, size_t msg_len);
00144
00145 Message(const Message &other);
00146
00147 Message(uint16_t type, uint8_t *payload, size_t payload_len,
00148 uint32_t timestamp = 0, uint8_t flags = 0, uint8_t version = 0);
00149
00150 virtual ~Message();
00151
00152 void send();
00153
00154 uint8_t getLength();
00155 uint8_t getLengthComp();
00156
00157 uint8_t getVersion();
00158
00159 uint32_t getTimestamp();
00160
00161 uint8_t getFlags();
00162
00163 uint16_t getType();
00164
00165 uint16_t getChecksum();
00166
00167 size_t getPayloadLength()
00168 {
00169 return total_len - HEADER_LENGTH - CRC_LENGTH;
00170 }
00171
00172 size_t getPayload(void *buf, size_t max_size);
00173
00174 size_t getTotalLength()
00175 {
00176 return total_len;
00177 }
00178
00179 size_t toBytes(void *buf, size_t buf_size);
00180
00181 bool isValid(char *whyNot = NULL, size_t strLen = 0);
00182
00183 bool isCommand()
00184 {
00185 return getType() < 0x4000;
00186 }
00187
00188 bool isRequest()
00189 {
00190 return (getType() >= 0x4000) && (getType() < 0x8000);
00191 }
00192
00193 bool isData()
00194 {
00195 return (getType() >= 0x8000) && (getType() < 0xC000);
00196 }
00197
00198 virtual std::ostream &printMessage(std::ostream &stream = std::cout);
00199
00200 void printRaw(std::ostream &stream = std::cout);
00201
00202
00203 static Message *factory(void *input, size_t msg_len);
00204
00205 static Message *popNext();
00206
00207 static Message *waitNext(double timeout = 0.0);
00208
00209 };
00210
00211 enum MessageTypes
00212 {
00213
00214
00215
00216 SET_PLATFORM_NAME = 0x0002,
00217 SET_PLATFORM_TIME = 0x0005,
00218 SET_SAFETY_SYSTEM = 0x0010,
00219 SET_DIFF_WHEEL_SPEEDS = 0x0200,
00220 SET_DIFF_CTRL_CONSTS = 0x0201,
00221 SET_DIFF_WHEEL_SETPTS = 0x0202,
00222 SET_ACKERMANN_SETPT = 0x0203,
00223 SET_VELOCITY_SETPT = 0x0204,
00224 SET_TURN_SETPT = 0x0205,
00225 SET_MAX_SPEED = 0x0210,
00226 SET_MAX_ACCEL = 0x0211,
00227 SET_GEAR_SETPOINT = 0x0212,
00228 SET_GPADC_OUTPUT = 0x0300,
00229 SET_GPIO_DIRECTION = 0x0301,
00230 SET_GPIO_OUTPUT = 0x0302,
00231 SET_PTZ_POSITION = 0x0400,
00232
00233
00234
00235
00236 CMD_PROCESSOR_RESET = 0x2000,
00237 CMD_RESTORE_SETTINGS = 0x2001,
00238 CMD_STORE_SETTINGS = 0x2002,
00239
00240
00241
00242
00243 REQUEST_ECHO = 0x4000,
00244 REQUEST_PLATFORM_INFO = 0x4001,
00245 REQUEST_PLATFORM_NAME = 0x4002,
00246 REQUEST_FIRMWARE_INFO = 0x4003,
00247 REQUEST_SYSTEM_STATUS = 0x4004,
00248 REQUEST_POWER_SYSTEM = 0x4005,
00249 REQUEST_SAFETY_SYSTEM = 0x4010,
00250 REQUEST_DIFF_WHEEL_SPEEDS = 0x4200,
00251 REQUEST_DIFF_CTRL_CONSTS = 0x4201,
00252 REQUEST_DIFF_WHEEL_SETPTS = 0x4202,
00253 REQUEST_ACKERMANN_SETPTS = 0x4203,
00254 REQUEST_VELOCITY_SETPT = 0x4204,
00255 REQUEST_TURN_SETPT = 0x4205,
00256 REQUEST_MAX_SPEED = 0x4210,
00257 REQUEST_MAX_ACCEL = 0x4211,
00258 REQUEST_GEAR_SETPT = 0x4212,
00259 REQUEST_GPADC_OUTPUT = 0x4300,
00260 REQUEST_GPIO_STATUS = 0x4301,
00261 REQUEST_GPADC_INPUT = 0x4303,
00262 REQUEST_PTZ_POSITION = 0x4400,
00263 REQUEST_DISTANCE_DATA = 0x4500,
00264 REQUEST_DISTANCE_TIMING = 0x4501,
00265 REQUEST_ORIENT = 0x4600,
00266 REQUEST_ROT_RATE = 0x4601,
00267 REQUEST_ACCEL = 0x4602,
00268 REQUEST_6AXIS = 0x4603,
00269 REQUEST_6AXIS_ORIENT = 0x4604,
00270 REQUEST_ENCODER = 0x4800,
00271 REQUEST_ENCODER_RAW = 0x4801,
00272
00273
00274
00275
00276 DATA_ECHO = 0x8000,
00277 DATA_PLATFORM_INFO = 0x8001,
00278 DATA_PLATFORM_NAME = 0x8002,
00279 DATA_FIRMWARE_INFO = 0x8003,
00280 DATA_SYSTEM_STATUS = 0x8004,
00281 DATA_POWER_SYSTEM = 0x8005,
00282 DATA_PROC_STATUS = 0x8006,
00283 DATA_SAFETY_SYSTEM = 0x8010,
00284 DATA_DIFF_WHEEL_SPEEDS = 0x8200,
00285 DATA_DIFF_CTRL_CONSTS = 0x8201,
00286 DATA_DIFF_WHEEL_SETPTS = 0x8202,
00287 DATA_ACKERMANN_SETPTS = 0x8203,
00288 DATA_VELOCITY_SETPT = 0x8204,
00289 DATA_TURN_SETPT = 0x8205,
00290 DATA_MAX_SPEED = 0x8210,
00291 DATA_MAX_ACCEL = 0x8211,
00292 DATA_GEAR_SETPT = 0x8212,
00293 DATA_GPADC_OUTPUT = 0x8300,
00294 DATA_GPIO_STATUS = 0x8301,
00295 DATA_GPADC_INPUT = 0x8303,
00296 DATA_PTZ_POSITION = 0x8400,
00297 DATA_DISTANCE_DATA = 0x8500,
00298 DATA_DISTANCE_TIMING = 0x8501,
00299 DATA_ORIENT = 0x8600,
00300 DATA_ROT_RATE = 0x8601,
00301 DATA_ACCEL = 0x8602,
00302 DATA_6AXIS = 0x8603,
00303 DATA_6AXIS_ORIENT = 0x8604,
00304 DATA_MAGNETOMETER = 0x8606,
00305 DATA_ENCODER = 0x8800,
00306 DATA_ENCODER_RAW = 0x8801,
00307 DATA_CURRENT_RAW = 0xA110,
00308 DATA_VOLTAGE_RAW = 0xA111,
00309 DATA_TEMPERATURE_RAW = 0xA112,
00310 DATA_ORIENT_RAW = 0xA113,
00311 DATA_GYRO_RAW = 0xA114,
00312 DATA_ACCEL_RAW = 0xA115,
00313 DATA_MAGNETOMETER_RAW = 0xA116
00314 };
00315
00316 };
00317
00318 std::ostream &operator<<(std::ostream &stream, clearpath::Message &msg);
00319
00320 #endif // CLEARPATH_MESSAGE_H