ROSConveyorController.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 "gazebo/physics/physics.hh"
18 #include "gazebo/common/common.hh"
19 #include "gazebo/gazebo.hh"
20 
21 #include <string>
22 
23 // ROS
24 #include <osrf_gear/ConveyorBeltControl.h>
25 #include <osrf_gear/ConveyorBeltState.h>
26 #include <osrf_gear/LogicalCameraImage.h>
27 #include <osrf_gear/Model.h>
28 #include <osrf_gear/Proximity.h>
29 #include <ros/ros.h>
30 
31 namespace gazebo
32 {
33 class ROSConveyorController : public WorldPlugin
34 {
39  private: physics::WorldPtr world;
40  private: double beltVelocity;
41 
43  private: std::string searchModelType = "unit_box";
44  private: bool modelDetected = false;
45 
47  {
48  this->rosnode->shutdown();
49  }
50 
51  public: void Load(physics::WorldPtr _parent, sdf::ElementPtr /*_sdf*/)
52  {
53  // Make sure the ROS node for Gazebo has already been initialized
54  if (!ros::isInitialized())
55  {
56  ROS_FATAL_STREAM("A ROS node for Gazebo has not been initialized, unable to load plugin. "
57  << "Load the Gazebo system plugin 'libgazebo_ros_api_plugin.so' in the gazebo_ros package)");
58  return;
59  }
60 
61  this->world = _parent;
62  this->rosnode = new ros::NodeHandle("");
63 
64  // Create a subscriber for the proximity sensor output
65  std::string sensorStateChangeTopic = "/ariac/proximity_sensor_changed";
66  this->proximitySensorSub =
67  this->rosnode->subscribe(sensorStateChangeTopic, 1000,
69 
70  // Create a subscriber for the break beam output
71  std::string breakBeamStateChangeTopic = "/ariac/break_beam_changed";
72  this->breakBeamSub =
73  this->rosnode->subscribe(breakBeamStateChangeTopic, 1000,
75 
76  // Create a subscriber for the logical camera images
77  std::string logicalCameraImageTopic = "/ariac/logical_camera";
78  this->logicalCameraImageSub =
79  this->rosnode->subscribe(logicalCameraImageTopic, 1000,
81 
82  // Create a client for the conveyor control commands
83  std::string conveyorControlTopic = "/conveyor/control";
84  this->controlClient = this->rosnode->serviceClient<osrf_gear::ConveyorBeltControl>(
85  conveyorControlTopic);
86 
87  this->beltVelocity = 0.5;
88 
89  // Turn belt on
90  this->SendControlRequest(this->beltVelocity);
91  }
92 
93  private: void OnSensorStateChange(const osrf_gear::Proximity::ConstPtr &_msg)
94  {
95  gzdbg << "Sensor state changed\n";
96 
97  bool sensorValue = _msg->object_detected;
98  bool controlCommand = !sensorValue; // on (true) or off (false)
99  this->SendControlRequest(this->beltVelocity * controlCommand);
100  }
101 
102  private: void SendControlRequest(double velocity)
103  {
104  osrf_gear::ConveyorBeltState controlMsg;
105  controlMsg.velocity = velocity;
106  osrf_gear::ConveyorBeltControl controlRequest;
107  controlRequest.request.state = controlMsg;
108  this->controlClient.call(controlRequest);
109  }
110 
111  private: void OnLogicalCameraImage(const osrf_gear::LogicalCameraImage::ConstPtr &_msg)
112  {
113  bool modelDetected = false;
114  for (osrf_gear::Model model : _msg->models)
115  {
116  if (model.type == this->searchModelType)
117  {
118  modelDetected = true;
119  break;
120  }
121  }
122  if (modelDetected != this->modelDetected)
123  {
124  this->SendControlRequest(!modelDetected * this->beltVelocity);
125  this->modelDetected = modelDetected;
126  if (!modelDetected)
127  gzdbg << "Object no longer detected by logical camera\n";
128  else
129  gzdbg << "Object detected by logical camera\n";
130  }
131  }
132 };
133 
134 // Register this plugin with the simulator
136 }
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
void OnLogicalCameraImage(const osrf_gear::LogicalCameraImage::ConstPtr &_msg)
GZ_REGISTER_WORLD_PLUGIN(ROSPopulationPlugin)
void OnSensorStateChange(const osrf_gear::Proximity::ConstPtr &_msg)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ROSCPP_DECL bool isInitialized()
bool call(MReq &req, MRes &res)
void Load(physics::WorldPtr _parent, sdf::ElementPtr)
#define ROS_FATAL_STREAM(args)
void SendControlRequest(double velocity)


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