ROSAGVPlugin.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 #include <gazebo/common/common.hh>
18 #include <ignition/math.hh>
19 #include <osrf_gear/SubmitTray.h>
20 #include "ROSAGVPlugin.hh"
21 
22 #include <string>
23 
24 using namespace gazebo;
25 
27 
30 {
31 }
32 
35 {
36  this->rosnode->shutdown();
37 }
38 
40 void ROSAGVPlugin::Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
41 {
42  std::string index;
43 
44  if (_sdf->HasElement("index"))
45  {
46  index = _sdf->Get<std::string>("index");
47  }
48  else
49  {
50  gzerr << "AGV is missing an index. The AGV will not work.\n";
51  }
52 
53  // load parameters
54  this->robotNamespace = "";
55  if (_sdf->HasElement("robotNamespace"))
56  {
57  this->robotNamespace = _sdf->GetElement(
58  "robotNamespace")->Get<std::string>() + "/";
59  }
60 
61  // Make sure the ROS node for Gazebo has already been initialized
62  if (!ros::isInitialized())
63  {
64  ROS_FATAL_STREAM("A ROS node for Gazebo has not been initialized,"
65  << "unable to load plugin. Load the Gazebo system plugin "
66  << "'libgazebo_ros_api_plugin.so' in the gazebo_ros package)");
67  return;
68  }
69 
70  this->agvName = std::string("agv") + index;
71 
72  std::string agvControlTopic = "/ariac/" + this->agvName;
73  ROS_DEBUG_STREAM("Using AGV control service topic: " << agvControlTopic);
74 
75  std::string submitTrayTopic = "submit_tray";
76  if (_sdf->HasElement("submit_tray_service_name"))
77  submitTrayTopic = _sdf->Get<std::string>("submit_tray_service_name");
78  ROS_DEBUG_STREAM("Using submit tray service topic: " << submitTrayTopic);
79 
80  this->rosnode = new ros::NodeHandle(this->robotNamespace);
81 
82  this->anim.reset(new gazebo::common::PoseAnimation(this->agvName, 22, false));
83 
84  gazebo::common::PoseKeyFrame *key = anim->CreateKeyFrame(0);
85  key->Translation(ignition::math::Vector3d(0.3, 3.3, 0));
86  key->Rotation(ignition::math::Quaterniond(0, 0, 3.1415));
87 
88  key = anim->CreateKeyFrame(4);
89  key->Translation(ignition::math::Vector3d(-4.2, 3.8, 0));
90  key->Rotation(ignition::math::Quaterniond(0, 0, 3.1415));
91 
92  key = anim->CreateKeyFrame(10);
93  key->Translation(ignition::math::Vector3d(-4.2, 9.45, 0));
94  key->Rotation(ignition::math::Quaterniond(0, 0, 3.1415));
95 
96  key = anim->CreateKeyFrame(16);
97  key->Translation(ignition::math::Vector3d(-4.2, 3.8, 0));
98  key->Rotation(ignition::math::Quaterniond(0, 0, 3.1415));
99 
100  key = anim->CreateKeyFrame(22);
101  key->Translation(ignition::math::Vector3d(0.3, 3.3, 0));
102  key->Rotation(ignition::math::Quaterniond(0, 0, 3.1415));
103 
104  this->model = _parent;
105 
106  this->rosService = this->rosnode->advertiseService(agvControlTopic,
107  &ROSAGVPlugin::OnCommand, this);
108 
109  // Client for submitting trays for inspection.
110  this->rosSubmitTrayClient =
111  this->rosnode->serviceClient<osrf_gear::SubmitTray>(submitTrayTopic);
112 }
113 
116  osrf_gear::AGVControl::Request &_req,
117  osrf_gear::AGVControl::Response &_res)
118 {
119  bool triggerAnim = this->agvName == "agv1" &&
120  (anim->GetTime() <= 0.0 || anim->GetTime() >= anim->GetLength());
121 
122  if (triggerAnim)
123  {
124  anim->SetTime(0);
125  this->model->SetAnimation(anim);
126  }
127 
128  if (!this->rosSubmitTrayClient.exists())
129  {
131  }
132 
133  // Make a service call to submit the tray for inspection.
134  osrf_gear::SubmitTray srv;
135  srv.request.tray_id.data = this->agvName + "::kit_tray::tray";
136  srv.request.kit_type.data = _req.kit_type.data;
137  this->rosSubmitTrayClient.call(srv);
138  if (!srv.response.success) {
139  ROS_ERROR_STREAM("Failed to submit tray for inspection.");
140  _res.success = false;
141  return false;
142  }
143  ROS_INFO_STREAM("Result of inspection: " << srv.response.inspection_result);
144  _res.success = true;
145 
146  return true;
147 }
ros::ServiceServer rosService
Receives service calls for controlling the AGV.
Definition: ROSAGVPlugin.hh:61
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
void Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
Load the plugin.
Definition: ROSAGVPlugin.cc:40
gazebo::common::PoseAnimationPtr anim
Robot animation.
Definition: ROSAGVPlugin.hh:67
bool OnCommand(osrf_gear::AGVControl::Request &_req, osrf_gear::AGVControl::Response &_res)
Receives requests on the conveyor belt&#39;s topic.
ROSCPP_DECL bool isInitialized()
bool call(MReq &req, MRes &res)
ROS implementation of the ConveyorBeltPlugin plugin.
Definition: ROSAGVPlugin.hh:31
virtual ~ROSAGVPlugin()
Destructor.
Definition: ROSAGVPlugin.cc:34
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
ros::ServiceClient rosSubmitTrayClient
Client for submitting trays for inspection.
Definition: ROSAGVPlugin.hh:64
std::string agvName
Name of the AGV.
Definition: ROSAGVPlugin.hh:52
bool waitForExistence(ros::Duration timeout=ros::Duration(-1))
#define ROS_FATAL_STREAM(args)
ROSAGVPlugin()
Constructor.
Definition: ROSAGVPlugin.cc:29
ros::NodeHandle * rosnode
ros node handle
Definition: ROSAGVPlugin.hh:58
gazebo::physics::ModelPtr model
Pointer to the model.
Definition: ROSAGVPlugin.hh:70
#define ROS_DEBUG_STREAM(args)
std::string robotNamespace
for setting ROS name space
Definition: ROSAGVPlugin.hh:55
#define ROS_INFO_STREAM(args)
GZ_REGISTER_MODEL_PLUGIN(GazeboRosP3D)
#define ROS_ERROR_STREAM(args)


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