Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <segbot_gazebo_plugins/gazebo_ros_video.h>
00029 #include <boost/lexical_cast.hpp>
00030
00031 namespace gazebo {
00032
00033
00034 GazeboRosVideo::GazeboRosVideo() {}
00035
00036
00037 GazeboRosVideo::~GazeboRosVideo() {}
00038
00039
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
00100
00101
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
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
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 }