VisualOdometryPTAMM.h

Go to the documentation of this file.
00001 //
00002 // Copyright 2008, 2009, 2010, 2011, 2012  Antonio Franchi and Paolo Stegagno
00003 //
00004 // This file is part of MIP.
00005 //
00006 // MIP is free software: you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation, either version 3 of the License, or
00009 // (at your option) any later version.
00010 //
00011 // MIP is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU General Public License
00017 // along with MIP. If not, see <http://www.gnu.org/licenses/>.
00018 //
00019 // Contact info: antonio.franchi@tuebingen.mpg.de stegagno@diag.uniroma1.it
00020 //
00021 // ----------------------------------------------------------------------------
00022 
00023 
00024 
00025 #ifndef _VISUALODOMETRYPTAMM_H
00026 #define _VISUALODOMETRYPTAMM_H
00027 
00028 
00029 #include "VisualOdometry.h"
00030 
00031 #include <cvd/Linux/v4lbuffer.h>
00032 #include <cvd/colourspace_convert.h>
00033 #include <cvd/colourspaces.h>
00034 #include <cvd/image.h>
00035 #include <cvd/byte.h>
00036 #include <cvd/rgb.h>
00037 #include <netinet/in.h>
00038 #include <netdb.h>
00039 #include <sys/socket.h>
00040 #include <arpa/inet.h>
00041 #include <stdio.h>
00042 #include <stdlib.h>
00043 #include <unistd.h>
00044 #include <pthread.h>
00045 #include <sys/types.h>
00046 #include <sys/un.h>
00047 #include <string.h>
00048 
00049 //#include <opencv2/cv.h>
00050 
00051 //#include <opencv/highgui.h>
00052 #include "opencv2/highgui/highgui.hpp"
00053 
00054 //#include <opencv2/cvaux.h>
00055 
00056 #include "opencv2/imgproc/imgproc.hpp"
00057 #include <iostream>
00058 #include <sstream>
00059 #include <pthread.h>
00060 
00061 #include <baselib.h>
00062 #include <VrpnTracker.h>
00063 
00064 #include <Camera.h>
00065 
00066 #include <time.h>
00067 
00068 #define PORT 7777
00069 
00070 using namespace MipBaselib;
00071 //using namespace CVD;
00072 using namespace std;
00073 
00074 namespace MipAlgorithms{
00078 
00082 
00084  /* @{ */
00085 
00086 
00087     //Pose3DandTime PTAMMdata;
00088 
00089     //pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
00090     //int poseReady = 0;
00091 
00092 
00093     class PTAMMVrpnHandler : public VrpnHandler{
00094     public:
00095       PTAMMVrpnHandler(Pose3DandTime* PTAMMdata);
00096 //      inline bool isDataReady(){ return dataReady;}
00097 //      inline void setDataState(bool value){ dataReady = value;}
00098     private:
00099         Pose3DandTime* handlerData;
00100         //bool dataReady;
00101         //timeval prevTime;
00102 
00103         virtual void handleVrpnData(){
00104                 timeval measureTime;
00105                 //Decimal x,y,z,roll,pitch,yaw;
00106   double matrix[3][3];
00107   VrpnTracker::quat2Mat(VrpnTracker::instance()->currPos.quat, matrix);
00108   double rpy[3];
00109   VrpnTracker::matToEA(matrix, rpy);
00110 
00111   handlerData->time = VrpnTracker::instance()->currPos.msg_time;
00112                 //timeStamp = (double) packageTime.tv_sec;
00113   handlerData->x = VrpnTracker::instance()->currPos.pos[0] / 1000.0;
00114   handlerData->y = VrpnTracker::instance()->currPos.pos[1] / 1000.0;
00115   handlerData->z = VrpnTracker::instance()->currPos.pos[2] / 1000.0;
00116   handlerData->roll  = rpy[0];
00117   handlerData->pitch = rpy[1];
00118   handlerData->yaw   = rpy[2];
00119                 //handlerData->upToDate = true;
00120 //                if ((handlerData->time.tv_sec - prevTime.tv_sec) * 1000000 + (handlerData->time.tv_usec - prevTime.tv_usec) > 0 )
00121 //                {
00122 //                    dataReady = true;
00123 //                }
00124 //                else
00125 //                {
00126 //                    cout << "PTAMMVrpnHandler :handleVrpnData() - WARNING! TIME IS NEGATIVE" << endl;
00127 //                }
00128 
00129                 //poseReady=1;
00130                 //cout << "time=" << timeStamp <<",x="<< x << ",y=" << y << ",z=" << z << ",r=" << roll << ",p=" << pitch << ",y=" << yaw << endl;
00131   //printf("time=%ld.%06ld,x=%5.2f, y=%5.2f, z=%5.2f, r=%5.2f, p=%5.2f, y=%5.2f\n",handlerData->time.tv_sec,handlerData->time.tv_usec,handlerData->x,handlerData->y,handlerData->z,handlerData->roll,handlerData->pitch,handlerData->yaw);
00132         }
00133     };
00134 
00135 
00136  class VisualOdometryPTAMM : public VisualOdometry {
00137 
00138     public:
00139         VisualOdometryPTAMM(IplImage* _imFrame);
00140         // Brief overload to also set the path of the logfile
00141         VisualOdometryPTAMM(IplImage* _imFrame, char* logFilePath);
00142         ~VisualOdometryPTAMM();
00143         bool getPose3DUnscaled(Pose3DandTime* pose);
00144         bool readDataFromPTAMM(Pose3DandTime* pose, Time* measTime, Decimal* delay);
00145         void checkAndSendImage(); // video-stream Thread
00146 
00147         void setImage(CVD::Image<CVD::byte>* imFrame); //TODO just in case we get CVD frame from Cam->getFrame()
00148         void setImageAndPose(IplImage* _imFrame, Pose3DandTime* pose = 0, int dataType = 0); // brief method overload to set external time stamp and pose
00149         void setImage(IplImage* imFrame);
00150         void show();
00151         bool loadImage();
00152         void clear();
00153         inline bool gotConnection(){return connection;};
00154         inline int getPoseType(){return poseType;};
00155         inline void setPoseType(int value){poseType = value;};
00156         inline void setLogActive(bool value){activateLog = value;};
00157         void setLogFile(char* value);
00158         /*void startDataLoop(){
00159         _ptammThread.startWork();
00160  }
00161  void stopDataLoop(){
00162         _ptammThread.stopWork();
00163         }*/
00164 
00165     private:
00166         
00167         int    serversock, clientsock;
00168         // Image stream variables
00169         pthread_t   thread_s;
00170         CVD::Image<CVD::byte> imageBW;
00171         pthread_mutex_t mutex;
00172         IplImage* _myFrame;
00173         //timeval frameTimeStamp;
00174         bool image_ready;
00175         // Vrpn variables
00176         pthread_t threadVrpn;
00177         PTAMMVrpnHandler* PTAMMdataHandler;
00178         bool connection; //check if PTAMM has connected to video stream server
00179 
00180         Pose3DandTime myRecPose3D; // Pose received via VRPN from PTAMM
00181 
00182         Pose3DandTime mySndPose;   // Pose to be sent to PTAMM for various porpouses
00183         int poseType;              // PTAMM map transformation, Filter est.
00184 
00185         bool poseReady;
00186         timeval prevTime;
00187         
00188         bool activateLog;
00189         char* ptammLog;
00190         ofstream logFilePTAMM;
00191         /*
00192         //Thread      _ptammThread;
00193         MipBaselib::Time lastFrameTime; //tempo del frame precedente
00194         MipBaselib::Time newFrameTime;  //tempo del nuovo frame
00195         int is_data_ready;
00196         int         serversock, clientsock;
00197         pthread_mutex_t mutex;
00198         CVD::Image<CVD::byte> imFrame;  //frame da trasmettere via socket
00199         //posa unscaled (SE3)
00200         */
00201     };
00202  /* @} */
00203 };// end namespace MipAlgorithms
00204  
00205 #endif

Generated on Mon Feb 20 07:01:06 2017 for MIP by  doxygen 1.5.6