This repository was archived by the owner on Mar 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_server_robots.cpp
More file actions
440 lines (375 loc) · 15.9 KB
/
cpp_server_robots.cpp
File metadata and controls
440 lines (375 loc) · 15.9 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#include "apstring.h"
#include "kRobot.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h> /* definition of struct sockaddr_in */
#include <netdb.h> /* definition of gethostbyname */
#include <arpa/inet.h> /* definition of inet_ntoa */
#define SOCKET_PORT 6800
//set serial port for linux
int send_to_khepera(kRobot *r, char *command){
int len=strlen(command),i=2,acum=0,j,leftSpeed,rightSpeed,ret;
char tmp[15];
if ((command[1]==',')||(command[1]=='\0')){
acum=(int)(command[0]-48);
i=1;
}
else acum=(int)((command[0]-48)*10)+(command[1]-48);
switch(acum){
case 1: {
/**************************************************************************************
* bool reset();
* Purpose: Resets the wheel counters to zero. Sets speed and acceleration
* to default settings.
* Preconditions: Serial communications open.
* Postconditions: Counters set to 0; Speed and acceleration are se to default.
* Returns true if completed sucessfully, false otherwise.
**************************************************************************************/
if(r->reset()) ret=1;
else ret=0;
break;
}
case 2: {
/**************************************************************************************
* bool moveForward();
* Purpose: Makes robot move forward indefinitely at speeds in private fields
* Preconditions: Serial communications open.
* Postconditions: Robot moves forward indefinitely. Returns true if
* completed sucessfully, false otherwise.
**************************************************************************************/
i++;//Apunto al primer parámetro
for(j=0;command[i]!=',';j++,i++) tmp[j]=command[i];
tmp[j]='\0';
leftSpeed=atoi(tmp);
i++;//Apunto al segundo parámetro
for(j=0;command[i]!='\0';j++,i++) tmp[j]=command[i];
tmp[j]='\0';
rightSpeed=atoi(tmp);
r->setWheelSpeed(leftSpeed*2, rightSpeed*2);
if(r->moveForward()) ret=1;
else ret=0;
break;
}
case 3: {
/**************************************************************************************
* bool moveForwardDistance(int distance);
* Purpose: Makes robot move forward n millimeters.
* Range is 0 < distance <= 670000 mm.
* Preconditions: Serial communications open; distance in millimeters.
* Postconditions: Robot moves forward n millimeters. Returns true if
* completed sucessfully, false otherwise.
**************************************************************************************/
i++;//Apunto al parámetro
for(j=0;command[i]!='\0';j++,i++) tmp[j]=command[i];
tmp[j]='\0';
leftSpeed=atoi(tmp);//Uso leftSpeed para guardar la distancia
if(r->moveForwardDistance(leftSpeed)) ret=1;
else ret=0;
break;
}
case 4: {
/**************************************************************************************
* bool moveBackward();
* Purpose: Makes robot move backwards indefinitely at speeds in private fields
* Preconditions: Serial communications open.
* Postconditions: Robot moves backwards indefinitely. Returns true if
* completed sucessfully, false otherwise.
**************************************************************************************/
i++;//Apunto al primer parámetro
for(j=0;command[i]!=',';j++,i++) tmp[j]=command[i];
tmp[j]='\0';
leftSpeed=atoi(tmp);
i++;//Apunto al segundo parámetro
for(j=0;command[i]!='\0';j++,i++) tmp[j]=command[i];
tmp[j]='\0';
rightSpeed=atoi(tmp);
r->setWheelSpeed(leftSpeed, rightSpeed);
if(r->moveBackward()) ret=1;
else ret=0;
break;
}
case 5: {
/**************************************************************************************
* bool moveBackwardDistance(int distance);
* Purpose: Makes robot move backward n millimeters.
* Range is 0 < distance <= 670000 mm.
* Preconditions: Serial communications open; distance in millimeters.
* Postconditions: Robot moves backwards n millimeters. Returns true if
* completed sucessfully, false otherwise.
**************************************************************************************/
i++;//Apunto al parámetro
for(j=0;command[i]!='\0';j++,i++) tmp[j]=command[i];
tmp[j]='\0';
leftSpeed=atoi(tmp);//Uso leftSpeed para guardar la distancia
if(r->moveBackwardDistance(leftSpeed)) ret=1;
else ret=0;
break;
}
case 6: {
/**************************************************************************************
* bool stop();
* Purpose: Stops the movement of the robot.
* Preconditions: Serial communications open; robot in motion.
* Postconditions: Robot stops. Returns true if completed sucessfully, false otherwise.
**************************************************************************************/
if(r->stop()) ret=1;
else ret=0;
break;
}
case 7: {
/**************************************************************************************
* bool turnLeft(int degree);
* Purpose: Makes robot turn left degrees passed as parameter->
* Preconditions: 0<=degrees<=360; Serial communications open.
* Postconditions: Robot turns right n degrees. Returns true if
* completed sucessfully, false otherwise.
**************************************************************************************/
i++;//Apunto al parámetro
for(j=0;command[i]!='\0';j++,i++) tmp[j]=command[i];
tmp[j]='\0';
leftSpeed=atoi(tmp);//Uso leftSpeed para guardar los grados
if(r->turnLeft(leftSpeed)) ret=1;
else ret=0;
wait(0.2);
break;
}
case 8: {
/**************************************************************************************
* bool turnRight(int degree);
* Purpose: Makes robot turn right degrees passed as parameter->
* Preconditions: 0<=degrees<=360; Serial communications open.
* Postconditions: Robot turns right n degrees. Returns true if
* completed sucessfully, false otherwise.
**************************************************************************************/
i++;//Apunto al parámetro
for(j=0;command[i]!='\0';j++,i++) tmp[j]=command[i];
tmp[j]='\0';
leftSpeed=atoi(tmp);//Uso leftSpeed para guardar los grados
if(r->turnRight(leftSpeed)) ret=1;
else ret=0;
wait(0.2);
break;
}
case 9: {
/**************************************************************************************
* int getLeftWheelCounter();
* Purpose: Reads left wheel counter->
* Preconditions: Serial communications open.
* Postconditions: Returns counter (+ forward, - reverse)
**************************************************************************************/
ret=r->getLeftWheelCounter();
break;
}
case 10: {
/**************************************************************************************
* int getRightWheelCounter();
* Purpose: Reads right wheel counter->
* Preconditions: Serial communications open.
* Postconditions: Returns counter (+ forward, - reverse)
**************************************************************************************/
ret=r->getRightWheelCounter();
break;
}
case 11: {
/**************************************************************************************
* int getLeftWheelSpeed();
* Purpose: Reads the speed and direction (+/-) of left wheel/motor in mm/sec.
* Preconditions: Serial communications open.
* Postconditions: Returns speed and direction of left wheel
**************************************************************************************/
ret=r->getLeftWheelSpeed();
break;
}
case 12: {
/**************************************************************************************
* int getRightWheelSpeed();
* Purpose: Reads the speed and direction (+/-) of right wheel/motor in mm/sec.
* Preconditions: Serial communications open.
* Postconditions: Returns speed and direction of right wheel
**************************************************************************************/
ret=r->getRightWheelSpeed();
break;
}
case 13: {
/**************************************************************************************
* bool setWheelSpeed(int leftSpeed, int rightSpeed);
* Purpose: Sets the speed and direction (+/-) of wheel/motor in mm/sec.
* Range is 0 < speed <= 1000 mm/sec.
* Preconditions: Serial communications open.
* Postconditions: Private fields are updated. Returns true if
* completed sucessfully, false otherwise.
**************************************************************************************/
i++;//Apunto al primer parámetro
for(j=0;command[i]!=',';j++,i++) tmp[j]=command[i];
tmp[j]='\0';
leftSpeed=atoi(tmp);
i++;//Apunto al segundo parámetro
for(j=0;command[i]!='\0';j++,i++) tmp[j]=command[i];
tmp[j]='\0';
rightSpeed=atoi(tmp);
if(r->setWheelSpeed(leftSpeed, rightSpeed)) ret=1;
else ret=0;
break;
}
case 14: {
/**************************************************************************************
* int getLeftWheelAcceleration();
* Purpose: Reads the acceleration of left wheel/motor in mm/sec^2.
* Preconditions: Serial communications open.
* Postconditions: Returns acceleration of left wheel
**************************************************************************************/
ret=r->getLeftWheelAcceleration();
break;
}
case 15: {
/**************************************************************************************
* int getRightWheelAcceleration();
* Purpose: Reads the acceleration of right wheel/motor in mm/sec^2.
* Preconditions: Serial communications open.
* Postconditions: Returns acceleration of right wheel
**************************************************************************************/
ret=r->getRightWheelAcceleration();
break;
}
case 16: {
/**************************************************************************************
* bool setWheelAcceleration(int leftAcc, int rightAcc);
* Purpose: Sets the acceleration and direction (+/-) of wheel/motor in mm/sec^2.
* Range is 0 < acceleration <= 396875 mm/sec^2.
* Preconditions: Serial communications open.
* Postconditions: Private fields are updated. Returns true if
* completed sucessfully, false otherwise.
**************************************************************************************/
i++;//Apunto al primer parámetro
for(j=0;command[i]!=',';j++,i++) tmp[j]=command[i];
tmp[j]='\0';
leftSpeed=atoi(tmp);//Uso leftSpeed para guardar la aceleración izquierda
i++;//Apunto al segundo parámetro
for(j=0;command[i]!='\0';j++,i++) tmp[j]=command[i];
tmp[j]='\0';
rightSpeed=atoi(tmp);//Uso rightSpeed para guardar la aceleración derecha
if(r->setWheelAcceleration(leftSpeed, rightSpeed)) ret=1;
else ret=0;
break;
}
case 20: {
/**************************************************************************************
* bool readProxSensors();
* Purpose: Reads the values of each of eight proximity sensors.
* Preconditions: Serial communication open.
* Postconditions: Stores values of proximity sensors in an array, indexed
* 0..7 as labeled on Khepera diagram. Returns true if
* completed sucessfully, false otherwise.
**************************************************************************************/
if(r->readProxSensors()) ret=1;
else ret=0;
break;
}
case 22: {
/**************************************************************************************
* int getProxSensor(int s);
* Purpose: Displays the value of requested sensor as numbered
* on Khepera diagram.
* Preconditions: 0<=s<8; Sensors updated; serial communication open.
* Postconditions: Value returned, 0<=val<1024, larger magnitidue is closer->
**************************************************************************************/
i++;//Apunto al parámetro
for(j=0;command[i]!='\0';j++,i++) tmp[j]=command[i];
tmp[j]='\0';
leftSpeed=atoi(tmp);//Uso leftSpeed para guardar el número de sensor
ret = r->getProxSensor(leftSpeed);
break;
}
default: ret = 1;
}
return ret;
}//Fin de int send_to_khepera(kRobot r, char *command)
int main()
{
kRobot r("/dev/ttyUSB0",true);
/*Declaraciones de variables*/
int des_socket, conex_val, nuevo_socket, long_server, long_cliente, pid_servidor;
struct sockaddr_in dir_serv, dir_cli;
char buffer[300],num[20];
int i,len,answer;
/*Se crea el socket*/
des_socket = socket(AF_INET, SOCK_STREAM, 0);
/*Verifica si fue creado*/
if(des_socket < 0)
{
printf("SERVIDOR: NO SE PUEDE ABRIR SOCK_STREAM\n");
return 1;
}
/*En la estructura dir_serv inicializa con cero tantos bytes como indique el segundo argumento*/
bzero((char *) &dir_serv,sizeof(dir_serv));
dir_serv.sin_family = AF_INET;
dir_serv.sin_port = htons((unsigned short)SOCKET_PORT);
dir_serv.sin_addr.s_addr = htonl(INADDR_ANY);
/*Asignamos un nombre al nuevo socket*/
conex_val = bind(des_socket, (struct sockaddr *) &dir_serv, sizeof(dir_serv));
/*Con este verificamos que la llamada al sistema bind haya asignado al socket una direccion valida
de manera que el servidor tenga una direccion de retorno donde pueda enviarle las respuestas*/
if (conex_val < 0)
{
printf("SERVIDOR: NO SE PUDO REALIZAR EL ENLACE CON EL PUERTO, DIRECCION DE RETORNO INVALIDA \n");
return 1;
}
/*Con esta llamada al sistema el servidor indica que esta dispuesto a recibir conexion.
Especifica que a lo sumo la cola de conexiones pendientes puede crecer hasta diez.*/
listen(des_socket, 10);
while (true){
printf("SERVIDOR ESPERANDO CONEXION ...?? \n");
long_cliente = sizeof(dir_cli);
/*Con accept s#include "apstring.h"e toma el primer pedido de conexion de la cola y se crea un nuevo socket con las
las mismas propiedades que des_socket */
nuevo_socket = accept(des_socket, (struct sockaddr *) &dir_cli, (socklen_t *)&long_cliente);
if(nuevo_socket < 0)
{
printf("SERVIDOR: ERROR AL TRATAR DE RECUPERAR EL PEDIDO DE CONEXION DE LA COLA...\n");
return 2;
}
/*Inicializa buffer*/
int y;
// for(y=0;y<300;y++){
// buffer[y]='\0';
// }
buffer[0]='\0';
/*Recibe el mensaje del nuevo socket creado con la llamada al sistema accept*/
recv(nuevo_socket, buffer, 100, 0);
// printf("SERVER: recibo en buffer %s\n",buffer);
/*Manda el comando al khepera y recibe la respuesta*/
if(!strcmp(buffer,"24")){
answer=send_to_khepera(&r, "20");
buffer[0]='\0';
char parametro[5];
char stri[1];
for(i=0;i<8;i++){
sprintf(stri,"%d",i);
strcpy(parametro, "22,");
strcat(parametro, stri);
answer=send_to_khepera(&r, parametro);
num[0]='\0';
sprintf(num,"%d",answer);
strcat(buffer,num);
strcat(buffer,";");
}
}else{
answer=send_to_khepera(&r, buffer);
/*Inicializa buffer*/
buffer[0]='\0';
num[0]='\0';
sprintf(num,"%d",answer);
strcat(buffer,num);
}
//r.cerrar();
send(nuevo_socket, buffer, strlen(buffer)+1, 0);
close(nuevo_socket);
}
close(des_socket);
return 0;
}//main