Advertisement
Guest User

GazeboRosRfidTag.cpp

a guest
May 8th, 2014
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.75 KB | None | 0 0
  1. /* Copyright (C)
  2.  *     Jonas Mellin & Zakiruz Zaman
  3.  * Licensed under the Apache License, Version 2.0 (the "License");
  4.  * you may not use this file except in compliance with the License.
  5.  * You may obtain a copy of the License at
  6.  *
  7.  *     http://www.apache.org/licenses/LICENSE-2.0
  8.  *
  9.  * Unless required by applicable law or agreed to in writing, software
  10.  * distributed under the License is distributed on an "AS IS" BASIS,
  11.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12.  * See the License for the specific language governing permissions and
  13.  * limitations under the License.
  14.  *
  15.  */
  16. /* Desc: RFID Tag
  17.  * Author: Jonas Mellin & Zakiruz Zaman
  18.  * Date: 6th December 2011
  19.  */
  20.  
  21. #include <gazebo/msgs/msgs.hh>
  22.  
  23. #include <map>
  24. #include <string>
  25. #include <gazebo/sensors/SensorTypes.hh>
  26. #include <gazebo/sensors/Sensor.hh>
  27.  
  28. #include <sdf/sdf.hh>
  29. #include <sdf/Param.hh>
  30. #include <gazebo/math/Pose.hh>
  31. #include <gazebo/math/Quaternion.hh>
  32. #include <tf/tf.h>
  33.  
  34.  
  35. #include <gazebo/physics/World.hh>
  36. #include <gazebo/physics/Entity.hh>
  37.  
  38. #include <gazebo/common/Exception.hh>
  39. #include <gazebo/transport/Node.hh>
  40. #include <gazebo/transport/Publisher.hh>
  41. #include <gazebo/math/Vector3.hh>
  42. #include <ros/ros.h>
  43.  
  44. #include <gazebo_plugins/GazeboRosRfidTag.h>
  45. #include <gazebo_plugins/GazeboRosRfid.h>
  46.  
  47. namespace gazebo
  48. {
  49. // Register this plugin with the simulator
  50. GZ_REGISTER_MODEL_PLUGIN(GazeboRosRfidTag);
  51.  
  52. ////////////////////////////////////////////////////////////////////////////////
  53. // Constructor
  54. GazeboRosRfidTag::GazeboRosRfidTag() : SensorPlugin()
  55. {
  56. }
  57.  
  58. ////////////////////////////////////////////////////////////////////////////////
  59. // Destructor
  60. GazeboRosRfidTag::~GazeboRosRfidTag()
  61. {
  62.   this->rosnode_->shutdown();
  63.   this->callback_queue_thread_.join();
  64.  
  65.   delete this->rosnode_;
  66. }
  67.  
  68. /////////////////////////////////////////////////
  69. void GazeboRosRfidTag::Load( physics::ModelPtr _parent, sdf::ElementPtr _sdf )
  70. {
  71.   this->parentSensor = boost::dynamic_pointer_cast<sensors::RFIDTag>(_parent);
  72.   if (!this->parentSensor)
  73.   {
  74.     ROS_ERROR("Rfid tag parent is not of type RFIDTag");
  75.     return;
  76.   }
  77.  
  78.   this->robot_namespace_ = "";
  79.   if (_sdf->HasElement("robotNamespace"))
  80.     this->robot_namespace_ =
  81.       _sdf->GetElement("robotNamespace")->Get<std::string>() + "/";
  82.  
  83.   // "publishing contact/collisions to this topic name: "
  84.   // << this->bumper_topic_name_ << std::endl;
  85.   this->scan_topic_name_ = "rfidscan_poses";
  86.   if (_sdf->GetElement("scanTopicName"))
  87.     this->scan_topic_name_ =
  88.       _sdf->GetElement("scanTopicName")->Get<std::string>();
  89.  
  90.    // Make sure the ROS node for Gazebo has already been initalized
  91.   if (!ros::isInitialized())
  92.   {
  93.     ROS_FATAL_STREAM("A ROS node for Gazebo has not been initialized, unable to load plugin. "
  94.       << "Load the Gazebo system plugin 'libgazebo_ros_api_plugin.so' in the gazebo_ros package)");
  95.     return;
  96.   }
  97.  
  98.   this->rosnode_ = new ros::NodeHandle(this->robot_namespace_);
  99.   // Add the tag to all the RFID sensors.
  100.   Sensor_V sensors = SensorManager::Instance()->GetSensors();
  101.   for (Sensor_V::iterator iter = sensors.begin(); iter != sensors.end(); ++iter)
  102.   {
  103.     if ((*iter)->GetType() == "rfid")
  104.     {
  105.       boost::shared_dynamic_cast<GazeboRosRfid>(*iter)->AddTag(this);
  106.     }
  107.   }
  108.  
  109.   // Initialize
  110.   // start custom queue for contact bumper
  111.   this->callback_queue_thread_ = boost::thread(
  112.       boost::bind(&GazeboRosRfidTag::ScanQueueThread, this));
  113.  
  114.   // Listen to the update event. This event is broadcast every
  115.   // simulation iteration.
  116.   this->update_connection_ = this->parentSensor->ConnectUpdated(
  117.      boost::bind(&GazeboRosRfidTag::OnScan, this));
  118.  
  119.   // Make sure the parent sensor is active.
  120.   this->parentSensor->SetActive(true);
  121. }
  122.  
  123. //////////////////////////////////////////////////
  124. void GazeboRosRfidTag::OnScan()
  125. {
  126.   if (this->scanPub)
  127.   {
  128.     msgs::Pose msg;
  129.     msgs::Set(&msg, entity->GetWorldPose());
  130.  
  131.     // msg.set_position(link->GetWorldPose().pos);
  132.     // msg.set_orientation(link->GetWorldPose().rot);
  133.     // msgs::LaserScan msg;
  134.  
  135.     // msg.set_frame(this->link->GetScopedName());
  136.     // msgs::Set(msg.mutable_offset(), this->GetPose());
  137.     // msg.set_angle_min( this->GetAngleMin().Radian() );
  138.     // msg.set_angle_max( this->GetAngleMax().Radian() );
  139.     // msg.set_angle_step( this->GetAngleResolution() );
  140.  
  141.     // msg.set_range_min( this->GetRangeMin() );
  142.     // msg.set_range_max( this->GetRangeMax() );
  143.  
  144.     // for (unsigned int i=0; i < (unsigned int)this->GetRangeCount(); i++)
  145.     // {
  146.     //   msg.add_ranges(this->laserShape->GetRange(i));
  147.     //   msg.add_intensities(0);
  148.     // }
  149.  
  150.     this->scanPub->Publish(msg);
  151.     // std::cout << "update impl for rfidtag called" << std::endl;
  152.   }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement