00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "SimpleMotionRecognition.h"
00011
00012 SimpleMotionRecognition::SimpleMotionRecognition(const SensorBehaviorControlInterfaces& interfaces)
00013 : SensorBehaviorControl(interfaces)
00014 {
00015 currentImage = 0;
00016 start = 1;
00017 imagesLeft = SIMPLEIMAGEBUFFERSIZE;
00018 motion = 0;
00019 wait = WAITMIN;
00020 halfCameraResolutionHeigth = image.cameraInfo.resolutionHeight / 2;
00021 halfCameraResolutionWidth = image.cameraInfo.resolutionWidth / 2;
00022 halfCameraOpeningAngleHeigth = image.cameraInfo.openingAngleHeight / 2.0;
00023 halfCameraOpeningAngleWidth = image.cameraInfo.openingAngleWidth / 2.0;
00024 reset = RESETCONST;
00025 searchStatus=0;
00026 }
00027
00028 int SimpleMotionRecognition::searchTilt() {
00029 if (searchStatus==-1)
00030 return headControlMode.directTilt/2;
00031 else
00032 return 0;
00033 }
00034
00035 int SimpleMotionRecognition::searchPan() {
00036 int back = 0;
00037 switch (searchStatus)
00038 {
00039 case -1: back = headControlMode.directPan/2; wait = WAITMIN*5/4; break;
00040 case 0: back = 0; wait = WAITMIN*3/2; break;
00041 case 1: back = -585390; wait = WAITMIN*5/4; break;
00042 case 2: back = -1370790; wait = WAITMIN*5/4; break;
00043 case 3: back = 0; wait = WAITMIN*3/2; break;
00044 case 4: back = 585390; wait = WAITMIN*5/4; break;
00045 case 5: back = 1370790; wait = WAITMIN*5/4; break;
00046 }
00047 searchStatus = (searchStatus+1)%6;
00048 return back;
00049 }
00050
00051 bool SimpleMotionRecognition::headIsNotInMotion() {
00052
00053 if (previousCameraMatrix == cameraMatrix) {
00054 if (wait>0)
00055 wait -= frameDiff;
00056 }
00057 else
00058 if (motion) {
00059 if (abs(motion[1].x) > abs(motion[1].y))
00060 wait = WAITMIN + abs(motion[1].x)*WAITCONST/toMicroRad(halfCameraOpeningAngleWidth);
00061 else
00062 wait = WAITMIN + abs(motion[1].y)*WAITCONST/toMicroRad(halfCameraOpeningAngleHeigth);
00063 }
00064
00065
00066
00067 return (wait<=0);
00068 }
00069
00070 Vector2<long> SimpleMotionRecognition::getAngleYZ(Vector2<int> point) {
00071 long tilt, pan;
00072 point = point - Vector2<int>(halfCameraResolutionWidth,halfCameraResolutionHeigth);
00073 point.y = -point.y;
00074 tilt = toMicroRad(((double)point.y / halfCameraResolutionHeigth) * halfCameraOpeningAngleHeigth);
00075 pan = -toMicroRad(((double)point.x/ halfCameraResolutionWidth) * halfCameraOpeningAngleWidth);
00076 return Vector2<long>(tilt, pan);
00077 }
00078
00079 Vector2<int> *SimpleMotionRecognition::getMotion() {
00080 int diff[2] = {0,0};
00081 int BPscount[2] = {0,0};
00082 Vector2<int> BPs[2] = { Vector2<int>(0,0), Vector2<int>(0,0) };
00083
00084
00085 for (int y = 0;y < imageBuffer[currentImage].cameraInfo.resolutionHeight;y+=3) {
00086 for (int x = 0;x < imageBuffer[currentImage].cameraInfo.resolutionWidth;x+=5) {
00087 for (int i=0;i < 2;i++)
00088 diff[i] = abs(imageBuffer[(currentImage+SIMPLEIMAGEBUFFERSIZE-2)%SIMPLEIMAGEBUFFERSIZE].image[y][0][x]
00089 - imageBuffer[(currentImage+SIMPLEIMAGEBUFFERSIZE-1+i)%SIMPLEIMAGEBUFFERSIZE].image[y][0][x])
00090 > MINDIFF;
00091
00092 if (diff[0] && diff[1]) {
00093 BPscount[0]++;
00094 BPs[0] += Vector2<int>(x,y);
00095 } else {
00096 if (!diff[0] && diff[1]) {
00097 BPscount[1]++;
00098 BPs[1] += Vector2<int>(x,y);
00099 }
00100 }
00101 }
00102 }
00103 if (BPscount[0] && BPscount[1]) {
00104 Vector2<int> *back = new Vector2<int>[2];
00105 back[0] = BPs[0] / BPscount[0];
00106 back[1] = BPs[1] / BPscount[1];
00107 return back;
00108 }
00109 return 0;
00110 }
00111
00112 void SimpleMotionRecognition::execute()
00113 {
00114
00115 if (start) {
00116 start = 0;
00117 headControlMode.headControlMode = HeadControlMode::direct;
00118 headControlMode.directTilt = 0;
00119 headControlMode.directPan = 0;
00120 headControlMode.directRoll = 0;
00121 motionRequest.motionType = MotionRequest::getup;
00122 } else {
00123 imageBuffer[currentImage] = image;
00124 frameDiff = (int)(imageBuffer[currentImage].frameNumber - imageBuffer[(currentImage+1)%SIMPLEIMAGEBUFFERSIZE].frameNumber);
00125 if (!imagesLeft) {
00126 if (headIsNotInMotion()) {
00127 motion = getMotion();
00128 if (motion) {
00129 motion[1].x=motion[1].x + (motion[1].x-motion[0].x)*frameDiff*SPEEDFAKTOR.x/100;
00130 motion[1].y=motion[1].y + (motion[1].y-motion[0].y)*frameDiff*SPEEDFAKTOR.y/100;
00131 ARROW(sketch, motion[0].x, motion[0].y, motion[1].x, motion[1].y, 3, Drawings::ps_solid, Drawings::yellow);
00132 newHeadPos = getAngleYZ(motion[1]);
00133 headControlMode.headControlMode = HeadControlMode::direct;
00134 headControlMode.directTilt = headControlMode.directTilt + newHeadPos.x;
00135 headControlMode.directPan = headControlMode.directPan + newHeadPos.y;
00136
00137
00138 searchStatus=-1;
00139 reset = RESETCONST/2;
00140 }
00141 else {
00142 reset -= frameDiff;
00143 if(reset<=0) {
00144 headControlMode.headControlMode = HeadControlMode::direct;
00145 headControlMode.directTilt = searchTilt();
00146 headControlMode.directPan = searchPan();
00147 headControlMode.directRoll = 0;
00148 reset = RESETCONST;
00149 }
00150 }
00151 }
00152 }
00153 else {
00154 imagesLeft--;
00155 }
00156 ++currentImage %= (SIMPLEIMAGEBUFFERSIZE);
00157 }
00158 previousCameraMatrix = cameraMatrix;
00159 DEBUG_DRAWING_FINISHED(sketch);
00160 }
00161
00162 bool SimpleMotionRecognition::handleMessage(InMessage& message)
00163 {
00164 bool handled = true;
00165 return handled;
00166 }
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202