gazebo_ros_elevator.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015 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 */
18 
19 using namespace gazebo;
20 
22 
25 {
26 }
27 
30 {
31  this->queue_.clear();
32  this->queue_.disable();
33  this->rosnode_->shutdown();
34  this->callbackQueueThread_.join();
35 
36  delete this->rosnode_;
37 }
38 
40 void GazeboRosElevator::Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
41 {
42  // load parameters
43  this->robotNamespace_ = "";
44  if (_sdf->HasElement("robotNamespace"))
45  {
46  this->robotNamespace_ = _sdf->GetElement(
47  "robotNamespace")->Get<std::string>() + "/";
48  }
49 
50  // Make sure the ROS node for Gazebo has already been initialized
51  if (!ros::isInitialized())
52  {
53  ROS_FATAL_STREAM_NAMED("elevator", "A ROS node for Gazebo has not been initialized,"
54  << "unable to load plugin. Load the Gazebo system plugin "
55  << "'libgazebo_ros_api_plugin.so' in the gazebo_ros package)");
56  return;
57  }
58 
59  std::string topic = "elevator";
60  if (_sdf->HasElement("topic"))
61  topic = _sdf->Get<std::string>("topic");
62 
63  ElevatorPlugin::Load(_parent, _sdf);
64 
65  this->rosnode_ = new ros::NodeHandle(this->robotNamespace_);
66 
68  ros::SubscribeOptions::create<std_msgs::String>(topic, 1,
69  boost::bind(&GazeboRosElevator::OnElevator, this, boost::placeholders::_1),
70  ros::VoidPtr(), &this->queue_);
71 
72  this->elevatorSub_ = this->rosnode_->subscribe(so);
73 
74  // start custom queue for elevator
75  this->callbackQueueThread_ =
76  boost::thread(boost::bind(&GazeboRosElevator::QueueThread, this));
77 }
78 
80 void GazeboRosElevator::OnElevator(const std_msgs::String::ConstPtr &_msg)
81 {
82  this->MoveToFloor(std::stoi(_msg->data));
83 }
84 
87 {
88  static const double timeout = 0.01;
89 
90  while (this->rosnode_->ok())
91  this->queue_.callAvailable(ros::WallDuration(timeout));
92 }
boost::shared_ptr< void >
gazebo
gazebo_ros_elevator.h
ros::CallbackQueue::clear
void clear()
gazebo::GazeboRosElevator::callbackQueueThread_
boost::thread callbackQueueThread_
Definition: gazebo_ros_elevator.h:68
gazebo::GazeboRosElevator::rosnode_
ros::NodeHandle * rosnode_
ros node handle
Definition: gazebo_ros_elevator.h:59
gazebo::GazeboRosElevator::queue_
ros::CallbackQueue queue_
Custom Callback Queue.
Definition: gazebo_ros_elevator.h:65
gazebo::GazeboRosElevator::elevatorSub_
ros::Subscriber elevatorSub_
Subscribes to a topic that controls the elevator.
Definition: gazebo_ros_elevator.h:62
gazebo::GazeboRosElevator::OnElevator
void OnElevator(const std_msgs::String::ConstPtr &_msg)
Receives messages on the elevator's topic.
Definition: gazebo_ros_elevator.cpp:80
gazebo::GazeboRosElevator::QueueThread
void QueueThread()
Queu to handle callbacks.
Definition: gazebo_ros_elevator.cpp:86
ROS_FATAL_STREAM_NAMED
#define ROS_FATAL_STREAM_NAMED(name, args)
gazebo::GazeboRosElevator
ROS implementation of the Elevator plugin.
Definition: gazebo_ros_elevator.h:35
gazebo::GZ_REGISTER_MODEL_PLUGIN
GZ_REGISTER_MODEL_PLUGIN(GazeboRosF3D)
ros::isInitialized
ROSCPP_DECL bool isInitialized()
ros::NodeHandle::subscribe
Subscriber subscribe(const std::string &topic, uint32_t queue_size, const boost::function< void(C)> &callback, const VoidConstPtr &tracked_object=VoidConstPtr(), const TransportHints &transport_hints=TransportHints())
gazebo::GazeboRosElevator::Load
void Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
Load the plugin.
Definition: gazebo_ros_elevator.cpp:40
gazebo::GazeboRosElevator::~GazeboRosElevator
virtual ~GazeboRosElevator()
Destructor.
Definition: gazebo_ros_elevator.cpp:29
ros::NodeHandle::ok
bool ok() const
gazebo::GazeboRosElevator::robotNamespace_
std::string robotNamespace_
for setting ROS name space
Definition: gazebo_ros_elevator.h:56
ros::SubscribeOptions
gazebo::GazeboRosElevator::GazeboRosElevator
GazeboRosElevator()
Constructor.
Definition: gazebo_ros_elevator.cpp:24
ros::WallDuration
ros::NodeHandle::shutdown
void shutdown()
ros::CallbackQueue::callAvailable
void callAvailable()
ros::CallbackQueue::disable
void disable()
ros::NodeHandle


gazebo_plugins
Author(s): John Hsu
autogenerated on Thu Sep 5 2024 02:49:55