Sensor.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009 by Ulrich Friedrich Klank <klank@in.tum.de>
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 3 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 
00023 #ifndef SENSOR_H
00024 #define SENSOR_H
00025 
00026 #include <vector>
00027 #include <cstdio>
00028 #include "Reading.h"
00029 
00030 
00031 #include <boost/thread.hpp>
00032 #include <boost/bind.hpp>
00033 #include <boost/thread/condition.hpp>
00034 
00035 using namespace boost;
00036 
00037 #define XML_NODE_SENSOR "Sensor"
00038 #define XML_PROPERTY_SENSORNAME "SensorName"
00039 
00040 
00041 #define XML_NODE_SENSORRELAY "SensorRelay"
00042 #define XML_ATTRIBUTE_TOPICNAME "TopicName"
00043 #define XML_ATTRIBUTE_SENSORTYPE "SensorType"
00044 #define XML_ATTRIBUTE_MESSAGTYPE "MessageType"
00045 #define XML_ATTRIBUTE_RATE  "Rate"
00046 
00047 namespace cop
00048 {
00049   class RelPose;
00050   class XMLTag;
00055   class Sensor
00056   {
00057   public:
00058 
00059       /***
00060       *   @brief Constructor with pose, initializes parameters
00061       */
00062       Sensor() :
00063          m_relPose(NULL),
00064          m_FrameCount(0),
00065          m_deletedOffset(0),
00066          m_max_cameraImages(2)
00067       {}
00068       /***
00069       *   @brief Constructor with pose, initializes parameters, sets the sensors pose
00070       */
00071       Sensor(RelPose* pose) :
00072          m_relPose(pose),
00073          m_FrameCount(0),
00074          m_deletedOffset(0),
00075          m_max_cameraImages(2)
00076       {};
00080       virtual ~Sensor();
00081 
00086       static Sensor* SensorFactory(XMLTag* tag);
00087 
00088   public:
00089       virtual void SaveTo(XMLTag* tag);
00093       virtual std::string GetName() const {return XML_NODE_SENSOR;};
00099       virtual void Show(const long frame = -1){}
00105       virtual Reading*  GetReading(const long &frame = -1) = 0;
00111       virtual bool CanSee(RelPose &pose) const {return false;}
00116       virtual bool      Start()                         = 0;
00121       virtual bool      Stop()
00122       {
00123           m_newDataArrived.notify_all();
00124           return true;
00125       }
00129       RelPose* GetRelPose(){return m_relPose;}
00130 
00131       virtual XMLTag* Save() = 0;
00135       virtual bool IsCamera()const  {return false;}
00136 
00141       std::string GetSensorID() const{return m_stSensorName;}
00142 
00147       virtual bool RequiresSensorList(){return false;}
00152       virtual void SetSensorList(std::vector<Sensor*>){};
00153 
00158       virtual std::pair<std::string, std::vector<double> > GetUnformatedCalibrationValues() const{return std::pair<std::string, std::vector<double> >();}
00159 
00163       virtual void ProjectPoint3DToSensor(const double &x, const double &y, const double &z, double &row, double &column)  const;
00168       void Publish3DData(const std::vector<double>& x, const std::vector<double>& y, const std::vector<double>& z);
00174       virtual bool DeleteReading();
00175 
00179       virtual void GetShowLock(){printf("Lock Sensor Show %s\n", m_stSensorName.c_str()); m_mutexShow.lock();}
00183       virtual void ReleaseShowLock(){printf("UnLock Sensor Show %s\n", m_stSensorName.c_str()); m_mutexShow.unlock();}
00184 
00189       virtual void WaitForNewData();
00190       virtual void PushBackAsync(){};
00191 
00192       virtual Reading* ApplySelfFilter(Reading* read){return read;}
00193   protected:
00194      virtual void PushBack(Reading* img);
00195      Reading* GetReading_Lock(size_t index);
00196      virtual void SetData(XMLTag* tag);
00197 
00198   protected:
00199       RelPose*          m_relPose;
00200       std::vector<Reading*> m_images;
00201       std::vector<Reading*> m_temp_images;
00202       long                              m_FrameCount;
00203       long                              m_deletedOffset;
00204       unsigned long m_max_cameraImages;
00205       std::string m_stSensorName;
00206 
00207       typedef boost::mutex::scoped_lock lock;
00208       boost::mutex m_mutexImageList;
00209       boost::mutex m_mutexShow;
00210       boost::condition m_newDataArrived;
00211   };
00212 }
00213 
00214 #include <ros/ros.h>
00215 #include <sensor_msgs/CameraInfo.h>
00216 
00217 namespace cop
00218 {
00219   template<class SensorType, class MessageType>
00220   class SensorNetworkRelay : public SensorType
00221   {
00222   public:
00223       /***
00224       *   @brief Constructor with pose, initializes parameters
00225       */
00226     SensorNetworkRelay()
00227        : SensorType(),
00228          m_readyToPub(true)
00229     {m_rateCounter = 0;m_bCameraInfo=false;};
00230       /***
00231       *   @brief Constructor with pose, initializes parameters, sets the sensors pose
00232       */
00236       virtual ~SensorNetworkRelay()
00237       {
00238         /*SensorType::~SensorType();*/
00239       };
00240       /***
00241       *  @brief
00242       *  @param tag data, containing the class to load
00243 
00244       virtual void SetData(XMLTag* tag); */
00248       virtual std::string GetName() const {throw "Error in SensorNetworkRelay::GetName(): This function has to be overwritten";};
00249 
00250       virtual MessageType ConvertData(Reading* img) = 0;
00251   protected:
00252      virtual void PushBackAsync()
00253      {
00254        try
00255        {
00256          if(++m_rateCounter >  m_rate)
00257          {
00258             m_rateCounter = 0;
00259             MessageType temp = ConvertData(m_curPubReading);
00260             m_pub.publish(temp);
00261             if(m_bCameraInfo)
00262             {
00263               m_cameraInfoMessage.header = temp.header;
00264               m_pubCamInfo.publish(m_cameraInfoMessage);
00265             }
00266          }
00267        }
00268        catch(const char* text)
00269        {
00270          printf("Error converting Data in SensorRelay::PushBack\n");
00271        }
00272        catch(...)
00273        {
00274          printf("Error publishing data in SensorRelay::PushBack\n");
00275        }
00276        m_readyToPub = true;
00277      }
00278 
00279      virtual void PushBack(Reading* img)
00280      {
00281        if(m_readyToPub)
00282        {
00283          m_readyToPub = false;
00284          m_curPubReading = img;
00285          SensorNetworkRelay::PushBackAsync();
00286        }
00287        Sensor::PushBack(img);
00288      }
00289 
00290   protected:
00291      std::string m_stTopic;
00292      ros::Publisher m_pub;
00293      bool m_bCameraInfo;
00294      bool m_readyToPub;
00295      Reading* m_curPubReading;
00296      ros::Publisher m_pubCamInfo;
00297      sensor_msgs::CameraInfo m_cameraInfoMessage;
00298      int m_rate;
00299      int m_rateCounter;
00300   };
00301 
00302 
00303   class MinimalCalibration
00304   {
00305   public:
00306     MinimalCalibration(){}
00307     MinimalCalibration(XMLTag* tag);
00308     void SetData(XMLTag* tag);
00309     MinimalCalibration(std::pair<std::string, std::vector<double> > calib_temp)
00310     {
00311       if(calib_temp.first.compare("RECTHALCONCALIB") == 0)
00312       {
00313         focal_length = calib_temp.second[0];
00314         pix_size_x = calib_temp.second[1];
00315         pix_size_y = calib_temp.second[2];
00316         proj_center_x = calib_temp.second[3];
00317         proj_center_y = calib_temp.second[4];
00318         width = calib_temp.second[5];
00319         height = calib_temp.second[6];
00320         distortion_param = 0;
00321       }
00322       else if(calib_temp.first.compare("HALCONCALIB") == 0)
00323       {
00324         focal_length = calib_temp.second[0];
00325         distortion_param = calib_temp.second[1];
00326         pix_size_x = calib_temp.second[2];
00327         pix_size_y = calib_temp.second[3];
00328         proj_center_x = calib_temp.second[4];
00329         proj_center_y = calib_temp.second[5];
00330         width = calib_temp.second[6];
00331         height = calib_temp.second[7];
00332       }
00333     };
00334 
00335     double focal_length;
00336     double pix_size_x;
00337     double pix_size_y;
00338     double proj_center_x;
00339     double proj_center_y;
00340     double width;
00341     double height;
00342     double distortion_param;
00343 
00344     void Project3DPoint(const double &x, const double &y, const double &z, double &row, double &column)
00345     {
00346       double temp1,temp2;
00347       if (focal_length > 0.0)
00348       {
00349         temp1 = focal_length * (x / z );
00350         temp2 = focal_length * (y / z );
00351       }
00352       else
00353       {
00354         temp1 = x;
00355         temp2 = y;
00356       }
00357       column = ( temp1 / pix_size_x ) + proj_center_x;
00358       row = ( temp2 / pix_size_y ) + proj_center_y;
00359     }
00360   };
00361 
00362 template<typename TypeReading, typename DataType> class ScopedImage
00363 {
00364 public:
00365   ScopedImage(std::vector<Sensor*> sensors, ReadingType_t type) /*:
00366    selected_sensor(ExtractSensor(sensors, type)),
00367    calib(selected_sensor->GetUnformatedCalibrationValues()),
00368    original(ExtractOriginal(selected_sensor, type)),
00369    sensor_pose_at_capture_time(original->GetPose()),
00370    converted(original->GetType() != type),
00371    copy(converted ? (TypeReading*)original->ConvertTo(type) : NULL)
00372    image(converted ? (copy->m_image) : ((TypeReading*)original)->m_image)*/
00373   {
00374    selected_sensor = ExtractSensor(sensors, type);
00375    copy = NULL;
00376 
00377    if(selected_sensor != NULL)
00378    {
00379      calib = selected_sensor->GetUnformatedCalibrationValues();
00380      original = ExtractOriginal(selected_sensor, type);
00381      sensor_pose_at_capture_time = original->GetPose();
00382      converted = original->GetType() != type;
00383      m_type= type;
00384     }
00385     else
00386     {
00387       printf("Failed to extract sensor\n");
00388       throw "ScopedImage: No sensors hits the requested criteria";
00389     }
00390    /* if(copy == NULL)
00391       throw "ScopedImage: No conversion to the requested type is available";*/
00392   }
00393 
00394   ~ScopedImage()
00395   {
00396     original->Free();
00397     if(copy != NULL)
00398       delete copy;
00399   }
00400 
00401   static Sensor* ExtractSensor(std::vector<Sensor*> sensors, ReadingType_t type)
00402   {
00403     for(size_t i = 0; i < sensors.size(); i++)
00404     {
00405       std::pair<std::string,  std::vector<double> > calib_temp = sensors[i]->GetUnformatedCalibrationValues();
00406       // TODO: sensors[i]->IsCamera()  => sensors[i]->ReadingType() == type
00407 
00408       if(((sensors[i]->IsCamera() && !(type == ReadingType_PointCloud)) ||
00409           (!sensors[i]->IsCamera() && (type == ReadingType_PointCloud))) &&
00410          (calib_temp.first.compare("RECTHALCONCALIB")) == 0 &&
00411          (calib_temp.second.size() == 7) )
00412       {
00413         printf("Got another  sensor that gives the necessary data\n");
00414         return sensors[i];
00415       }
00416     }
00417     return NULL;
00418   }
00419 
00420 
00421   static Reading* ExtractOriginal(Sensor* sensor, ReadingType_t type)
00422   {
00423     if(sensor == NULL)
00424       throw "ScopedImage: No sensors fulfills this algorithm requirements";
00425     return sensor->GetReading(-1);
00426   }
00427 
00428 
00429   DataType& operator* ()
00430   {
00431     if(converted )
00432     {
00433       if(copy == NULL)
00434       {
00435         printf("Conversion to be done\n");
00436         copy = (TypeReading*)(original->ConvertTo(m_type));
00437         printf("Conversion done\n");
00438       }
00439       return copy->m_image;
00440     }
00441     else
00442       return ((TypeReading*)original)->m_image;
00443   }
00444   Sensor* selected_sensor;
00445   MinimalCalibration calib;
00446 private:
00447   Reading* original;
00448 public:
00449   RelPose* sensor_pose_at_capture_time;
00450 private:
00451   bool converted;
00452   ReadingType_t m_type;
00453   TypeReading* copy;
00454   /*TypeReading* copy;
00455     DataType& image;*/
00456 
00457 };
00458 
00459 
00460 }
00461 #endif /*CAMERA_H*/


cognitive_perception
Author(s): Ulrich F Klank
autogenerated on Mon Oct 6 2014 10:48:45