ROSConveyorController.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 "gazebo/physics/physics.hh"
00018 #include "gazebo/common/common.hh"
00019 #include "gazebo/gazebo.hh"
00020 
00021 #include <string>
00022 
00023 // ROS
00024 #include <osrf_gear/ConveyorBeltControl.h>
00025 #include <osrf_gear/ConveyorBeltState.h>
00026 #include <osrf_gear/LogicalCameraImage.h>
00027 #include <osrf_gear/Model.h>
00028 #include <osrf_gear/Proximity.h>
00029 #include <ros/ros.h>
00030 
00031 namespace gazebo
00032 {
00033 class ROSConveyorController : public WorldPlugin
00034 {
00035   private: ros::NodeHandle* rosnode;
00036   private: ros::ServiceClient controlClient;
00037   private: ros::Subscriber proximitySensorSub;
00038   private: ros::Subscriber breakBeamSub;
00039   private: physics::WorldPtr world;
00040   private: double beltVelocity;
00041 
00042   private: ros::Subscriber logicalCameraImageSub;
00043   private: std::string searchModelType = "unit_box";
00044   private: bool modelDetected = false;
00045 
00046   public: ~ROSConveyorController()
00047   {
00048     this->rosnode->shutdown();
00049   }
00050 
00051   public: void Load(physics::WorldPtr _parent, sdf::ElementPtr /*_sdf*/)
00052   {
00053     // Make sure the ROS node for Gazebo has already been initialized
00054     if (!ros::isInitialized())
00055     {
00056       ROS_FATAL_STREAM("A ROS node for Gazebo has not been initialized, unable to load plugin. "
00057         << "Load the Gazebo system plugin 'libgazebo_ros_api_plugin.so' in the gazebo_ros package)");
00058       return;
00059     }
00060 
00061     this->world = _parent;
00062     this->rosnode = new ros::NodeHandle("");
00063 
00064     // Create a subscriber for the proximity sensor output
00065     std::string sensorStateChangeTopic = "/ariac/proximity_sensor_changed";
00066     this->proximitySensorSub =
00067       this->rosnode->subscribe(sensorStateChangeTopic, 1000,
00068         &ROSConveyorController::OnSensorStateChange, this);
00069 
00070     // Create a subscriber for the break beam output
00071     std::string breakBeamStateChangeTopic = "/ariac/break_beam_changed";
00072     this->breakBeamSub =
00073       this->rosnode->subscribe(breakBeamStateChangeTopic, 1000,
00074         &ROSConveyorController::OnSensorStateChange, this);
00075 
00076     // Create a subscriber for the logical camera images
00077     std::string logicalCameraImageTopic = "/ariac/logical_camera";
00078     this->logicalCameraImageSub =
00079       this->rosnode->subscribe(logicalCameraImageTopic, 1000,
00080         &ROSConveyorController::OnLogicalCameraImage, this);
00081 
00082     // Create a client for the conveyor control commands
00083     std::string conveyorControlTopic = "/conveyor/control";
00084     this->controlClient = this->rosnode->serviceClient<osrf_gear::ConveyorBeltControl>(
00085       conveyorControlTopic);
00086 
00087     this->beltVelocity = 0.5;
00088 
00089     // Turn belt on
00090     this->SendControlRequest(this->beltVelocity);
00091   }
00092 
00093   private: void OnSensorStateChange(const osrf_gear::Proximity::ConstPtr &_msg)
00094   {
00095     gzdbg << "Sensor state changed\n";
00096 
00097     bool sensorValue = _msg->object_detected;
00098     bool controlCommand = !sensorValue; // on (true) or off (false)
00099     this->SendControlRequest(this->beltVelocity * controlCommand);
00100   }
00101 
00102   private: void SendControlRequest(double velocity)
00103   {
00104     osrf_gear::ConveyorBeltState controlMsg;
00105     controlMsg.velocity = velocity;
00106     osrf_gear::ConveyorBeltControl controlRequest;
00107     controlRequest.request.state = controlMsg;
00108     this->controlClient.call(controlRequest);
00109   }
00110 
00111   private: void OnLogicalCameraImage(const osrf_gear::LogicalCameraImage::ConstPtr &_msg)
00112   {
00113     bool modelDetected = false;
00114     for (osrf_gear::Model model : _msg->models)
00115     {
00116       if (model.type == this->searchModelType)
00117       {
00118         modelDetected = true;
00119         break;
00120       }
00121     }
00122     if (modelDetected != this->modelDetected)
00123     {
00124       this->SendControlRequest(!modelDetected * this->beltVelocity);
00125       this->modelDetected = modelDetected;
00126       if (!modelDetected)
00127         gzdbg << "Object no longer detected by logical camera\n";
00128       else
00129         gzdbg << "Object detected by logical camera\n";
00130     }
00131   }
00132 };
00133 
00134 // Register this plugin with the simulator
00135 GZ_REGISTER_WORLD_PLUGIN(ROSConveyorController)
00136 }


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