ROSPopulationPlugin.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 <cstdlib>
00019 #include <iostream>
00020 #include <memory>
00021 #include <string>
00022 #include <gazebo/common/Console.hh>
00023 #include <ros/ros.h>
00024 #include <sdf/sdf.hh>
00025 #include "osrf_gear/ROSPopulationPlugin.hh"
00026 #include "osrf_gear/PopulationControl.h"
00027 #include "osrf_gear/PopulationState.h"
00028 
00029 namespace gazebo
00030 {
00033   struct ROSPopulationPluginPrivate
00034   {
00036     public: std::unique_ptr<ros::NodeHandle> rosnode;
00037 
00039     public: ros::Publisher statePub;
00040 
00042     public: ros::ServiceServer controlService;
00043   };
00044 }
00045 
00046 using namespace gazebo;
00047 
00048 GZ_REGISTER_WORLD_PLUGIN(ROSPopulationPlugin);
00049 
00051 ROSPopulationPlugin::ROSPopulationPlugin()
00052   : PopulationPlugin(),
00053     dataPtr(new ROSPopulationPluginPrivate)
00054 {
00055 }
00056 
00058 ROSPopulationPlugin::~ROSPopulationPlugin()
00059 {
00060   this->dataPtr->rosnode->shutdown();
00061 }
00062 
00064 void ROSPopulationPlugin::Load(physics::WorldPtr _world, sdf::ElementPtr _sdf)
00065 {
00066   // Make sure the ROS node for Gazebo has already been initialized
00067   if (!ros::isInitialized())
00068   {
00069     ROS_FATAL_STREAM("A ROS node for Gazebo has not been initialized,"
00070         << "unable to load plugin. Load the Gazebo system plugin "
00071         << "'libgazebo_ros_api_plugin.so' in the gazebo_ros package)");
00072     return;
00073   }
00074 
00075   // Load SDF parameters.
00076   std::string robotNamespace = "";
00077   if (_sdf->HasElement("robot_namespace"))
00078   {
00079     robotNamespace = _sdf->GetElement(
00080       "robot_namespace")->Get<std::string>() + "/";
00081   }
00082 
00083   std::string controlTopic = "population/control";
00084   if (_sdf->HasElement("control_topic"))
00085     controlTopic = _sdf->Get<std::string>("control_topic");
00086 
00087   std::string stateTopic = "population/state";
00088   if (_sdf->HasElement("state_topic"))
00089     stateTopic = _sdf->Get<std::string>("state_topic");
00090 
00091   PopulationPlugin::Load(_world, _sdf);
00092 
00093   this->dataPtr->rosnode.reset(new ros::NodeHandle(robotNamespace));
00094 
00095   // During competition, this environment variable will be set and the
00096   // population control service won't be available.
00097   auto v = std::getenv("ARIAC_COMPETITION");
00098   if (!v)
00099   {
00100     // Service for controlling the plugin.
00101     this->dataPtr->controlService =
00102       this->dataPtr->rosnode->advertiseService(controlTopic,
00103         &ROSPopulationPlugin::OnPopulationControl, this);
00104   }
00105 
00106   // Message used for publishing the state of the gripper.
00107   this->dataPtr->statePub = this->dataPtr->rosnode->advertise<
00108     osrf_gear::PopulationState>(stateTopic, 1000);
00109 }
00110 
00112 void ROSPopulationPlugin::Reset()
00113 {
00114   PopulationPlugin::Reset();
00115 }
00116 
00117 bool ROSPopulationPlugin::OnPopulationControl(
00118   osrf_gear::PopulationControl::Request &_req,
00119   osrf_gear::PopulationControl::Response &_res)
00120 {
00121   _res.success = true;
00122 
00123   if (_req.action == "pause")
00124     this->Pause();
00125   else if (_req.action == "resume")
00126     this->Resume();
00127   else if (_req.action == "restart")
00128     this->Restart();
00129   else
00130   {
00131     gzerr << "ROSPopulationPlugin::OnPopulationControl() error: Illegal "
00132           << "action received: [" << _req.action << "]" << std::endl;
00133     _res.success = false;
00134   }
00135 
00136   return _res.success;
00137 }
00138 
00140 void ROSPopulationPlugin::Publish() const
00141 {
00142   osrf_gear::PopulationState msg;
00143   msg.enabled = this->Enabled();
00144   this->dataPtr->statePub.publish(msg);
00145 }


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