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 "ROSConveyorBeltPlugin.hh" 00018 00019 #include <string> 00020 00021 using namespace gazebo; 00022 00023 GZ_REGISTER_MODEL_PLUGIN(ROSConveyorBeltPlugin); 00024 00026 ROSConveyorBeltPlugin::ROSConveyorBeltPlugin() 00027 { 00028 } 00029 00031 ROSConveyorBeltPlugin::~ROSConveyorBeltPlugin() 00032 { 00033 this->rosnode_->shutdown(); 00034 } 00035 00037 void ROSConveyorBeltPlugin::Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf) 00038 { 00039 // load parameters 00040 this->robotNamespace_ = ""; 00041 if (_sdf->HasElement("robot_namespace")) 00042 { 00043 this->robotNamespace_ = _sdf->GetElement( 00044 "robot_namespace")->Get<std::string>() + "/"; 00045 } 00046 00047 // Make sure the ROS node for Gazebo has already been initialized 00048 if (!ros::isInitialized()) 00049 { 00050 ROS_FATAL_STREAM("A ROS node for Gazebo has not been initialized," 00051 << "unable to load plugin. Load the Gazebo system plugin " 00052 << "'libgazebo_ros_api_plugin.so' in the gazebo_ros package)"); 00053 return; 00054 } 00055 00056 std::string topic = "conveyor/control"; 00057 if (_sdf->HasElement("topic")) 00058 topic = _sdf->Get<std::string>("topic"); 00059 00060 ConveyorBeltPlugin::Load(_parent, _sdf); 00061 00062 this->rosnode_ = new ros::NodeHandle(this->robotNamespace_); 00063 00064 this->controlService_ = this->rosnode_->advertiseService(topic, 00065 &ROSConveyorBeltPlugin::OnControlCommand, this); 00066 } 00067 00069 bool ROSConveyorBeltPlugin::OnControlCommand( 00070 osrf_gear::ConveyorBeltControl::Request &_req, 00071 osrf_gear::ConveyorBeltControl::Response &_res) 00072 { 00073 gzdbg << "Control command received\n"; 00074 this->SetVelocity(_req.state.velocity); 00075 _res.success = true; 00076 return true; 00077 }