-
Notifications
You must be signed in to change notification settings - Fork 5
/
apoloPort.cpp
321 lines (289 loc) · 8.52 KB
/
apoloPort.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
#include "apoloPort.h"
#include "apoloMessage.h"
#define TAM_APOLO_BUFFER 90000
ApoloPort::ApoloPort(int port,vector<SimulatedWorld*>*listWorlds)
{
world=listWorlds;
sock=new Socket();
sock->initServer(port);
Thid.Start(&ApoloPort::handleConnections,this,sock);
}
bool ApoloPort::getDependentUltrasonicSensors(PositionableEntity *object, vector<UltrasonicSensor *> &v)
{
ReferenceSystem *location = object->getReferenciableLocation();
int i, n = location->getNumberOfDependents();
UltrasonicSensor *aux;
for (i = 0; i<n; i++) {
aux = dynamic_cast<UltrasonicSensor *>((location->getDependent(i))->getOwner());
if (aux)v.push_back(aux);
}
if (v.size() > 0)return true;
return false;
}
PositionableEntity *ApoloPort::getElement(char *nworld,char *name,int *worldindex)
{
//if nworld=0 busca en todos los mundos
PositionableEntity *elem=0;
*worldindex=-1;
if(nworld==0){
if(name==0)return 0;
for(int i=0; i<world->size();i++){
elem=(*world)[i]->getWorld()->getObjectByName(name);
if(elem){
*worldindex=i;
return elem;
}
}
}
else
{
if(name==0)return 0;
for(int i=0; i<world->size();i++){
if((*world)[i]->getName().compare(nworld)==0)
{
*worldindex=i;
return ((*world)[i]->getWorld()->getObjectByName(name));
}
}
}
//world & element not found
return 0;
}
void *ApoloPort::handleConnections(void *server)
{
Socket *aux_sock= (Socket*) server;
Socket socket=*aux_sock;
PositionableEntity *pos;
RobotSim *robot;
LaserSensorSim *laser;
WheeledBaseSim *wb;
static int valid=0, total=0;
while(1)
{
Socket *temp=socket.acceptConnection();
char buffer[TAM_APOLO_BUFFER];
int size = 0;
while(temp->IsConnected())
{
int lect = 0;
if(0<(lect=temp->Receive(buffer+size, TAM_APOLO_BUFFER -size,-1)))
{
size += lect;
ApoloMessage *m=0;
char *pbuffer=buffer;//recorrepbuffer
while(m=ApoloMessage::getApoloMessage(&pbuffer,size))
{
size -= m->getSize();
total++;
char *nworld=m->getWorld();
char *name=m->getObjectName();
int worldindex=0;
PositionableEntity *element=getElement(nworld,name,&worldindex);
//DISABLES ALL AUTOMATIC SIMULATIONS To avoid concurrents access
//TODO: not stoping the simulation... just avoid the concurrency (disable on timer while procesing a single message)
for (int i = 0; i < world->size(); i++)
(*world)[i]->getChild()->SetPlaySimu(false);
//END OF IT
switch(m->getType())
{
case AP_CHECKJOINTS:
case AP_SETJOINTS:
if(element){
robot=dynamic_cast<RobotSim*>(element);
int numJoints=m->getCharAt(0);
for(int i=0;i<numJoints;i++)
if(robot)robot->setJointValue(i,m->getDoubleAt(1+8*i));
if(m->getType()==AP_CHECKJOINTS){
bool res=false;
if(robot){
res=robot->checkRobotColision();
}
char resp[50];
int tam=ApoloMessage::writeBOOL(resp,res);
temp->Send(resp,tam);
}
valid++;
}
break;
case AP_GETLOCATION:
case AP_GETLOCATION_WB:
if(element){
double d[6];
Vector3D p=element->getRelativePosition();
double o[3];
element->getRelativeOrientation(d[3],d[4],d[5]);
for(int i=0;i<3;i++)d[i]=p[i];
char resp[70];
int tam;
if(m->getType()==AP_GETLOCATION)tam=ApoloMessage::writeDoubleVector(resp,6,d);
else {
d[3]=d[5];
tam=ApoloMessage::writeDoubleVector(resp,4,d);
}
temp->Send(resp,tam);
valid++;
}
break;
case AP_PLACE:
if(element){
double d[6];
for(int i=0;i<6;i++)d[i]=m->getDoubleAt(8*i);
element->setAbsoluteT3D(Transformation3D(d[0],d[1],d[2],d[3],d[4],d[5]));
valid++;
}
break;
case AP_PLACE_WB: //place a wheeled based robot. computes the grounded location and collision
if(element){
wb=dynamic_cast<WheeledBaseSim *>(element);
bool res=false;
double d[4];
for(int i=0;i<4;i++)d[i]=m->getDoubleAt(8*i);
Transformation3D taux(d[0],d[1],d[2],mr::Z_AXIS,d[3]);
if(wb)res=wb->dropWheeledBase(taux);
if(res){
res=!(wb->getWorld()->checkCollisionWith(*wb));
}
char resp[50];
int tam=ApoloMessage::writeBOOL(resp,res);
temp->Send(resp,tam);
valid++;
}
break;
case AP_MOVE_WB:
if(element){
wb=dynamic_cast<WheeledBaseSim *>(element);
double d[3];
for(int i=0;i<3;i++)d[i]=m->getDoubleAt(8*i);
wb->move(d[0],d[1]);
wb->simulate(d[2]);
wb->move(0,0);
bool res = wb->getMoveSuccess();
char resp[50];
int tam = ApoloMessage::writeBOOL(resp, res);
temp->Send(resp, tam);
valid++;
}
break;
case AP_UPDATEWORLD:
if(worldindex>=0){//updates one world
(*world)[worldindex]->getChild()->RefreshChild();
}else //updates all worlds
{
for(int i=0; i<world->size();i++)
(*world)[i]->getChild()->RefreshChild();
}
valid++;
break;
case AP_LINK_TO_ROBOT_TCP:
if(element){
robot=dynamic_cast<RobotSim*>(element);
char *objectname=m->getStringAt(0);
PositionableEntity *target=getElement(nworld,objectname,&worldindex);
if(target&&robot){
target->LinkTo(robot->getTcp());
}
valid++;
}
break;
case AP_GET_LASER_DATA:
if (element) {
laser = dynamic_cast<LaserSensorSim*>(element);
laser->simulate(0);
LaserData data;
laser->getData(data);
int num = (int)data.size();
char *resp = new char[sizeof(double)*num+20];
int tam = ApoloMessage::writeDoubleVector(resp, data.getRanges());
temp->Send(resp, tam);
delete [] resp;
valid++;
}
break;
case AP_GET_LASER_LM:
if (element) {
laser = dynamic_cast<LaserSensorSim*>(element);
vector<LaserSensorSim::LandMarkInfo> v;
laser->simulate(0);
laser->detectLandMarks(v);
int num = (int)v.size();
char *resp = new char[(2*sizeof(double)+2)*num + 20];
int tam = ApoloMessage::writeLandMarkInfoVector(resp, v);
temp->Send(resp, tam);
delete[] resp;
valid++;
}
break;
case AP_GET_WB_ODOMETRY:
if (element) {
wb = dynamic_cast<WheeledBaseSim *>(element);
Transformation2D odom = (wb->getWBodometry()).get();
double dresp[3] = { odom.x,odom.y,odom.theta.getValue() };
char resp[70];
int tam;
tam = ApoloMessage::writeDoubleVector(resp, 3, dresp);
temp->Send(resp, tam);
valid++;
}
break;
case AP_RESET_ODOMETRY:
if (element) {
wb = dynamic_cast<WheeledBaseSim *>(element);
bool res = false;
double d[3];
for (int i = 0; i<3; i++)d[i] = m->getDoubleAt(8 * i);
if (wb) {
(wb->getWBodometry()).reset(wb);
(wb->getWBodometry()).set(d[0], d[1], d[2]);
valid++;
}
}
break;
case AP_GET_USENSOR:
if (element) {
UltrasonicSensor *usensor = dynamic_cast<UltrasonicSensor *>(element);
if (!usensor)break;
usensor->simulate(0);
double measure = usensor->getDistance();
char resp[70];
int tam;
tam = ApoloMessage::writeDoubleVector(resp, 1, &measure);
temp->Send(resp, tam);
valid++;
}
break;
case AP_GET_DEP_USENSORS:
if (element) {
vector<UltrasonicSensor *> v;
if (getDependentUltrasonicSensors(element, v))
{
int n = v.size();
double *vaux = new double[n];
for (int i = 0; i < n; i++) {
v[i]->simulate(0);
vaux[i] = v[i]->getDistance();
}
char *resp=new char[8*n+10];
int tam;
tam = ApoloMessage::writeDoubleVector(resp, n, vaux);
temp->Send(resp, tam);
valid++;
delete[] vaux;
delete[] resp;
}
}
break;
}
delete m;//aseguro limpieza
}
char messageLog[150];
sprintf(messageLog,"Commands Executed: %d/%d",valid,total);
wxLogStatus(messageLog);
//temp->Send(request,50);
//prepare the buffer with the remaining data
for (int i = 0; i < size; i++)buffer[i] = pbuffer[i];
}
}
temp->close();
delete temp;
}
}