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

Modules/SensorBehaviorControl/SimpleMotionRecognition.cpp

Go to the documentation of this file.
00001 /**
00002 * @file SimpleMotionRecognition.cpp
00003 *
00004 * Implementation of class SimpleMotionRecognition
00005 *
00006 * @author <a href="mailto:ordyniak@informatik.hu-berlin.de">Sebastian Ordyniak</a>
00007 * @author <a href="mailto:richert@informatik.hu-berlin.de">Marten Richert</a>
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 //vergleiche previousOdometry und odometryData
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 //    else
00065 //      wait = WAITMIN;
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(/*atan*/((double)point.y / halfCameraResolutionHeigth) * halfCameraOpeningAngleHeigth);
00075   pan = -toMicroRad(/*atan*/((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           //headControlMode.directTilt = newHeadPos.x;
00137           //headControlMode.directPan = newHeadPos.y;
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 * Change log :
00170 * 
00171 * $Log: SimpleMotionRecognition.cpp,v $
00172 * Revision 1.2  2004/05/23 12:08:26  loetzsch
00173 * clean up in class HeadControlMode
00174 *
00175 * Revision 1.1.1.1  2004/05/22 17:20:53  cvsadm
00176 * created new repository GT2004_WM
00177 *
00178 * Revision 1.8  2004/03/08 02:11:50  roefer
00179 * Interfaces should be const
00180 *
00181 * Revision 1.7  2004/01/13 02:17:01  richert
00182 * finetuning SimpleMotionDetector
00183 *
00184 * Revision 1.6  2004/01/10 17:03:09  roefer
00185 * Warnings removed
00186 *
00187 * Revision 1.5  2004/01/10 10:11:36  roefer
00188 * Corrected
00189 *
00190 * Revision 1.4  2004/01/09 20:09:50  richert
00191 * some improovements
00192 *
00193 * Revision 1.3  2004/01/09 16:57:35  loetzsch
00194 * now also compiles under vc .NET
00195 *
00196 * Revision 1.2  2004/01/08 22:01:26  richert
00197 * warning removed
00198 *
00199 * Revision 1.1  2004/01/08 21:51:40  richert
00200 * beginning of "old" SimpleMotionRecognition
00201 *
00202 */

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