gazebo_ros_video.cc
Go to the documentation of this file.
00001 /*
00002  *  Gazebo - Outdoor Multi-Robot Simulator
00003  *  Copyright (C) 2003
00004  *     Nate Koenig & Andrew Howard
00005  *
00006  *  This program 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 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program 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 this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 /*
00022  * Desc: 3D position interface for ground truth.
00023  * Author: Sachin Chitta and John Hsu
00024  * Date: 1 June 2008
00025  * SVN info: $Id$
00026  */
00027 
00028 #include <segbot_gazebo_plugins/gazebo_ros_video.h>
00029 #include <boost/lexical_cast.hpp>
00030 
00031 namespace gazebo {
00032 
00033   // Constructor
00034   GazeboRosVideo::GazeboRosVideo() {}
00035 
00036   // Destructor
00037   GazeboRosVideo::~GazeboRosVideo() {}
00038 
00039   // Load the controller
00040   void GazeboRosVideo::Load(
00041       rendering::VisualPtr _parent, sdf::ElementPtr _sdf) {
00042 
00043     this->model = _parent;
00044     this->updateConnection = 
00045       event::Events::ConnectPreRender(
00046           boost::bind(&GazeboRosVideo::UpdateChild, this));
00047 
00048     this->modelNamespace = "";
00049     if (!_sdf->HasElement("robotNamespace")) {
00050       ROS_WARN("VideoPlugin missing <robotNamespace>, defaults to \"%s\"",
00051           this->modelNamespace.c_str());
00052     } else {
00053       this->modelNamespace = 
00054         _sdf->GetElement("robotNamespace")->GetValueString();
00055     }
00056 
00057     this->topicName = "image_raw";
00058     if (!_sdf->HasElement("topicName")) {
00059       ROS_WARN("VideoPlugin (%s) missing <topicName>, defaults to \"%s\"",
00060           this->modelNamespace.c_str(), this->topicName.c_str());
00061     } else {
00062       this->topicName = _sdf->GetElement("topicName")->GetValueString();
00063     }
00064 
00065     this->height = 240;
00066     if (!_sdf->HasElement("height")) {
00067       ROS_WARN("VideoPlugin (%s) missing <height>, defaults to %i",
00068           this->modelNamespace.c_str(), this->height);
00069     } else {
00070       sdf::ParamPtr heightParam = _sdf->GetElement("height")->GetValue();
00071       if (heightParam->IsInt()) {
00072         heightParam->Get(this->height);
00073       } else {
00074         std::string heightStr;
00075         heightParam->Get(heightStr);
00076         this->height = boost::lexical_cast<int>(heightStr);
00077       }
00078     }
00079 
00080     this->width = 320;
00081     if (!_sdf->HasElement("width")) {
00082       ROS_WARN("VideoPlugin (%s) missing <width>, defaults to %i",
00083           this->modelNamespace.c_str(), this->height);
00084     } else {
00085       sdf::ParamPtr widthParam = _sdf->GetElement("width")->GetValue();
00086       if (widthParam->IsInt()) {
00087         widthParam->Get(this->width);
00088       } else {
00089         std::string widthStr;
00090         widthParam->Get(widthStr);
00091         this->width = boost::lexical_cast<int>(widthStr);
00092       }
00093     }
00094 
00095     std::string _name = this->modelNamespace + "_visual";
00096     video_visual_.reset(
00097         new VideoVisual(_name, _parent, this->height, this->width));
00098 
00099     //TODO: Do not use global queue. Remove Image Transport usage here
00100 
00101     // Initialize the ROS node and subscribe to cmd_vel
00102     int argc = 0;
00103     char** argv = NULL;
00104     ros::init(argc, argv, "video_plugin", 
00105         ros::init_options::NoSigintHandler | ros::init_options::AnonymousName);
00106     rosnode_.reset(new ros::NodeHandle(this->modelNamespace));
00107     it_.reset(new image_transport::ImageTransport(*rosnode_));
00108     camera_subscriber_ = 
00109       it_->subscribe(this->topicName, 1, &GazeboRosVideo::processImage, this);
00110     ROS_INFO("VideoPlugin (%s) has started!", this->modelNamespace.c_str());
00111     new_image_available_ = false;
00112 
00113     this->callback_queue_thread_ = 
00114       boost::thread(boost::bind(&GazeboRosVideo::QueueThread, this));
00115   }
00116 
00117   // Update the controller
00118   void GazeboRosVideo::UpdateChild() {
00119     boost::mutex::scoped_lock scoped_lock(m_image_);
00120     if (new_image_available_) {
00121       video_visual_->render(image_->image);
00122     }
00123     new_image_available_ = false;
00124   }
00125 
00126   void GazeboRosVideo::processImage(const sensor_msgs::ImageConstPtr &msg) {
00127     // Get a reference to the image from the image message pointer
00128     boost::mutex::scoped_lock scoped_lock(m_image_);
00129     image_ = cv_bridge::toCvCopy(msg, "bgr8");
00130     new_image_available_ = true;
00131   }
00132 
00133   void GazeboRosVideo::QueueThread() {
00134     ros::Rate r(10);
00135     while (rosnode_->ok()) {
00136       ros::spinOnce();
00137       r.sleep();
00138     }
00139   }
00140 
00141   GZ_REGISTER_VISUAL_PLUGIN(GazeboRosVideo);
00142 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends


segbot_gazebo_plugins
Author(s): Piyush Khandelwal
autogenerated on Mon Aug 5 2013 12:10:02