-
Notifications
You must be signed in to change notification settings - Fork 5
/
apoloMessage.cpp
361 lines (351 loc) · 10 KB
/
apoloMessage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include "apoloMessage.h"
#include <string.h>
/**UTILITY UNIONS FOR CONVERSIONS**/
union double2byteConversor
{
char bytes[8];
double real;
};
union int2byteConversor
{
char bytes[4];
int integer;
};
typedef unsigned char uchar;
/**
writes intp buffer the message for moving a robot in a worl
if world=null... is equivalent to any.
returns the message size
**/
inline int Apolo_writeString(char *buffer, char *cad){
int n=0,len;
if(cad!=0){ //not null
if(cad[0]==0)buffer[n++]=0;//empty string
else{
len=1+(uchar)strlen(cad);
((uchar *)buffer)[n++]=(uchar)((len>255)?255:len);
for(int i=0;i<len-1;i++)buffer[n++]=cad[i];
buffer[n++]=0;
}
}else buffer[n++]=0;
return n;
}
inline int Apolo_writeDouble(char *buffer, double val){
double2byteConversor aux;
aux.real=val;
for(int i=0;i<8;i++)buffer[i]=aux.bytes[i];
return 8;
}
inline int Apolo_writeUInt16(char *message, int &num)
{
if(num>65535)num=65535;
if(num<0)num=0;
((uchar *)message)[0]=(uchar)(num%256);
((uchar *)message)[1]=(uchar)(num/256);
return 2;
}
inline void Apolo_insertSize(char *message, int size)//size including the header
{
((uchar *)message)[2]=(uchar)(size%256);
((uchar *)message)[3]=(uchar)(size/256);
}
//tamaño minimo de mensaje es 5
inline int Apolo_writeHeader(char*buffer,char command) //escribe la cabecera
{
int n=0;
buffer[0]='a';
buffer[1]='a';
Apolo_insertSize(buffer,5);
buffer[4]=command;
return 5;
}
int ApoloMessage::writeSetRobotJoints(char *buffer, char *world, char *robot, int num, double *values)
{
int n=0;
n+= Apolo_writeHeader(buffer,AP_SETJOINTS);//command
n+= Apolo_writeString(buffer+n,world);//world
n+= Apolo_writeString(buffer+n,robot);//robot
if(num<0)num=0;
if(num>255)num=255;
((uchar *)buffer)[n++]=(uchar)num;//num joints
for(int i=0;i<num;i++)
n+= Apolo_writeDouble(buffer+n,values[i]);
Apolo_insertSize(buffer,n);
return n;
}
int ApoloMessage::writePlaceObject(char *buffer, char *world,char *object, double *xyzrpy)
{
int n=0,i;
n+= Apolo_writeHeader(buffer,AP_PLACE);//command
n+= Apolo_writeString(buffer+n,world);//world
n+= Apolo_writeString(buffer+n,object);//object
for(i=0;i<6;i++)
n+= Apolo_writeDouble(buffer+n,xyzrpy[i]);
Apolo_insertSize(buffer,n);
return n;
}
int ApoloMessage::writeMoveWheeledBase(char *buffer, char *world,char *robot, double *sp_rs_t)
{
int n=0,i;
n+= Apolo_writeHeader(buffer,AP_MOVE_WB);//command
n+= Apolo_writeString(buffer+n,world);//world
n+= Apolo_writeString(buffer+n,robot);//robot
for(i=0;i<3;i++)//speed, rot speed, time
n+= Apolo_writeDouble(buffer+n,sp_rs_t[i]);
Apolo_insertSize(buffer,n);
return n;
}
int ApoloMessage::writePlaceWheeledBase(char *buffer, char *world,char *robot, double *xyzy)
{
int n=0,i;
n+= Apolo_writeHeader(buffer,AP_PLACE_WB);//command
n+= Apolo_writeString(buffer+n,world);//world
n+= Apolo_writeString(buffer+n,robot);//robot
for(i=0;i<4;i++)//x,y,z, rot z
n+= Apolo_writeDouble(buffer+n,xyzy[i]);
Apolo_insertSize(buffer,n);
return n;
}
//the same message But changes the command id
int ApoloMessage::writeCheckColision(char *buffer, char *world, char *robot, int num, double *values)
{
int n=writeSetRobotJoints(buffer,world,robot,num,values);
buffer[4]=AP_CHECKJOINTS;
return n;
}
int ApoloMessage::writeGetLocation(char *buffer, char *world,char *object)
{
int n=0,i;
n+= Apolo_writeHeader(buffer,AP_GETLOCATION);//command
n+= Apolo_writeString(buffer+n,world);//world
n+= Apolo_writeString(buffer+n,object);//robot
Apolo_insertSize(buffer,n);
return n;
}
int ApoloMessage::writeGetLocationWheeledBase(char *buffer, char *world,char *robot)
{
int n=0,i;
n+= Apolo_writeHeader(buffer,AP_GETLOCATION_WB);//command
n+= Apolo_writeString(buffer+n,world);//world
n+= Apolo_writeString(buffer+n,robot);//robot
Apolo_insertSize(buffer,n);
return n;
}
int ApoloMessage::writeUpdateWorld(char *buffer, char *world)
{
int n=0;
n+= Apolo_writeHeader(buffer,AP_UPDATEWORLD);//command
n+= Apolo_writeString(buffer+n,world);//world
Apolo_insertSize(buffer,n);
return n;
}
int ApoloMessage::writeLinkToRobotTCP(char *buffer, char *world,char *robot,char *object)
{
int n=0,i;
n+= Apolo_writeHeader(buffer,AP_LINK_TO_ROBOT_TCP);//command
n+= Apolo_writeString(buffer+n,world);//world
n+= Apolo_writeString(buffer+n,robot);//robot
n+= Apolo_writeString(buffer+n,object);//robot
Apolo_insertSize(buffer,n);
return n;
}
int ApoloMessage::writeGetLaserData(char *buffer, char *world, char *laser)
{
int n = 0;
n += Apolo_writeHeader(buffer, AP_GET_LASER_DATA);//command
n += Apolo_writeString(buffer + n, world);//world
n += Apolo_writeString(buffer + n, laser);//laser
Apolo_insertSize(buffer, n);
return n;
}
int ApoloMessage::writeGetLaserLandMarks(char *buffer, char *world, char *laser)//AP_GET_LASER_LM
{
int n = 0;
n += Apolo_writeHeader(buffer, AP_GET_LASER_LM);//command
n += Apolo_writeString(buffer + n, world);//world
n += Apolo_writeString(buffer + n, laser);//laser
Apolo_insertSize(buffer, n);
return n;
}
int ApoloMessage::writeGetOdometry(char *buffer, char *world, char *robot)
{
int n = 0;
n += Apolo_writeHeader(buffer, AP_GET_WB_ODOMETRY);//command
n += Apolo_writeString(buffer + n, world);//world
n += Apolo_writeString(buffer + n, robot);//robot
Apolo_insertSize(buffer, n);
return n;
}
int ApoloMessage::writeGetUltrasonicSensor(char *buffer, char *world, char* name)
{
int n = 0;
n += Apolo_writeHeader(buffer, AP_GET_USENSOR);//command
n += Apolo_writeString(buffer + n, world);//world
n += Apolo_writeString(buffer + n, name);//laser
Apolo_insertSize(buffer, n);
return n;
}
int ApoloMessage::writeGetDependentUltrasonicSensors(char *buffer, char *world, char* object)
{
int n = 0;
n += Apolo_writeHeader(buffer, AP_GET_DEP_USENSORS);//command
n += Apolo_writeString(buffer + n, world);//world
n += Apolo_writeString(buffer + n, object);//laser
Apolo_insertSize(buffer, n);
return n;
}
int ApoloMessage::writeResetOdometry(char *buffer, char *world, char *robot, double *xyt) //AP_RESET_ODOMETRY
{
int n = 0, i;
n += Apolo_writeHeader(buffer, AP_RESET_ODOMETRY);//command
n += Apolo_writeString(buffer + n, world);//world
n += Apolo_writeString(buffer + n, robot);//robot
for (i = 0; i<3; i++)//x,y, rot z
n += Apolo_writeDouble(buffer + n, xyt[i]);
Apolo_insertSize(buffer, n);
return n;
}
int ApoloMessage::writeDoubleVector(char *buffer, int num, double *d)
{
int n=0;
n+= Apolo_writeHeader(buffer,AP_DVECTOR);//command
n+= Apolo_writeUInt16(buffer+n,num);
for(int i=0;i<num;i++)
n+= Apolo_writeDouble(buffer+n,d[i]);
Apolo_insertSize(buffer,n);
return n;
}
int ApoloMessage::writeDoubleVector(char *buffer, std::vector<double> v)
{
int n = 0;
n += Apolo_writeHeader(buffer, AP_DVECTOR);//command
int num = (int)v.size();
n += Apolo_writeUInt16(buffer + n, num);
for (int i=0; i<num; ++i)
n += Apolo_writeDouble(buffer + n, v[i]);
Apolo_insertSize(buffer, n);
return n;
}
int ApoloMessage::writeLandMarkInfoVector(char *buffer, std::vector<mr::LaserSensorSim::LandMarkInfo> &v)
{
int n = 0;
n += Apolo_writeHeader(buffer, AP_LM_INFO);//command
int num = (int)v.size();
n += Apolo_writeUInt16(buffer + n, num);
for (int i = 0; i < num; ++i) {
n += Apolo_writeUInt16(buffer + n, v[i].ID);
n += Apolo_writeDouble(buffer + n, v[i].ang);
n += Apolo_writeDouble(buffer + n, v[i].dist);
}
Apolo_insertSize(buffer, n);
return n;
}
int ApoloMessage::writeBOOL(char *buffer, bool val)
{
int n=0;
char command=AP_FALSE;
if(val)command=AP_TRUE;
n+= Apolo_writeHeader(buffer,command);//command
Apolo_insertSize(buffer,n);
return n;
}
ApoloMessage::ApoloMessage(char *buffer,int size,char type)
{
char *aux;
pData=buffer;
this->size=size;
this->type=type;
if(type==AP_NONE)return;
world=bindata=name=0;
switch(type)
{//command with world and name
case AP_SETJOINTS:
case AP_CHECKJOINTS:
case AP_PLACE:
case AP_PLACE_WB:
case AP_MOVE_WB:
case AP_GETLOCATION_WB:
case AP_GETLOCATION:
case AP_LINK_TO_ROBOT_TCP:
case AP_GET_LASER_DATA:
case AP_GET_WB_ODOMETRY:
case AP_GET_USENSOR:
case AP_GET_DEP_USENSORS:
case AP_GET_LASER_LM:
case AP_RESET_ODOMETRY:
if(pData[5]!=0){
world=pData+6;
aux=world+((uchar *)pData)[5];
}else aux=pData+6;
if(aux[0]!=0){
name=aux+1;
aux=name+((uchar *)aux)[0];
}else aux++;
bindata=aux;
break;
case AP_UPDATEWORLD://commands with world only
if(pData[5]!=0){
world=pData+6;
aux=world+((uchar *)pData)[5];
}else aux=pData+6;
bindata=aux;
break;
default: //commands without world neither
bindata=pData+5;
break;
}
}
//se considera que el buffer contiene mensajes completos (pueden ser varios)... si son parciales, se desecharán
ApoloMessage *ApoloMessage::getApoloMessage(char **buffer, int max)
{
int i=0;
while(i+4<max){
if(((*buffer)[i]=='a')&&((*buffer)[i+1]=='a'))
{
int size=(((uchar *)(*buffer))[i+2])+(((uchar *)(*buffer))[i+3])*255;
char type=(*buffer)[i+4];
//si el mensaje es correcto... crea el mensaje y situa el puntero al final
//ojo... el mensaje mantiene unos punteros sobre el buffer original. El mensaje no reserva memoria
if(i+size<=max){
ApoloMessage *message=new ApoloMessage((*buffer)+i,size,type);
*buffer=*buffer+size;
return message;
}//si no lo es retorna null
else return 0;
}
i++;
}
return 0;
}
int ApoloMessage::getIntAt(int offset)
{
int2byteConversor aux;
if(offset+(bindata-pData)+4>size)return 0;
for(int i=0;i<4;i++)aux.bytes[i]=bindata[offset+i];
return aux.integer;
}
int ApoloMessage::getUInt16At(int offset)
{
if(offset+(bindata-pData)+2>size)return 0;
return (((uchar *)(bindata))[offset])+(((uchar *)(bindata))[offset+1])*255;
}
double ApoloMessage::getDoubleAt(int offset)
{
double2byteConversor aux;
if(offset+(bindata-pData)+8>size)return 0;
for(int i=0;i<8;i++)aux.bytes[i]=bindata[offset+i];
return aux.real;
}
char ApoloMessage::getCharAt(int offset)
{
if(offset+(bindata-pData)+1>size)return 0;
return bindata[offset];
}
char *ApoloMessage::getStringAt(int offset)
{
if(offset+(bindata-pData)+1>size)return 0;
uchar tam=((uchar *)(bindata))[offset];
if(tam==0)return 0;
if(offset+(bindata-pData)+tam+1>size)return 0;
else return bindata+offset+1;
}