ROSVacuumGripperPlugin.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 <memory>
00019 #include <string>
00020 #include <ros/ros.h>
00021 #include <sdf/sdf.hh>
00022 #include "osrf_gear/ROSVacuumGripperPlugin.hh"
00023 #include "osrf_gear/VacuumGripperControl.h"
00024 #include "osrf_gear/VacuumGripperState.h"
00025 
00026 namespace gazebo
00027 {
00030   struct ROSVacuumGripperPluginPrivate
00031   {
00033     public: std::unique_ptr<ros::NodeHandle> rosnode;
00034 
00036     public: ros::Publisher statePub;
00037 
00039     public: ros::ServiceServer controlService;
00040   };
00041 }
00042 
00043 using namespace gazebo;
00044 
00045 GZ_REGISTER_MODEL_PLUGIN(ROSVacuumGripperPlugin);
00046 
00048 ROSVacuumGripperPlugin::ROSVacuumGripperPlugin()
00049   : VacuumGripperPlugin(),
00050     dataPtr(new ROSVacuumGripperPluginPrivate)
00051 {
00052 }
00053 
00055 ROSVacuumGripperPlugin::~ROSVacuumGripperPlugin()
00056 {
00057   this->dataPtr->rosnode->shutdown();
00058 }
00059 
00061 void ROSVacuumGripperPlugin::Load(physics::ModelPtr _parent,
00062     sdf::ElementPtr _sdf)
00063 {
00064   // Make sure the ROS node for Gazebo has already been initialized
00065   if (!ros::isInitialized())
00066   {
00067     ROS_FATAL_STREAM("A ROS node for Gazebo has not been initialized,"
00068         << "unable to load plugin. Load the Gazebo system plugin "
00069         << "'libgazebo_ros_api_plugin.so' in the gazebo_ros package)");
00070     return;
00071   }
00072 
00073   // Load SDF parameters.
00074   std::string robotNamespace = "";
00075   if (_sdf->HasElement("robot_namespace"))
00076   {
00077     robotNamespace = _sdf->GetElement(
00078       "robot_namespace")->Get<std::string>() + "/";
00079   }
00080 
00081   std::string controlTopic = "gripper/control";
00082   if (_sdf->HasElement("control_topic"))
00083     controlTopic = _sdf->Get<std::string>("control_topic");
00084 
00085   std::string stateTopic = "gripper/state";
00086   if (_sdf->HasElement("state_topic"))
00087     stateTopic = _sdf->Get<std::string>("state_topic");
00088 
00089   VacuumGripperPlugin::Load(_parent, _sdf);
00090 
00091   this->dataPtr->rosnode.reset(new ros::NodeHandle(robotNamespace));
00092 
00093   // Service for controlling the gripper.
00094   this->dataPtr->controlService =
00095     this->dataPtr->rosnode->advertiseService(controlTopic,
00096       &ROSVacuumGripperPlugin::OnGripperControl, this);
00097 
00098   // Message used for publishing the state of the gripper.
00099   this->dataPtr->statePub = this->dataPtr->rosnode->advertise<
00100     osrf_gear::VacuumGripperState>(stateTopic, 1000);
00101 }
00102 
00104 void ROSVacuumGripperPlugin::Reset()
00105 {
00106   VacuumGripperPlugin::Reset();
00107 }
00108 
00109 bool ROSVacuumGripperPlugin::OnGripperControl(
00110   osrf_gear::VacuumGripperControl::Request &_req,
00111   osrf_gear::VacuumGripperControl::Response &_res)
00112 {
00113   if (_req.enable)
00114     this->Enable();
00115   else
00116     this->Disable();
00117 
00118   _res.success = true;
00119   return _res.success;
00120 }
00121 
00123 void ROSVacuumGripperPlugin::Publish() const
00124 {
00125   osrf_gear::VacuumGripperState msg;
00126   msg.attached = this->Attached();
00127   msg.enabled = this->Enabled();
00128   this->dataPtr->statePub.publish(msg);
00129 }


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