ROSPopulationPlugin.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 
18 #include <cstdlib>
19 #include <iostream>
20 #include <memory>
21 #include <string>
22 #include <gazebo/common/Console.hh>
23 #include <ros/ros.h>
24 #include <sdf/sdf.hh>
26 #include "osrf_gear/PopulationControl.h"
27 #include "osrf_gear/PopulationState.h"
28 
29 namespace gazebo
30 {
34  {
36  public: std::unique_ptr<ros::NodeHandle> rosnode;
37 
40 
43  };
44 }
45 
46 using namespace gazebo;
47 
49 
52  : PopulationPlugin(),
53  dataPtr(new ROSPopulationPluginPrivate)
54 {
55 }
56 
59 {
60  this->dataPtr->rosnode->shutdown();
61 }
62 
64 void ROSPopulationPlugin::Load(physics::WorldPtr _world, sdf::ElementPtr _sdf)
65 {
66  // Make sure the ROS node for Gazebo has already been initialized
67  if (!ros::isInitialized())
68  {
69  ROS_FATAL_STREAM("A ROS node for Gazebo has not been initialized,"
70  << "unable to load plugin. Load the Gazebo system plugin "
71  << "'libgazebo_ros_api_plugin.so' in the gazebo_ros package)");
72  return;
73  }
74 
75  // Load SDF parameters.
76  std::string robotNamespace = "";
77  if (_sdf->HasElement("robot_namespace"))
78  {
79  robotNamespace = _sdf->GetElement(
80  "robot_namespace")->Get<std::string>() + "/";
81  }
82 
83  std::string controlTopic = "population/control";
84  if (_sdf->HasElement("control_topic"))
85  controlTopic = _sdf->Get<std::string>("control_topic");
86 
87  std::string stateTopic = "population/state";
88  if (_sdf->HasElement("state_topic"))
89  stateTopic = _sdf->Get<std::string>("state_topic");
90 
91  PopulationPlugin::Load(_world, _sdf);
92 
93  this->dataPtr->rosnode.reset(new ros::NodeHandle(robotNamespace));
94 
95  // During competition, this environment variable will be set and the
96  // population control service won't be available.
97  auto v = std::getenv("ARIAC_COMPETITION");
98  if (!v)
99  {
100  // Service for controlling the plugin.
101  this->dataPtr->controlService =
102  this->dataPtr->rosnode->advertiseService(controlTopic,
104  }
105 
106  // Message used for publishing the state of the gripper.
107  this->dataPtr->statePub = this->dataPtr->rosnode->advertise<
108  osrf_gear::PopulationState>(stateTopic, 1000);
109 }
110 
113 {
114  PopulationPlugin::Reset();
115 }
116 
118  osrf_gear::PopulationControl::Request &_req,
119  osrf_gear::PopulationControl::Response &_res)
120 {
121  _res.success = true;
122 
123  if (_req.action == "pause")
124  this->Pause();
125  else if (_req.action == "resume")
126  this->Resume();
127  else if (_req.action == "restart")
128  this->Restart();
129  else
130  {
131  gzerr << "ROSPopulationPlugin::OnPopulationControl() error: Illegal "
132  << "action received: [" << _req.action << "]" << std::endl;
133  _res.success = false;
134  }
135 
136  return _res.success;
137 }
138 
141 {
142  osrf_gear::PopulationState msg;
143  msg.enabled = this->Enabled();
144  this->dataPtr->statePub.publish(msg);
145 }
msg
virtual void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf)
bool Enabled() const
True when the plugin is enabled or false if it&#39;s paused.
GZ_REGISTER_WORLD_PLUGIN(ROSPopulationPlugin)
void Pause()
Pause the object population.
void Resume()
Resume the object population after a pause.
ROSCPP_DECL bool isInitialized()
virtual ~ROSPopulationPlugin()
Destructor.
ros::ServiceServer controlService
Receives service calls to control the plugin.
virtual void Reset()
Documentation inherited.
std::unique_ptr< ros::NodeHandle > rosnode
ROS node handle.
#define ROS_FATAL_STREAM(args)
bool OnPopulationControl(osrf_gear::PopulationControl::Request &_req, osrf_gear::PopulationControl::Response &_res)
Receives messages for controlling the object population plugin on the object population&#39;s topic...
virtual void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf)
virtual void Publish() const
Overwrite this method for sending periodic updates with the plugin state.
std::unique_ptr< ROSPopulationPluginPrivate > dataPtr
A plugin that allows models to be spawned at a given location in a specific simulation time...
ROS interface for the Population plugin.
virtual void Restart()
Restart the the object population.
ros::Publisher statePub
Publishes the state of the plugin.


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