00001
00002
00003
00004
00005
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
00033
00034
00035
00036
00037
00038
00039
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)
00064 targetX = 0.0;
00065 else
00066 targetX = psd - (distanceToGround - distanceOffset - distanceControlInterval);
00067
00068
00069 targetX *= maxSpeedX/distanceControlInterval;
00070
00071
00072
00073
00074
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
00083 }
00084 else
00085 {
00086
00087 }
00088
00089 if (cameraImage.getClassifiedColor(x + midX, y) == green)
00090 {
00091 greenRight++;
00092
00093 }
00094 else
00095 {
00096
00097 }
00098 }
00099 }
00100
00101
00102
00103 OUTPUT(idText, text, " " << greenLeft << " : " << greenRight);
00104
00105 sum = greenLeft + greenRight;
00106 if (sum == 0) sum = 1;
00107
00108
00109
00110
00111 if (speedX.getVal() > .7 * maxSpeedX)
00112 {
00113 targetPhi = 1.2 * (greenLeft - greenRight) / sum;
00114
00115 }
00116
00117
00118
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
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)
00169 {
00170
00171
00172
00173
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
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
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
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348