ConveyorController.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-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 #include <string>
18 
19 #include "gazebo/physics/physics.hh"
20 #include "gazebo/common/common.hh"
21 #include "gazebo/gazebo.hh"
22 
23 namespace gazebo
24 {
25 class ConveyorController : public WorldPlugin
26 {
27  private: transport::PublisherPtr controlPub;
28  private: transport::SubscriberPtr sensorSub;
29  private: physics::WorldPtr world;
30  private: double beltVelocity;
31 
32  public: void Load(physics::WorldPtr _parent, sdf::ElementPtr /*_sdf*/)
33  {
34  this->world = _parent;
35  // Create a new transport node
36  transport::NodePtr node(new transport::Node());
37 
38  // Initialize the node with the world name
39  node->Init(_parent->GetName());
40 
41  // Create a subscriber for the proximity sensor output
42  std::string sensorStateChangeTopic =
43  "~/laser/proximity_ray_plugin/state_change";
44  sensorSub =
45  node->Subscribe(sensorStateChangeTopic, &ConveyorController::OnSensorStateChange, this);
46 
47  // Create a publisher for the conveyor control commands
48  std::string conveyorControlTopic =
49  "~/conveyor_belt_contact/conveyor_belt_plugin/control_command";
50  this->controlPub =
51  node->Advertise<msgs::Header>(conveyorControlTopic);
52 
53  this->beltVelocity = 0.5;
54 
55  // Create the message
56  // TODO: make custom belt control message (or add Double to standard gazebo msgs...)
57  msgs::Header msg;
58  bool controlCommand = true;
59  double desiredVelocity = controlCommand*this->beltVelocity;
60  msg.set_str_id(std::to_string(desiredVelocity));
61  msgs::Set(msg.mutable_stamp(), _parent->GetSimTime());
62 
63  // Send the message
64  this->controlPub->Publish(msg);
65  }
66 
67  private: void OnSensorStateChange(ConstHeaderPtr& _msg)
68  {
69  gzdbg << "Sensor state changed.\n";
70  msgs::Header msg;
71 
72  bool sensorValue = _msg->index();
73  bool controlCommand;
74  std::string outputFunction = _msg->str_id();
75  if ("normally_open" == outputFunction) {
76  controlCommand = !sensorValue;
77  }
78  else if ("normally_closed" == outputFunction) {
79  controlCommand = sensorValue;
80  }
81  else {
82  gzerr << "output_function can only be either normally_open or normally_closed" << std::endl;
83  return;
84  }
85  double desiredVelocity = controlCommand*this->beltVelocity;
86  msg.set_str_id(std::to_string(desiredVelocity));
87  msgs::Set(msg.mutable_stamp(), this->world->GetSimTime());
88 
89  // Send the message
90  this->controlPub->Publish(msg);
91  }
92 };
93 
94 // Register this plugin with the simulator
96 }
msg
GZ_REGISTER_WORLD_PLUGIN(ROSPopulationPlugin)
transport::PublisherPtr controlPub
void OnSensorStateChange(ConstHeaderPtr &_msg)
void Load(physics::WorldPtr _parent, sdf::ElementPtr)
transport::SubscriberPtr sensorSub


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