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 "gazebo/physics/physics.hh"
00018 #include "gazebo/common/common.hh"
00019 #include "gazebo/gazebo.hh"
00020
00021 #include <string>
00022
00023
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 )
00052 {
00053
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
00065 std::string sensorStateChangeTopic = "/ariac/proximity_sensor_changed";
00066 this->proximitySensorSub =
00067 this->rosnode->subscribe(sensorStateChangeTopic, 1000,
00068 &ROSConveyorController::OnSensorStateChange, this);
00069
00070
00071 std::string breakBeamStateChangeTopic = "/ariac/break_beam_changed";
00072 this->breakBeamSub =
00073 this->rosnode->subscribe(breakBeamStateChangeTopic, 1000,
00074 &ROSConveyorController::OnSensorStateChange, this);
00075
00076
00077 std::string logicalCameraImageTopic = "/ariac/logical_camera";
00078 this->logicalCameraImageSub =
00079 this->rosnode->subscribe(logicalCameraImageTopic, 1000,
00080 &ROSConveyorController::OnLogicalCameraImage, this);
00081
00082
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
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;
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
00135 GZ_REGISTER_WORLD_PLUGIN(ROSConveyorController)
00136 }