Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

Modules/SensorBehaviorControl/ObstacleAvoiderOnGreenFieldERS7.cpp

Go to the documentation of this file.
00001 /**
00002 * @file ObstacleAvoiderOnGreenFieldERS7.cpp
00003 * This file contains a class that demonstrates walking.
00004 * 
00005 * @author <A href=mailto:jhoffman@informatik.hu-berlin.de>Jan Hoffmann</A>
00006 */
00007 
00008 #include "ObstacleAvoiderOnGreenFieldERS7.h"
00009 #include "Tools/Debugging/GenericDebugData.h"
00010 
00011 
00012 
00013 ObstacleAvoiderOnGreenFieldERS7::ObstacleAvoiderOnGreenFieldERS7(const SensorBehaviorControlInterfaces& interfaces)
00014 : SensorBehaviorControl(interfaces), 
00015 maxSpeedX(140),
00016 distanceToGround(300),
00017 distanceOffset(50),
00018 distanceControlInterval(50),
00019 speedX(0, 0.6, 0.0, 0, -maxSpeedX/2, maxSpeedX, 100),
00020 speedY(0, 1, 0, 0, -50, 50, 50),
00021 speedPhi(0, .1, 0, 0, -pi_4, pi_4, pi/10),
00022 gridSize(4),
00023 timeOfLastExecute(0),
00024 localColTableInitialized(false)
00025 {
00026 }
00027 
00028 void ObstacleAvoiderOnGreenFieldERS7::init() {}
00029 
00030 void ObstacleAvoiderOnGreenFieldERS7::execute()
00031 {
00032   /* The module was not active for a while */
00033   /*
00034   unsigned long currentTime = SystemCall::getCurrentSystemTime();
00035   if((currentTime - timeOfLastExecute) > 2000) init();
00036   timeOfLastExecute = currentTime;
00037   */
00038 
00039   /* [JH] use the "proper" way to access the color table */
00040   Image cameraImage = image;
00041   if(!localColTableInitialized)
00042   {
00043     cameraImage.setColorTable(&colorTable);
00044   }
00045   else 
00046   {
00047     cameraImage.setColorTable(&localColTable);    
00048   }
00049   
00050   
00051   double sum,
00052     greenRight = 0, 
00053     greenLeft = 0, 
00054     targetY = 0, 
00055     targetPhi, 
00056     targetX,
00057     psd = (sensorDataBuffer.lastFrame().data[SensorData::headPsdFar] / 1000) - 100.0; 
00058   
00059   int x, y, midX = image.cameraInfo.resolutionWidth / 2;//104
00060 
00061   distanceToGround = 350;
00062 
00063 //  if (psd > distanceToGround * 1.4) // don't jump off cliff!
00064 //    targetX = 0.0; // don't use negative nuber because robot might "reverse" off table
00065 //  else
00066 
00067   
00068     targetX = psd - 200; // - (distanceToGround - distanceOffset - distanceControlInterval);
00069   //shouldn't this be = psp - distanceOffset - (distanceToGround) ?
00070 
00071 
00072     // now it's really hitting something!
00073     if (sensorDataBuffer.lastFrame().data[SensorData::bodyPsd] == 100000)
00074     {
00075       targetX = -100;
00076     }
00077 //  targetX *= maxSpeedX/distanceControlInterval;
00078   
00079   //OUTPUT(idText, text, "target: " << targetX << ", psd: " << psd << ", speedX: " << speedX.getVal() << ", P speed: " << speedX.getWeightP());
00080   
00081   
00082   /** calc how much green is in both halves */
00083   for(x = gridSize/2; x < midX; x += gridSize)
00084   {
00085     for(y = gridSize/2; y < image.cameraInfo.resolutionHeight; y += gridSize)
00086     {
00087       if (cameraImage.getClassifiedColor(x, y) == green)
00088       {
00089         greenLeft++;
00090         //cameraImage.image[y][2][x] = 255;
00091       }
00092       else
00093       {
00094         //cameraImage.image[y][0][x] = 0;
00095       }
00096 
00097       if (cameraImage.getClassifiedColor(x + midX, y) == green)
00098       {
00099         greenRight++;
00100         //cameraImage.image[y][1][x + midX] = 255;
00101       }
00102       else 
00103       {
00104         //cameraImage.image[y][0][x + midX] = 255;
00105       }
00106     }
00107   }
00108 
00109   //INIT_DEBUG_IMAGE(classificationY, cameraImage);
00110   //SEND_DEBUG_IMAGE(classificationY);
00111   //OUTPUT(idText, text, " " << greenLeft << " : " << greenRight); 
00112 
00113   sum = greenLeft + greenRight;
00114   if (sum == 0) sum = 1;
00115   
00116   /** if robot is moving forward, turn where there's more
00117   *   green
00118   */
00119   if (speedX.getVal() > .7 * maxSpeedX)
00120   {
00121     targetPhi = 1.2 * (greenLeft - greenRight) / sum;
00122     //targetPhi *= .5;
00123   }
00124   /** if robot is too close to something, stick to the
00125   * direction it last turned to until obstacle is no
00126   * longer in front of robot
00127   */
00128   else 
00129   {
00130     if (speedPhi.getVal() < 0)
00131       targetPhi = -pi;
00132     else
00133       targetPhi = pi;
00134   }
00135   
00136   if (greenLeft > greenRight) 
00137   {
00138     ledRequest.redBottomLEDs = LEDRequest::leftOnly;
00139   }
00140   else if (greenLeft < greenRight)
00141   {
00142     ledRequest.redBottomLEDs = LEDRequest::rightOnly;
00143   }
00144   else if (greenLeft == greenRight)
00145   {
00146     if (greenLeft == 0)
00147       ledRequest.redBottomLEDs = LEDRequest::bothOff;
00148     else
00149       ledRequest.redBottomLEDs = LEDRequest::bothOn;
00150   }
00151   else 
00152     ledRequest.redBottomLEDs = LEDRequest::bothOff;
00153       
00154   ledRequest.redTopLEDs = LEDRequest::bothOff;
00155   
00156   if(sensorDataBuffer.lastFrame().data[SensorData::tailPan] > 0)
00157   {
00158     speedPhi.approximateVal(targetPhi);
00159     speedX.approximateVal(targetX);
00160     timeToCalibrate = 0;
00161     soundRequest.soundID = SoundRequest::okay;
00162     headControlMode.headControlMode = HeadControlMode::lookStraightAhead;
00163   }
00164   else //stop
00165   {
00166     headControlMode.headControlMode = HeadControlMode::lookStraightAhead;
00167     if (SystemCall::getTimeSince(timeToCalibrate) < 2000)
00168       soundRequest.soundID = SoundRequest::notokay;
00169     else 
00170       soundRequest.soundID = SoundRequest::none;
00171     speedPhi.approximateVal(0);
00172     speedX.approximateVal(0);
00173     if (timeToCalibrate > 0)
00174     {
00175       ledRequest.redTopLEDs = LEDRequest::bothOn;
00176       if (SystemCall::getTimeSince(timeToCalibrate) > 2000) // wait for one sec to allow motion to subside
00177       {
00178 /*        if(robotState.getSwitches() == RobotState::headDown)
00179         {
00180           soundRequest.soundID = SoundRequest::rob005;
00181           calibrate();
00182         }
00183 */
00184       }
00185     }
00186     else
00187     {
00188       timeToCalibrate = SystemCall::getCurrentSystemTime();
00189       ledRequest.redTopLEDs = LEDRequest::bothOff;
00190     }
00191   }
00192      
00193   motionRequest.tailRequest.tailRequestID = TailRequest::twoPositionSwitchHorizontal;
00194   motionRequest.motionType = MotionRequest::walk;
00195   motionRequest.walkRequest.walkType = WalkRequest::normal;
00196 
00197   /* set the motion request */
00198   motionRequest.motionType=MotionRequest::walk;
00199   if (fabs(speedX.getVal()) > 1)
00200     motionRequest.walkRequest.walkParams.translation.x = speedX.getVal();
00201   else  
00202     motionRequest.walkRequest.walkParams.translation.x = 0.0;
00203   if (fabs(speedY.getVal()) > 1)
00204     motionRequest.walkRequest.walkParams.translation.y = targetY;
00205   else 
00206     motionRequest.walkRequest.walkParams.translation.y = 0.0;
00207   if (fabs(speedPhi.getVal()) > .05)
00208     motionRequest.walkRequest.walkParams.rotation = speedPhi.getVal();
00209   else 
00210     motionRequest.walkRequest.walkParams.rotation = 0.0;
00211 }
00212 
00213 
00214 
00215 void ObstacleAvoiderOnGreenFieldERS7::calibrate()
00216 {
00217   //distanceToGround = (sensorDataBuffer.lastFrame().data[SensorData::psd] / 1000) - 100;
00218 
00219   localColTable.clear();
00220 
00221   int x, y;
00222 
00223   for(x = gridSize/2; x < image.cameraInfo.resolutionWidth; x += gridSize)
00224   {
00225     for(y = gridSize/2; y < image.cameraInfo.resolutionHeight; y += gridSize)
00226     {
00227       localColTable.addColorClass(green, image.image[y][0][x], image.image[y][1][x], image.image[y][2][x], 2);
00228     }
00229   }
00230 
00231   localColTableInitialized = true;
00232 }
00233 
00234 
00235 
00236 bool ObstacleAvoiderOnGreenFieldERS7::handleMessage(InMessage& message)
00237 {
00238   bool handled = false;
00239   
00240   switch(message.getMessageID())
00241   {
00242   case idGenericDebugData:
00243     GenericDebugData d;
00244     message.bin >> d;
00245     if(d.id == GenericDebugData::braitenbergPIDs)
00246     {
00247       OUTPUT(idText,text,"generic debug message (Braitenberg) handled by module ObstacleAvoiderOnGreenFieldERS7");
00248       
00249       speedX.setWeightP(d.data[0]);
00250       speedX.setWeightI(d.data[1]);
00251       speedX.setWeightD(d.data[2]);
00252       
00253       speedPhi.setWeightP(d.data[3]);
00254       speedPhi.setWeightI(d.data[4]);
00255       speedPhi.setWeightD(d.data[5]);
00256       
00257       distanceOffset = d.data[6];
00258       distanceControlInterval = d.data[7];
00259       distanceToGround = d.data[8];
00260     }
00261     handled = true;
00262     break;
00263 
00264   }
00265   return handled;
00266 }
00267 
00268 
00269 /*
00270 * Change log :
00271 * 
00272 * $Log: ObstacleAvoiderOnGreenFieldERS7.cpp,v $
00273 * Revision 1.4  2004/06/02 17:18:22  spranger
00274 * MotionRequest cleanup
00275 *
00276 * Revision 1.3  2004/05/24 14:15:49  juengel
00277 * New button evaluation.
00278 *
00279 * Revision 1.2  2004/05/23 12:08:26  loetzsch
00280 * clean up in class HeadControlMode
00281 *
00282 * Revision 1.1.1.1  2004/05/22 17:20:53  cvsadm
00283 * created new repository GT2004_WM
00284 *
00285 * Revision 1.3  2004/03/17 10:22:55  jhoffman
00286 * added ERS7 support, removed some image size related bugs
00287 *
00288 * Revision 1.2  2004/03/08 02:11:50  roefer
00289 * Interfaces should be const
00290 *
00291 * Revision 1.1  2004/02/27 14:16:20  jhoffman
00292 * - screen layout "save" works properly
00293 * - delete now has "confirm" dialog
00294 *
00295 */

Generated on Thu Sep 23 19:57:32 2004 for GT2004 by doxygen 1.3.6