Fawkes API  Fawkes Development Version
direct_com_message.h
1 /***************************************************************************
2  * direct_com_message.h - Message for RobotinoDirectThread
3  *
4  * Created: Mon Apr 04 15:40:32 2016
5  * Copyright 2011-2016 Tim Niemueller [www.niemueller.de]
6  ****************************************************************************/
7 
8 /* This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Library General Public License for more details.
17  *
18  * Read the full text in the LICENSE.GPL file in the doc directory.
19  */
20 
21 #ifndef __PLUGINS_ROBOTINO_DIRECT_COM_MESSAGE_H_
22 #define __PLUGINS_ROBOTINO_DIRECT_COM_MESSAGE_H_
23 
24 #include <core/exception.h>
25 
26 #include <cstdint>
27 #include <boost/asio.hpp>
28 
30 {
31  public:
32  /// shared pointer to direct com message
33  typedef std::shared_ptr<DirectRobotinoComMessage> pointer;
34 
35  /// @cond INTERNAL
36  typedef enum {
37  CMDID_NONE = 0,
38  CMDID_GET_HW_VERSION = 1,
39  CMDID_HW_VERSION = 2,
40  CMDID_GET_SW_VERSION = 3,
41  CMDID_SW_VERSION = 4,
42  CMDID_GET_DISTANCE_SENSOR_READINGS = 5,
43  CMDID_DISTANCE_SENSOR_READINGS = 6,
44  CMDID_GET_ADC_PARAMETERS = 7,
45  CMDID_ADC_PARAMETERS = 8,
46  CMDID_SET_MOTOR_SPEED = 9,
47  CMDID_GET_ALL_MOTOR_SPEEDS = 10,
48  CMDID_ALL_MOTOR_SPEEDS = 11,
49  CMDID_SET_MOTOR_POSITION = 12,
50  CMDID_GET_ALL_MOTOR_POSITIONS = 13,
51  CMDID_ALL_MOTOR_POSITIONS = 14,
52  CMDID_SET_MOTOR_PID_PARAMETERS = 15,
53  CMDID_GET_ALL_MOTOR_PID_PARAMETERS = 16,
54  CMDID_ALL_MOTOR_PID_PARAMETERS = 17,
55  CMDID_SET_ALL_DIGITAL_OUTPUTS = 18,
56  CMDID_SET_ALL_RELAYS = 19,
57  CMDID_SET_ODOMETRY = 20,
58  CMDID_SET_ODOMETRY_ROTATION = 21,
59  CMDID_GET_ODOMETRY = 22,
60  CMDID_ODOMETRY = 23,
61  CMDID_GET_ALL_MOTOR_CURRENT_READINGS = 26,
62  CMDID_ALL_MOTOR_CURRENT_READINGS = 27,
63  CMDID_GET_ALL_ANALOG_INPUTS = 32,
64  CMDID_ALL_ANALOG_INPUTS = 33,
65  CMDID_GET_ALL_DIGITAL_INPUTS = 34,
66  CMDID_ALL_DIGITAL_INPUTS = 35,
67  CMDID_GET_BUMPER = 36,
68  CMDID_BUMPER = 37,
69  CMDID_GET_POWER_BUTTON = 38,
70  CMDID_POWER_BUTTON = 39,
71  CMDID_SET_FPGA_POWER = 40,
72  CMDID_GET_FPGA_POWER = 41,
73  CMDID_FPGA_POWER = 42,
74  CMDID_GET_PWR_OK_STATE = 43,
75  CMDID_PWR_OK_STATE = 44,
76  CMDID_SET_PWR_OK_STATE = 45,
77  CMDID_SET_PWM = 46,
78  CMDID_SET_MOTOR_ON = 47,
79  CMDID_SET_PWRBTN = 48,
80  CMDID_SET_SYS_RESET = 49,
81  CMDID_GET_COM_EXPRESS_STATES = 50,
82  CMDID_COM_EXPRESS_STATES = 51,
83  CMDID_GET_ALL_MOTOR_READINGS = 52,
84  CMDID_ALL_MOTOR_READINGS = 53,
85  CMDID_GET_IP_ADDRESS = 54,
86  CMDID_IP_ADDRESS = 55,
87  CMDID_SET_IP_ADDRESS = 56,
88  CMDID_SET_EMERGENCY_BUMPER = 57,
89  CMDID_SET_MOTOR_MODE = 58,
90  CMDID_RESET_LPC = 59,
91  CMDID_POWER_OFF = 60,
92  CMDID_SET_SAFTEY_VELOCITY = 61,
93  CMDID_GET_SAFTEY_VELOCITY = 62,
94  CMDID_SAFTEY_VELOCITY = 63,
95  CMDID_POWER_SOURCE_READINGS = 65,
96  CMDID_SET_MOTOR_ACCEL_LIMITS = 66,
97  CMDID_MOTOR_ACCEL_LIMITS = 67,
98  CMDID_GET_MOTOR_ACCEL_LIMITS = 68,
99  CMDID_GET_GYRO_Z_ANGLE = 69,
100  CMDID_GYRO_Z_ANGLE = 70,
101  CMDID_GET_CAN_MSG = 71,
102  CMDID_CAN_MSG = 72,
103  CMDID_SET_NRST = 73,
104  CMDID_GET_NRST = 74,
105  CMDID_NRST = 75,
106  CMDID_SET_BOOT = 76,
107  CMDID_GET_BOOT = 77,
108  CMDID_BOOT = 78,
109  CMDID_CONFIG_RESET = 79,
110  CMDID_CHARGER_INFO = 80,
111  CMDID_CHARGER_EVENT = 81,
112  CMDID_CHARGER_VERSION = 82,
113  CMDID_CHARGER_GET_VERSION = 83,
114  CMDID_CHARGER_CLEAR_ERROR = 84,
115  CMDID_CHARGER_ERROR = 85,
116  CMDID_SET_BATTERY_MIN = 86,
117  CMDID_GET_BATTERY_MIN = 87,
118  CMDID_BATTERY_MIN = 88,
119  CMDID_GET_GYRODATA = 89,
120  CMDID_GYRODATA = 90,
121  CMDID_GET_GPAIN = 91,
122  CMDID_GPAIN = 92,
123  CMDID_GET_VERSIONBITS = 93,
124  CMDID_VERSIONBITS = 94,
125  CMDID_GYRO_SET_PARAM = 95,
126  CMDID_GYRO_GET_PARAM = 96,
127  CMDID_GYRO_PARAM = 97,
128  CMDID_INFO = 250,
129  CMDID_WARNING = 251,
130  CMDID_ERROR = 252,
131  CMDID_LINGO_A = 253,
132  CMDID_LINGO_B = 254,
133  CMDID_LINGO_C = 255
134  } command_id_t;
135 
136  typedef enum {
137  READ,
138  WRITE
139  } mode_t;
140 
141  static const unsigned char MSG_HEAD;
142  static const unsigned char MSG_DATA_ESCAPE;
143  static const unsigned char MSG_DATA_MANGLE;
144  static const unsigned int MSG_METADATA_SIZE;
145  /// @endcond INTERNAL
146 
148  {
149  public:
150  ChecksumError(unsigned int expected, unsigned int received,
151  unsigned char byte1, unsigned char byte2);
152  };
153 
155  DirectRobotinoComMessage(command_id_t cmdid);
156  DirectRobotinoComMessage(const unsigned char *msg, size_t msg_size);
157  virtual ~DirectRobotinoComMessage();
158 
159  void add_command(command_id_t cmdid);
160  void add_int8(int8_t value);
161  void add_uint8(uint8_t value);
162  void add_int16(int16_t value);
163  void add_uint16(uint16_t value);
164  void add_int32(int32_t value);
165  void add_uint32(uint32_t value);
166  void add_float(float value);
167 
168  void rewind();
169  command_id_t next_command();
170  uint8_t command_length() const;
171  command_id_t command_id() const;
172  int8_t get_int8();
173  uint8_t get_uint8();
174  int16_t get_int16();
175  uint16_t get_uint16();
176  int32_t get_int32();
177  uint32_t get_uint32();
178  float get_float();
179  std::string get_string();
180 
181  void skip_int8();
182  void skip_uint8();
183  void skip_int16();
184  void skip_uint16();
185  void skip_int32();
186  void skip_uint32();
187  void skip_float();
188 
189  boost::asio::const_buffer buffer();
190 
191  void pack();
192  uint16_t checksum() const;
193 
194  std::string to_string(bool escaped = false);
195 
196  static size_t unescape(unsigned char *unescaped, size_t unescaped_size,
197  const unsigned char *escaped, size_t escaped_size);
198 
199  static uint16_t parse_uint16(const unsigned char *buf);
200 
201  size_t escaped_data_size();
202  size_t payload_size();
203  size_t data_size();
204 
205  private:
206  void ctor();
207  void assert_mode(mode_t mode) const;
208  void assert_command() const;
209  void assert_command_data(uint8_t size) const;
210 
211  void inc_payload_by(uint16_t count);
212  void escape();
213  size_t unescape_data();
214  void check_checksum() const;
215 
216  private:
217  mode_t mode_;
218 
219  unsigned char *data_;
220  unsigned short data_size_;
221  unsigned short payload_size_;
222  unsigned char *escaped_data_;
223  unsigned short escaped_data_size_;
224 
225  unsigned char *cur_cmd_;
226  unsigned char *cur_data_;
227 
228 };
229 
230 
231 #endif
232 
void add_uint32(uint32_t value)
Add 32-bit unsigned integer to current command.
int8_t get_int8()
Get 8-bit signed integer from current command.
void add_uint16(uint16_t value)
Add 16-bit unsigned integer to current command.
void add_command(command_id_t cmdid)
Add a command header.
ChecksumError(unsigned int expected, unsigned int received, unsigned char byte1, unsigned char byte2)
Constructor.
DirectRobotinoComMessage()
Constructor.
void add_uint8(uint8_t value)
Add 8-bit unsigned integer to current command.
void skip_uint16()
Skip 16-bit unsigned integer from current command.
void skip_int8()
Skip 8-bit signed integer from current command.
std::string to_string(bool escaped=false)
Generate string representation of message.
void rewind()
Rewind to read again from start.
std::string get_string()
Get string from current command.
void skip_float()
Skip float from current command.
virtual ~DirectRobotinoComMessage()
Destructor.
float get_float()
Get float from current command.
std::shared_ptr< DirectRobotinoComMessage > pointer
shared pointer to direct com message
boost::asio::const_buffer buffer()
Get access to buffer for sending.
Robotino communication message.
size_t data_size()
Get internal data buffer size.
size_t payload_size()
Get payload size.
void add_int16(int16_t value)
Add 16-bit signed integer to current command.
Base class for exceptions in Fawkes.
Definition: exception.h:36
void add_int32(int32_t value)
Add 32-bit signed integer to current command.
void skip_int32()
Skip 32-bit signed integer from current command.
void skip_int16()
Skip 16-bit signed integer from current command.
uint32_t get_uint32()
Get 32-bit unsigned integer from current command.
uint16_t get_uint16()
Get 16-bit unsigned integer from current command.
int32_t get_int32()
Get 32-bit signed integer from current command.
command_id_t next_command()
Get next available command.
static uint16_t parse_uint16(const unsigned char *buf)
Parse 16-bit unsigned integer from given buffer.
static size_t unescape(unsigned char *unescaped, size_t unescaped_size, const unsigned char *escaped, size_t escaped_size)
Unescape a number of unescaped bytes.
void skip_uint8()
Skip 8-bit unsigned integer from current command.
uint16_t checksum() const
Get checksum of message.
command_id_t command_id() const
Get ID of current command.
void skip_uint32()
Skip 32-bit unsigned integer from current command.
void add_int8(int8_t value)
Add 8-bit signed integer to current command.
int16_t get_int16()
Get 16-bit signed integer from current command.
uint8_t get_uint8()
Get 8-bit unsigned integer from current command.
size_t escaped_data_size()
Size of escaped buffer.
void add_float(float value)
Add float to current command.
uint8_t command_length() const
Get length of current command.
Excpetion thrown on checksum errors.