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

Modules/SensorBehaviorControl/BB2004Calibrator.cpp

Go to the documentation of this file.
00001 /**
00002 * @file BB2004Calibrator.cpp
00003 *
00004 * Implementation of class BB2004Calibrator.
00005 *
00006 * @author <a href="mailto:roefer@tzi.de">Thomas Röfer</a>
00007 */
00008 
00009 #include "BB2004Calibrator.h"
00010 #include "Tools/Actorics/Kinematics.h"
00011 #include "Representations/Perception/BodyPosture.h"
00012 
00013 void CalibrationIndividual::init(const BBIndividual&)
00014 {
00015 //  ASSERT(sizeof(RobotCalibration) / sizeof(double) == numOfGenes);
00016   memcpy(&genes[0], (const void*) &getRobotConfiguration().getRobotCalibration(), sizeof(RobotCalibration));
00017   fitness = 1; 
00018   mutate();
00019 }
00020   
00021 void CalibrationIndividual::interpolate(const BBIndividual& other)
00022 {
00023   const CalibrationIndividual& o = (const CalibrationIndividual&) other;
00024   for(int i = 0; i < numOfGenes; ++i)
00025   {
00026     double f = random(),
00027           f1 = f * fitness,
00028           f2 = (1 - f) * o.fitness;
00029     f = f1 / (f1 + f2);
00030     genes[i] = genes[i] * f + o.genes[i] * (1 - f);
00031   }
00032 }
00033 
00034 void CalibrationIndividual::mutate()
00035 {
00036   for(int i = 0; i < numOfGenes; ++i)
00037     genes[i] += 0.1 * (-0.5 + random());
00038 }
00039 
00040 void CalibrationIndividual::extrapolate(const BBIndividual& other)
00041 {
00042   const CalibrationIndividual& o = (const CalibrationIndividual&) other;
00043   for(int i = 0; i < numOfGenes; ++i)
00044   {
00045     double f = random(),
00046            f1 = f * fitness,
00047            f2 = (1 - f) * o.fitness;
00048     f = f1 / (f1 + f2) * 2;
00049     if(f < 1) // other dominates
00050       genes[i] = o.genes[i] + (o.genes[i] - genes[i]) * (1 - f);
00051     else // this dominates
00052       genes[i] += (genes[i] - o.genes[i]) * (f - 1);
00053   }
00054 }
00055 
00056 void CalibrationIndividual::select() const
00057 {
00058   RobotCalibration& r = const_cast<RobotCalibration&>((const RobotCalibration&) genes[0]);
00059   /*r.bodyTiltOffset = 0.00454082;
00060   r.bodyRollOffset = 0.0243264;
00061   r.neckTiltOffset = 0.0188557;
00062   r.headTiltOffset = -0.0326878;*/
00063   r.tiltFactor = r.panFactor = r.tilt2Factor = 1;
00064   getRobotConfiguration().setRobotCalibration(r);
00065   //getRobotConfiguration().setRobotCalibration((const RobotCalibration&) genes[0]);
00066 }
00067 
00068 void CalibrationIndividual::dump() const
00069 {
00070   for(int i = 0; i < numOfGenes; ++i)
00071     getDebugOut().text << " " << genes[i];
00072   getDebugOut().finishMessage(idText);
00073 }
00074 
00075 BB2004Calibrator::BB2004Calibrator(const SensorBehaviorControlInterfaces& interfaces)
00076 : SensorBehaviorControl(interfaces),
00077   population(numOfIndividuals, CalibrationIndividual()),
00078   imageProcessor(ImageProcessorInterfaces(
00079     imageBuffer,
00080     cameraMatrix,
00081     const_cast<ColorTable&>(colorTable),
00082     robotPose,
00083     ballModel,
00084     playerPoseCollection,
00085     robotState,
00086     calibrationRequest,
00087     landmarksPercept,
00088     ballPercept,
00089     linesPercept,
00090     edgesPercept,
00091     playersPercept,
00092     obstaclesPercept,
00093     specialPercept)),
00094   evolutions(-1)
00095 {
00096   for(int i = 0; i < numOfIndividuals; ++i)
00097     count[i] = 0;
00098 }
00099 
00100 void BB2004Calibrator::execute()
00101 {
00102   int i;
00103   for(i = 0; i < sensorDataBuffer.numOfFrames; ++i)
00104     buffer.add(sensorDataBuffer.frame[i]);
00105  
00106   for(i = 0; i < buffer.getNumberOfEntries(); ++i)
00107     if(buffer[i].frameNumber == imageBuffer.frameNumber)
00108       break;
00109 
00110   if(evolutions < 0)
00111   {
00112     if(--evolutions == 
00113 #ifdef _WIN32
00114       -2
00115 #else
00116       -300
00117 #endif
00118       ) // wait 300 images and let the robot stand up
00119     {
00120       timeStamp = SystemCall::getCurrentSystemTime();
00121       evolutions = 0;
00122     }
00123   }
00124   else if(i < buffer.getNumberOfEntries() && image.frameNumber != imageBuffer.frameNumber)
00125     evolve(buffer[i]);
00126 
00127   imageBuffer = image;
00128   motionRequest.motionType = MotionRequest::walk;
00129   motionRequest.walkRequest.walkType = WalkRequest::normal;
00130   motionRequest.walkRequest.walkParams = Pose2D();
00131   motionRequest.tailRequest.tailRequestID = TailRequest::noTailWag;
00132   headControlMode.headControlMode = HeadControlMode::calibrate;
00133 }
00134 
00135 void BB2004Calibrator::evolve(const SensorData& sensorData)
00136 {
00137   int i;
00138   for(i = 0; i < numOfIndividuals; ++i)
00139   {
00140     population[i].select();
00141     RobotVertices rob;
00142     Kinematics::calcNeckAndLegPositions(sensorData, rob);
00143     BodyPosture bodyPosture;
00144     bodyPosture.bodyTiltCalculatedFromLegSensors = rob.bodyTilt;
00145     bodyPosture.bodyRollCalculatedFromLegSensors = rob.bodyRoll;
00146     bodyPosture.neckHeightCalculatedFromLegSensors = rob.neckHeight;
00147     
00148     double tilt = fromMicroRad(sensorData.data[SensorData::neckTilt]);
00149     double pan  = fromMicroRad(sensorData.data[SensorData::headPan]);
00150     double roll = fromMicroRad(sensorData.data[SensorData::headTilt]);
00151     Kinematics::calculateCameraMatrix(tilt, pan, roll, bodyPosture.bodyTiltCalculatedFromLegSensors, bodyPosture.neckHeightCalculatedFromLegSensors, cameraMatrix);
00152     imageProcessor.execute();
00153     if(linesPercept.numberOfPoints[LinesPercept::border])
00154     {
00155       for(int j = 0; j < linesPercept.numberOfPoints[LinesPercept::border]; ++j)
00156       {
00157         Vector2<double> v(linesPercept.points[LinesPercept::border][j].x, linesPercept.points[LinesPercept::border][j].y);
00158         Vector2<double> vMin = observationTable[LinesPercept::border].getClosestPoint(v);
00159         if(vMin.x != 1000000)
00160           fitness[i] += v - vMin; //fabs(zModel - zObs) + fabs(yModel - yObs);
00161         else
00162           fitness[i] += Vector2<double>(10000,10000);
00163         ++count[i];
00164       }
00165     }
00166   }
00167 
00168     //OUTPUT(idText, text, sensorData.frameNumber);
00169   if(SystemCall::getTimeSince(timeStamp) > waitPerGeneration)
00170   {
00171     for(i = 0; i < numOfIndividuals; ++i)
00172     {
00173       population[i].setFitness(exp(-0.1 * (count[i] ? fitness[i].abs() / count[i] : 1)));
00174       fitness[i] = Vector2<double>();
00175       count[i] = 0;
00176     }
00177     const CalibrationIndividual& best = (const CalibrationIndividual&) population.getFittest();
00178     ++evolutions;
00179     OUTPUT(idText, text, "Generation " << evolutions << ", fitness " << best.getFitness() << ", genes:");
00180     best.dump();
00181 
00182     // interchangingly interpolate and extrapolate
00183     if(evolutions & 1)
00184     {
00185       population.interpolate();
00186       getDebugOut().text << "Interpolating:";
00187     }
00188     else
00189     {
00190       population.extrapolate();
00191       getDebugOut().text << "Extrapolating:";
00192     }
00193     
00194     // print statistics
00195     Statistics& s = population.getStatistics();
00196     int num = s.analyze();
00197     for(i = 0; i < num; ++i)
00198       getDebugOut().text << " " << s[i];
00199     getDebugOut().finishMessage(idText);
00200     timeStamp = SystemCall::getCurrentSystemTime();
00201   }
00202 }
00203 
00204 bool BB2004Calibrator::handleMessage(InMessage& message)
00205 {
00206   return false;
00207 }
00208 
00209 /*
00210 * Change log :
00211 * 
00212 * $Log: BB2004Calibrator.cpp,v $
00213 * Revision 1.7  2004/06/15 10:58:27  thomas
00214 * added edge-specialist, edges-percept, debug-drawings etc. (not yet called from image-processor)
00215 *
00216 * Revision 1.6  2004/06/02 17:18:22  spranger
00217 * MotionRequest cleanup
00218 *
00219 * Revision 1.5  2004/05/27 19:38:06  loetzsch
00220 * compilation fixed
00221 *
00222 * Revision 1.4  2004/05/27 17:13:37  jhoffman
00223 * - renaming: tilt1 -> neckTilt,  pan -> headPan,  tilt2 -> headTilt
00224 * - clipping included for setJoints
00225 * - removed some microrad/rad-bugs
00226 * - bodyPosture constructor and "=" operator fixed
00227 *
00228 * Revision 1.3  2004/05/26 17:21:47  dueffert
00229 * better data types used
00230 *
00231 * Revision 1.2  2004/05/22 22:52:02  juengel
00232 * Renamed ballP_osition to ballModel.
00233 *
00234 * Revision 1.1.1.1  2004/05/22 17:20:51  cvsadm
00235 * created new repository GT2004_WM
00236 *
00237 * Revision 1.8  2004/04/09 11:35:51  roefer
00238 * Bremen Byters German Open check-in
00239 *
00240 * Revision 1.7  2004/03/20 09:55:25  roefer
00241 * Preparation for improved odometry
00242 *
00243 * Revision 1.6  2004/03/13 17:20:42  roefer
00244 * Different parameters for simulation and real robot
00245 *
00246 * Revision 1.5  2004/03/13 17:14:36  roefer
00247 * Different parameters for simulation and real robot
00248 *
00249 * Revision 1.4  2004/03/11 00:06:18  roefer
00250 * Parameter tuning
00251 *
00252 * Revision 1.3  2004/03/10 07:59:26  roefer
00253 * New head control mode
00254 *
00255 * Revision 1.2  2004/03/08 02:11:49  roefer
00256 * Interfaces should be const
00257 *
00258 * Revision 1.1  2004/03/04 23:00:54  roefer
00259 * Added (so far empty) BB2004Calibrator
00260 *
00261 */

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