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 <boost/asio.hpp>
27#include <cstdint>
28
30{
31public:
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 { READ, WRITE } mode_t;
137
138 static const unsigned char MSG_HEAD;
139 static const unsigned char MSG_DATA_ESCAPE;
140 static const unsigned char MSG_DATA_MANGLE;
141 static const unsigned int MSG_METADATA_SIZE;
142 /// @endcond INTERNAL
143
145 {
146 public:
147 ChecksumError(unsigned int expected,
148 unsigned int received,
149 unsigned char byte1,
150 unsigned char byte2);
151 };
152
154 DirectRobotinoComMessage(command_id_t cmdid);
155 DirectRobotinoComMessage(const unsigned char *msg, size_t msg_size);
158
160
161 void add_command(command_id_t cmdid);
162 void add_int8(int8_t value);
163 void add_uint8(uint8_t value);
164 void add_int16(int16_t value);
165 void add_uint16(uint16_t value);
166 void add_int32(int32_t value);
167 void add_uint32(uint32_t value);
168 void add_float(float value);
169
170 void rewind();
171 command_id_t next_command();
172 uint8_t command_length() const;
173 command_id_t command_id() const;
174 int8_t get_int8();
175 uint8_t get_uint8();
176 int16_t get_int16();
177 uint16_t get_uint16();
178 int32_t get_int32();
179 uint32_t get_uint32();
180 float get_float();
181 std::string get_string();
182
183 void skip_int8();
184 void skip_uint8();
185 void skip_int16();
186 void skip_uint16();
187 void skip_int32();
188 void skip_uint32();
189 void skip_float();
190
191 boost::asio::const_buffer buffer();
192
193 void pack();
194 uint16_t checksum() const;
195
196 std::string to_string(bool escaped = false);
197
198 static size_t unescape(unsigned char * unescaped,
199 size_t unescaped_size,
200 const unsigned char *escaped,
201 size_t escaped_size);
202
203 static uint16_t parse_uint16(const unsigned char *buf);
204
205 size_t escaped_data_size();
206 size_t payload_size();
207 size_t data_size();
208
209private:
210 void ctor();
211 void assert_mode(mode_t mode) const;
212 void assert_command() const;
213 void assert_command_data(uint8_t size) const;
214
215 void inc_payload_by(uint16_t count);
216 void escape();
217 size_t unescape_data();
218 void check_checksum() const;
219
220private:
221 mode_t mode_;
222
223 unsigned char *data_;
224 unsigned short data_size_;
225 unsigned short payload_size_;
226 unsigned char *escaped_data_;
227 unsigned short escaped_data_size_;
228
229 unsigned char *cur_cmd_;
230 unsigned char *cur_data_;
231};
232
233#endif
Excpetion thrown on checksum errors.
ChecksumError(unsigned int expected, unsigned int received, unsigned char byte1, unsigned char byte2)
Constructor.
Robotino communication message.
uint16_t checksum() const
Get checksum of message.
int32_t get_int32()
Get 32-bit signed integer from current command.
std::string to_string(bool escaped=false)
Generate string representation of message.
size_t escaped_data_size()
Size of escaped buffer.
void add_uint16(uint16_t value)
Add 16-bit unsigned integer to current command.
void rewind()
Rewind to read again from start.
boost::asio::const_buffer buffer()
Get access to buffer for sending.
int8_t get_int8()
Get 8-bit signed integer from current command.
void add_command(command_id_t cmdid)
Add a command header.
void skip_float()
Skip float from current command.
uint8_t get_uint8()
Get 8-bit unsigned integer from current command.
void add_uint32(uint32_t value)
Add 32-bit unsigned integer to current command.
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_int32()
Skip 32-bit signed integer from current command.
void skip_uint16()
Skip 16-bit unsigned integer from current command.
void add_uint8(uint8_t value)
Add 8-bit unsigned integer to current command.
command_id_t next_command()
Get next available command.
std::shared_ptr< DirectRobotinoComMessage > pointer
shared pointer to direct com message
void add_int16(int16_t value)
Add 16-bit signed integer to current command.
void skip_int16()
Skip 16-bit signed integer from current command.
void skip_uint32()
Skip 32-bit unsigned integer from current command.
int16_t get_int16()
Get 16-bit signed integer from current command.
static uint16_t parse_uint16(const unsigned char *buf)
Parse 16-bit unsigned integer from given buffer.
size_t payload_size()
Get payload size.
size_t data_size()
Get internal data buffer size.
uint16_t get_uint16()
Get 16-bit unsigned integer from current command.
uint8_t command_length() const
Get length of current command.
void add_float(float value)
Add float to current command.
uint32_t get_uint32()
Get 32-bit unsigned integer from current command.
float get_float()
Get float from current command.
virtual ~DirectRobotinoComMessage()
Destructor.
void skip_uint8()
Skip 8-bit unsigned integer from current command.
DirectRobotinoComMessage & operator=(const DirectRobotinoComMessage &other)
Assignment operator.
void add_int8(int8_t value)
Add 8-bit signed integer to current command.
void add_int32(int32_t value)
Add 32-bit signed integer to current command.
std::string get_string()
Get string from current command.
void skip_int8()
Skip 8-bit signed integer from current command.
DirectRobotinoComMessage()
Constructor.
command_id_t command_id() const
Get ID of current command.
Base class for exceptions in Fawkes.
Definition: exception.h:36