evarobot_ros_bumper.cpp
Go to the documentation of this file.
00001 #include <boost/bind.hpp>
00002 #include <stdio.h>
00003 
00004 #include <map>
00005 #include <string>
00006 
00007 #include <gazebo/common/Plugin.hh>
00008 #include "gazebo/physics/physics.hh"
00009 #include "gazebo/common/common.hh"
00010 
00011 #include <gazebo/physics/World.hh>
00012 #include <gazebo/physics/HingeJoint.hh>
00013 #include <gazebo/physics/Contact.hh>
00014 #include <gazebo/sensors/Sensor.hh>
00015 #include <gazebo/sensors/sensors.hh>
00016 #include <gazebo/sensors/ContactSensor.hh>
00017 #include <sdf/sdf.hh>
00018 #include <sdf/Param.hh>
00019 #include <gazebo/common/Exception.hh>
00020 #include <gazebo/sensors/SensorTypes.hh>
00021 
00022 #include <tf/tf.h>
00023 
00024 #include "ros/ros.h"
00025 #include "geometry_msgs/Twist.h"
00026 #include "geometry_msgs/PoseStamped.h"
00027 #include "sensor_msgs/LaserScan.h"
00028 
00029 #include "gazebo/sensors/RaySensor.hh"
00030 #include "gazebo/sensors/SensorManager.hh"
00031 
00032 #include "im_msgs/BumperState.h"
00033 #include "im_msgs/Bumper.h"
00034 
00035 #include "gazebo_msgs/ContactsState.h"
00036 
00037 
00038 namespace gazebo
00039 {   
00040   class ROSBumperPlugin : public ModelPlugin
00041   {   
00042     public: ROSBumperPlugin()
00043     {
00044       // Start up ROS
00045       std::string name1 = "sensor"; // this is what appears in the rostopics
00046       int argc = 0;
00047       ros::init(argc, NULL, name1);      
00048     }
00049     public: ~ROSBumperPlugin()
00050     {
00051       delete this->nh;
00052     }
00053 
00054     public: void Load(physics::ModelPtr _parent, sdf::ElementPtr /*_sdf*/)
00055     {
00056      // Store the pointer to the model
00057       this->model = _parent;
00058 
00059       // ROS Nodehandle
00060       this->nh = new ros::NodeHandle("~");
00061 
00062 
00063       this->nh = new ros::NodeHandle("~");
00064       this->pubbumper = this->nh->advertise<im_msgs::Bumper>("sensor/Bumper",1000);
00065 
00066 //***********************************************
00067           sensors::SensorPtr bumpers[3];
00068                 
00069       bumpers[0] = sensors::SensorManager::Instance()->GetSensor("bumper0");
00070       bumpers[1] = sensors::SensorManager::Instance()->GetSensor("bumper1");
00071       bumpers[2] = sensors::SensorManager::Instance()->GetSensor("bumper2");
00072 
00073 
00074           for(int i = 0; i < 3; i++)
00075           {
00076                   if(!bumpers[i])
00077                         printf("bumper %d is NULL\n", i);
00078                   this->contactsensors[i] = boost::dynamic_pointer_cast<sensors::ContactSensor>(bumpers[i]);
00079           if(!this->contactsensors[i])
00080                         printf("contactsensor %d is NULL\n", i);
00081           }
00082  
00083      
00084 //***********************************************
00085 
00086       // Listen to the update event. This event is broadcast every
00087       // simulation iteration.
00088       // for gazebo 1.4 or lower it should be ConnectWorldUpdateStart
00089       this->updateConnection = event::Events::ConnectWorldUpdateBegin(
00090           boost::bind(&ROSBumperPlugin::OnUpdate, this));
00091           
00092     }
00093 
00094     // Called by the world update start event
00095     public: void OnUpdate()
00096     {
00097       
00098            ros::Time current_time, last_time;
00099             current_time = ros::Time::now();
00100             last_time = ros::Time::now();
00101 
00102           
00103                         // Publish          
00104             im_msgs::Bumper totalbumper;
00105                         
00106             totalbumper.header.stamp=current_time; //ros::Time::now();
00107             totalbumper.header.frame_id="base_scan";
00108             
00109             totalbumper.state.clear();
00110             totalbumper.state.resize(3);
00111             
00112             
00113                         
00114 
00115             
00116             for(int i = 0; i < 3; i++)
00117             {
00118                                 im_msgs::BumperState bumper_state;
00119                                 
00120                                 bumper_state.bumper_state = true;
00121                                 
00122                                 unsigned int collCount;// = this->contactsensors[i]->GetCollisionContactCount(this->contactsensors[i]->GetCollisionName(this->contactsensors[i]->GetCollisionCount()));
00123                                 
00124                                 contact_bilgisi = this->contactsensors[i]->GetContacts();
00125                                         
00126                                 collCount = contact_bilgisi.contact_size();
00127                                 
00128                                 if(collCount > 0)
00129                                         bumper_state.bumper_state = false;
00130                                 
00131                                 totalbumper.state[i] = bumper_state;    
00132                                 
00133                         }
00134             
00135            
00136  
00137             this->pubbumper.publish(totalbumper);
00138                         // Publish (END)
00139 
00140             ros::spinOnce();
00141     }
00142 
00143 
00144     // Pointer to the model
00145     private: physics::ModelPtr model;
00146 
00147     private: sensors::ContactSensorPtr contactsensors[3];
00148     
00149     // Pointer to the update event connection
00150     private: event::ConnectionPtr updateConnection;
00151 
00152     // ROS Nodehandle
00153     private: ros::NodeHandle* nh;
00154 
00155         // ROS Publisher
00156         ros::Publisher pubbumper;
00157         
00158         private: gazebo::msgs::Contacts contact_bilgisi;
00159 
00160 
00161   };
00162 
00163   // Register this plugin with the simulator
00164   GZ_REGISTER_MODEL_PLUGIN(ROSBumperPlugin)
00165 }


evarobot_gazebo
Author(s): Mehmet Akcakoca
autogenerated on Thu Jun 6 2019 18:18:10