00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <iostream>
00030 using namespace std;
00031
00032 #include "../sbpl/headers.h"
00033
00034
00035 void PrintUsage(char *argv[])
00036 {
00037 SBPL_PRINTF("USAGE: %s <cfg file> [motionprimitivesfile.mprim for xythetalattice planning]\n", argv[0]);
00038 }
00039
00040
00041 int plan2d(int argc, char *argv[])
00042 {
00043
00044 int bRet = 0;
00045 double allocated_time_secs = 100.0;
00046 MDPConfig MDPCfg;
00047 bool bsearchuntilfirstsolution = true;
00048
00049
00050 EnvironmentNAV2D environment_nav2D;
00051 if(!environment_nav2D.InitializeEnv(argv[1]))
00052 {
00053 SBPL_ERROR("ERROR: InitializeEnv failed\n");
00054 throw new SBPL_Exception();
00055 }
00056
00057
00058 if(!environment_nav2D.InitializeMDPCfg(&MDPCfg))
00059 {
00060 SBPL_ERROR("ERROR: InitializeMDPCfg failed\n");
00061 throw new SBPL_Exception();
00062 }
00063
00064
00065
00066 vector<int> solution_stateIDs_V;
00067 bool bforwardsearch = false;
00068 ARAPlanner planner(&environment_nav2D, bforwardsearch);
00069
00070
00071
00072
00073 planner.set_search_mode(bsearchuntilfirstsolution);
00074
00075
00076 if(planner.set_start(MDPCfg.startstateid) == 0)
00077 {
00078 SBPL_ERROR("ERROR: failed to set start state\n");
00079 throw new SBPL_Exception();
00080 }
00081
00082 if(planner.set_goal(MDPCfg.goalstateid) == 0)
00083 {
00084 SBPL_ERROR("ERROR: failed to set goal state\n");
00085 throw new SBPL_Exception();
00086 }
00087
00088 planner.set_initialsolution_eps(3.0);
00089
00090 SBPL_PRINTF("start planning...\n");
00091 bRet = planner.replan(allocated_time_secs, &solution_stateIDs_V);
00092 SBPL_PRINTF("done planning\n");
00093 std::cout << "size of solution=" << solution_stateIDs_V.size() << std::endl;
00094
00095 environment_nav2D.PrintTimeStat(stdout);
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132 #ifndef ROS
00133 const char* sol = "sol.txt";
00134 #endif
00135 FILE* fSol = SBPL_FOPEN(sol, "w");
00136 if(fSol == NULL){
00137 SBPL_ERROR("ERROR: could not open solution file\n");
00138 throw new SBPL_Exception();
00139 }
00140 for(unsigned int i = 0; i < solution_stateIDs_V.size(); i++) {
00141 environment_nav2D.PrintState(solution_stateIDs_V[i], false, fSol);
00142 }
00143 SBPL_FCLOSE(fSol);
00144
00145 environment_nav2D.PrintTimeStat(stdout);
00146
00147
00148 if(bRet)
00149 {
00150
00151 SBPL_PRINTF("Solution is found\n");
00152 }
00153 else
00154 SBPL_PRINTF("Solution does not exist\n");
00155
00156 SBPL_FFLUSH(NULL);
00157
00158
00159 return bRet;
00160 }
00161
00162 int plan2duu(int argc, char *argv[])
00163 {
00164
00165 int bRet = 0;
00166 double allocated_time_secs = 100.0;
00167 MDPConfig MDPCfg;
00168
00169
00170 EnvironmentNAV2DUU environment_nav2Duu;
00171 if(!environment_nav2Duu.InitializeEnv(argv[1]))
00172 {
00173 SBPL_ERROR("ERROR: InitializeEnv failed\n");
00174 throw new SBPL_Exception();
00175 }
00176
00177
00178 if(!environment_nav2Duu.InitializeMDPCfg(&MDPCfg))
00179 {
00180 SBPL_ERROR("ERROR: InitializeMDPCfg failed\n");
00181 throw new SBPL_Exception();
00182 }
00183
00184
00185
00186 PPCPPlanner planner(&environment_nav2Duu, environment_nav2Duu.SizeofCreatedEnv(), environment_nav2Duu.SizeofH());
00187
00188
00189 if(planner.set_start(MDPCfg.startstateid) == 0)
00190 {
00191 SBPL_ERROR("ERROR: failed to set start state\n");
00192 throw new SBPL_Exception();
00193 }
00194 if(planner.set_goal(MDPCfg.goalstateid) == 0)
00195 {
00196 SBPL_ERROR("ERROR: failed to set goal state\n");
00197 throw new SBPL_Exception();
00198 }
00199
00200
00201 SBPL_PRINTF("start planning...\n");
00202 float ExpectedCost, ProbofReachGoal;
00203 vector<sbpl_PolicyStatewithBinaryh_t> SolutionPolicy;
00204 bRet = planner.replan(allocated_time_secs, &SolutionPolicy, &ExpectedCost, &ProbofReachGoal);
00205 SBPL_PRINTF("done planning\n");
00206
00207
00208 if(bRet)
00209 {
00210
00211 SBPL_PRINTF("Solution is found: exp. cost=%f probofreachgoal=%f\n", ExpectedCost, ProbofReachGoal);
00212 }
00213 else
00214 SBPL_PRINTF("Solution does not exist\n");
00215
00216 SBPL_FFLUSH(NULL);
00217
00218
00219 return bRet;
00220 }
00221
00222
00223 int planxythetalat(int argc, char *argv[])
00224 {
00225
00226 int bRet = 0;
00227 double allocated_time_secs = 10;
00228 MDPConfig MDPCfg;
00229 bool bsearchuntilfirstsolution = false;
00230
00231
00232 vector<sbpl_2Dpt_t> perimeterptsV;
00233 sbpl_2Dpt_t pt_m;
00234 double halfwidth = 0.01;
00235 double halflength = 0.01;
00236 pt_m.x = -halflength;
00237 pt_m.y = -halfwidth;
00238 perimeterptsV.push_back(pt_m);
00239 pt_m.x = halflength;
00240 pt_m.y = -halfwidth;
00241 perimeterptsV.push_back(pt_m);
00242 pt_m.x = halflength;
00243 pt_m.y = halfwidth;
00244 perimeterptsV.push_back(pt_m);
00245 pt_m.x = -halflength;
00246 pt_m.y = halfwidth;
00247 perimeterptsV.push_back(pt_m);
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257 EnvironmentNAVXYTHETALAT environment_navxythetalat;
00258
00259 if(argc == 3)
00260 {
00261 if(!environment_navxythetalat.InitializeEnv(argv[1], perimeterptsV, argv[2]))
00262 {
00263 SBPL_ERROR("ERROR: InitializeEnv failed\n");
00264 throw new SBPL_Exception();
00265 }
00266 }
00267 else
00268 {
00269 if(!environment_navxythetalat.InitializeEnv(argv[1], perimeterptsV, NULL))
00270 {
00271 SBPL_ERROR("ERROR: InitializeEnv failed\n");
00272 throw new SBPL_Exception();
00273 }
00274 }
00275
00276
00277 if(!environment_navxythetalat.InitializeMDPCfg(&MDPCfg))
00278 {
00279 SBPL_ERROR("ERROR: InitializeMDPCfg failed\n");
00280 throw new SBPL_Exception();
00281 }
00282
00283
00284
00285 vector<int> solution_stateIDs_V;
00286 bool bforwardsearch = false;
00287
00288 ARAPlanner planner(&environment_navxythetalat, bforwardsearch);
00289
00290
00291 if(planner.set_start(MDPCfg.startstateid) == 0)
00292 {
00293 SBPL_ERROR("ERROR: failed to set start state\n");
00294 throw new SBPL_Exception();
00295 }
00296
00297 if(planner.set_goal(MDPCfg.goalstateid) == 0)
00298 {
00299 SBPL_ERROR("ERROR: failed to set goal state\n");
00300 throw new SBPL_Exception();
00301 }
00302 planner.set_initialsolution_eps(3.0);
00303
00304
00305 planner.set_search_mode(bsearchuntilfirstsolution);
00306
00307 SBPL_PRINTF("start planning...\n");
00308 bRet = planner.replan(allocated_time_secs, &solution_stateIDs_V);
00309 SBPL_PRINTF("done planning\n");
00310 SBPL_PRINTF("size of solution=%d\n",(unsigned int)solution_stateIDs_V.size());
00311
00312 environment_navxythetalat.PrintTimeStat(stdout);
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357 #ifndef ROS
00358 const char* sol = "sol.txt";
00359 #endif
00360 FILE* fSol = SBPL_FOPEN(sol, "w");
00361 if(fSol == NULL){
00362 SBPL_ERROR("ERROR: could not open solution file\n");
00363 throw new SBPL_Exception();
00364 }
00365 vector<EnvNAVXYTHETALAT3Dpt_t> xythetaPath;
00366 environment_navxythetalat.ConvertStateIDPathintoXYThetaPath(&solution_stateIDs_V, &xythetaPath);
00367 SBPL_PRINTF("solution size=%d\n", (unsigned int)xythetaPath.size());
00368 for(unsigned int i = 0; i < xythetaPath.size(); i++) {
00369 SBPL_FPRINTF(fSol, "%.3f %.3f %.3f\n", xythetaPath.at(i).x, xythetaPath.at(i).y, xythetaPath.at(i).theta);
00370 }
00371 SBPL_FCLOSE(fSol);
00372
00373 environment_navxythetalat.PrintTimeStat(stdout);
00374
00375
00376 if(bRet)
00377 {
00378
00379 SBPL_PRINTF("Solution is found\n");
00380 }
00381 else
00382 SBPL_PRINTF("Solution does not exist\n");
00383
00384 SBPL_FFLUSH(NULL);
00385
00386
00387 return bRet;
00388 }
00389
00390
00391
00392
00393
00394
00395 int planxythetamlevlat(int argc, char *argv[])
00396 {
00397
00398 int bRet = 0;
00399 double allocated_time_secs = 10;
00400 MDPConfig MDPCfg;
00401 bool bsearchuntilfirstsolution = false;
00402
00403
00404
00405 vector<sbpl_2Dpt_t> perimeterptsV;
00406 sbpl_2Dpt_t pt_m;
00407 double halfwidth = 0.3;
00408 double halflength = 0.25;
00409 pt_m.x = -halflength;
00410 pt_m.y = -halfwidth;
00411 perimeterptsV.push_back(pt_m);
00412 pt_m.x = halflength;
00413 pt_m.y = -halfwidth;
00414 perimeterptsV.push_back(pt_m);
00415 pt_m.x = halflength;
00416 pt_m.y = halfwidth;
00417 perimeterptsV.push_back(pt_m);
00418 pt_m.x = -halflength;
00419 pt_m.y = halfwidth;
00420 perimeterptsV.push_back(pt_m);
00421
00422
00423
00424
00425
00426
00427
00428
00429 EnvironmentNAVXYTHETAMLEVLAT environment_navxythetalat;
00430
00431 if(argc == 3)
00432 {
00433 if(!environment_navxythetalat.InitializeEnv(argv[1], perimeterptsV, argv[2]))
00434 {
00435 SBPL_ERROR("ERROR: InitializeEnv failed\n");
00436 throw new SBPL_Exception();
00437 }
00438 }
00439 else
00440 {
00441 if(!environment_navxythetalat.InitializeEnv(argv[1], perimeterptsV, NULL))
00442 {
00443 SBPL_ERROR("ERROR: InitializeEnv failed\n");
00444 throw new SBPL_Exception();
00445 }
00446 }
00447
00448
00449
00450 vector<sbpl_2Dpt_t> perimeterptsVV[2];
00451 perimeterptsVV[0].clear();
00452 halfwidth = 0.3;
00453 halflength = 0.75;
00454 pt_m.x = -halflength;
00455 pt_m.y = -halfwidth;
00456 perimeterptsVV[0].push_back(pt_m);
00457 pt_m.x = halflength;
00458 pt_m.y = -halfwidth;
00459 perimeterptsVV[0].push_back(pt_m);
00460 pt_m.x = halflength;
00461 pt_m.y = halfwidth;
00462 perimeterptsVV[0].push_back(pt_m);
00463 pt_m.x = -halflength;
00464 pt_m.y = halfwidth;
00465 perimeterptsVV[0].push_back(pt_m);
00466
00467
00468
00469 int numofaddlevels = 1;
00470 SBPL_PRINTF("Number of additional levels = %d\n", numofaddlevels);
00471 unsigned char cost_inscribed_thresh_addlevels[2];
00472 unsigned char cost_possibly_circumscribed_thresh_addlevels[2];
00473 cost_inscribed_thresh_addlevels[0] = 255;
00474 cost_possibly_circumscribed_thresh_addlevels[0] = 0;
00475 cost_inscribed_thresh_addlevels[1] = 255;
00476 cost_possibly_circumscribed_thresh_addlevels[1] = 0;
00477 if(!environment_navxythetalat.InitializeAdditionalLevels(numofaddlevels, perimeterptsVV,
00478 cost_inscribed_thresh_addlevels, cost_possibly_circumscribed_thresh_addlevels))
00479 {
00480 SBPL_ERROR("ERROR: InitializeAdditionalLevels failed with numofaddlevels=%d\n", numofaddlevels);
00481 throw new SBPL_Exception();
00482
00483 }
00484
00485
00486
00487
00488
00489
00490
00491
00492 int addlevind = 0;
00493 if(!environment_navxythetalat.Set2DMapforAddLev(
00494 (const unsigned char**)(environment_navxythetalat.GetEnvNavConfig()->Grid2D),addlevind))
00495 {
00496 SBPL_ERROR("ERROR: Setting Map for the Additional Level failed with level %d\n", addlevind);
00497 throw new SBPL_Exception();
00498
00499 }
00500
00501
00502 if(!environment_navxythetalat.InitializeMDPCfg(&MDPCfg))
00503 {
00504 SBPL_ERROR("ERROR: InitializeMDPCfg failed\n");
00505 throw new SBPL_Exception();
00506 }
00507
00508
00509
00510 vector<int> solution_stateIDs_V;
00511 bool bforwardsearch = false;
00512 ADPlanner planner(&environment_navxythetalat, bforwardsearch);
00513
00514 if(planner.set_start(MDPCfg.startstateid) == 0)
00515 {
00516 SBPL_ERROR("ERROR: failed to set start state\n");
00517 throw new SBPL_Exception();
00518 }
00519
00520 if(planner.set_goal(MDPCfg.goalstateid) == 0)
00521 {
00522 SBPL_ERROR("ERROR: failed to set goal state\n");
00523 throw new SBPL_Exception();
00524 }
00525 planner.set_initialsolution_eps(3.0);
00526
00527
00528 planner.set_search_mode(bsearchuntilfirstsolution);
00529
00530 SBPL_PRINTF("start planning...\n");
00531 bRet = planner.replan(allocated_time_secs, &solution_stateIDs_V);
00532 SBPL_PRINTF("done planning\n");
00533 std::cout << "size of solution=" << solution_stateIDs_V.size() << std::endl;
00534
00535 environment_navxythetalat.PrintTimeStat(stdout);
00536
00537 #ifndef ROS
00538 const char* sol = "sol.txt";
00539 #endif
00540 FILE* fSol = SBPL_FOPEN(sol, "w");
00541 if(fSol == NULL){
00542 SBPL_ERROR("ERROR: could not open solution file\n");
00543 throw new SBPL_Exception();
00544 }
00545 vector<EnvNAVXYTHETALAT3Dpt_t> xythetaPath;
00546 environment_navxythetalat.ConvertStateIDPathintoXYThetaPath(&solution_stateIDs_V, &xythetaPath);
00547 SBPL_PRINTF("solution size=%d\n", (unsigned int)xythetaPath.size());
00548 for(unsigned int i = 0; i < xythetaPath.size(); i++) {
00549 SBPL_FPRINTF(fSol, "%.3f %.3f %.3f\n", xythetaPath.at(i).x, xythetaPath.at(i).y, xythetaPath.at(i).theta);
00550 }
00551 SBPL_FCLOSE(fSol);
00552
00553 environment_navxythetalat.PrintTimeStat(stdout);
00554
00555
00556 if(bRet)
00557 {
00558
00559 SBPL_PRINTF("Solution is found\n");
00560 }
00561 else
00562 SBPL_PRINTF("Solution does not exist\n");
00563
00564 SBPL_FFLUSH(NULL);
00565
00566
00567 return bRet;
00568 }
00569
00570
00571
00572
00573 int planandnavigate2d(int argc, char *argv[])
00574 {
00575 double allocated_time_secs_foreachplan = 0.2;
00576 MDPConfig MDPCfg;
00577 EnvironmentNAV2D environment_nav2D;
00578 EnvironmentNAV2D trueenvironment_nav2D;
00579 int size_x=-1,size_y=-1;
00580 int startx = 0, starty = 0;
00581 int goalx=-1, goaly=-1;
00582 #ifndef ROS
00583 const char* sol = "sol.txt";
00584 #endif
00585 FILE* fSol = SBPL_FOPEN(sol, "w");
00586 if(fSol == NULL){
00587 SBPL_ERROR("ERROR: could not open solution file\n");
00588 throw new SBPL_Exception();
00589 }
00590
00591
00592 bool bPrint = true;
00593 int x,y;
00594 vector<int> preds_of_changededgesIDV;
00595 vector<nav2dcell_t> changedcellsV;
00596 nav2dcell_t nav2dcell;
00597 unsigned char obsthresh = 0;
00598 srand(0);
00599 int plantime_over1secs=0, plantime_over0p5secs=0, plantime_over0p1secs=0, plantime_over0p05secs=0, plantime_below0p05secs=0;
00600
00601
00602 if(!trueenvironment_nav2D.SetEnvParameter("is16connected", 1))
00603 {
00604 SBPL_ERROR("ERROR: failed to set parameters\n");
00605 throw new SBPL_Exception();
00606 }
00607 if(!environment_nav2D.SetEnvParameter("is16connected", 1))
00608 {
00609 SBPL_ERROR("ERROR: failed to set parameters\n");
00610 throw new SBPL_Exception();
00611 }
00612
00613
00614
00615 if(!trueenvironment_nav2D.InitializeEnv(argv[1]))
00616 {
00617 SBPL_ERROR("ERROR: InitializeEnv failed\n");
00618 throw new SBPL_Exception();
00619 }
00620 trueenvironment_nav2D.GetEnvParms(&size_x, &size_y, &startx, &starty, &goalx, &goaly, &obsthresh);
00621 unsigned char* map = (unsigned char*)calloc(size_x*size_y, sizeof(unsigned char));
00622
00623
00624 if(bPrint) SBPL_PRINTF("true map:\n");
00625 for(y = 0; bPrint && y < size_y; y++){
00626 for(x = 0; x < size_x; x++){
00627 SBPL_PRINTF("%d ", (int)trueenvironment_nav2D.IsObstacle(x,y));
00628 }
00629 SBPL_PRINTF("\n");
00630 }
00631 if(bPrint) SBPL_PRINTF("System Pause (return=%d)\n",system("pause"));
00632
00633
00634 if(!environment_nav2D.InitializeEnv(size_x, size_y, map, startx, starty, goalx, goaly, obsthresh)){
00635 SBPL_ERROR("ERROR: InitializeEnv failed\n");
00636 throw new SBPL_Exception();
00637 }
00638
00639
00640 if(!environment_nav2D.InitializeMDPCfg(&MDPCfg))
00641 {
00642 SBPL_ERROR("ERROR: InitializeMDPCfg failed\n");
00643 throw new SBPL_Exception();
00644 }
00645
00646
00647
00648 vector<int> solution_stateIDs_V;
00649 bool bforwardsearch = false;
00650 ARAPlanner planner(&environment_nav2D, bforwardsearch);
00651
00652
00653 planner.set_initialsolution_eps(2.0);
00654
00655
00656
00657 if(planner.set_start(MDPCfg.startstateid) == 0)
00658 {
00659 SBPL_ERROR("ERROR: failed to set start state\n");
00660 throw new SBPL_Exception();
00661 }
00662 if(planner.set_goal(MDPCfg.goalstateid) == 0)
00663 {
00664 SBPL_ERROR("ERROR: failed to set goal state\n");
00665 throw new SBPL_Exception();
00666 }
00667
00668
00669 planner.set_search_mode(false);
00670
00671
00672
00673 int goalthresh = 0;
00674 while(abs(startx - goalx) > goalthresh || abs(starty - goaly) > goalthresh){
00675
00676
00677 bool bChanges = false;
00678 preds_of_changededgesIDV.clear();
00679 changedcellsV.clear();
00680
00681 int dX = 0;
00682 int dY = 0;
00683 for(dX = -2; dX <= 2 ; dX++){
00684 for(dY = -2; dY <= 2 ; dY++){
00685 int x = startx + dX;
00686 int y = starty + dY;
00687 if(x < 0 || x >= size_x || y < 0 || y >= size_y)
00688 continue;
00689 int index = x + y*size_x;
00690 unsigned char truecost = trueenvironment_nav2D.GetMapCost(x,y);
00691 if(map[index] != truecost){
00692 map[index] = truecost;
00693 environment_nav2D.UpdateCost(x,y,map[index]);
00694 SBPL_PRINTF("setting cost[%d][%d] to %d\n", x,y,map[index]);
00695 bChanges = true;
00696
00697 nav2dcell.x = x;
00698 nav2dcell.y = y;
00699 changedcellsV.push_back(nav2dcell);
00700 }
00701 }
00702 }
00703
00704 double TimeStarted = clock();
00705
00706 if(bChanges){
00707 planner.costs_changed();
00708
00709
00710
00711
00712
00713
00714
00715
00716 }
00717
00718
00719
00720 SBPL_FPRINTF(fSol, "%d %d ", startx, starty);
00721
00722
00723 bool bPlanExists = false;
00724 while(bPlanExists == false){
00725 SBPL_PRINTF("new planning...\n");
00726 bPlanExists = (planner.replan(allocated_time_secs_foreachplan, &solution_stateIDs_V) == 1);
00727 SBPL_PRINTF("done with the solution of size=%d\n", (unsigned int)solution_stateIDs_V.size());
00728 environment_nav2D.PrintTimeStat(stdout);
00729 if(bPlanExists == false) throw new SBPL_Exception();
00730
00731
00732
00733
00734
00735 }
00736
00737 double plantime_secs = (clock()-TimeStarted)/((double)CLOCKS_PER_SEC);
00738 SBPL_FPRINTF(fSol, "%.5f %.5f\n", plantime_secs, planner.get_solution_eps());
00739 SBPL_FFLUSH(fSol);
00740 if(plantime_secs > 1.0)
00741 plantime_over1secs++;
00742 else if(plantime_secs > 0.5)
00743 plantime_over0p5secs++;
00744 else if(plantime_secs > 0.1)
00745 plantime_over0p1secs++;
00746 else if(plantime_secs > 0.05)
00747 plantime_over0p05secs++;
00748 else
00749 plantime_below0p05secs++;
00750
00751
00752 int startindex = startx + starty*size_x;
00753 int goalindex = goalx + goaly*size_x;
00754 for(y = 0; bPrint && y < size_y; y++){
00755 for(x = 0; x < size_x; x++){
00756 int index = x + y*size_x;
00757
00758
00759 bool bOnthePath = false;
00760 for(int j = 1; j < (int)solution_stateIDs_V.size(); j++)
00761 {
00762 int newx, newy;
00763 environment_nav2D.GetCoordFromState(solution_stateIDs_V[j], newx, newy);
00764 if(x == newx && y == newy)
00765 bOnthePath = true;
00766 }
00767
00768 if (index != startindex && index != goalindex && !bOnthePath)
00769 SBPL_PRINTF("%3d ", map[index]);
00770 else if(index == startindex)
00771 SBPL_PRINTF(" R ");
00772 else if(index == goalindex)
00773 SBPL_PRINTF(" G ");
00774 else if (bOnthePath)
00775 SBPL_PRINTF(" * ");
00776 else
00777 SBPL_PRINTF(" ? ");
00778 }
00779 SBPL_PRINTF("\n");
00780 }
00781 if(bPrint) SBPL_PRINTF("System Pause (return=%d)\n",system("pause"));
00782
00783
00784
00785 if(bPlanExists && (int)solution_stateIDs_V.size() > 1){
00786
00787 int newx, newy;
00788 environment_nav2D.GetCoordFromState(solution_stateIDs_V[1], newx, newy);
00789
00790 if(trueenvironment_nav2D.GetMapCost(newx,newy) >= obsthresh)
00791 {
00792 SBPL_ERROR("ERROR: robot is commanded to move into an obstacle\n");
00793 throw new SBPL_Exception();
00794 }
00795
00796
00797 SBPL_PRINTF("moving from %d %d to %d %d\n", startx, starty, newx, newy);
00798 startx = newx;
00799 starty = newy;
00800
00801
00802 environment_nav2D.SetStart(startx, starty);
00803
00804
00805 if(planner.set_start(solution_stateIDs_V[1]) == 0){
00806 SBPL_ERROR("ERROR: failed to update robot pose in the planner\n");
00807 throw new SBPL_Exception();
00808 }
00809 }
00810
00811 }
00812
00813
00814 SBPL_PRINTF("stats: plantimes over 1 secs=%d; over 0.5 secs=%d; over 0.1 secs=%d; over 0.05 secs=%d; below 0.05 secs=%d\n",
00815 plantime_over1secs, plantime_over0p5secs, plantime_over0p1secs, plantime_over0p05secs, plantime_below0p05secs);
00816 SBPL_FPRINTF(fSol, "stats: plantimes over 1 secs=%d; over 0.5; secs=%d; over 0.1 secs=%d; over 0.05 secs=%d; below 0.05 secs=%d\n",
00817 plantime_over1secs, plantime_over0p5secs, plantime_over0p1secs, plantime_over0p05secs, plantime_below0p05secs);
00818
00819 if(bPrint) SBPL_PRINTF("System Pause (return=%d)\n",system("pause"));
00820
00821 SBPL_FFLUSH(NULL);
00822 SBPL_FCLOSE(fSol);
00823
00824
00825 return 1;
00826 }
00827
00828
00829
00830
00831 int planandnavigatexythetalat(int argc, char *argv[])
00832 {
00833
00834 double allocated_time_secs_foreachplan = 1.0;
00835 MDPConfig MDPCfg;
00836 EnvironmentNAVXYTHETALAT environment_navxythetalat;
00837 EnvironmentNAVXYTHETALAT trueenvironment_navxythetalat;
00838 int size_x=-1,size_y=-1;
00839 double startx = 0, starty = 0, starttheta = 0;
00840 double goalx=-1, goaly=-1, goaltheta = -1;
00841 double goaltol_x = 0.001, goaltol_y = 0.001, goaltol_theta = 0.001;
00842 #ifndef ROS
00843 const char* sol = "sol.txt";
00844 #endif
00845 FILE* fSol = SBPL_FOPEN(sol, "w");
00846 if(fSol == NULL){
00847 SBPL_ERROR("ERROR: could not open solution file\n");
00848 throw new SBPL_Exception();
00849 }
00850
00851
00852 bool bPrint = false, bPrintMap = false;
00853 int x,y;
00854 vector<int> preds_of_changededgesIDV;
00855 vector<nav2dcell_t> changedcellsV;
00856 nav2dcell_t nav2dcell;
00857 int i;
00858 double cellsize_m, nominalvel_mpersecs, timetoturn45degsinplace_secs;
00859 bool bsearchuntilfirstsolution = false;
00860 unsigned char obsthresh = 0;
00861 unsigned char costinscribed_thresh = 0;
00862 unsigned char costcircum_thresh = 0;
00863 char sMotPrimFilename[1024];
00864 char* pMotPrimFilename = NULL;
00865
00866
00867 if(argc > 2)
00868 {
00869 strcpy(sMotPrimFilename, argv[2]);
00870 pMotPrimFilename = sMotPrimFilename;
00871 SBPL_PRINTF("using motion primitive file %s\n", pMotPrimFilename);
00872 }
00873 else
00874 {
00875 pMotPrimFilename = NULL;
00876 SBPL_PRINTF("no motion primitives file provided\n");
00877 }
00878
00879
00880
00881
00882 if(!trueenvironment_navxythetalat.InitializeEnv(argv[1]))
00883 {
00884 SBPL_ERROR("ERROR: InitializeEnv failed\n");
00885 throw new SBPL_Exception();
00886 }
00887
00888
00889 vector<SBPL_xytheta_mprimitive> motionprimitiveV;
00890 trueenvironment_navxythetalat.GetEnvParms(&size_x, &size_y, &startx, &starty, &starttheta, &goalx, &goaly, &goaltheta, &cellsize_m,
00891 &nominalvel_mpersecs, &timetoturn45degsinplace_secs, &obsthresh, &motionprimitiveV);
00892 costinscribed_thresh = trueenvironment_navxythetalat.GetEnvParameter("cost_inscribed_thresh");
00893 costcircum_thresh = trueenvironment_navxythetalat.GetEnvParameter("cost_possibly_circumscribed_thresh");
00894
00895 unsigned char* map = (unsigned char*)calloc(size_x*size_y, sizeof(unsigned char));
00896
00897
00898 if(bPrintMap) SBPL_PRINTF("true map:\n");
00899 for(y = 0; bPrintMap && y < size_y; y++){
00900 for(x = 0; x < size_x; x++){
00901 SBPL_PRINTF("%3d ", trueenvironment_navxythetalat.GetMapCost(x,y));
00902 }
00903 SBPL_PRINTF("\n");
00904 }
00905 if(bPrint) SBPL_PRINTF("System Pause (return=%d)\n",system("pause"));
00906
00907 SBPL_PRINTF("start: %f %f %f, goal: %f %f %f\n", startx, starty, starttheta, goalx, goaly, goaltheta);
00908
00909
00910 vector<sbpl_2Dpt_t> perimeterptsV;
00911 sbpl_2Dpt_t pt_m;
00912 double halfwidth = 0.025;
00913 double halflength = 0.025;
00914 pt_m.x = -halflength;
00915 pt_m.y = -halfwidth;
00916 perimeterptsV.push_back(pt_m);
00917 pt_m.x = halflength;
00918 pt_m.y = -halfwidth;
00919 perimeterptsV.push_back(pt_m);
00920 pt_m.x = halflength;
00921 pt_m.y = halfwidth;
00922 perimeterptsV.push_back(pt_m);
00923 pt_m.x = -halflength;
00924 pt_m.y = halfwidth;
00925 perimeterptsV.push_back(pt_m);
00926
00927
00928 double maxx = 0;
00929 double maxy = 0;
00930 for(i = 0; i < (int)perimeterptsV.size(); i++)
00931 {
00932 if(maxx < fabs(perimeterptsV.at(i).x))
00933 maxx = fabs(perimeterptsV.at(i).x);
00934 if(maxy < fabs(perimeterptsV.at(i).y))
00935 maxy = fabs(perimeterptsV.at(i).y);
00936 }
00937
00938
00939 int sensingrange_c = (int)(__max(maxx, maxy)/cellsize_m) + 2;
00940 SBPL_PRINTF("sensing range=%d cells\n", sensingrange_c);
00941 vector<sbpl_2Dcell_t> sensecells;
00942 for(i = -sensingrange_c; i <= sensingrange_c; i++)
00943 {
00944 sbpl_2Dcell_t sensecell;
00945
00946 sensecell.x = i;
00947 sensecell.y = sensingrange_c;
00948 sensecells.push_back(sensecell);
00949 sensecell.x = i;
00950 sensecell.y = -sensingrange_c;
00951 sensecells.push_back(sensecell);
00952 sensecell.x = sensingrange_c;
00953 sensecell.y = i;
00954 sensecells.push_back(sensecell);
00955 sensecell.x = -sensingrange_c;
00956 sensecell.y = i;
00957 sensecells.push_back(sensecell);
00958 }
00959
00960
00961
00962
00963 if(!environment_navxythetalat.SetEnvParameter("cost_inscribed_thresh", costinscribed_thresh))
00964 {
00965 SBPL_ERROR("ERROR: failed to set parameters\n");
00966 throw new SBPL_Exception();
00967 }
00968 if(!environment_navxythetalat.SetEnvParameter("cost_possibly_circumscribed_thresh", costcircum_thresh))
00969 {
00970 SBPL_ERROR("ERROR: failed to set parameters\n");
00971 throw new SBPL_Exception();
00972 }
00973
00974
00975 if(!environment_navxythetalat.InitializeEnv(size_x, size_y, 0, 0,0,0,0,0,0,
00976 goaltol_x, goaltol_y, goaltol_theta, perimeterptsV,
00977 cellsize_m, nominalvel_mpersecs, timetoturn45degsinplace_secs,
00978 obsthresh, pMotPrimFilename)){
00979 SBPL_ERROR("ERROR: InitializeEnv failed\n");
00980 throw new SBPL_Exception();
00981 }
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993 environment_navxythetalat.SetStart(startx, starty,starttheta);
00994
00995
00996 if(!environment_navxythetalat.InitializeMDPCfg(&MDPCfg))
00997 {
00998 SBPL_ERROR("ERROR: InitializeMDPCfg failed\n");
00999 throw new SBPL_Exception();
01000 }
01001
01002
01003
01004 vector<int> solution_stateIDs_V;
01005 bool bforwardsearch = false;
01006 ARAPlanner planner(&environment_navxythetalat, bforwardsearch);
01007
01008
01009 planner.set_initialsolution_eps(5.0);
01010
01011
01012 planner.set_search_mode(bsearchuntilfirstsolution);
01013
01014
01015 if(planner.set_start(MDPCfg.startstateid) == 0)
01016 {
01017 SBPL_ERROR("ERROR: failed to set start state\n");
01018 throw new SBPL_Exception();
01019 }
01020
01021
01022 MDPCfg.goalstateid = environment_navxythetalat.SetGoal(goalx, goaly, goaltheta);
01023
01024
01025 if(planner.set_goal(MDPCfg.goalstateid) == 0)
01026 {
01027 SBPL_ERROR("ERROR: failed to set goal state\n");
01028 throw new SBPL_Exception();
01029 }
01030
01031
01032 int goalx_c = CONTXY2DISC(goalx, cellsize_m);
01033 int goaly_c = CONTXY2DISC(goaly, cellsize_m);
01034 int goaltheta_c = ContTheta2Disc(goaltheta, NAVXYTHETALAT_THETADIRS);
01035 SBPL_PRINTF("goal_c: %d %d %d\n", goalx_c, goaly_c, goaltheta_c);
01036 vector<EnvNAVXYTHETALAT3Dpt_t> xythetaPath;
01037 while(fabs(startx - goalx) > goaltol_x || fabs(starty - goaly) > goaltol_y || fabs(starttheta - goaltheta) > goaltol_theta){
01038
01039
01040 bool bChanges = false;
01041 preds_of_changededgesIDV.clear();
01042 changedcellsV.clear();
01043 for(i = 0; i < (int)sensecells.size(); i++){
01044 int x = CONTXY2DISC(startx,cellsize_m) + sensecells.at(i).x;
01045 int y = CONTXY2DISC(starty,cellsize_m) + sensecells.at(i).y;
01046 if(x < 0 || x >= size_x || y < 0 || y >= size_y)
01047 continue;
01048 int index = x + y*size_x;
01049 unsigned char truecost = trueenvironment_navxythetalat.GetMapCost(x,y);
01050 if(map[index] != truecost){
01051 map[index] = truecost;
01052 environment_navxythetalat.UpdateCost(x,y,map[index]);
01053 SBPL_PRINTF("setting cost[%d][%d] to %d\n", x,y,map[index]);
01054 bChanges = true;
01055
01056 nav2dcell.x = x;
01057 nav2dcell.y = y;
01058 changedcellsV.push_back(nav2dcell);
01059 }
01060 }
01061
01062 double TimeStarted = clock();
01063
01064 if(bChanges){
01065 planner.costs_changed();
01066
01067
01068
01069
01070
01071
01072
01073 }
01074
01075 int startx_c = CONTXY2DISC(startx,cellsize_m);
01076 int starty_c = CONTXY2DISC(starty,cellsize_m);
01077 int starttheta_c = ContTheta2Disc(starttheta, NAVXYTHETALAT_THETADIRS);
01078
01079
01080 bool bPlanExists = false;
01081
01082 SBPL_PRINTF("new planning...\n");
01083 bPlanExists = (planner.replan(allocated_time_secs_foreachplan, &solution_stateIDs_V) == 1);
01084 SBPL_PRINTF("done with the solution of size=%d and sol. eps=%f\n", (unsigned int)solution_stateIDs_V.size(), planner.get_solution_eps());
01085 environment_navxythetalat.PrintTimeStat(stdout);
01086
01087 SBPL_FPRINTF(fSol, "plan time=%.5f eps=%.2f\n", (clock()-TimeStarted)/((double)CLOCKS_PER_SEC), planner.get_solution_eps());
01088 SBPL_FFLUSH(fSol);
01089
01090 xythetaPath.clear();
01091 environment_navxythetalat.ConvertStateIDPathintoXYThetaPath(&solution_stateIDs_V, &xythetaPath);
01092 SBPL_PRINTF("actual path (with intermediate poses) size=%d\n", (unsigned int)xythetaPath.size());
01093 for(unsigned int i = 0; i < xythetaPath.size(); i++) {
01094 SBPL_FPRINTF(fSol, "%.3f %.3f %.3f\n", xythetaPath.at(i).x, xythetaPath.at(i).y, xythetaPath.at(i).theta);
01095 }
01096 SBPL_FPRINTF(fSol, "*********\n");
01097
01098 for(int j = 1; j < (int)solution_stateIDs_V.size(); j++)
01099 {
01100 int newx, newy, newtheta=0;
01101 environment_navxythetalat.GetCoordFromState(solution_stateIDs_V[j], newx, newy, newtheta);
01102 SBPL_FPRINTF(fSol, "%d %d %d\n", newx, newy, newtheta);
01103 }
01104 SBPL_FFLUSH(fSol);
01105
01106
01107
01108 int startindex = startx_c + starty_c*size_x;
01109 int goalindex = goalx_c + goaly_c*size_x;
01110 for(y = 0; bPrintMap && y < size_y; y++){
01111 for(x = 0; x < size_x; x++){
01112 int index = x + y*size_x;
01113
01114
01115 bool bOnthePath = false;
01116 for(int j = 1; j < (int)solution_stateIDs_V.size(); j++)
01117 {
01118 int newx, newy, newtheta=0;
01119 environment_navxythetalat.GetCoordFromState(solution_stateIDs_V[j], newx, newy, newtheta);
01120 if(x == newx && y == newy)
01121 bOnthePath = true;
01122 }
01123
01124 if (index != startindex && index != goalindex && !bOnthePath)
01125 SBPL_PRINTF("%3d ", map[index]);
01126 else if(index == startindex)
01127 SBPL_PRINTF(" X ");
01128 else if(index == goalindex)
01129 SBPL_PRINTF(" G ");
01130 else if (bOnthePath)
01131 SBPL_PRINTF(" * ");
01132 else
01133 SBPL_PRINTF("? ");
01134 }
01135 SBPL_PRINTF("\n");
01136 }
01137
01138
01139 if(bPlanExists && (int) xythetaPath.size() > 1){
01140
01141 int newx, newy, newtheta;
01142
01143
01144 environment_navxythetalat.GetCoordFromState(solution_stateIDs_V[1], newx, newy, newtheta);
01145
01146 SBPL_PRINTF("moving from %d %d %d to %d %d %d\n", startx_c, starty_c, starttheta_c, newx, newy, newtheta);
01147
01148
01149 if(!trueenvironment_navxythetalat.IsValidConfiguration(newx,newy,newtheta))
01150 {
01151 SBPL_ERROR("ERROR: robot is commanded to move into an invalid configuration according to true environment\n");
01152 throw new SBPL_Exception();
01153 }
01154 if(!trueenvironment_navxythetalat.IsValidConfiguration(newx,newy,newtheta))
01155 {
01156 SBPL_ERROR("ERROR: robot is commanded to move into an invalid configuration\n");
01157 throw new SBPL_Exception();
01158 }
01159
01160
01161 startx = DISCXY2CONT(newx, cellsize_m);
01162 starty = DISCXY2CONT(newy, cellsize_m);
01163 starttheta = DiscTheta2Cont(newtheta, NAVXYTHETALAT_THETADIRS);
01164
01165
01166 int newstartstateID = environment_navxythetalat.SetStart(startx, starty,starttheta);
01167
01168
01169 if(planner.set_start(newstartstateID) == 0){
01170 SBPL_ERROR("ERROR: failed to update robot pose in the planner\n");
01171 throw new SBPL_Exception();
01172 }
01173 }
01174 else
01175 {
01176 SBPL_PRINTF("No move is made\n");
01177 }
01178
01179 if(bPrint) SBPL_PRINTF("System Pause (return=%d)\n",system("pause"));
01180
01181
01182 }
01183
01184 SBPL_PRINTF("goal reached!\n");
01185
01186 SBPL_FFLUSH(NULL);
01187 SBPL_FCLOSE(fSol);
01188
01189
01190 return 1;
01191 }
01192
01193
01194 int planrobarm(int argc, char *argv[])
01195 {
01196
01197 int bRet = 0;
01198 double allocated_time_secs = 5.0;
01199 MDPConfig MDPCfg;
01200
01201
01202 EnvironmentROBARM environment_robarm;
01203 if(!environment_robarm.InitializeEnv(argv[1]))
01204 {
01205 SBPL_ERROR("ERROR: InitializeEnv failed\n");
01206 throw new SBPL_Exception();
01207 }
01208
01209
01210 if(!environment_robarm.InitializeMDPCfg(&MDPCfg))
01211 {
01212 SBPL_ERROR("ERROR: InitializeMDPCfg failed\n");
01213 throw new SBPL_Exception();
01214 }
01215
01216
01217
01218
01219 vector<int> solution_stateIDs_V;
01220 bool bforwardsearch = true;
01221 ARAPlanner planner(&environment_robarm, bforwardsearch);
01222
01223
01224
01225 if(planner.set_start(MDPCfg.startstateid) == 0)
01226 {
01227 SBPL_ERROR("ERROR: failed to set start state\n");
01228 throw new SBPL_Exception();
01229 }
01230
01231 if(planner.set_goal(MDPCfg.goalstateid) == 0)
01232 {
01233 SBPL_ERROR("ERROR: failed to set goal state\n");
01234 throw new SBPL_Exception();
01235 }
01236
01237 SBPL_PRINTF("start planning...\n");
01238 bRet = planner.replan(allocated_time_secs, &solution_stateIDs_V);
01239 SBPL_PRINTF("done planning\n");
01240 std::cout << "size of solution=" << solution_stateIDs_V.size() << std::endl;
01241
01242 #ifndef ROS
01243 const char* sol = "sol.txt";
01244 #endif
01245 FILE* fSol = SBPL_FOPEN(sol, "w");
01246 if(fSol == NULL){
01247 SBPL_ERROR("ERROR: could not open solution file\n");
01248 throw new SBPL_Exception();
01249 }
01250 for(unsigned int i = 0; i < solution_stateIDs_V.size(); i++) {
01251 environment_robarm.PrintState(solution_stateIDs_V[i], true, fSol);
01252 }
01253 SBPL_FCLOSE(fSol);
01254
01255
01256
01257 if(bRet)
01258 {
01259
01260 SBPL_PRINTF("Solution is found\n");
01261 }
01262 else
01263 SBPL_PRINTF("Solution does not exist\n");
01264
01265 SBPL_FFLUSH(NULL);
01266
01267
01268 return bRet;
01269 }
01270
01271
01272
01273
01274
01275 int main(int argc, char *argv[])
01276 {
01277 #ifdef ROS
01278 ros::init(argc, argv, "sbpl_test");
01279 #endif
01280
01281 if(argc < 2)
01282 {
01283 PrintUsage(argv);
01284 throw new SBPL_Exception();
01285 }
01286
01287
01288
01289
01290
01291
01292
01293
01294
01295
01296
01297
01298 planxythetalat(argc, argv);
01299
01300
01301
01302
01303
01304
01305
01306
01307
01308
01309
01310
01311
01312
01313
01314
01315
01316
01317 return 0;
01318 }
01319
01320
01321
01322
01323