00001 00002 #include "Robots/BeoHawk/vision/VisionBrainComponentI.H" 00003 #include "Component/ModelParam.H" 00004 #include "Component/ModelOptionDef.H" 00005 00006 #ifndef VISIONBRAINCOMPONENTI_C 00007 #define VISIONBRAINCOMPONENTI_C 00008 00009 const ModelOptionCateg MOC_VisionBrainComponent = { 00010 MOC_SORTPRI_3, "VisionBrainComponent Related Options" }; 00011 00012 const ModelOptionDef OPT_CameraSource = 00013 { MODOPT_ARG(std::string), "CameraSource", &MOC_VisionBrainComponent, OPTEXP_CORE, 00014 "The string description to be matched against incoming Retina messages\n" 00015 "when using Ice as an input source", 00016 "camera-source", '\0', "<string>", "Camera0" }; 00017 00018 const ModelOptionDef OPT_DesiredFramerate = 00019 { MODOPT_ARG(int), "ProcessFPS", &MOC_VisionBrainComponent, OPTEXP_CORE, 00020 "The desired framerate at which the component processes incoming images", 00021 "process-fps", '\0', "<int>", "-1" }; 00022 00023 // ###################################################################### 00024 VisionBrainComponentI::VisionBrainComponentI(OptionManager& mgr, 00025 const std::string& descrName, const std::string& tagName) : 00026 RobotBrainComponent(mgr, descrName, tagName), 00027 itsFrameCount(0), 00028 desiredFramerate(&OPT_DesiredFramerate, this, 0), 00029 itsCameraSource(&OPT_CameraSource, this, 0), 00030 itsCurrentImgFwdCam(false), 00031 timer(1000), 00032 lastFrameCount(-1) 00033 { 00034 } 00035 00036 // ###################################################################### 00037 VisionBrainComponentI::~VisionBrainComponentI() 00038 { 00039 } 00040 00041 void VisionBrainComponentI::registerVisionTopics() 00042 { 00043 LINFO("Registering VisionBrainComponent Messages/Subscriptions"); 00044 this->registerSubscription("RetinaMessageTopic"); 00045 } 00046 00047 void VisionBrainComponentI::registerVisionPublisher(const std::string& MessageTopic) 00048 { 00049 this->registerPublisher(MessageTopic); 00050 registerVisionTopics(); 00051 } 00052 00053 bool VisionBrainComponentI::publishVisionMsg(const::std::string& MessageTopic, RobotSimEvents::EventMessagePtr msg) 00054 { 00055 return this->publish(MessageTopic,msg); 00056 } 00057 00058 void VisionBrainComponentI::evolve() 00059 { 00060 int framerate = desiredFramerate.getVal(); 00061 00062 if (framerate == 0) 00063 usleep(ZERO_RATE_WAIT_TIME*1000); 00064 else { 00065 if (itsCurrentImg.initialized() && itsFrameCount != lastFrameCount) { 00066 lastFrameCount = itsFrameCount; 00067 updateFrame(itsCurrentImg, itsCurrentImgFwdCam); 00068 updateFrame(itsCurrentImg, itsCurrentCameraID); 00069 } 00070 int ms_left = (1000/framerate) - (int) timer.get(); 00071 if (framerate > 0 && ms_left > 0) 00072 usleep((ms_left)*1000); 00073 } 00074 00075 //There used to be an ifs hack here. It is gone now. If you want to read from an ifs 00076 // then just start-up a retina which reads from the stream. This is now much cleaner 00077 // and properly structured. Also, if you want your inherited class to write to an ofs 00078 // then include the ofs in that class, not here, as many of the classes which inherit 00079 // this one have no need for an ofs. 00080 00081 } 00082 00083 // ###################################################################### 00084 void VisionBrainComponentI::updateMessage(const RobotSimEvents::EventMessagePtr& eMsg, 00085 const Ice::Current&) 00086 { 00087 //Get a retina message 00088 if(eMsg->ice_isA("::RobotSimEvents::RetinaMessage")) 00089 { 00090 RobotSimEvents::RetinaMessagePtr retinaMessage = RobotSimEvents::RetinaMessagePtr::dynamicCast(eMsg); 00091 int framerate = desiredFramerate.getVal(); 00092 00093 //if our FPS == -1, we always process 00094 bool processMessage = (framerate == -1); 00095 //if our FPS > -1, then we process only when the timers run out 00096 if (framerate > 0 && timer.get() > (unsigned int) (1000/framerate)) { 00097 timer.reset(); 00098 processMessage = true; 00099 } 00100 00101 if (processMessage) 00102 { 00103 itsImgMutex.lock(); 00104 if(retinaMessage->cameraID == itsCameraSource.getVal() || itsCameraSource.getVal() == "all") 00105 { 00106 Image<byte > retinaImage = Ice2Image<byte >(retinaMessage->img); 00107 itsCurrentImg = retinaImage; 00108 itsCurrentCameraID = retinaMessage->cameraID; 00109 00110 if(retinaMessage->cameraID == "FwdCamera") 00111 itsCurrentImgFwdCam = true; 00112 else 00113 itsCurrentImgFwdCam = false; 00114 00115 itsFrameCount++; 00116 } 00117 itsImgMutex.unlock(); 00118 } 00119 } 00120 } 00121 00122 #endif