VrMagicRosBridge_host.cpp
Go to the documentation of this file.
00001 
00002 #include "VrMagicRosBridge_host.h"
00003 
00004 VrMagicRosBridge_host::VrMagicRosBridge_host() : _rate(0)
00005 {
00006     _loopRate = 0;
00007 
00008     //get launch param:
00009     ros::NodeHandle privNh("~");
00010     std::string pub_name;
00011     std::string ip_smartcam;
00012     int port_smartcam;
00013 
00014     privNh.param("pub_name",pub_name,std::string("vrmagic_image_"));
00015     privNh.param("ip_smartcam",ip_smartcam,std::string("192.168.3.101"));
00016     privNh.param<int>("port_smartcam",port_smartcam, 1234);
00017 
00018     _pubName_base = pub_name;
00019     _smartcamHandler = new ohm::VrMagicHandler_roshost(ip_smartcam, (unsigned int)port_smartcam);
00020 }
00021 
00022 VrMagicRosBridge_host::~VrMagicRosBridge_host()
00023 {
00024     delete _smartcamHandler;
00025     delete _rate;
00026     //todo delete msg objs...
00027 }
00028 
00029 void VrMagicRosBridge_host::start(const unsigned int rate)
00030 {
00031     delete _rate;
00032     _loopRate = rate;
00033     _rate = new ros::Rate(_loopRate);
00034     this->run();
00035 }
00036 
00037 void VrMagicRosBridge_host::run()
00038 {
00039 
00040     //connect to camera
00041     ROS_INFO("Waiting for connenction to Smartcam : IP: %s, PORT: %d",_smartcamHandler->getIp().c_str(),_smartcamHandler->getPort());
00042     _smartcamHandler->connect();
00043     ROS_INFO("Connected to Smartcam: IP: %s, PORT: %d",_smartcamHandler->getIp().c_str(),_smartcamHandler->getPort());
00044 
00045 
00046     while(ros::ok())
00047     {
00048         //trigger smartcam
00049         //if(_smartcamHandler->triggerImage() == 0)
00050         {
00051             if(_smartcamHandler->readImage(_imgSmarcam) == 0)
00052             {
00053                 unsigned int id = _imgSmarcam.id;
00054                 if(!this->provePublisherExist(id))
00055                 {
00056                     ROS_INFO("Added Publischer with id: %d", id);
00057                     this->addPublisher(id);
00058                 }
00059                 this->setMsgImage(id);
00060                 _publishers[id].publish(*_msgImgs[id]);
00061             }
00062             else
00063             {
00064                 ROS_ERROR("ERROR WHILE READING LAST IMAGE... now will continue");
00065                 _rate->sleep();
00066             }
00067         }
00068         //only publishing
00069         //ros::spinOnce();
00070        //todo .... prove if need
00071        //_rate->sleep();
00072     }
00073 }
00074 
00075 int VrMagicRosBridge_host::addPublisher(unsigned int id)
00076 {
00077     std::stringstream sstr;
00078     sstr << id;
00079     std::string id_str = sstr.str();
00080 
00081     //init publisher
00082     ros::Publisher pub = _nh.advertise<sensor_msgs::Image>(_pubName_base + id_str,1);
00083     _publishers.insert(std::make_pair(id, pub));
00084 
00085     //create ros-msg
00086     sensor_msgs::Image* msg = new sensor_msgs::Image;
00087     msg->width = 0;
00088     msg->height = 0;
00089     msg->step = 0;
00090     msg->header.frame_id = id_str;
00091     _msgImgs.insert(std::make_pair(id,msg));
00092 
00093     //create seq for ros-msg
00094     unsigned int seq = 0;
00095     _seq.insert(std::make_pair(id,seq));
00096 
00097     return 0;
00098 }
00099 
00100 int VrMagicRosBridge_host::removePublischer(unsigned int id)
00101 {
00102     //todo
00103     return 0;
00104 }
00105 
00106 bool VrMagicRosBridge_host::provePublisherExist(unsigned int id)
00107 {
00108     //prof if key exists
00109     if(_publishers.count(id) > 0)
00110     {
00111         return true;
00112     }
00113     return false;
00114 }
00115 
00116 void VrMagicRosBridge_host::setMsgImage(unsigned int id)
00117 {
00118     //set ROS-Time and seq counter
00119     _msgImgs[id]->header.stamp = ros::Time::now();
00120     _msgImgs[id]->header.seq   = _seq[id]++;
00121 
00125     if( _msgImgs[id]->width == _imgSmarcam.width &&
00126         _msgImgs[id]->height == _imgSmarcam.height &&
00127         _msgImgs[id]->step == _imgSmarcam.bytePerPixel * _imgSmarcam.width)
00128     {
00129         //copy new image in ros-msg
00130         memcpy(&_msgImgs[id]->data[0],_imgSmarcam.data,_imgSmarcam.dataSize);
00131         return;
00132     }
00133 
00134 
00135     //further code is executed if the image dimensions chaged.
00136 
00137     _msgImgs[id]->width = _imgSmarcam.width;
00138     _msgImgs[id]->height = _imgSmarcam.height;
00139     _msgImgs[id]->step = _imgSmarcam.bytePerPixel * _imgSmarcam.width;
00140     _msgImgs[id]->is_bigendian = 0;
00141 
00142     if(_imgSmarcam.channels == 1)
00143     {
00144         if(_imgSmarcam.bytePerPixel == 1)
00145             _msgImgs[id]->encoding = sensor_msgs::image_encodings::MONO8;
00146         else if(_imgSmarcam.bytePerPixel == 2)
00147             _msgImgs[id]->encoding = sensor_msgs::image_encodings::MONO16;
00148         else
00149         {
00150             ROS_ERROR("FALSE ENCODING GIVEN at 1 channel... will set to MONO8");
00151             _msgImgs[id]->encoding = sensor_msgs::image_encodings::MONO8;
00152         }
00153     }
00154     else if(_imgSmarcam.channels == 3)
00155     {
00156         if(_imgSmarcam.bytePerPixel == 3)
00157             _msgImgs[id]->encoding = sensor_msgs::image_encodings::BGR8;
00158         else if(_imgSmarcam.bytePerPixel == 6)
00159             _msgImgs[id]->encoding = sensor_msgs::image_encodings::BGR16;
00160         else
00161         {
00162             ROS_ERROR("FALSE ENCODING GIVEN at 3 channel... will set to BGR8");
00163             _msgImgs[id]->encoding = sensor_msgs::image_encodings::BGR8;
00164         }
00165     }
00166     else if(_imgSmarcam.channels == 4)
00167     {
00168         if(_imgSmarcam.bytePerPixel == 4)
00169             _msgImgs[id]->encoding = sensor_msgs::image_encodings::RGBA8;
00170         else if(_imgSmarcam.bytePerPixel == 8)
00171             _msgImgs[id]->encoding = sensor_msgs::image_encodings::RGBA16;
00172         else
00173         {
00174             ROS_ERROR("FALSE ENCODING GIVEN at 4 channel... will set to RGBA8");
00175             _msgImgs[id]->encoding = sensor_msgs::image_encodings::RGBA8;
00176         }
00177     }
00178     else
00179     {
00180         ROS_ERROR("FALSE ENCODING GIVEN: %d channel, %d Byte per Pixel... will set to RGB8", _imgSmarcam.channels, _imgSmarcam.bytePerPixel);
00181         _msgImgs[id]->encoding = sensor_msgs::image_encodings::RGB8;
00182     }
00183 
00184     ROS_INFO("selected Encoding: %s", _msgImgs[id]->encoding.c_str());
00185 
00186     ROS_INFO("resize data to: %d",_imgSmarcam.width * _imgSmarcam.height * _imgSmarcam.bytePerPixel);
00187     _msgImgs[id]->data.resize(_imgSmarcam.width * _imgSmarcam.height * _imgSmarcam.bytePerPixel);
00188     //copy image in ros-msg
00189     ROS_INFO("Copy data size: %d", _imgSmarcam.dataSize);
00190     memcpy(&_msgImgs[id]->data[0],_imgSmarcam.data,_imgSmarcam.dataSize);
00191     ROS_INFO("Copy data rdy");
00192 }


vrmagic_ros_bridge_server
Author(s): Michael Schmidpeter
autogenerated on Thu Aug 27 2015 15:40:39