ConveyorController.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012-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 #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 /*_sdf*/)
00033   {
00034     this->world = _parent;
00035     // Create a new transport node
00036     transport::NodePtr node(new transport::Node());
00037 
00038     // Initialize the node with the world name
00039     node->Init(_parent->GetName());
00040 
00041     // Create a subscriber for the proximity sensor output
00042     std::string sensorStateChangeTopic =
00043       "~/laser/proximity_ray_plugin/state_change";
00044     sensorSub =
00045       node->Subscribe(sensorStateChangeTopic, &ConveyorController::OnSensorStateChange, this);
00046 
00047     // Create a publisher for the conveyor control commands
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     // Create the message
00056     // TODO: make custom belt control message (or add Double to standard gazebo msgs...)
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     // Send the message
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     // Send the message
00090     this->controlPub->Publish(msg);
00091   }
00092 };
00093 
00094 // Register this plugin with the simulator
00095 GZ_REGISTER_WORLD_PLUGIN(ConveyorController)
00096 }


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