ROSAGVPlugin.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 #include <gazebo/common/common.hh>
00018 #include <ignition/math.hh>
00019 #include <osrf_gear/SubmitTray.h>
00020 #include "ROSAGVPlugin.hh"
00021 
00022 #include <string>
00023 
00024 using namespace gazebo;
00025 
00026 GZ_REGISTER_MODEL_PLUGIN(ROSAGVPlugin);
00027 
00029 ROSAGVPlugin::ROSAGVPlugin()
00030 {
00031 }
00032 
00034 ROSAGVPlugin::~ROSAGVPlugin()
00035 {
00036   this->rosnode->shutdown();
00037 }
00038 
00040 void ROSAGVPlugin::Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
00041 {
00042   std::string index;
00043 
00044   if (_sdf->HasElement("index"))
00045   {
00046     index = _sdf->Get<std::string>("index");
00047   }
00048   else
00049   {
00050     gzerr << "AGV is missing an index. The AGV will not work.\n";
00051   }
00052 
00053   // load parameters
00054   this->robotNamespace = "";
00055   if (_sdf->HasElement("robotNamespace"))
00056   {
00057     this->robotNamespace = _sdf->GetElement(
00058         "robotNamespace")->Get<std::string>() + "/";
00059   }
00060 
00061   // Make sure the ROS node for Gazebo has already been initialized
00062   if (!ros::isInitialized())
00063   {
00064     ROS_FATAL_STREAM("A ROS node for Gazebo has not been initialized,"
00065         << "unable to load plugin. Load the Gazebo system plugin "
00066         << "'libgazebo_ros_api_plugin.so' in the gazebo_ros package)");
00067     return;
00068   }
00069 
00070   this->agvName = std::string("agv") + index;
00071 
00072   std::string agvControlTopic = "/ariac/" + this->agvName;
00073   ROS_DEBUG_STREAM("Using AGV control service topic: " << agvControlTopic);
00074 
00075   std::string submitTrayTopic = "submit_tray";
00076   if (_sdf->HasElement("submit_tray_service_name"))
00077     submitTrayTopic = _sdf->Get<std::string>("submit_tray_service_name");
00078   ROS_DEBUG_STREAM("Using submit tray service topic: " << submitTrayTopic);
00079 
00080   this->rosnode = new ros::NodeHandle(this->robotNamespace);
00081 
00082   this->anim.reset(new gazebo::common::PoseAnimation(this->agvName, 22, false));
00083 
00084   gazebo::common::PoseKeyFrame *key = anim->CreateKeyFrame(0);
00085   key->Translation(ignition::math::Vector3d(0.3, 3.3, 0));
00086   key->Rotation(ignition::math::Quaterniond(0, 0, 3.1415));
00087 
00088   key = anim->CreateKeyFrame(4);
00089   key->Translation(ignition::math::Vector3d(-4.2, 3.8, 0));
00090   key->Rotation(ignition::math::Quaterniond(0, 0, 3.1415));
00091 
00092   key = anim->CreateKeyFrame(10);
00093   key->Translation(ignition::math::Vector3d(-4.2, 9.45, 0));
00094   key->Rotation(ignition::math::Quaterniond(0, 0, 3.1415));
00095 
00096   key = anim->CreateKeyFrame(16);
00097   key->Translation(ignition::math::Vector3d(-4.2, 3.8, 0));
00098   key->Rotation(ignition::math::Quaterniond(0, 0, 3.1415));
00099 
00100   key = anim->CreateKeyFrame(22);
00101   key->Translation(ignition::math::Vector3d(0.3, 3.3, 0));
00102   key->Rotation(ignition::math::Quaterniond(0, 0, 3.1415));
00103 
00104   this->model = _parent;
00105 
00106   this->rosService = this->rosnode->advertiseService(agvControlTopic,
00107       &ROSAGVPlugin::OnCommand, this);
00108 
00109   // Client for submitting trays for inspection.
00110   this->rosSubmitTrayClient =
00111     this->rosnode->serviceClient<osrf_gear::SubmitTray>(submitTrayTopic);
00112 }
00113 
00115 bool ROSAGVPlugin::OnCommand(
00116   osrf_gear::AGVControl::Request &_req,
00117   osrf_gear::AGVControl::Response &_res)
00118 {
00119   bool triggerAnim = this->agvName == "agv1" &&
00120     (anim->GetTime() <= 0.0 || anim->GetTime() >= anim->GetLength());
00121 
00122   if (triggerAnim)
00123   {
00124     anim->SetTime(0);
00125     this->model->SetAnimation(anim);
00126   }
00127 
00128   if (!this->rosSubmitTrayClient.exists())
00129   {
00130     this->rosSubmitTrayClient.waitForExistence();
00131   }
00132 
00133   // Make a service call to submit the tray for inspection.
00134   osrf_gear::SubmitTray srv;
00135   srv.request.tray_id.data = this->agvName + "::kit_tray::tray";
00136   srv.request.kit_type.data = _req.kit_type.data;
00137   this->rosSubmitTrayClient.call(srv);
00138   if (!srv.response.success) {
00139     ROS_ERROR_STREAM("Failed to submit tray for inspection.");
00140     _res.success = false;
00141     return false;
00142   }
00143   ROS_INFO_STREAM("Result of inspection: " << srv.response.inspection_result);
00144   _res.success = true;
00145 
00146   return true;
00147 }


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