-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserverpgm1.cpp
More file actions
393 lines (333 loc) · 11.4 KB
/
serverpgm1.cpp
File metadata and controls
393 lines (333 loc) · 11.4 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
/* Example code for formation controller of Truck-Trailer.
*
* Copyright (C) 2014 Jennifer David. All rights reserved.
*
* BSD license:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of
* contributors to this software may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR THE CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <iomanip>
#include <fstream>
#include <string>
#include "Aria.h"
#include "ArNetworking.h"
#define PI 3.14
using namespace std;
using std::ofstream;
int main(int argc, char *argv[])
{
Aria::init();
// set up our parser
ArArgumentParser parser(&argc, argv);
// load the default arguments
parser.loadDefaultArguments();
// robot
ArRobot robot;
// set up our simple connector
ArRobotConnector robotConnector(&parser, &robot);
// add a gyro, it'll see if it should attach to the robot or not
ArAnalogGyro gyro(&robot);
// set up the robot for connecting
if (!robotConnector.connectRobot())
{
printf("Could not connect to robot... exiting\n");
Aria::exit(1);
}
// our base server object
ArServerBase server;
ArServerSimpleOpener simpleOpener(&parser);
ArClientSwitchManager clientSwitchManager(&server, &parser);
// parse the command line... fail and print the help if the parsing fails
// or if the help was requested
if (!Aria::parseArgs() || !parser.checkHelpAndWarnUnparsed())
{
Aria::logOptions();
Aria::exit(1);
}
// Set up where we'll look for files such as user/password
char fileDir[1024];
ArUtil::addDirectories(fileDir, sizeof(fileDir), Aria::getDirectory(),
"ArNetworking/examples");
// first open the server up
if (!simpleOpener.open(&server, fileDir, 240))
{
if (simpleOpener.wasUserFileBad())
printf("Bad user/password/permissions file\n");
else
printf("Could not open server port\n");
exit(1);
}
// Range devices:
ArSonarDevice sonarDev;
robot.addRangeDevice(&sonarDev);
ArBumpers bumpers;
robot.addRangeDevice(&bumpers);
// attach services to the server
ArServerInfoRobot serverInfoRobot(&server, &robot);
ArServerInfoSensor serverInfoSensor(&server, &robot);
ArServerInfoDrawings drawings(&server);
// modes for controlling robot movement
ArServerModeStop modeStop(&server, &robot);
ArServerModeRatioDrive modeRatioDrive(&server, &robot);
ArServerModeWander modeWander(&server, &robot);
modeStop.addAsDefaultMode();
modeStop.activate();
// set up the simple commands
ArServerHandlerCommands commands(&server);
ArServerSimpleComUC uCCommands(&commands, &robot); // send commands directly to microcontroller
ArServerSimpleComMovementLogging loggingCommands(&commands, &robot); // control debug logging
ArServerSimpleComGyro gyroCommands(&commands, &robot, &gyro); // configure gyro
ArServerSimpleComLogRobotConfig configCommands(&commands, &robot); // control more debug logging
ArServerSimpleServerCommands serverCommands(&commands, &server); // control ArNetworking debug logging
ArServerSimpleLogRobotDebugPackets logRobotDebugPackets(&commands, &robot, "."); // debugging tool
// This is an older drive mode. ArServerModeDrive is newer and generally performs better,
// but you can use this for old clients if neccesary.
//ArServerModeDrive modeDrive(&server, &robot);
//modeDrive.addControlCommands(&commands); // configure the drive modes (e.g. enable/disable safe drive)
ArServerHandlerConfig serverHandlerConfig(&server, Aria::getConfig()); // make a config handler
ArLog::addToConfig(Aria::getConfig()); // let people configure logging
// You can use this class to send a set of arbitrary strings
// for MobileEyes to display, this is just a small example
ArServerInfoStrings stringInfo(&server);
Aria::getInfoGroup()->addAddStringCallback(stringInfo.getAddStringFunctor());
Aria::getInfoGroup()->addStringInt(
"Motor Packet Count", 10,
new ArConstRetFunctorC<int, ArRobot>(&robot,
&ArRobot::getMotorPacCount));
/*
Aria::getInfoGroup()->addStringInt(
"Laser Packet Count", 10,
new ArRetFunctorC<int, ArSick>(&sick,
&ArSick::getSickPacCount));
*/
// start the robot running, true means that if we lose connection the run thread stops
robot.enableMotors();
robot.runAsync(true);
drawings.addRobotsRangeDevices(&robot);
// log whatever we wanted to before the runAsync
simpleOpener.checkAndLog();
// now let it spin off in its own thread
server.runAsync();
ArUtil::sleep(5000);
ArLog::log(ArLog::Normal, "Server now running on port %d. Press Control-C to exit.", server.getTcpPort());
/********************************************************************************************************************************************************/
char M, T, t, P, p;
int X2,Y2,X1,Y1;
float theta1,theta2;
ArUtil::sleep(1000);
fflush(stdout);
printf("Enter the mode required: Truck-trailer -t/T or Parallel-Parking -p/P \n");
fflush(stdout);
fflush(stdin);
scanf ("%c",&M);
fflush(stdout);
fflush(stdin);
printf("Selected mode is %c \n",M);
fflush(stdout);
ofstream modefile("/home/jeni/Downloads/mode.txt");
modefile<<M<<endl;
if ((M=='t') || (M=='T'))
{
ofstream truckfile("/home/jeni/Downloads/reqdpose.txt");
X1=-1000;
Y1=0;
theta1=0;
truckfile<<X1<<"\n"<<Y1<<"\n"<<theta1<<endl;
}
else if ((M=='p') || (M=='P'))
{
ofstream truckfile("/home/jeni/Downloads/reqdpose.txt");
X1=0;
Y1=500;
theta1=0;
truckfile<<X1<<"\n"<<Y1<<"\n"<<theta1<<endl;
}
/***********************************************************************************************************************************************************/
//Waiting for the trailer to establish the virtual link
printf("Making the robot to run in sarc.......\n");
fflush(stdout);
/**********************************************************************************************************************************************/
int X,Y,Vel,Rotvel,VLL,VRL;
float Th;
char O,s,S,c,C;
/*********************************************************************************************************************************************************/
// float X=0, Y=0, Th=0,Vel=0,Rotvel=0, VLL=0,VRL=0;
cout << "\n" << "X"<< X <<"\t"<<"Y"<<Y<<"\t"<<"Th"<<Th;
robot.moveTo(ArPose(0,0,0));
X=robot.getX();
Y=robot.getY();
Th=robot.getTh();
cout << "\n" << "X"<< X <<"\t"<<"Y"<<Y<<"\t"<<"Th"<<Th;
ArUtil::sleep(1000);
/******************************************************************************************************************************************************/
/*********************************************************************************************************************************************************/
double transvelocity, rotvelocity;
double turnangle, Rot=0,transVel=0;
double dist=0,angle=0,obsangle,Vavoid,obsdist,AvoidObstacle=0;
double Xrange[8];
double Range[8]; double safewander =0, Th0=0,obsangleinit;
int count =0, J=0; double L=0, K; double t0=0, t1=0; double test=0;
obsdist = 1100;
turnangle = 20;
Vavoid = 150;
int i=0;
char timestamp[24];
ofstream myfile("/home/jeni/Downloads/robotpose.xls");
myfile<<"TIME"<<"\t"<<"\t"<<"Xpos"<<"\t"<<"YPos"<<"\t"<<"Th"<<"\t"<<"Vel"<<"\t"<<"Rot"<<"\t"<<"VLL"<<"\t"<<"VRL"<<"\t"<<endl;
ArUtil::sleep(3000);
while(robot.isRunning())
{ robot.lock();
//keyHandler.checkKeys();
time_t t = time(NULL);
/// get the values of the server robot.
strftime(timestamp, 24, "%Y-%m-%d %H:%M:%S", localtime(&t));
X = robot.getX();
Y= robot.getY();
Th=robot.getTh();
Vel = robot.getVel();
Rot= robot.getRotVel();
VLL = robot.getLeftVel();
VRL= robot.getRightVel();
dist = (robot.checkRangeDevicesCurrentPolar(-90,90, &angle)- robot.getRobotRadius());
if (abs((int)angle)>180)
{ angle = 0;
}
if (angle*-1 >360)
{angle =0;
}
if (angle<0)
obsangle = -1;
else if (angle>0)
obsangle = 1;
else
obsangle = 1;
if (J==0)
{
if (dist < obsdist)
{ t0 = t1;
t1 = dist;
AvoidObstacle=1; safewander = 0;
count = 0;
if (angle<0)
obsangle = -1;
else if (angle>0)
obsangle = 1;
else
obsangle = 1;
transVel = 160;
test = Vavoid * dist/obsdist;
Rotvel = 5*obsangle*-1;
}
else if (dist>obsdist)
{ t1=dist;
if ((count*500) > 6000) //500
{
AvoidObstacle =0; safewander = 1;
Th == Th; Th0 = Th;
if(L==0) // turn left
{
J=1;K=0;
obsangleinit = obsangle;
transVel = 160; // 100% of maximum
Rotvel = 5 ;//30 % of Maximum
count == count;
L=1;
}
else if(L==1)//turn right
{
J=1; K=1;
obsangleinit = obsangle;
transVel = 160;
Rotvel = -5 ;//-30
count == count;
}
}
else
{ J=0;
AvoidObstacle = 0; safewander =0;
transVel = 160;
Rotvel =0;
count = count + 1;
}
}
}
else if (J<=20)//16
{ safewander =1;
J=J+1;
if (K==0) // turn left
{
if (dist>obsdist)
{
transVel = 160;
Rotvel = 5; //30
count == count;
L=1;
}
else
{ transVel=160; //150
Rotvel=5*obsangle*-1;
J=0; L=1;
}
}
else // turn right
{ safewander = 1;
if (dist>obsdist)
{transVel = 160;//150
Rotvel = -5; //3.4
count == count;
L=0;K=1;
}
else
{ transVel=160;
Rotvel=5*obsangle*-1;
J=0;L=0;
}
}
}
else
{ J=0; safewander=0; t1=dist;
transVel = 160;
Rotvel = 0;
count =0;
}
transvelocity = transVel;
rotvelocity = Rotvel; // 100
robot.setVel(transvelocity);
robot.setRotVel(rotvelocity);
cout << "\n" << "X"<< X <<"\t"<<"Y"<<Y<<"\t"<<"Th"<<Th;
myfile<<timestamp<<X<<"\t"<<Y<<"\t"<<Th<<"\t"<<Vel<<"\t"<<Rotvel<<"\t"<<VLL<<"\t"<<VRL<<"\t"<<endl;
i++;
robot.unlock();
ArUtil::sleep(1000); // 500,200
}
myfile.close();
clientSwitchManager.runAsync();
robot.waitForRunExit();
Aria::exit(0);
}