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

Modules/SensorBehaviorControl/ObstacleAvoiderOnGreenField.cpp

Go to the documentation of this file.
00001 /**
00002 * @file ObstacleAvoiderOnGreenField.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 "ObstacleAvoiderOnGreenField.h"
00009 #include "Tools/Debugging/GenericDebugData.h"
00010 
00011 
00012 
00013 ObstacleAvoiderOnGreenField::ObstacleAvoiderOnGreenField(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 ObstacleAvoiderOnGreenField::init() {}
00029 
00030 void ObstacleAvoiderOnGreenField::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 = 0, 
00056     targetX,
00057     psd = (sensorDataBuffer.lastFrame().data[SensorData::psd] / 1000) - 100.0; 
00058   
00059   int x, y, midX = image.cameraInfo.resolutionWidth / 2;
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     targetX = psd - (distanceToGround - distanceOffset - distanceControlInterval);
00067   //shouldn't this be = psp - distanceOffset - (distanceToGround) ?
00068 
00069   targetX *= maxSpeedX/distanceControlInterval;
00070   
00071   //OUTPUT(idText, text, "target: " << targetX << ", psd: " << psd << ", speedX: " << speedX.getVal() << ", P speed: " << speedX.getWeightP());
00072   
00073   
00074   /** calc how much green is in both halves */
00075   for(x = gridSize/2; x < midX; x += gridSize)
00076   {
00077     for(y = gridSize/2; y < image.cameraInfo.resolutionHeight; y += gridSize)
00078     {
00079       if (cameraImage.getClassifiedColor(x, y) == green)
00080       {
00081         greenLeft++;
00082         //cameraImage.image[y][2][x] = 255;
00083       }
00084       else
00085       {
00086         //cameraImage.image[y][0][x] = 0;
00087       }
00088 
00089       if (cameraImage.getClassifiedColor(x + midX, y) == green)
00090       {
00091         greenRight++;
00092         //cameraImage.image[y][1][x + midX] = 255;
00093       }
00094       else 
00095       {
00096         //cameraImage.image[y][0][x + midX] = 255;
00097       }
00098     }
00099   }
00100 
00101   //INIT_DEBUG_IMAGE(classificationY, cameraImage);
00102   //SEND_DEBUG_IMAGE(classificationY);
00103   OUTPUT(idText, text, " " << greenLeft << " : " << greenRight); 
00104 
00105   sum = greenLeft + greenRight;
00106   if (sum == 0) sum = 1;
00107   
00108   /** if robot is moving forward, turn where there's more
00109   *   green
00110   */
00111   if (speedX.getVal() > .7 * maxSpeedX)
00112   {
00113     targetPhi = 1.2 * (greenLeft - greenRight) / sum;
00114     //targetPhi *= .5;
00115   }
00116   /** if robot is too close to something, stick to the
00117   * direction it last turned to until obstacle is no
00118   * longer in front of robot
00119   */
00120   else 
00121   {
00122     if (speedPhi.getVal() < 0)
00123       targetPhi = -pi;
00124     else if (speedPhi.getVal() > 0)
00125       targetPhi = pi;
00126   }
00127   
00128   if (greenLeft > greenRight) 
00129   {
00130     ledRequest.redBottomLEDs = LEDRequest::leftOnly;
00131   }
00132   else if (greenLeft < greenRight)
00133   {
00134     ledRequest.redBottomLEDs = LEDRequest::rightOnly;
00135   }
00136   else if (greenLeft == greenRight)
00137   {
00138     if (greenLeft == 0)
00139       ledRequest.redBottomLEDs = LEDRequest::bothOff;
00140     else
00141       ledRequest.redBottomLEDs = LEDRequest::bothOn;
00142   }
00143   else 
00144     ledRequest.redBottomLEDs = LEDRequest::bothOff;
00145       
00146   ledRequest.redTopLEDs = LEDRequest::bothOff;
00147   
00148   if(sensorDataBuffer.lastFrame().data[SensorData::tailPan] > 0)
00149   {
00150     speedPhi.approximateVal(targetPhi);
00151     speedX.approximateVal(targetX);
00152     timeToCalibrate = 0;
00153     soundRequest.soundID = SoundRequest::okay;
00154     headControlMode.headControlMode = HeadControlMode::lookStraightAhead;
00155   }
00156   else //stop
00157   {
00158     headControlMode.headControlMode = HeadControlMode::lookStraightAhead;
00159     if (SystemCall::getTimeSince(timeToCalibrate) < 2000)
00160       soundRequest.soundID = SoundRequest::notokay;
00161     else 
00162       soundRequest.soundID = SoundRequest::none;
00163     speedPhi.approximateVal(0);
00164     speedX.approximateVal(0);
00165     if (timeToCalibrate > 0)
00166     {
00167       ledRequest.redTopLEDs = LEDRequest::bothOn;
00168       if (SystemCall::getTimeSince(timeToCalibrate) > 2000) // wait for one sec to allow motion to subside
00169       {
00170 /*        if(robotState.getSwitches() == RobotState::headFrontDown)
00171         {
00172           soundRequest.soundID = SoundRequest::rob005;
00173           calibrate();
00174         }
00175 */
00176       }
00177     }
00178     else
00179     {
00180       timeToCalibrate = SystemCall::getCurrentSystemTime();
00181       ledRequest.redTopLEDs = LEDRequest::bothOff;
00182     }
00183   }
00184      
00185   motionRequest.tailRequest.tailRequestID = TailRequest::twoPositionSwitchHorizontal;
00186   motionRequest.motionType = MotionRequest::walk;
00187   motionRequest.walkRequest.walkType = WalkRequest::normal;
00188 
00189   /* set the motion request */
00190   motionRequest.motionType=MotionRequest::walk;
00191   if (fabs(speedX.getVal()) > 1)
00192     motionRequest.walkRequest.walkParams.translation.x = speedX.getVal();
00193   else  
00194     motionRequest.walkRequest.walkParams.translation.x = 0.0;
00195   if (fabs(speedY.getVal()) > 1)
00196     motionRequest.walkRequest.walkParams.translation.y = targetY;
00197   else 
00198     motionRequest.walkRequest.walkParams.translation.y = 0.0;
00199   if (fabs(speedPhi.getVal()) > .05)
00200     motionRequest.walkRequest.walkParams.rotation = speedPhi.getVal();
00201   else 
00202     motionRequest.walkRequest.walkParams.rotation = 0.0;
00203 }
00204 
00205 
00206 
00207 
00208 void ObstacleAvoiderOnGreenField::calibrate()
00209 {
00210   //distanceToGround = (sensorDataBuffer.lastFrame().data[SensorData::psd] / 1000) - 100;
00211 
00212   localColTable.clear();
00213 
00214   int x, y;
00215 
00216   for(x = gridSize/2; x < image.cameraInfo.resolutionWidth; x += gridSize)
00217   {
00218     for(y = gridSize/2; y < image.cameraInfo.resolutionHeight; y += gridSize)
00219     {
00220       localColTable.addColorClass(green, image.image[y][0][x], image.image[y][1][x], image.image[y][2][x], 2);
00221     }
00222   }
00223 
00224   localColTableInitialized = true;
00225 }
00226 
00227 
00228 
00229 bool ObstacleAvoiderOnGreenField::handleMessage(InMessage& message)
00230 {
00231   bool handled = false;
00232   
00233   switch(message.getMessageID())
00234   {
00235   case idGenericDebugData:
00236     GenericDebugData d;
00237     message.bin >> d;
00238     if(d.id == GenericDebugData::braitenbergPIDs)
00239     {
00240       OUTPUT(idText,text,"generic debug message (Braitenberg) handled by module ObstacleAvoiderOnGreenField");
00241       
00242       speedX.setWeightP(d.data[0]);
00243       speedX.setWeightI(d.data[1]);
00244       speedX.setWeightD(d.data[2]);
00245       
00246       speedPhi.setWeightP(d.data[3]);
00247       speedPhi.setWeightI(d.data[4]);
00248       speedPhi.setWeightD(d.data[5]);
00249       
00250       distanceOffset = d.data[6];
00251       distanceControlInterval = d.data[7];
00252       distanceToGround = d.data[8];
00253     }
00254     handled = true;
00255     break;
00256 
00257   }
00258   return handled;
00259 }
00260 
00261 
00262 /*
00263 * Change log :
00264 * 
00265 * $Log: ObstacleAvoiderOnGreenField.cpp,v $
00266 * Revision 1.4  2004/06/02 17:18:22  spranger
00267 * MotionRequest cleanup
00268 *
00269 * Revision 1.3  2004/05/24 14:15:49  juengel
00270 * New button evaluation.
00271 *
00272 * Revision 1.2  2004/05/23 12:08:26  loetzsch
00273 * clean up in class HeadControlMode
00274 *
00275 * Revision 1.1.1.1  2004/05/22 17:20:52  cvsadm
00276 * created new repository GT2004_WM
00277 *
00278 * Revision 1.9  2004/03/19 19:03:49  roefer
00279 * Warnings removed
00280 *
00281 * Revision 1.8  2004/03/17 10:22:55  jhoffman
00282 * added ERS7 support, removed some image size related bugs
00283 *
00284 * Revision 1.7  2004/03/08 02:11:49  roefer
00285 * Interfaces should be const
00286 *
00287 * Revision 1.6  2004/01/12 16:00:47  jhoffman
00288 * *** empty log message ***
00289 *
00290 * Revision 1.5  2003/12/31 23:50:39  roefer
00291 * Removed inclusion of RobotDimensions.h because it is not used
00292 *
00293 * Revision 1.4  2003/12/15 11:49:06  juengel
00294 * Introduced CameraInfo
00295 *
00296 * Revision 1.3  2003/12/09 16:28:15  jhoffman
00297 * - added sounds to obstacle avoider on green field
00298 * - added calibration mode for obstacle avoider on green field
00299 *
00300 * Revision 1.2  2003/11/22 09:33:01  jhoffman
00301 * added "Yet Another Inv Kin Walking Engine"
00302 * experimental stage, doesn't do much yet
00303 * no Fourier inside!
00304 *
00305 * Revision 1.1  2003/10/06 14:10:15  cvsadm
00306 * Created GT2004 (M.J.)
00307 *
00308 * Revision 1.2  2003/09/26 11:38:52  juengel
00309 * - sorted tools
00310 * - clean-up in DataTypes
00311 *
00312 * Revision 1.1.1.1  2003/07/02 09:40:24  cvsadm
00313 * created new repository for the competitions in Padova from the 
00314 * tamara CVS (Tuesday 2:00 pm)
00315 *
00316 * removed unused solutions
00317 *
00318 * Revision 1.9  2003/06/27 13:22:52  jhoffman
00319 * minor note
00320 *
00321 * Revision 1.8  2003/06/11 16:10:31  dueffert
00322 * some cleanup
00323 *
00324 * Revision 1.7  2003/05/02 18:15:18  risler
00325 * SensorDataBuffer added
00326 * replaced SensorData with SensorDataBuffer
00327 * full SensorData resolution now accessible
00328 *
00329 * Revision 1.6  2003/03/05 15:27:28  jhoffman
00330 * no message
00331 *
00332 * Revision 1.5  2003/02/21 18:32:04  roefer
00333 * pColorTable -> colorTable finished
00334 *
00335 * Revision 1.4  2003/02/18 21:29:17  osterhues
00336 * Changed all instances of ColorTable64 to new base class ColorTable
00337 *
00338 * Revision 1.3  2003/02/17 10:55:08  dueffert
00339 * greenhills backport
00340 *
00341 * Revision 1.2  2003/02/02 23:29:21  jhoffman
00342 * adjustments for it to work reliably and with any walking engine
00343 *
00344 * Revision 1.1  2003/01/30 18:26:40  jhoffman
00345 * added ObstacleAvoider
00346 *
00347 
00348 */

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