ROSLogicalCameraPlugin.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright 2016 Open Source Robotics Foundation
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  *
00016 */
00017 
00018 #include "ROSLogicalCameraPlugin.hh"
00019 
00020 #include "osrf_gear/ARIAC.hh"
00021 #include "osrf_gear/LogicalCameraImage.h"
00022 
00023 #include <gazebo/physics/Link.hh>
00024 #include <gazebo/physics/Model.hh>
00025 #include <gazebo/physics/World.hh>
00026 #include <gazebo/sensors/Sensor.hh>
00027 #include <gazebo/sensors/SensorManager.hh>
00028 
00029 #include <string>
00030 
00031 using namespace gazebo;
00032 
00033 GZ_REGISTER_MODEL_PLUGIN(ROSLogicalCameraPlugin);
00034 
00036 ROSLogicalCameraPlugin::ROSLogicalCameraPlugin()
00037 {
00038 }
00039 
00041 ROSLogicalCameraPlugin::~ROSLogicalCameraPlugin()
00042 {
00043   this->rosnode->shutdown();
00044 }
00045 
00047 void ROSLogicalCameraPlugin::Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
00048 {
00049   // load parameters
00050   this->robotNamespace = "logical_camera";
00051   if (_sdf->HasElement("robotNamespace"))
00052   {
00053     this->robotNamespace = _sdf->GetElement(
00054         "robotNamespace")->Get<std::string>() + "/";
00055   }
00056 
00057   // Make sure the ROS node for Gazebo has already been initialized
00058   if (!ros::isInitialized())
00059   {
00060     ROS_FATAL_STREAM("A ROS node for Gazebo has not been initialized,"
00061         << "unable to load plugin. Load the Gazebo system plugin "
00062         << "'libgazebo_ros_api_plugin.so' in the gazebo_ros package)");
00063     return;
00064   }
00065 
00066   this->model = _parent;
00067   this->node = transport::NodePtr(new transport::Node());
00068   this->node->Init(this->model->GetWorld()->GetName());
00069   this->rosnode = new ros::NodeHandle(this->robotNamespace);
00070 
00071   this->FindLogicalCamera();
00072   if (!this->sensor)
00073   {
00074     gzerr << "No logical camera found on any link\n";
00075     return;
00076   }
00077 
00078   std::string imageTopic_ros = _parent->GetName();
00079   if (_sdf->HasElement("image_topic_ros")) {
00080     imageTopic_ros = _sdf->Get<std::string>("image_topic_ros");
00081   }
00082 
00083   this->imageSub = this->node->Subscribe(this->sensor->Topic(),
00084           &ROSLogicalCameraPlugin::OnImage, this);
00085   gzdbg << "Subscribing to gazebo topic: " << this->sensor->Topic() << "\n";
00086 
00087   this->imagePub = this->rosnode->advertise<osrf_gear::LogicalCameraImage>(imageTopic_ros, 1, true);
00088   gzdbg << "Publishing to ROS topic: " << imagePub.getTopic() << "\n";
00089 }
00090 
00091 void ROSLogicalCameraPlugin::FindLogicalCamera()
00092 {
00093   sensors::SensorManager* sensorManager = sensors::SensorManager::Instance();
00094 
00095   // Go through each link's sensors until a logical camera is found
00096   for (physics::LinkPtr link : this->model->GetLinks())
00097   {
00098     for (unsigned int i = 0; i < link->GetSensorCount(); ++i)
00099     {
00100       sensors::SensorPtr sensor = sensorManager->GetSensor(link->GetSensorName(i));
00101       if (sensor->Type() == "logical_camera")
00102       {
00103         this->sensor = sensor;
00104         break;
00105       }
00106     }
00107     if (this->sensor)
00108     {
00109       this->cameraLink = link;
00110       break;
00111     }
00112   }
00113 }
00114 
00116 void ROSLogicalCameraPlugin::OnImage(ConstLogicalCameraImagePtr &_msg)
00117 {
00118   osrf_gear::LogicalCameraImage imageMsg;
00119   msgs::Vector3d cameraPosition = _msg->pose().position();
00120   msgs::Quaternion cameraOrientation = _msg->pose().orientation();
00121   imageMsg.pose.position.x = cameraPosition.x();
00122   imageMsg.pose.position.y = cameraPosition.y();
00123   imageMsg.pose.position.z = cameraPosition.z();
00124   imageMsg.pose.orientation.x = cameraOrientation.x();
00125   imageMsg.pose.orientation.y = cameraOrientation.y();
00126   imageMsg.pose.orientation.z = cameraOrientation.z();
00127   imageMsg.pose.orientation.w = cameraOrientation.w();
00128 
00129   for (int i = 0; i < _msg->model_size(); ++i)
00130   {
00131     msgs::Vector3d position = _msg->model(i).pose().position();
00132     msgs::Quaternion orientation = _msg->model(i).pose().orientation();
00133     osrf_gear::Model modelMsg;
00134     modelMsg.pose.position.x = position.x();
00135     modelMsg.pose.position.y = position.y();
00136     modelMsg.pose.position.z = position.z();
00137     modelMsg.pose.orientation.x = orientation.x();
00138     modelMsg.pose.orientation.y = orientation.y();
00139     modelMsg.pose.orientation.z = orientation.z();
00140     modelMsg.pose.orientation.w = orientation.w();
00141     modelMsg.type = ariac::DetermineModelType(_msg->model(i).name());
00142     imageMsg.models.push_back(modelMsg);
00143   }
00144   this->imagePub.publish(imageMsg);
00145 }


osrf_gear
Author(s):
autogenerated on Mon Sep 5 2016 03:41:33