MRXT: The Multi-Robot eXploration Tool
Multi-Robot autonomous exploration and mapping simulator.
|
00001 #include "reactive.h" 00002 #include "math.h" 00003 00004 reactive::reactive(const ConfigFile& config): 00005 vmax (config.read<float>("VMAX")), 00006 wmax (config.read<float>("WMAX")), 00007 k1 (config.read<float>("K1")), 00008 k2 (config.read<float>("K2")), 00009 resolution (0), 00010 avoidObstaclesEnabled (true), 00011 goToFrontierEnabled (true), 00012 goToUnexploredZonesEnabled (true), 00013 avoidOtherRobotsEnabled (true), 00014 goToPrecisePosesEnabled (false), 00015 goToGoalEnabled (false), 00016 avoidObstaclesWeight (config.read<float>("WEIGHTAVOBS")), 00017 goToFrontierWeight (config.read<float>("WEIGHTGOFRO")), 00018 goToUnexploredZonesWeight (config.read<float>("WEIGHTGOUZ")), 00019 avoidOtherRobotsWeight (config.read<float>("WEIGHTAVROB")), 00020 goToPrecisePosesWeight (config.read<float>("WEIGHTGOPRE")), 00021 goToGoalWeight (config.read<float>("WEIGHTGOGOAL")), 00022 avoidObstaclesWidth (config.read<float>("SIGMAAVOBS")), 00023 goToFrontierWidth (config.read<float>("SIGMAGOFRO")), 00024 goToUnexploredZonesWidth (config.read<float>("SIGMAGOUZ")), 00025 avoidOtherRobotsWidth (config.read<float>("SIGMAAVROB")), 00026 goToPrecisePosesWidth (config.read<float>("SIGMAGOPRE")), 00027 goToGoalWidth (config.read<float>("SIGMAGOGOAL")), 00028 localMinimumDetected (false) 00029 { 00030 } 00031 00032 00033 speed reactive::regulator(const pose& pos, pointf& f){ 00034 speed velo; 00035 00036 if (f.x!=0 || f.y!=0){ 00037 float ang = (atan2(f.y, f.x) - pos.th ); 00038 if (ang>=PI) ang += (float)(-2*PI); 00039 else if (ang<-PI) ang += (float)(2*PI); 00040 00041 velo.w = k1*ang; 00042 if (velo.w>=wmax) velo.w = wmax; 00043 else if (velo.w<-wmax) velo.w = -wmax; 00044 00045 velo.v = vmax/(k2*fabs(ang)+1); 00046 00047 } 00048 else{ 00049 velo.v=0; 00050 velo.w=0; 00051 } 00052 return velo; 00053 } 00054 00055 void reactive::disableAll(){ 00056 goToUnexploredZonesEnabled=false; 00057 goToFrontierEnabled=false; 00058 avoidOtherRobotsEnabled=false; 00059 avoidObstaclesEnabled=false; 00060 goToPrecisePosesEnabled=false; 00061 goToGoalEnabled=false; 00062 } 00063 00064 pointf reactive::getResponse(localPotentialField& lpf){ 00065 00066 pointf ctrlAction; 00067 int i,j; 00068 float vali=0; 00069 float valj=0; 00070 00071 float min=lpf.get(0,0); 00072 float max=lpf.get(0,0); 00073 int mini=0, minj=0; 00074 00075 pose currentPos = mySlam->getPos(number); 00076 point currentCell = mySlam->getCell(number); 00077 00078 float incX = (currentPos.x - ((currentCell.x+0.5)*mySlam->getResolution() + mySlam->getXOrigin()))/mySlam->getResolution(); 00079 float incY = (currentPos.y - ((currentCell.y+0.5)*mySlam->getResolution() + mySlam->getYOrigin()))/mySlam->getResolution(); 00080 00081 //printf("x: %f y: %f - cellx: %d, celly: %d, incX: %f, incY: %f\n",currentPos.x, currentPos.y, currentCell.x, currentCell.y,incX,incY); 00082 00083 float sum=0; 00084 00085 for(i=0;i< lpf.getWidth(); i++){ 00086 for(j=0;j< lpf.getHeight(); j++){ 00087 if (min > lpf.get(i,j)){ 00088 min = lpf.get(i,j); 00089 mini=i; 00090 minj=j; 00091 } 00092 if (max < lpf.get(i,j)) max = lpf.get(i,j); 00093 } 00094 } 00095 00096 int imin,imax,jmin,jmax; 00097 00098 imin = (mini-1>=0)? mini-1:0; 00099 jmin = (minj-1>=0)? minj-1:0; 00100 imax = (mini+1<lpf.getWidth())? mini+1 : lpf.getWidth()-1; 00101 jmax = (minj+1<lpf.getHeight())? minj+1 : lpf.getHeight()-1; 00102 00103 //printf("%d,%d,%d,%d\n", imin,imax,jmin,jmax); 00104 for(i=imin;i<=imax; i++){ 00105 for(j=jmin;j<=jmax; j++){ 00106 float aux = ((max-min)-(lpf.get(i,j)-min)); 00107 vali += aux*i; 00108 valj += aux*j; 00109 sum += aux; 00110 } 00111 } 00112 00113 if (sum==0){ 00114 ctrlAction.x = 0; 00115 ctrlAction.y = 0; 00116 } 00117 else{ 00118 ctrlAction.x = ((vali-incX)/sum)-(lpf.getWidth()-1)/2; 00119 ctrlAction.y = ((valj-incY)/sum)-(lpf.getHeight()-1)/2; 00120 } 00121 //printf("fx: %f, fy: %f\n",ctrlAction.x, ctrlAction.y); 00122 00123 if ((ctrlAction.x*ctrlAction.x + ctrlAction.y*ctrlAction.y) < 1.5){ 00124 localMinimumDetected = true; 00125 //printf("LOCAL MINIMUM DETECTED!!!!!!\n"); 00126 } 00127 else 00128 localMinimumDetected = false; 00129 00130 return ctrlAction; 00131 } 00132