forked from mhernando/Apolo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apoloMessage.cpp
270 lines (264 loc) · 6.9 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
#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 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 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 writeUInt16(char *message, int &num)
{
if(num>65535)num=65535;
if(num<0)num=0;
((uchar *)message)[0]=(uchar)(num%255);
((uchar *)message)[1]=(uchar)(num/255);
return 2;
}
inline void insertSize(char *message, int size)//size including the header
{
((uchar *)message)[2]=(uchar)(size%255);
((uchar *)message)[3]=(uchar)(size/255);
}
//tamaño minimo de mensaje es 5
inline int writeHeader(char*buffer,char command) //escribe la cabecera
{
int n=0;
buffer[0]='a';
buffer[1]='a';
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+=writeHeader(buffer,AP_SETJOINTS);//command
n+=writeString(buffer+n,world);//world
n+=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+=writeDouble(buffer+n,values[i]);
insertSize(buffer,n);
return n;
}
int ApoloMessage::writePlaceObject(char *buffer, char *world,char *object, double *xyzrpy)
{
int n=0,i;
n+=writeHeader(buffer,AP_PLACE);//command
n+=writeString(buffer+n,world);//world
n+=writeString(buffer+n,object);//object
for(i=0;i<6;i++)
n+=writeDouble(buffer+n,xyzrpy[i]);
insertSize(buffer,n);
return n;
}
int ApoloMessage::writeMoveWheeledBase(char *buffer, char *world,char *robot, double *sp_rs_t)
{
int n=0,i;
n+=writeHeader(buffer,AP_MOVE_WB);//command
n+=writeString(buffer+n,world);//world
n+=writeString(buffer+n,robot);//robot
for(i=0;i<3;i++)//speed, rot speed, time
n+=writeDouble(buffer+n,sp_rs_t[i]);
insertSize(buffer,n);
return n;
}
int ApoloMessage::writePlaceWheeledBase(char *buffer, char *world,char *robot, double *xyzy)
{
int n=0,i;
n+=writeHeader(buffer,AP_PLACE_WB);//command
n+=writeString(buffer+n,world);//world
n+=writeString(buffer+n,robot);//robot
for(i=0;i<4;i++)//x,y,z, rot z
n+=writeDouble(buffer+n,xyzy[i]);
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+=writeHeader(buffer,AP_GETLOCATION);//command
n+=writeString(buffer+n,world);//world
n+=writeString(buffer+n,object);//robot
insertSize(buffer,n);
return n;
}
int ApoloMessage::writeGetLocationWheeledBase(char *buffer, char *world,char *robot)
{
int n=0,i;
n+=writeHeader(buffer,AP_GETLOCATION_WB);//command
n+=writeString(buffer+n,world);//world
n+=writeString(buffer+n,robot);//robot
insertSize(buffer,n);
return n;
}
int ApoloMessage::writeUpdateWorld(char *buffer, char *world)
{
int n=0;
n+=writeHeader(buffer,AP_UPDATEWORLD);//command
n+=writeString(buffer+n,world);//world
insertSize(buffer,n);
return n;
}
int ApoloMessage::writeLinkToRobotTCP(char *buffer, char *world,char *robot,char *object)
{
int n=0,i;
n+=writeHeader(buffer,AP_LINK_TO_ROBOT_TCP);//command
n+=writeString(buffer+n,world);//world
n+=writeString(buffer+n,robot);//robot
n+=writeString(buffer+n,object);//robot
insertSize(buffer,n);
return n;
}
int ApoloMessage::writeDoubleVector(char *buffer, int num, double *d)
{
int n=0;
n+=writeHeader(buffer,AP_DVECTOR);//command
n+=writeUInt16(buffer+n,num);
for(int i=0;i<num;i++)
n+=writeDouble(buffer+n,d[i]);
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+=writeHeader(buffer,command);//command
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:
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;
}