00001 #include <assert.h>
00002 #include "synchronization/semaphore.h"
00003 #include "PL_RRT.h"
00004
00005 #include "planners/aca/jma_switch.h"
00006 #include "planners/aca/jma_aca_macros.h"
00007 #include <iosfwd>
00008 #include <iostream>
00009 #include <kinematics/configuration.h>
00010 #include "opengl/glos.h"
00011 #include <gl/gl.h>
00012
00013 #define THICKNESS 3
00014 #define WIN_WIDTH 1000
00015 #define WIN_HEIGHT 500
00016 #define EPSILON 1.0e-3
00017 #define BIG_NUMBER 1.0e+37
00018
00019
00020
00021
00022
00023 PL_RRT::~PL_RRT()
00024 {
00025
00026 }
00027
00028 bool PL_RRT::Plan()
00029 {
00030
00031
00032
00033 PlannerBase::StartTimer() ;
00034 path.Clear() ;
00035
00036
00037 if( collisionDetector->IsInterfering( GetStartConfig() ) )
00038 {
00039 path.AppendPoint( GetGoalConfig() ) ;
00040 path.AppendPoint( GetGoalConfig() ) ;
00041 return false ;
00042 }
00043 if( collisionDetector->IsInterfering( GetGoalConfig() ) )
00044 {
00045 path.AppendPoint( GetStartConfig() ) ;
00046 path.AppendPoint( GetStartConfig() ) ;
00047 return false ;
00048 }
00049
00050
00051
00052 SetStart_and_GoalConfig (GetStartConfig(), GetGoalConfig());
00053
00054 int i;
00055 int n_landmarks;
00056
00057
00058
00059 bool path_found=false;
00060
00061 JMA_Roadmap_Tree *temp_tree;
00062
00063 temp_tree = &tree;
00064 Semaphore semaphore( guid );
00065 semaphore.Lock();
00066
00067
00068 if(!( collisionDetector->IsInterferingLinear(GetStartConfig(),GetGoalConfig())))
00069 path_found=true;
00070 else
00071 {
00072
00073 do
00074 {
00075 Get_random_config(&random_config);
00076 if ((Extend_trapped(temp_tree, &(temp_tree->n_nodes), &random_config))!=3)
00077 {
00078 if(!( collisionDetector->IsInterferingLinear(new_config,GetGoalConfig())))
00079 {
00080 last_config=new_config;
00081 path_found=true;
00082 (tree).node[tree.n_nodes].landmark.dof=GetGoalConfig();
00083 (tree).node[tree.n_nodes].father_id=tree.n_nodes-1;
00084 ++(tree.n_nodes);
00085 break;
00086 }
00087 }
00088 semaphore.Unlock();
00089 semaphore.Lock();
00090 } while ((!path_found) && (HasTimeLimitExpired() == false) );
00091 }
00092 semaphore.Unlock();
00093
00094
00095
00096
00097
00098
00099
00100
00101 if (path_found)
00102 {
00103
00104 GetIdsPath(&tree,tree.n_nodes-1,ids_list,&n_landmarks);
00105
00106 for( i = n_landmarks-1; i >=0; i--)
00107 {
00108 int pointIndex = ids_list[ i ] ;
00109 Configuration point = tree.node[ pointIndex ].landmark.dof ;
00110 path.AppendPoint( point ) ;
00111 }
00112 path.AppendPoint( GetGoalConfig() ) ;
00113 }
00114 else
00115 {
00116 if( path.Size() == 0 )
00117 {
00118 path.AppendPoint( GetStartConfig() ) ;
00119 }
00120 return false ;
00121 }
00122
00123
00124 PA_Points the_path;
00125 int size=path.Size();
00126 the_path.AppendPoint(path[0]);
00127 Configuration p1 = path[ 0 ] ;
00128 int n=size-1;
00129 int indicater=0;
00130 while (indicater!=size-1)
00131 {
00132 Configuration p2 = path[n];
00133 while (collisionDetector->IsInterferingLinear( p1, p2 ))
00134 {
00135 n=n-1;
00136 if (n==indicater)
00137 assert(false);
00138 p2=path[n];
00139 }
00140 the_path.AppendPoint (p2);
00141 p1=p2;
00142 indicater=n;
00143 n=size-1;
00144 }
00145 path.Clear();
00146 for (n=0;n<the_path.Size();n++)
00147 path.AppendPoint(the_path[n]);
00148 the_path.Clear();
00149
00150
00151 for( i = 0; i < path.Size() - 1 ; i++ )
00152 {
00153 Configuration p1 = path[ i ] ;
00154 Configuration p2 = path[ i + 1 ] ;
00155 assert( !collisionDetector->IsInterferingLinear( p1, p2 ) ) ;
00156 if( collisionDetector->IsInterferingLinear( p1, p2 ) )
00157 {
00158 assert( false ) ;
00159 }
00160 }
00161
00162 return true ;
00163 }
00164
00165 int PL_RRT::Get_random_config(Configuration* the_random_config)
00166 {
00167 Configuration config= GetStartConfig() ;
00168
00169
00170
00171 do
00172 {
00173 for( int j = 0; j < config.DOF(); j++ )
00174 {
00175 double jointMax = collisionDetector->JointMax( j ) ;
00176 double jointMin = collisionDetector->JointMin( j ) ;
00177 double random01 = (double) switch_random()/BIGEST_RND_NUMBER ;
00178 double jointVal = jointMin + random01 * ( jointMax - jointMin );
00179 config[ j ] = jointVal ;
00180 }
00181 } while( collisionDetector->IsInterfering(config ) );
00182 (*the_random_config) = config ;
00183 return 0;
00184 }
00185
00186 int PL_RRT::Extend_trapped(JMA_Roadmap_Tree* Ptr_tree, int* the_n_nodes, Configuration* the_random_config)
00187 {
00188
00189 int near_neighbour_id=Get_nearest_neighbour(Ptr_tree, the_random_config, &near_config);
00190
00191 Get_new_config(&random_config, &near_config, &new_config);
00192 if(!( collisionDetector->IsInterferingLinear(near_config,new_config)))
00193 {
00194 (*Ptr_tree).node[*the_n_nodes].landmark.dof=new_config;
00195 new_config=new_config;
00196 (*Ptr_tree).node[*the_n_nodes].father_id=near_neighbour_id;
00197 ++(*the_n_nodes);
00198 return 1;
00199 }
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211 return 3;
00212 }
00213
00214 int PL_RRT::Get_nearest_neighbour(JMA_Roadmap_Tree* Ptr_tree1, Configuration* the_random_config, Configuration* the_near_config)
00215 {
00216 double min_distance=10000.0;
00217 double temp_distance;
00218 int indicator;
00219 for (int i=0; i<(Ptr_tree1->n_nodes); i++)
00220 {
00221 temp_distance=MyDistanceFunc((*Ptr_tree1).node[i].landmark.dof, *the_random_config);
00222 if(min_distance>temp_distance)
00223 {
00224 min_distance=temp_distance;
00225 indicator=i;
00226 }
00227 }
00228 *the_near_config=(*Ptr_tree1).node[indicator].landmark.dof;
00229 return indicator;
00230 }
00231
00232
00233 int PL_RRT::Get_new_config(Configuration* the_random_config, Configuration* the_near_config, Configuration* the_new_config)
00234 {
00235 double Fixed_Distance=0.5;
00236 double pp=MyDistanceFunc((*the_random_config),(*the_near_config));
00237 (*the_new_config)=(*the_near_config)*(1-Fixed_Distance/pp)+(*the_random_config)*(Fixed_Distance/pp);
00238 return 0;
00239 }
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249 double PL_RRT::MyDistanceFunc (const Configuration& conf1, const Configuration& conf2) const
00250 {
00251
00252 return collisionDetector->DistanceBetween( conf1, conf2 ) ;
00253
00254 }
00255
00256
00257 void PL_RRT::InitRoadmapTree( JMA_Roadmap_Tree* Ptr_tree,
00258 const int n_dof1,
00259 const JMA_Configuration init_conf) const
00260 {
00261 double dist;
00262 (*Ptr_tree).N_dof = n_dof1;
00263 (*Ptr_tree).n_nodes = 1;
00264 (*Ptr_tree).max_dist = 0.0;
00265 (*Ptr_tree).last_max_dist = -1.0;
00266 (*Ptr_tree).best_father_id = 0;
00267 (*Ptr_tree).node[0].N_embryos = 0;
00268 (*Ptr_tree).node[0].father_id = NIL;
00269
00270
00271 (*Ptr_tree).node[0].landmark.dof = init_conf.dof;
00272 (*Ptr_tree).node[0].landmark.dist = init_conf.dist;
00273 dist = 0.0;
00274 }
00275
00276
00277 void PL_RRT::GetIdsPath (JMA_Roadmap_Tree* Ptr_tree,
00278 const int landmark_id, int ids_list[],
00279 int* n_landmarks)
00280 {
00281
00282
00283
00284 int counter=1,id;
00285
00286 id = landmark_id;
00287 ids_list[counter-1]=id;
00288 while (id!=0)
00289 {
00290 id = (*Ptr_tree).node[id].father_id;
00291 ids_list[counter]=id;
00292 ++counter;
00293 }
00294 (*n_landmarks) = counter;
00295 }
00296
00297
00298 void PL_RRT::SetStart_and_GoalConfig (const Configuration& configuration1,const Configuration& configuration2)
00299 {
00300 init_conf.dof = configuration1 ;
00301 init_conf.dist = 0 ;
00302 goal_conf.dof = configuration2 ;
00303 goal_conf.dist = 0 ;
00304 InitRoadmapTree( &tree, GetStartConfig().DOF(), init_conf);
00305 }
00306
00307
00308 bool PL_RRT::DrawExplicit () const
00309 {
00310 Semaphore semaphore( this->guid );
00311 semaphore.Lock();
00312
00313 glClearColor( 0.0f, 0.0f, 0.2f, 1.0f );
00314 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00315
00316
00317 BOOL lightingState = glIsEnabled( GL_LIGHTING );
00318 glDisable( GL_LIGHTING );
00319
00320
00321 BOOL zbufferState = glIsEnabled( GL_DEPTH_TEST );
00322 glDisable( GL_DEPTH_TEST );
00323
00324
00325 glShadeModel( GL_SMOOTH );
00326
00327
00328 int i = 0;
00329 glColor4d( 1.0, 1.0, 1.0, 1.0 );
00330
00331 glPointSize(5.0f);
00332
00333 int dof = this->GetGoalConfig().DOF();
00334
00335 double g0, g1, g2;
00336
00337 double v0, v1, p0, p1;
00338 double v2 = 0;
00339 double p2 = 0;
00340 for( i = 0; i < tree.n_nodes; i++ )
00341 {
00342 JMA_Node node = tree.node[ i ];
00343 JMA_Configuration current = node.landmark;
00344
00345
00346 v0 = current.dof[ 0 ];
00347 v1 = current.dof[ 1 ];
00348 if( dof > 2 )
00349 {
00350 v2 = current.dof[ 2 ];
00351 }
00352
00353 glBegin( GL_POINTS );
00354 glColor3f(1.0f,1.0f,0.0f);
00355 glVertex3f( v0, v1, v2 );
00356 glEnd();
00357
00358 int parent = node.father_id;
00359 if( parent == -1 )
00360 {
00361 }
00362 else
00363 {
00364 JMA_Node parentNode = tree.node[ parent ];
00365 JMA_Configuration parentConfig = parentNode.landmark;
00366
00367
00368 p0 = parentConfig.dof[ 0 ];
00369 p1 = parentConfig.dof[ 1 ];
00370 if( dof > 2 )
00371 {
00372 p2 = parentConfig.dof[ 2 ];
00373 }
00374
00375
00376 glBegin( GL_LINES );
00377 glColor3f(1.0f,0.5f,0.0f);
00378 glVertex3f( v0, v1, v2 );
00379 glColor3f(1.0f,0.0f,0.0f);
00380 glVertex3f( p0, p1, p2 );
00381 glEnd();
00382 }
00383
00384
00385 }
00386
00387
00388 glColor3f( 1.0f, 1.0f, 0.0f );
00389 glBegin( GL_POINTS );
00390 g0 = g1 = g2 = 0;
00391 if( dof > 0 )
00392 {
00393 g0 = this->GetGoalConfig()[ 0 ];
00394 }
00395 if( dof > 1 )
00396 {
00397 g1 = this->GetGoalConfig()[ 1 ];
00398 }
00399 if( dof > 2 )
00400 {
00401 g2 = this->GetGoalConfig()[ 2 ];
00402 }
00403
00404 glColor3f(0.0f,0.0f,1.0f);
00405
00406 glVertex3f( g0, g1, g2 );
00407 glEnd();
00408
00409 glColor3f( 1.0f, 1.0f, 0.0f );
00410 glBegin( GL_POINTS );
00411 g0 = g1 = g2 = 0;
00412 if( dof > 0 )
00413 {
00414 g0 = this->GetStartConfig()[ 0 ];
00415 }
00416 if( dof > 1 )
00417 {
00418 g1 = this->GetStartConfig()[ 1 ];
00419 }
00420 if( dof > 2 )
00421 {
00422 g2 = this->GetStartConfig()[ 2 ];
00423 }
00424
00425 glColor3f(0.0f,0.0f,1.0f);
00426
00427 glVertex3f( g0, g1, g2 );
00428 glEnd();
00429
00430 if( lightingState != 0x00 )
00431 {
00432 glEnable( GL_LIGHTING );
00433 }
00434
00435
00436 if( zbufferState )
00437 {
00438 glEnable( GL_DEPTH_TEST );
00439 }
00440 else
00441 {
00442 glDisable( GL_DEPTH_TEST );
00443 }
00444
00445 semaphore.Unlock();
00446 return true;
00447 }