/home/fwph/code/wurde/modules/visionModule/Operators/DataCollector.cpp

Go to the documentation of this file.
00001 #include "DataCollector.H"
00002 #include <Logger.H>
00003 #include <string>
00004 #include <opencv/cv.h>
00005 #include <opencv/highgui.h>
00006 
00007 using namespace std;
00008 using namespace WURDE;
00009 using namespace WURDEVision;
00010 
00011 DataCollector::DataCollector(std::string name) : VisionOperator(name), 
00012                                                  m_myRF(STRAT_ASSIGNED, "DataRangeFinderClient"), 
00013                                                  m_myPTU(STRAT_ASSIGNED, "DataPTUClient") {
00014         
00015         registerConsumer(&m_myRF);
00016         registerConsumer(&m_myPTU);
00017         
00018 }
00019 
00020 void DataCollector::initOperator() {
00021         // Potentially add two image saving threads here so that this operator doesn't
00022         // hold things up too badly.
00023         
00024         t1state=STATE_RUN;
00025        t2state=STATE_RUN;
00026        t3state=STATE_RUN;
00027        
00028        m_newt1image=false;
00029        m_newt2image=false;
00030        m_newt3image=false;
00031        
00032        m_t1image=cvCreateImage(cvSize(320, 240), IPL_DEPTH_8U, 3);
00033        m_t2image=cvCreateImage(cvSize(320, 240), IPL_DEPTH_8U, 3);
00034        m_t3image=cvCreateImage(cvSize(320, 240), IPL_DEPTH_8U, 3);
00035        
00036        pthread_create(&thread1,NULL,runDataCollectorThread1,(void *)this);
00037        pthread_create(&thread2,NULL,runDataCollectorThread2,(void *)this);
00038        pthread_create(&thread3,NULL,runDataCollectorThread3,(void *)this);
00039        
00040        if(g_globalConfiguration.haveModuleOption(m_name,"imageDir")){
00041                m_imageDir=g_globalConfiguration.getModuleOption(m_name,"imageDir");
00042        }else{
00043                m_imageDir=g_globalConfiguration.getDataDirectory();
00044        }
00045        
00046        if(g_globalConfiguration.haveModuleOption(m_name,"imagePrefix")){
00047               m_imagePrefix=g_globalConfiguration.getModuleOption(m_name,"imagePrefix");
00048        }else{
00049                m_imagePrefix=m_name;
00050        }
00051        
00052        if(g_globalConfiguration.haveModuleOption(m_name,"laserOutputFile")){
00053                m_outfilename=g_globalConfiguration.getModuleOption(m_name,"laserOutputFile");
00054                if(m_outfilename[0]!='/'){
00055                        m_outfilename=g_globalConfiguration.getDataDirectory()+"/"+m_outfilename;
00056                }
00057        }else{
00058                m_outfilename=g_globalConfiguration.getDataDirectory()+"/"+"laseroutdata.txt";
00059        }
00060        
00061        m_outfile.open(m_outfilename.c_str());
00062        
00063        m_state=STATE_RUN;
00064        m_format=FORMAT_CHAR_111_RGB;
00065        
00066        next=1;
00067 }
00068 
00069 void DataCollector::runOperator(WURDEImage mImage) {
00070         
00071         if(m_myRF.newData()){
00072                 
00073                 if(next==1){
00074                         if(!m_newt1image){              
00075                                 if(m_t1image->width!=mImage.second->width||
00076                                    m_t1image->height!=mImage.second->height){
00077                                         m_t1image=NULL;
00078                                         m_t1image=cvCreateImage(cvSize(mImage.second->width, mImage.second->height), IPL_DEPTH_8U, 3);
00079                                 }
00080                                 
00081                                 t1time=mImage.first;
00082                                 // memcpy(m_t1image->imageData,mImage.second->imageData,3*mImage.second->height*mImage.second->width);
00083                                 cvConvertImage(mImage.second,m_t1image,CV_CVTIMG_SWAP_RB);
00084                                 next=2;
00085                                 
00086                                 m_newt1image=true;
00087                         }
00088                 }else if (next==2){
00089                         if(!m_newt2image){
00090                                 if(m_t2image->width!=mImage.second->width||
00091                                    m_t2image->height!=mImage.second->height){
00092                                         //                          cvReleaseImage(&m_t2image);
00093                                         m_t2image=NULL;
00094                                         m_t2image=cvCreateImage(cvSize(mImage.second->width, mImage.second->height), IPL_DEPTH_8U, 3);
00095                                 }
00096                                 t2time=mImage.first;
00097                                 //memcpy(m_t2image->imageData,mImage.second->imageData,3*mImage.second->height*mImage.second->width);
00098                                 cvConvertImage(mImage.second,m_t2image,CV_CVTIMG_SWAP_RB);
00099                                 next=3;
00100                                 m_newt2image=true;
00101                         }
00102                 }else{
00103                         if(!m_newt3image){
00104                                 if(m_t3image->width!=mImage.second->width||
00105                                    m_t3image->height!=mImage.second->height){
00106                                         //                          cvReleaseImage(&m_t3image);
00107                                         m_t3image=NULL;
00108                                         m_t3image=cvCreateImage(cvSize(mImage.second->width, mImage.second->height), IPL_DEPTH_8U, 3);
00109                                 }
00110                                 
00111                                 t3time=mImage.first;
00112                                 cvConvertImage(mImage.second,m_t3image,CV_CVTIMG_SWAP_RB);
00113                                 //                   memcpy(m_t3image->imageData,mImage.second->imageData,3*mImage.second->height*mImage.second->width);
00114                                 next=1;
00115                                 m_newt3image=true;
00116                         }
00117                 }
00118                 
00119                 m_myRF.getNextData();
00120                 
00121                 vector<double>::iterator rangeiter;
00122                 
00123                 for(rangeiter=m_myRF.data.ranges.begin();
00124                     rangeiter!=m_myRF.data.ranges.end();
00125                     rangeiter++){
00126                         
00127                         m_outfile << (*rangeiter) << " ";
00128                 }
00129                 
00130                 if(m_myPTU.newInfo()){
00131                         m_myPTU.getNextInfo();
00132                 }
00133                 
00134                 m_outfile << m_myPTU.info.pan.getValue() << " " << m_myPTU.info.tilt.getValue();
00135                 
00136                 m_outfile << endl << flush;
00137                 
00138         }
00139         
00140 }
00141 
00142 void DataCollector::quitOperator() {
00143         m_outfile.close();
00144         t1state=STATE_QUIT;
00145         t2state=STATE_QUIT;
00146         t3state=STATE_QUIT;
00147         
00148         pthread_join(thread1,NULL);
00149         pthread_join(thread2,NULL);
00150         pthread_join(thread3,NULL);
00151         
00152         
00153         cvReleaseImage(&m_t1image);
00154         cvReleaseImage(&m_t2image);
00155         cvReleaseImage(&m_t3image);
00156         
00157         m_t1image=NULL;
00158         m_t2image=NULL;
00159         m_t3image=NULL;
00160 }
00161 
00162 void DataCollector::fileWriterThread1(){
00163         while(t1state==STATE_RUN){
00164                 if(m_newt1image){
00165                         std::stringstream filename;
00166                         filename.setf(ios_base::showpoint);
00167                         filename.setf(ios_base::fixed,ios_base::floatfield);
00168                         double stamp;
00169                         stamp=t1time.getSeconds()+t1time.getUSeconds()/1000000.0;
00170                         
00171                         filename << m_imageDir << "/" << m_imagePrefix << "-" << stamp << ".ppm";
00172                         g_debug("Thread 1 writing..");
00173                         cvSaveImage((filename.str()).c_str(),m_t1image);
00174                         
00175                         m_newt1image=false;
00176                 }
00177                 usleep(50000);
00178         }
00179 }
00180 
00181 void DataCollector::fileWriterThread2(){
00182         while(t1state==STATE_RUN){
00183                 if(m_newt2image){
00184                         std::stringstream filename;
00185                         filename.setf(ios_base::showpoint);
00186                         filename.setf(ios_base::fixed,ios_base::floatfield);
00187                         double stamp;
00188                         stamp=t2time.getSeconds()+t2time.getUSeconds()/1000000.0;
00189                         filename << m_imageDir << "/" << m_imagePrefix << "-" << stamp << ".ppm";
00190                         g_debug("Thread 2 writing..");
00191                         cvSaveImage((filename.str()).c_str(),m_t2image);
00192                         
00193                         m_newt2image=false;
00194                 }
00195                 usleep(50000);
00196         }
00197 }
00198 
00199 void DataCollector::fileWriterThread3(){
00200         while(t1state==STATE_RUN){
00201                 if(m_newt3image){
00202                         std::stringstream filename;
00203                         filename.setf(ios_base::showpoint);
00204                         filename.setf(ios_base::fixed,ios_base::floatfield);
00205                         double stamp;
00206                         stamp=t3time.getSeconds()+t3time.getUSeconds()/1000000.0;
00207                         
00208                         filename << m_imageDir << "/" << m_imagePrefix << "-" << stamp << ".ppm";
00209                         g_debug("Thread 3 writing..");
00210                         cvSaveImage((filename.str()).c_str(),m_t3image) ;
00211                         
00212                         m_newt3image=false;
00213                         
00214                 }             usleep(50000);
00215         }
00216 }
00217 
00218 extern "C" {
00219         void *runDataCollectorThread1(void *ptr){
00220                 DataCollector *theObject=(DataCollector *)ptr;
00221                 
00222                 theObject->fileWriterThread1();
00223                 pthread_exit(NULL);
00224         }
00225         
00226         void *runDataCollectorThread2(void *ptr){
00227                 DataCollector *theObject=(DataCollector *)ptr;
00228                 
00229                 theObject->fileWriterThread2();
00230                 pthread_exit(NULL);
00231         }
00232         
00233         void *runDataCollectorThread3(void *ptr){
00234                 DataCollector *theObject=(DataCollector *)ptr;
00235                 
00236                 theObject->fileWriterThread3();
00237                 pthread_exit(NULL);
00238         }
00239         
00240         VisionOperator *DataCollector_factory(std::string name) {
00241                 return new DataCollector(name);
00242         }
00243         
00244         class DataCollectorProxy { 
00245         public:
00246                 DataCollectorProxy(){
00247                         operatorFactory["DataCollector"] = DataCollector_factory;
00248                 }
00249         };
00250         
00251         DataCollectorProxy p_imagewriter;
00252 }

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