main.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, Maxim Likhachev
00003  * All rights reserved.
00004  * 
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  * 
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the University of Pennsylvania nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  * 
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
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; //in seconds
00046         MDPConfig MDPCfg;
00047         bool bsearchuntilfirstsolution = true;
00048         
00049         //Initialize Environment (should be called before initializing anything else)
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         //Initialize MDP Info
00058         if(!environment_nav2D.InitializeMDPCfg(&MDPCfg))
00059         {
00060                 SBPL_ERROR("ERROR: InitializeMDPCfg failed\n");
00061                 throw new SBPL_Exception();
00062         }
00063 
00064 
00065         //plan a path
00066         vector<int> solution_stateIDs_V;
00067         bool bforwardsearch = false;
00068         ARAPlanner planner(&environment_nav2D, bforwardsearch);
00069         //RSTARPlanner planner(&environment_nav2D, bforwardsearch);
00070         //anaPlanner planner(&environment_nav2D, bforwardsearch);
00071 
00072         //set search mode
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     SBPL_PRINTF("start planning...\n");
00099         bRet = planner.replan(allocated_time_secs, &solution_stateIDs_V);
00100     SBPL_PRINTF("done planning\n");
00101         std::cout << "size of solution=" << solution_stateIDs_V.size() << std::endl;
00102 
00103     environment_nav2D.PrintTimeStat(stdout);
00104 
00105     SBPL_PRINTF("start planning...\n");
00106         bRet = planner.replan(allocated_time_secs, &solution_stateIDs_V);
00107     SBPL_PRINTF("done planning\n");
00108         std::cout << "size of solution=" << solution_stateIDs_V.size() << std::endl;
00109 
00110     environment_nav2D.PrintTimeStat(stdout);
00111 
00112     SBPL_PRINTF("start planning...\n");
00113         bRet = planner.replan(allocated_time_secs, &solution_stateIDs_V);
00114     SBPL_PRINTF("done planning\n");
00115         std::cout << "size of solution=" << solution_stateIDs_V.size() << std::endl;
00116 
00117     environment_nav2D.PrintTimeStat(stdout);
00118 
00119     SBPL_PRINTF("start planning...\n");
00120         bRet = planner.replan(allocated_time_secs, &solution_stateIDs_V);
00121     SBPL_PRINTF("done planning\n");
00122         std::cout << "size of solution=" << solution_stateIDs_V.size() << std::endl;
00123 
00124     environment_nav2D.PrintTimeStat(stdout);
00125 
00126     SBPL_PRINTF("start planning...\n");
00127         bRet = planner.replan(allocated_time_secs, &solution_stateIDs_V);
00128     SBPL_PRINTF("done planning\n");
00129         std::cout << "size of solution=" << solution_stateIDs_V.size() << std::endl;
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         //print a path
00148         if(bRet)
00149         {
00150                 //print the solution
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; //in seconds
00167         MDPConfig MDPCfg;
00168         
00169         //Initialize Environment (should be called before initializing anything else)
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         //Initialize MDP Info
00178         if(!environment_nav2Duu.InitializeMDPCfg(&MDPCfg))
00179         {
00180                 SBPL_ERROR("ERROR: InitializeMDPCfg failed\n");
00181                 throw new SBPL_Exception();
00182         }
00183 
00184 
00185         //create the planner
00186         PPCPPlanner planner(&environment_nav2Duu, environment_nav2Duu.SizeofCreatedEnv(), environment_nav2Duu.SizeofH());
00187         
00188         //set start and goal
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                 //print the solution
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; //in seconds
00228         MDPConfig MDPCfg;
00229         bool bsearchuntilfirstsolution = false;
00230 
00231         //set the perimeter of the robot (it is given with 0,0,0 robot ref. point for which planning is done)
00232         vector<sbpl_2Dpt_t> perimeterptsV;
00233         sbpl_2Dpt_t pt_m;
00234         double halfwidth = 0.01; //0.3;
00235         double halflength = 0.01; //0.45;
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         //clear perimeter
00251         //perimeterptsV.clear();
00252         //pt_m.x = 0.0;
00253         //pt_m.y = 0.0;
00254         //perimeterptsV.push_back(pt_m);
00255 
00256         //Initialize Environment (should be called before initializing anything else)
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         //Initialize MDP Info
00277         if(!environment_navxythetalat.InitializeMDPCfg(&MDPCfg))
00278         {
00279                 SBPL_ERROR("ERROR: InitializeMDPCfg failed\n");
00280                 throw new SBPL_Exception();
00281         }
00282 
00283 
00284         //plan a path
00285         vector<int> solution_stateIDs_V;
00286         bool bforwardsearch = false;
00287         //ADPlanner planner(&environment_navxythetalat, bforwardsearch);
00288         ARAPlanner planner(&environment_navxythetalat, bforwardsearch);
00289         //anaPlanner planner(&environment_navxythetalat, bforwardsearch);
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         //set search mode
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     if(planner.set_start(environment_navxythetalat.SetStart(1.6,0.5,0)) == 0)
00316         {
00317             SBPL_ERROR("ERROR: failed to set start state\n");
00318             throw new SBPL_Exception();
00319         }
00320 
00321 
00322     SBPL_PRINTF("start planning...\n");
00323         bRet = planner.replan(allocated_time_secs, &solution_stateIDs_V);
00324     SBPL_PRINTF("done planning\n");
00325         std::cout << "size of solution=" << solution_stateIDs_V.size() << std::endl;
00326 
00327     environment_navxythetalat.PrintTimeStat(stdout);
00328 
00329 
00330     SBPL_PRINTF("start planning...\n");
00331         bRet = planner.replan(allocated_time_secs, &solution_stateIDs_V);
00332     SBPL_PRINTF("done planning\n");
00333         std::cout << "size of solution=" << solution_stateIDs_V.size() << std::endl;
00334 
00335     environment_navxythetalat.PrintTimeStat(stdout);
00336 
00337     SBPL_PRINTF("start planning...\n");
00338         bRet = planner.replan(allocated_time_secs, &solution_stateIDs_V);
00339     SBPL_PRINTF("done planning\n");
00340         std::cout << "size of solution=" << solution_stateIDs_V.size() << std::endl;
00341 
00342     environment_navxythetalat.PrintTimeStat(stdout);
00343 
00344     SBPL_PRINTF("start planning...\n");
00345         bRet = planner.replan(allocated_time_secs, &solution_stateIDs_V);
00346     SBPL_PRINTF("done planning\n");
00347         std::cout << "size of solution=" << solution_stateIDs_V.size() << std::endl;
00348 
00349     environment_navxythetalat.PrintTimeStat(stdout);
00350 
00351     SBPL_PRINTF("start planning...\n");
00352         bRet = planner.replan(allocated_time_secs, &solution_stateIDs_V);
00353     SBPL_PRINTF("done planning\n");
00354         std::cout << "size of solution=" << solution_stateIDs_V.size() << std::endl;
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         //print a path
00376         if(bRet)
00377         {
00378                 //print the solution
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 //planning with multiple levels x,y,theta lattice (for example, base and upper body)
00391 //there are no additional degrees of freedom. 
00392 //It is just that each level may have a different footprint 
00393 //and should be checked against the cost map at corresponding height
00394 //It is useful for doing planning for navigation by a tall ground robot operating in a cluttered 3D map
00395 int planxythetamlevlat(int argc, char *argv[])
00396 {
00397 
00398         int bRet = 0;
00399         double allocated_time_secs = 10; //in seconds
00400         MDPConfig MDPCfg;
00401         bool bsearchuntilfirstsolution = false;
00402 
00403         //set the perimeter of the robot (it is given with 0,0,0 robot ref. point for which planning is done)
00404         //this is for the default level - base level
00405         vector<sbpl_2Dpt_t> perimeterptsV;
00406         sbpl_2Dpt_t pt_m;
00407         double halfwidth = 0.3; //0.3;
00408         double halflength = 0.25; //0.45;
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         //clear perimeter
00423         //perimeterptsV.clear();
00424         //pt_m.x = 0.0;
00425         //pt_m.y = 0.0;
00426         //perimeterptsV.push_back(pt_m);
00427 
00428         //Initialize Environment (should be called before initializing anything else)
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         //this is for the second level - upper body level
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         //enable the second level
00469         int numofaddlevels = 1;
00470         SBPL_PRINTF("Number of additional levels = %d\n", numofaddlevels);
00471         unsigned char cost_inscribed_thresh_addlevels[2]; //size should be at least numofaddlevels
00472         unsigned char cost_possibly_circumscribed_thresh_addlevels[2]; //size should be at least numofaddlevels
00473         cost_inscribed_thresh_addlevels[0] = 255; //no costs are indicative of whether a cell is within inner circle
00474         cost_possibly_circumscribed_thresh_addlevels[0] = 0; //no costs are indicative of whether a cell is within outer circle
00475         cost_inscribed_thresh_addlevels[1] = 255; //no costs are indicative of whether a cell is within inner circle
00476         cost_possibly_circumscribed_thresh_addlevels[1] = 0; //no costs are indicative of whether a cell is within outer circle
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         //set the map for the second level (index parameter for the additional levels and is zero based)
00486         //for this example, we pass in the same map as the map for the base. In general, it can be a totally different map
00487         //as it corresponds to a different level
00488         //NOTE: this map has to have costs set correctly with respect to inner and outer radii of the robot
00489         //if the second level of the robot has these radii different than at the base level, then costs
00490         //should reflect this 
00491         //(see explanation for cost_possibly_circumscribed_thresh and cost_inscribed_thresh parameters in environment_navxythetalat.h file)
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         //Initialize MDP Info
00502         if(!environment_navxythetalat.InitializeMDPCfg(&MDPCfg))
00503         {
00504                 SBPL_ERROR("ERROR: InitializeMDPCfg failed\n");
00505                 throw new SBPL_Exception();
00506         }
00507 
00508 
00509         //plan a path
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         //set search mode
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         //print a path
00556         if(bRet)
00557         {
00558                 //print the solution
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; //in seconds
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   //int dx[8] = {-1, -1, -1,  0,  0,  1,  1,  1};
00591   //int dy[8] = {-1,  0,  1, -1,  1, -1,  0,  1};
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         //set parameters - should be done before initialization 
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     //initialize true map and robot map
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         //print the map
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         //Initialize Environment (should be called before initializing anything else)
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         //Initialize MDP Info
00640         if(!environment_nav2D.InitializeMDPCfg(&MDPCfg))
00641         {
00642                 SBPL_ERROR("ERROR: InitializeMDPCfg failed\n");
00643                 throw new SBPL_Exception();
00644         }
00645 
00646 
00647         //create a planner
00648         vector<int> solution_stateIDs_V;
00649         bool bforwardsearch = false;
00650     ARAPlanner planner(&environment_nav2D, bforwardsearch);
00651         //RSTARPlanner planner(&environment_nav2D, bforwardsearch);
00652 
00653         planner.set_initialsolution_eps(2.0);
00654 
00655 
00656     //set the start and goal configurations
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         //set search mode
00669         planner.set_search_mode(false); 
00670 
00671 
00672     //now comes the main loop
00673     int goalthresh = 0;
00674     while(abs(startx - goalx) > goalthresh || abs(starty - goaly) > goalthresh){
00675 
00676         //simulate sensor data update
00677         bool bChanges = false;
00678                 preds_of_changededgesIDV.clear();
00679                 changedcellsV.clear();
00680                 //two-step horizon
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                                         //store the changed cells
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(); //use by ARA* planner (non-incremental)
00708 
00709                         //the following two lines are used by AD* planner (incremental)
00710                         /*
00711                         //get the affected states
00712                         environment_nav2D.GetPredsofChangedEdges(&changedcellsV, &preds_of_changededgesIDV);
00713                         //let know the incremental planner about them
00714                         planner.update_preds_of_changededges(&preds_of_changededgesIDV); 
00715                         */
00716         }
00717                 //planner.force_planning_from_scratch();
00718 
00719 
00720         SBPL_FPRINTF(fSol, "%d %d ",  startx, starty);
00721 
00722         //plan a path 
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             //for(unsigned int i = 0; i < solution_stateIDs_V.size(); i++) {
00732             //environment_nav2D.PrintState(solution_stateIDs_V[i], true, fSol);
00733             //}
00734             //SBPL_FPRINTF(fSol, "*********\n");
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                 //print the map
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                                 //check to see if it is on the path
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         //move along the path
00785         if(bPlanExists && (int)solution_stateIDs_V.size() > 1){
00786             //get coord of the successor
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             //move
00797             SBPL_PRINTF("moving from %d %d to %d %d\n", startx, starty, newx, newy);
00798             startx = newx;
00799             starty = newy;
00800                         
00801             //update the environment
00802             environment_nav2D.SetStart(startx, starty);
00803             
00804             //update the planner
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         //print stats
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; //in seconds
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     //int dx[8] = {-1, -1, -1,  0,  0,  1,  1,  1};
00851     //int dy[8] = {-1,  0,  1, -1,  1, -1,  0,  1};
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         //get the name of motion primitives file
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         //srand(0);
00880 
00881     //initialize true map and robot map
00882         if(!trueenvironment_navxythetalat.InitializeEnv(argv[1]))
00883         {
00884                 SBPL_ERROR("ERROR: InitializeEnv failed\n");
00885                 throw new SBPL_Exception();
00886         }
00887 
00888         //get environment parameters
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         //print the map
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         //set the perimeter of the robot (it is given with 0,0,0 robot ref. point for which planning is done)
00910         vector<sbpl_2Dpt_t> perimeterptsV;
00911         sbpl_2Dpt_t pt_m;
00912         double halfwidth = 0.025; //0.3;
00913         double halflength = 0.025; //0.45;
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         //compute sensing
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         //TODO - when running it, no obstacle show up when it is 20 and not 2 - bug
00938         //TODO - make sensingrange to be the length of the longest motion primitive
00939         int sensingrange_c = (int)(__max(maxx, maxy)/cellsize_m) + 2; //should be big enough to cover the motion primitive length
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         //Set environment parameters (should be done before initialize function is called)
00962         //set parameters - should be done before initialization 
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         //Initialize Environment (should be called before initializing anything else)
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     if(!environment_navxythetalat.InitializeEnv(size_x, size_y, map, startx, starty, starttheta, goalx, goaly, goaltheta, 
00985                 goaltol_x, goaltol_y, goaltol_theta, perimeterptsV,
00986                 cellsize_m, nominalvel_mpersecs, timetoturn45degsinplace_secs, 
00987                 obsthresh)){
00988                 SBPL_ERROR("ERROR: InitializeEnv failed\n");
00989                 throw new SBPL_Exception();
00990         }
00991         */
00992         //set start state
00993         environment_navxythetalat.SetStart(startx, starty,starttheta);
00994 
00995         //Initialize MDP Info
00996         if(!environment_navxythetalat.InitializeMDPCfg(&MDPCfg))
00997         {
00998                 SBPL_ERROR("ERROR: InitializeMDPCfg failed\n");
00999                 throw new SBPL_Exception();
01000         }
01001 
01002 
01003         //create a planner
01004         vector<int> solution_stateIDs_V;
01005         bool bforwardsearch = false;
01006     ARAPlanner planner(&environment_navxythetalat, bforwardsearch);
01007         //ADPlanner planner(&environment_navxythetalat, bforwardsearch);
01008 
01009         planner.set_initialsolution_eps(5.0); 
01010 
01011         //set search mode
01012         planner.set_search_mode(bsearchuntilfirstsolution);
01013 
01014     //set the start and goal configurations
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     //now comes the main loop
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         //simulate sensor data update
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                                 //store the changed cells
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(); //use by ARA* planner (non-incremental)
01066 
01067                         //get the affected states
01068                         //environment_navxythetalat.GetPredsofChangedEdges(&changedcellsV, &preds_of_changededgesIDV);
01069                         //let know the incremental planner about them
01070                         //planner.update_preds_of_changededges(&preds_of_changededgesIDV); //use by AD* planner (incremental)
01071                         //SBPL_PRINTF("%d states were affected\n", preds_of_changededgesIDV.size());
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         //plan a path 
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                 //print the map
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                                 //check to see if it is on the path
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         //move along the path
01139         if(bPlanExists && (int) xythetaPath.size() > 1){
01140             //get coord of the successor
01141             int newx, newy, newtheta;
01142                         
01143                         //move until we move into the end of motion primitive
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                         //this check is weak since true configuration does not know the actual perimeter of the robot
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             //move
01161             startx = DISCXY2CONT(newx, cellsize_m);
01162             starty = DISCXY2CONT(newy, cellsize_m);
01163                         starttheta = DiscTheta2Cont(newtheta, NAVXYTHETALAT_THETADIRS);
01164                         
01165             //update the environment
01166             int newstartstateID = environment_navxythetalat.SetStart(startx, starty,starttheta);
01167 
01168             //update the planner
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; //in seconds
01199         MDPConfig MDPCfg;
01200         
01201         //Initialize Environment (should be called before initializing anything else)
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         //Initialize MDP Info
01210         if(!environment_robarm.InitializeMDPCfg(&MDPCfg))
01211         {
01212                 SBPL_ERROR("ERROR: InitializeMDPCfg failed\n");
01213                 throw new SBPL_Exception();
01214         }
01215 
01216         //srand(1); 
01217 
01218         //plan a path
01219         vector<int> solution_stateIDs_V;
01220         bool bforwardsearch = true;
01221         ARAPlanner planner(&environment_robarm, bforwardsearch);
01222         //RSTARPlanner planner(&environment_robarm, bforwardsearch);
01223         //anaPlanner planner(&environment_robarm, bforwardsearch);
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         //print a path
01257         if(bRet)
01258         {
01259                 //print the solution
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     //2D planning
01288         //usage: exename 2Denvironmentfile.cfg
01289         //2Denvironmentfile.cfg files can be found sbpl/env_examples/nav2d
01290     //plan2d(argc, argv);
01291     //planandnavigate2d(argc, argv);
01292 
01293     //xytheta planning
01294         //usage: exename 3Denvironmentfile.cfg motionprimitivesfile.mprim 
01295         //3Denvironmentfile.cfg files can be found sbpl/env_examples/nav3d
01296         //the motionprimitives files can be found in matlab/mprim directory
01297         //note: the resolution of the motion primitives files should match the resolution of the cfg files
01298     planxythetalat(argc, argv);
01299 
01300     //xytheta planning
01301         //usage: see the comments for planxythetalat() above
01302     //planandnavigatexythetalat(argc, argv);
01303 
01304         //xytheta with multiple levels (i.e., base of the robot and upper body)
01305         //usage: see the comments for planxythetalat() above
01306         //planxythetamlevlat(argc, argv);
01307 
01308     //robotarm planning
01309         //usage: exename robarmenvironmentfile.cfg
01310         //robarmenvironmentfile.cfg files can be found sbpl/env_examples/robarm
01311     //planrobarm(argc, argv);
01312 
01313         //plan under uncertainty (with incomplete information to be exact)
01314         //not functional yet
01315         //plan2duu(argc, argv); //not fully implemented yet
01316 
01317         return 0;
01318 }
01319 
01320 
01321 
01322 
01323 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


sbpl
Author(s): Maxim Likhachev/maximl@seas.upenn.edu
autogenerated on Fri Jan 18 2013 13:41:53