00001
00002
00003
00004
00005
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
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,
00056 targetX,
00057 psd = (sensorDataBuffer.lastFrame().data[SensorData::headPsdFar] / 1000) - 100.0;
00058
00059 int x, y, midX = image.cameraInfo.resolutionWidth / 2;
00060
00061 distanceToGround = 350;
00062
00063
00064
00065
00066
00067
00068 targetX = psd - 200;
00069
00070
00071
00072
00073 if (sensorDataBuffer.lastFrame().data[SensorData::bodyPsd] == 100000)
00074 {
00075 targetX = -100;
00076 }
00077
00078
00079
00080
00081
00082
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
00091 }
00092 else
00093 {
00094
00095 }
00096
00097 if (cameraImage.getClassifiedColor(x + midX, y) == green)
00098 {
00099 greenRight++;
00100
00101 }
00102 else
00103 {
00104
00105 }
00106 }
00107 }
00108
00109
00110
00111
00112
00113 sum = greenLeft + greenRight;
00114 if (sum == 0) sum = 1;
00115
00116
00117
00118
00119 if (speedX.getVal() > .7 * maxSpeedX)
00120 {
00121 targetPhi = 1.2 * (greenLeft - greenRight) / sum;
00122
00123 }
00124
00125
00126
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
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)
00177 {
00178
00179
00180
00181
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
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
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
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