gazebo_ros_thermal_camera.cpp
Go to the documentation of this file.
00001 //=================================================================================================
00002 // Copyright (c) 2012, Stefan Kohlbrecher, TU Darmstadt
00003 // All rights reserved.
00004 
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are met:
00007 //     * Redistributions of source code must retain the above copyright
00008 //       notice, this list of conditions and the following disclaimer.
00009 //     * Redistributions in binary form must reproduce the above copyright
00010 //       notice, this list of conditions and the following disclaimer in the
00011 //       documentation and/or other materials provided with the distribution.
00012 //     * Neither the name of the Simulation, Systems Optimization and Robotics
00013 //       group, TU Darmstadt nor the names of its contributors may be used to
00014 //       endorse or promote products derived from this software without
00015 //       specific prior written permission.
00016 
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00018 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00019 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00020 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 //=================================================================================================
00032 /*
00033  *  Gazebo - Outdoor Multi-Robot Simulator
00034  *  Copyright (C) 2003
00035  *     Nate Koenig & Andrew Howard
00036  *
00037  *  This program is free software; you can redistribute it and/or modify
00038  *  it under the terms of the GNU General Public License as published by
00039  *  the Free Software Foundation; either version 2 of the License, or
00040  *  (at your option) any later version.
00041  *
00042  *  This program is distributed in the hope that it will be useful,
00043  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00044  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00045  *  GNU General Public License for more details.
00046  *
00047  *  You should have received a copy of the GNU General Public License
00048  *  along with this program; if not, write to the Free Software
00049  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00050  *
00051  */
00052 
00053 #include <hector_gazebo_thermal_camera/gazebo_ros_thermal_camera.h>
00054 
00055 #include <gazebo/sensors/Sensor.hh>
00056 #include <gazebo/sensors/SensorTypes.hh>
00057 
00058 #include <sensor_msgs/image_encodings.h>
00059 
00060 #include <gazebo/gazebo_config.h>
00061 
00062 namespace gazebo
00063 {
00064 
00066 // Constructor
00067 template <class Base>
00068 GazeboRosThermalCamera_<Base>::GazeboRosThermalCamera_()
00069 {
00070 }
00071 
00073 // Destructor
00074 template <class Base>
00075 GazeboRosThermalCamera_<Base>::~GazeboRosThermalCamera_()
00076 {
00077 }
00078 
00079 template <class Base>
00080 void GazeboRosThermalCamera_<Base>::Load(sensors::SensorPtr _parent, sdf::ElementPtr _sdf)
00081 {
00082   Base::Load(_parent, _sdf);
00083   // copying from CameraPlugin into GazeboRosCameraUtils
00084   this->parentSensor_ = this->parentSensor;
00085   this->width_ = this->width;
00086   this->height_ = this->height;
00087   this->depth_ = this->depth;
00088   this->format_ = this->format;
00089 
00090   this->image_connect_count_ = boost::shared_ptr<int>(new int);
00091   *this->image_connect_count_ = 0;
00092   this->image_connect_count_lock_ = boost::shared_ptr<boost::mutex>(new boost::mutex);
00093   this->was_active_ = boost::shared_ptr<bool>(new bool);
00094   *this->was_active_ = false;
00095 
00096   LoadImpl(_parent, _sdf);
00097   GazeboRosCameraUtils::Load(_parent, _sdf);
00098 }
00099 
00101 // Update the controller
00102 template <class Base>
00103 void GazeboRosThermalCamera_<Base>::OnNewFrame(const unsigned char *_image,
00104     unsigned int _width, unsigned int _height, unsigned int _depth,
00105     const std::string &_format)
00106 {
00107   if (!this->initialized_ || this->height_ <=0 || this->width_ <=0)
00108     return;
00109 
00110 #if (GAZEBO_MAJOR_VERSION > 6)
00111   this->sensor_update_time_ = this->parentSensor_->LastUpdateTime();
00112 #else
00113   this->sensor_update_time_ = this->parentSensor_->GetLastUpdateTime();
00114 #endif
00115 
00116   if (!this->parentSensor->IsActive())
00117   {
00118     if ((*this->image_connect_count_) > 0)
00119       // do this first so there's chance for sensor to run 1 frame after activate
00120       this->parentSensor->SetActive(true);
00121   }
00122   else
00123   {
00124     if ((*this->image_connect_count_) > 0)
00125     {
00126       common::Time cur_time = this->world_->GetSimTime();
00127       if (cur_time - this->last_update_time_ >= this->update_period_)
00128       {
00129         this->PutCameraData(_image);
00130         this->PublishCameraInfo();
00131         this->last_update_time_ = cur_time;
00132       }
00133     }
00134   }
00135 }
00136 
00137 template <class Base>
00138 void GazeboRosThermalCamera_<Base>::OnNewImageFrame(const unsigned char *_image,
00139     unsigned int _width, unsigned int _height, unsigned int _depth,
00140     const std::string &_format)
00141 {
00142   OnNewFrame(_image, _width, _height, _depth, _format);
00143 }
00144 
00146 // Put camera_ data to the interface
00147 template <class Base>
00148 void GazeboRosThermalCamera_<Base>::PutCameraData(const unsigned char *_src, common::Time &last_update_time)
00149 {
00150   this->sensor_update_time_ = last_update_time;
00151   this->PutCameraData(_src);
00152 }
00153 
00154 template <class Base>
00155 void GazeboRosThermalCamera_<Base>::PutCameraData(const unsigned char *_src)
00156 {
00157   if (!this->initialized_ || this->height_ <=0 || this->width_ <=0)
00158     return;
00159 
00160   this->lock_.lock();
00161 
00162   // copy data into image
00163   this->image_msg_.header.frame_id = this->frame_name_;
00164   this->image_msg_.header.stamp.sec = this->sensor_update_time_.sec;
00165   this->image_msg_.header.stamp.nsec = this->sensor_update_time_.nsec;
00166 
00168   if ((*this->image_connect_count_) > 0)
00169   {
00170     this->image_msg_.width = this->width_;
00171     this->image_msg_.height = this->height_;
00172     this->image_msg_.encoding = sensor_msgs::image_encodings::MONO8;
00173     this->image_msg_.step = this->image_msg_.width;
00174 
00175     size_t size = this->width_ * this->height_;
00176 
00177     std::vector<uint8_t>& data (this->image_msg_.data);
00178     data.resize(size);
00179 
00180     size_t img_index = 0;
00181 
00182     for (size_t i = 0; i < size; ++i){
00183       if ((_src[img_index] >254) && (_src[img_index+1] < 1) && (_src[img_index+2] < 1)){
00184         //RGB [255,0,0] translates to white (white hot)
00185         data[i]= 255;
00186       }else{
00187         //Everything else is written to the MONO8 output image much darker
00188         data[i]= (_src[img_index] + _src[img_index+1] + _src[img_index+2]) /8 ;
00189       }
00190       img_index += 3;
00191     }
00192 
00193     // publish to ros
00194     this->image_pub_.publish(this->image_msg_);
00195   }
00196 
00197   this->lock_.unlock();
00198 }
00199 
00200 }


hector_gazebo_thermal_camera
Author(s): Stefan Kohlbrecher
autogenerated on Sat Jun 8 2019 19:52:42