/home/fwph/code/wurde/rde/core/capabilities/VisionControl.cpp

Go to the documentation of this file.
00001 
00029 #include <VisionControl.H>
00030 #include <CommsManager.H>
00031 #include <Logger.H>
00032 
00033 // $Id: Type.cpp 68 2007-02-01 17:56:57Z fwph $
00034 
00035 using namespace WURDE;
00036 
00037 VisionControl::VisionControl(const std::string & streamname){
00038        init(streamname,STRAT_NORMAL,"Any");
00039 
00040        //global name will be set later
00041        m_haveGlobalname=false;
00042 }
00043 
00044 VisionControl::VisionControl(const ConnectionStrategy & strategy,const std::string & gblName){
00045        init("Any",strategy,gblName);
00046 
00047        if(strategy==STRAT_NORMAL){
00048               //then the global name really specifies the streamname,
00049               // and the global name should be assigned automagically once we have
00050               // confirmation that the stream has been started
00051               m_haveGlobalname=false;   
00052        }
00053 
00054        m_haveStream=false;
00055        
00056 }
00057 
00058 VisionControl::VisionControl(const std::string &streamname,const std::string &gblName){
00059        init(streamname,STRAT_NORMAL,gblName);
00060 }
00061 
00062 void VisionControl::init(const std::string &streamname,const ConnectionStrategy &strategy,const std::string &gblName){
00063   m_globalname=gblName;
00064   m_streamname=streamname;
00065 
00066   myCOMObject=NULL;
00067   
00068   //  data.source=streamname.c_str();
00069   requests=info;
00070 
00071   m_queue=false;
00072   m_supplier=false;
00073   m_doTimestamp=true;
00074   m_doTag=true;
00075 
00076   m_doPublishData=false;
00077   m_doPublishInfo=false;
00078 
00079   m_initialize=false;
00080   m_strategy=strategy;
00081 
00082   m_haveStream=true;
00083   m_haveGlobalname=true;
00084   m_doPublishData=false;
00085   m_doPublishInfo=false;
00086 
00087   m_doAutoPing=true;
00088 
00089   info.source=streamname;
00090   requests.source=m_globalname;
00091   info.target="Any";
00092   requests.target=streamname;
00093   info.timestamp.now();
00094   requests.timestamp.now();
00095 
00096   m_streamRequest=STAT_NULL;
00097   
00098   m_sourceModule="Unknown";
00099 }
00100 
00101 
00102 VisionControl::~VisionControl(){
00103   myManager=NULL;
00104   if(myCOMObject){
00105 
00106          //      delete myCOMObject;
00107   }
00108 }
00109 
00110 
00111 void VisionControl::activateSupplier(){
00112        if(m_haveStream){
00113               m_supplier=true;
00114               m_globalname=m_streamname;
00115               m_haveGlobalname=true;
00116               m_initialize=false;
00117               if(myCOMObject){
00118                       //                     myCOMObject=new CMUIPCRobotProtocol::CMUIPCVisionControl(m_globalname);
00119                      myCOMObject->setGlobalName(m_globalname);
00120                      myCOMObject->setStreamName(m_streamname);
00121                      myCOMObject->setInfo(&requests);
00122                      myCOMObject->setRequests(&info);
00123                      myCOMObject->setStaging(&data);
00124                      myCOMObject->activateSupplier();
00125                      myCOMObject->setAutoPing(m_doAutoPing);
00126                      myCOMObject->setInfoVector(&m_requestVector);
00127                      myCOMObject->setRequestVector(&m_infoVector);
00128                      myCOMObject->setDataVector(&m_dataVector);
00129 
00130                      data.source=m_streamname;
00131                      info.source=m_streamname;
00132 
00133                      if(m_queue){
00134                             myCOMObject->setQueueMode(true);
00135                      }
00136 
00137               }else{
00138                       g_fatal("Attempted to activateSupplier before setting a COMObject Please just use registerSupplier.");
00139                       exit(-1);
00140               }
00141         if(m_supplier){
00142                 info.options.setCMode(C_SUPPLIER);
00143         }else{
00144                 info.options.setCMode(C_CONSUMER);
00145         }
00146         if(m_supplier){
00147                 info.stopOperators.setCMode(C_SUPPLIER);
00148         }else{
00149                 info.stopOperators.setCMode(C_CONSUMER);
00150         }
00151         if(m_supplier){
00152                 info.visionOperators.setCMode(C_SUPPLIER);
00153         }else{
00154                 info.visionOperators.setCMode(C_CONSUMER);
00155         }
00156        }else{
00157               //this shouldn't happen, but there's no real problem with it.
00158               //it'd just be dumb.
00159               m_initialize=true;
00160               m_supplier=true;
00161        }
00162        
00163        requests=info;
00164 
00165 }
00166 
00167 void VisionControl::activateConsumer(){
00168        std::string localname;
00169        
00170        if(m_haveStream){
00171               m_initialize=false;
00172               m_supplier=false;
00173               if(myCOMObject&&myManager){
00174                       /* global name so we don't have conflicts with the naming service */
00175                       if(!m_haveGlobalname&&m_strategy==STRAT_NORMAL){
00176                               m_globalname=myManager->getName()+VISIONCONTROL+m_streamname+"Consumer";
00177                               m_haveGlobalname=true;
00178                       }
00179 
00180                      myCOMObject->setGlobalName(m_globalname);
00181                      myCOMObject->setStreamName(m_streamname);
00182                      requests.source=m_globalname;
00183                      data.source=m_streamname;
00184                      info.source=m_streamname;
00185                      myCOMObject->setInfo(&info);
00186                      myCOMObject->setRequests(&requests);
00187                      myCOMObject->setStaging(&data);
00188                      myCOMObject->setAutoPing(m_doAutoPing);
00189                      myCOMObject->activateConsumer();
00190                      myCOMObject->setInfoVector(&m_infoVector);
00191                      myCOMObject->setRequestVector(&m_requestVector);
00192                      myCOMObject->setDataVector(&m_dataVector);
00193 
00194                      if(m_queue){
00195                             myCOMObject->setQueueMode(true);
00196                      }
00197               }else{
00198                       g_fatal("Attempted to activateConsumer before setting a COMObject and CommsManager. Please use registerConsumer instead.");
00199                      exit(-1);
00200               }
00201 
00202         if(m_supplier){
00203                 info.options.setCMode(C_SUPPLIER);
00204         }else{
00205                 info.options.setCMode(C_CONSUMER);
00206         }
00207         if(m_supplier){
00208                 info.stopOperators.setCMode(C_SUPPLIER);
00209         }else{
00210                 info.stopOperators.setCMode(C_CONSUMER);
00211         }
00212         if(m_supplier){
00213                 info.visionOperators.setCMode(C_SUPPLIER);
00214         }else{
00215                 info.visionOperators.setCMode(C_CONSUMER);
00216         }
00217        }else{
00218               //no stream, so we can't connect yet. tell the system 
00219               //to initialize as soon as we've got one
00220               m_initialize=true;
00221        }
00222 
00223 }
00224 
00225 void VisionControl::runUpdate(){
00226        if(myCOMObject){
00227 
00228               myCOMObject->runUpdate(m_doPublishData,m_doPublishInfo);
00229               
00230               m_doPublishData=false;
00231               m_doPublishInfo=false;
00232               
00233        }
00234 }
00235 
00236 void VisionControl::setManager(CommsManager *myManager_){
00237 
00238   if(!myCOMObject){
00239     myManager=myManager_;
00240   }
00241 
00242 }
00243 
00244 void VisionControl::setQueueMode(const bool & val){
00245 
00246        if(!myCOMObject){
00247               m_queue=val;
00248        }else if(!m_queue&&val){
00249               m_queue=val;
00250               myCOMObject->setDataVector(&m_dataVector);
00251               myCOMObject->setQueueMode(true);
00252 
00253        }else if(!val&&m_queue){
00254               m_queue=val;
00255               myCOMObject->setQueueMode(false);
00256               while(!m_dataVector.empty()){
00257                      m_dataVector.pop();
00258               }
00259               while(!m_infoVector.empty()){
00260                      m_infoVector.pop();
00261               }
00262               while(!m_requestVector.empty()){
00263                      m_requestVector.pop();
00264               }
00265        }
00266 }
00267 
00268 bool VisionControl::newData(){
00269        if(m_queue){
00270               return !m_dataVector.empty();
00271        }else{
00272               if(myCOMObject){
00273                      return myCOMObject->newData();
00274               }
00275        }
00276 
00277        return false;
00278 }
00279 
00280 bool VisionControl::newInfo(){
00281        if(m_queue){
00282               if(m_supplier){
00283                      return !m_requestVector.empty();
00284               }
00285 
00286               return !m_infoVector.empty();
00287        }else{
00288               if(myCOMObject){
00289                      return myCOMObject->newInfo();
00290               }
00291        }
00292 
00293 
00294        return false;
00295 }
00296 
00297 void VisionControl::publishData(){
00298        if(m_supplier){
00299               if(m_doTimestamp){
00300                      data.timestamp.now();
00301               }
00302               if(m_doTag){
00303                      data.source=m_globalname;
00304               }
00305               if(m_queue){
00306                      VisionControlDataStruct temp;
00307                      temp.source=data.source;
00308 temp.timestamp=data.timestamp;
00309  
00310                      m_dataVector.push(temp);
00311                               //                     m_dataVector.push(data);
00312               }
00313               m_doPublishData=true;
00314        }
00315 }
00316 
00317 void VisionControl::publishInfo(){
00318        if(m_supplier){
00319 
00320               info.mType=MESSAGE_INFO;
00321               if(m_doTimestamp){
00322                      info.timestamp.now();
00323               }
00324 
00325               if(m_doTag){
00326                      info.source=m_globalname;
00327                      info.target="Any";
00328               }
00329 
00330               if(m_queue){
00331                      VisionControlInfoStruct temp;
00332                      
00333                     
00334                      temp.source=info.source;
00335 temp.target=info.target;
00336 temp.options=info.options;
00337 temp.timestamp=info.timestamp;
00338 temp.stopOperators=info.stopOperators;
00339 temp.visionOperators=info.visionOperators;
00340 temp.mType=info.mType;
00341 
00342                      m_infoVector.push(temp);
00343                     
00344                      //              m_infoVector.push(info);
00345               }
00346 
00347               m_doPublishInfo=true;
00348        }else{
00349 
00350               requests.mType=MESSAGE_REQUEST;
00351               
00352               if(m_doTimestamp){
00353                      requests.timestamp.now();
00354               }
00355 
00356               if(m_doTag){
00357                      requests.source=m_globalname;
00358                      requests.target=m_streamname;
00359               }
00360 
00361               if(m_queue){
00362                      VisionControlInfoStruct temp;
00363         if(m_supplier){
00364                 temp.options.setCMode(C_SUPPLIER);
00365         }else{
00366                 temp.options.setCMode(C_CONSUMER);
00367         }
00368         if(m_supplier){
00369                 temp.stopOperators.setCMode(C_SUPPLIER);
00370         }else{
00371                 temp.stopOperators.setCMode(C_CONSUMER);
00372         }
00373         if(m_supplier){
00374                 temp.visionOperators.setCMode(C_SUPPLIER);
00375         }else{
00376                 temp.visionOperators.setCMode(C_CONSUMER);
00377         }
00378                      temp.source=requests.source;
00379 temp.target=requests.target;
00380 temp.options=requests.options;
00381 temp.timestamp=requests.timestamp;
00382 temp.stopOperators=requests.stopOperators;
00383 temp.visionOperators=requests.visionOperators;
00384 temp.mType=requests.mType;
00385 
00386                      m_requestVector.push(temp);
00387 
00388                      //              m_requestVector.push(requests);
00389               }
00390 
00391               m_doPublishInfo=true;
00392        }
00393 }
00394 
00395 VisionControlDataStruct VisionControl::getNextData(){
00396        if(m_queue){
00397               VisionControlDataStruct retval;
00398               retval.source="NULL";
00399               if(m_dataVector.empty()){
00400                      return data;
00401               }
00402              
00403               retval.source=m_dataVector.front().source;
00404 retval.timestamp=m_dataVector.front().timestamp;
00405 
00406                        //             retval=m_dataVector.front();
00407               m_dataVector.pop();
00408               data=retval;
00409        
00410               return retval;
00411        }else{
00412               myCOMObject->resetData();
00413               return data;
00414        }
00415 }
00416 
00417 VisionControlInfoStruct VisionControl::getNextInfo(){
00418        if(m_queue){
00419               VisionControlInfoStruct retval;
00420         if(m_supplier){
00421                 retval.options.setCMode(C_SUPPLIER);
00422         }else{
00423                 retval.options.setCMode(C_CONSUMER);
00424         }
00425         if(m_supplier){
00426                 retval.stopOperators.setCMode(C_SUPPLIER);
00427         }else{
00428                 retval.stopOperators.setCMode(C_CONSUMER);
00429         }
00430         if(m_supplier){
00431                 retval.visionOperators.setCMode(C_SUPPLIER);
00432         }else{
00433                 retval.visionOperators.setCMode(C_CONSUMER);
00434         }
00435               retval.source="NULL";
00436               if(m_supplier){
00437                      if(m_requestVector.empty()){
00438                             return requests;
00439                      }
00440                      retval.source=m_requestVector.front().source;
00441 retval.target=m_requestVector.front().target;
00442 retval.options=m_requestVector.front().options;
00443 retval.timestamp=m_requestVector.front().timestamp;
00444 retval.stopOperators=m_requestVector.front().stopOperators;
00445 retval.visionOperators=m_requestVector.front().visionOperators;
00446 retval.mType=m_requestVector.front().mType;
00447 
00448                               //                     retval=m_requestVector.front();
00449         if(m_supplier){
00450                 retval.options.setCMode(C_SUPPLIER);
00451         }else{
00452                 retval.options.setCMode(C_CONSUMER);
00453         }
00454         if(m_supplier){
00455                 retval.stopOperators.setCMode(C_SUPPLIER);
00456         }else{
00457                 retval.stopOperators.setCMode(C_CONSUMER);
00458         }
00459         if(m_supplier){
00460                 retval.visionOperators.setCMode(C_SUPPLIER);
00461         }else{
00462                 retval.visionOperators.setCMode(C_CONSUMER);
00463         }
00464                      m_requestVector.pop();
00465                      requests=retval;
00466                      return retval;
00467               }else{
00468                      if(m_infoVector.empty()){
00469                             return info;
00470                      }
00471                      retval.source=m_infoVector.front().source;
00472 retval.target=m_infoVector.front().target;
00473 retval.options=m_infoVector.front().options;
00474 retval.timestamp=m_infoVector.front().timestamp;
00475 retval.stopOperators=m_infoVector.front().stopOperators;
00476 retval.visionOperators=m_infoVector.front().visionOperators;
00477 retval.mType=m_infoVector.front().mType;
00478 
00479         if(m_supplier){
00480                 retval.options.setCMode(C_SUPPLIER);
00481         }else{
00482                 retval.options.setCMode(C_CONSUMER);
00483         }
00484         if(m_supplier){
00485                 retval.stopOperators.setCMode(C_SUPPLIER);
00486         }else{
00487                 retval.stopOperators.setCMode(C_CONSUMER);
00488         }
00489         if(m_supplier){
00490                 retval.visionOperators.setCMode(C_SUPPLIER);
00491         }else{
00492                 retval.visionOperators.setCMode(C_CONSUMER);
00493         }
00494                      m_infoVector.pop();
00495                      info=retval;
00496                      // the requests struct really should get the values in 
00497                      // info
00498                      requests=info;
00499                      
00500                      return retval;
00501               }
00502        }else{
00503               myCOMObject->resetInfo();
00504               if(m_supplier){
00505         if(m_supplier){
00506                 requests.options.setCMode(C_SUPPLIER);
00507         }else{
00508                 requests.options.setCMode(C_CONSUMER);
00509         }
00510         if(m_supplier){
00511                 requests.stopOperators.setCMode(C_SUPPLIER);
00512         }else{
00513                 requests.stopOperators.setCMode(C_CONSUMER);
00514         }
00515         if(m_supplier){
00516                 requests.visionOperators.setCMode(C_SUPPLIER);
00517         }else{
00518                 requests.visionOperators.setCMode(C_CONSUMER);
00519         }
00520                      return requests;
00521               }else{
00522         if(m_supplier){
00523                 info.options.setCMode(C_SUPPLIER);
00524         }else{
00525                 info.options.setCMode(C_CONSUMER);
00526         }
00527         if(m_supplier){
00528                 info.stopOperators.setCMode(C_SUPPLIER);
00529         }else{
00530                 info.stopOperators.setCMode(C_CONSUMER);
00531         }
00532         if(m_supplier){
00533                 info.visionOperators.setCMode(C_SUPPLIER);
00534         }else{
00535                 info.visionOperators.setCMode(C_CONSUMER);
00536         }
00537                      // the requests struct really should get the values in 
00538                      // info
00539                      requests=info;
00540                      return info;
00541               }
00542        }
00543 }
00544 
00545 void VisionControl::setStreamName(const std::string & name){
00546 
00547        if(m_supplier&&name=="Any"){
00548               return;
00549        }
00550 
00551        m_streamname=name;
00552        m_haveStream=true;
00553 
00554        if(m_supplier){
00555               m_globalname=name;
00556        }
00557        
00558        if(myCOMObject){
00559               myCOMObject->setStreamName(name);
00560        }
00561 
00562        if(m_initialize){
00563               if(m_supplier){
00564                      activateSupplier();
00565               }else{
00566                      activateConsumer();
00567               }
00568        }
00569 
00570        m_streamRequest=STAT_OKAY;
00571 }
00572 
00573 void VisionControl::setGlobalName(const std::string & name){
00574        
00575        if(name=="Any"){
00576               return;
00577        }
00578 
00579        m_globalname=name;
00580        m_haveGlobalname=true;
00581        if(m_supplier){
00582               m_streamname=name;
00583        }
00584 
00585        if(myCOMObject){
00586               myCOMObject->setGlobalName(name);
00587        }
00588 }
00589 
00590 void VisionControl::doPing(){
00591        if(myCOMObject){
00592               myCOMObject->doPing();
00593        }
00594 }
00595 
00596 void VisionControl::setAutoPing(const bool & val){
00597        m_doAutoPing=val; 
00598        if(myCOMObject){
00599               myCOMObject->setAutoPing(val);
00600        }
00601 }
00602 
00603 

Generated on Thu Feb 1 15:31:54 2007 for WURDE by  doxygen 1.5.1