ROSLogicalCameraPlugin.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2016 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 
19 
20 #include "osrf_gear/ARIAC.hh"
21 #include "osrf_gear/LogicalCameraImage.h"
22 
23 #include <gazebo/physics/Link.hh>
24 #include <gazebo/physics/Model.hh>
25 #include <gazebo/physics/World.hh>
26 #include <gazebo/sensors/Sensor.hh>
27 #include <gazebo/sensors/SensorManager.hh>
28 
29 #include <string>
30 
31 using namespace gazebo;
32 
34 
37 {
38 }
39 
42 {
43  this->rosnode->shutdown();
44 }
45 
47 void ROSLogicalCameraPlugin::Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
48 {
49  // load parameters
50  this->robotNamespace = "logical_camera";
51  if (_sdf->HasElement("robotNamespace"))
52  {
53  this->robotNamespace = _sdf->GetElement(
54  "robotNamespace")->Get<std::string>() + "/";
55  }
56 
57  // Make sure the ROS node for Gazebo has already been initialized
58  if (!ros::isInitialized())
59  {
60  ROS_FATAL_STREAM("A ROS node for Gazebo has not been initialized,"
61  << "unable to load plugin. Load the Gazebo system plugin "
62  << "'libgazebo_ros_api_plugin.so' in the gazebo_ros package)");
63  return;
64  }
65 
66  this->model = _parent;
67  this->node = transport::NodePtr(new transport::Node());
68  this->node->Init(this->model->GetWorld()->GetName());
69  this->rosnode = new ros::NodeHandle(this->robotNamespace);
70 
71  this->FindLogicalCamera();
72  if (!this->sensor)
73  {
74  gzerr << "No logical camera found on any link\n";
75  return;
76  }
77 
78  std::string imageTopic_ros = _parent->GetName();
79  if (_sdf->HasElement("image_topic_ros")) {
80  imageTopic_ros = _sdf->Get<std::string>("image_topic_ros");
81  }
82 
83  this->imageSub = this->node->Subscribe(this->sensor->Topic(),
85  gzdbg << "Subscribing to gazebo topic: " << this->sensor->Topic() << "\n";
86 
87  this->imagePub = this->rosnode->advertise<osrf_gear::LogicalCameraImage>(imageTopic_ros, 1, true);
88  gzdbg << "Publishing to ROS topic: " << imagePub.getTopic() << "\n";
89 }
90 
92 {
93  sensors::SensorManager* sensorManager = sensors::SensorManager::Instance();
94 
95  // Go through each link's sensors until a logical camera is found
96  for (physics::LinkPtr link : this->model->GetLinks())
97  {
98  for (unsigned int i = 0; i < link->GetSensorCount(); ++i)
99  {
100  sensors::SensorPtr sensor = sensorManager->GetSensor(link->GetSensorName(i));
101  if (sensor->Type() == "logical_camera")
102  {
103  this->sensor = sensor;
104  break;
105  }
106  }
107  if (this->sensor)
108  {
109  this->cameraLink = link;
110  break;
111  }
112  }
113 }
114 
116 void ROSLogicalCameraPlugin::OnImage(ConstLogicalCameraImagePtr &_msg)
117 {
118  osrf_gear::LogicalCameraImage imageMsg;
119  msgs::Vector3d cameraPosition = _msg->pose().position();
120  msgs::Quaternion cameraOrientation = _msg->pose().orientation();
121  imageMsg.pose.position.x = cameraPosition.x();
122  imageMsg.pose.position.y = cameraPosition.y();
123  imageMsg.pose.position.z = cameraPosition.z();
124  imageMsg.pose.orientation.x = cameraOrientation.x();
125  imageMsg.pose.orientation.y = cameraOrientation.y();
126  imageMsg.pose.orientation.z = cameraOrientation.z();
127  imageMsg.pose.orientation.w = cameraOrientation.w();
128 
129  for (int i = 0; i < _msg->model_size(); ++i)
130  {
131  msgs::Vector3d position = _msg->model(i).pose().position();
132  msgs::Quaternion orientation = _msg->model(i).pose().orientation();
133  osrf_gear::Model modelMsg;
134  modelMsg.pose.position.x = position.x();
135  modelMsg.pose.position.y = position.y();
136  modelMsg.pose.position.z = position.z();
137  modelMsg.pose.orientation.x = orientation.x();
138  modelMsg.pose.orientation.y = orientation.y();
139  modelMsg.pose.orientation.z = orientation.z();
140  modelMsg.pose.orientation.w = orientation.w();
141  modelMsg.type = ariac::DetermineModelType(_msg->model(i).name());
142  imageMsg.models.push_back(modelMsg);
143  }
144  this->imagePub.publish(imageMsg);
145 }
sensors::SensorPtr sensor
The logical camera sensor.
void publish(const boost::shared_ptr< M > &message) const
virtual ~ROSLogicalCameraPlugin()
Destructor.
ROS publisher for the logical camera.
void OnImage(ConstLogicalCameraImagePtr &_msg)
Callback for when logical camera images are received.
ROSCPP_DECL bool isInitialized()
physics::LinkPtr cameraLink
Link that holds the logical camera.
transport::SubscriberPtr imageSub
Subscription to logical camera image messages from gazebo.
ros::Publisher imagePub
ROS publisher for the logical camera image.
transport::NodePtr node
Node for communication with gazebo.
physics::ModelPtr model
Model that contains the logical camera.
void Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
Load the plugin.
#define ROS_FATAL_STREAM(args)
void FindLogicalCamera()
Searches the model links for a logical camera sensor.
ros::NodeHandle * rosnode
ros node handle
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
std::string robotNamespace
for setting ROS name space
TrayID_t DetermineModelType(const std::string &modelName)
Determine the type of a gazebo model from its name.
Definition: ARIAC.hh:222
GZ_REGISTER_MODEL_PLUGIN(GazeboRosP3D)
std::string getTopic() const


osrf_gear
Author(s):
autogenerated on Wed Sep 7 2016 03:48:12