Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <string>
00018
00019 #include "gazebo/physics/physics.hh"
00020 #include "gazebo/common/common.hh"
00021 #include "gazebo/gazebo.hh"
00022
00023 namespace gazebo
00024 {
00025 class ConveyorController : public WorldPlugin
00026 {
00027 private: transport::PublisherPtr controlPub;
00028 private: transport::SubscriberPtr sensorSub;
00029 private: physics::WorldPtr world;
00030 private: double beltVelocity;
00031
00032 public: void Load(physics::WorldPtr _parent, sdf::ElementPtr )
00033 {
00034 this->world = _parent;
00035
00036 transport::NodePtr node(new transport::Node());
00037
00038
00039 node->Init(_parent->GetName());
00040
00041
00042 std::string sensorStateChangeTopic =
00043 "~/laser/proximity_ray_plugin/state_change";
00044 sensorSub =
00045 node->Subscribe(sensorStateChangeTopic, &ConveyorController::OnSensorStateChange, this);
00046
00047
00048 std::string conveyorControlTopic =
00049 "~/conveyor_belt_contact/conveyor_belt_plugin/control_command";
00050 this->controlPub =
00051 node->Advertise<msgs::Header>(conveyorControlTopic);
00052
00053 this->beltVelocity = 0.5;
00054
00055
00056
00057 msgs::Header msg;
00058 bool controlCommand = true;
00059 double desiredVelocity = controlCommand*this->beltVelocity;
00060 msg.set_str_id(std::to_string(desiredVelocity));
00061 msgs::Set(msg.mutable_stamp(), _parent->GetSimTime());
00062
00063
00064 this->controlPub->Publish(msg);
00065 }
00066
00067 private: void OnSensorStateChange(ConstHeaderPtr& _msg)
00068 {
00069 gzdbg << "Sensor state changed.\n";
00070 msgs::Header msg;
00071
00072 bool sensorValue = _msg->index();
00073 bool controlCommand;
00074 std::string outputFunction = _msg->str_id();
00075 if ("normally_open" == outputFunction) {
00076 controlCommand = !sensorValue;
00077 }
00078 else if ("normally_closed" == outputFunction) {
00079 controlCommand = sensorValue;
00080 }
00081 else {
00082 gzerr << "output_function can only be either normally_open or normally_closed" << std::endl;
00083 return;
00084 }
00085 double desiredVelocity = controlCommand*this->beltVelocity;
00086 msg.set_str_id(std::to_string(desiredVelocity));
00087 msgs::Set(msg.mutable_stamp(), this->world->GetSimTime());
00088
00089
00090 this->controlPub->Publish(msg);
00091 }
00092 };
00093
00094
00095 GZ_REGISTER_WORLD_PLUGIN(ConveyorController)
00096 }