ROSInterface.h
Go to the documentation of this file.
00001 /* 
00002  * Copyright (c) 2013 University of Jaume-I.
00003  * All rights reserved. This program and the accompanying materials
00004  * are made available under the terms of the GNU Public License v3.0
00005  * which accompanies this distribution, and is available at
00006  * http://www.gnu.org/licenses/gpl.html
00007  * 
00008  * Contributors:
00009  *     Mario Prats
00010  *     Javier Perez
00011  */
00012 
00013 #ifndef ROSINTERFACE_H_
00014 #define ROSINTERFACE_H_
00015 
00016 //UWSIM
00017 #include "SimulatorConfig.h"
00018 
00019 #include "URDFRobot.h"
00020 #include "SimulatedIAUV.h"
00021 #include "VirtualCamera.h"
00022 #include "VirtualRangeSensor.h"
00023 #include "PressureSensor.h"
00024 #include "GPSSensor.h"
00025 #include "DVLSensor.h"
00026 #include "HUDCamera.h"
00027 #include "MultibeamSensor.h"
00028 #include "UWSimUtils.h"
00029 #include "BulletPhysics.h"
00030 
00031 //OSG
00032 #include <OpenThreads/Thread>
00033 #include <osg/PrimitiveSet>
00034 #include <osg/Geode>
00035 #include <osg/Vec3>
00036 #include <osg/Vec4>
00037 #include <osg/Drawable>
00038 #include <osg/Geometry>
00039 
00040 //STL
00041 #include <vector>
00042 
00043 #include <boost/shared_ptr.hpp>
00044 
00045 //ROS
00046 #include <ros/ros.h>
00047 #include <tf/transform_broadcaster.h>
00048 #include <tf/transform_listener.h>
00049 #include <sensor_msgs/JointState.h>
00050 #include <nav_msgs/Odometry.h>
00051 #include <sensor_msgs/Image.h>
00052 #include <sensor_msgs/CameraInfo.h>
00053 #include <sensor_msgs/distortion_models.h>
00054 #include <sensor_msgs/Range.h>
00055 #include <image_transport/image_transport.h>
00056 #include <geometry_msgs/TwistStamped.h>
00057 #include <geometry_msgs/Pose.h>
00058 //#include <cola2_common/NavigationData.h>
00059 #include <robot_state_publisher/robot_state_publisher.h>
00060 #include <pcl_ros/point_cloud.h>
00061 
00062 //Max time (in seconds) between two consecutive control references
00063 #define MAX_ELAPSED     1
00064 
00065 class ROSInterface : public OpenThreads::Thread
00066 {
00067 protected:
00068   std::string topic;
00069   ros::NodeHandle nh_;
00070   static ros::Time current_time_;
00071 
00072 public:
00073   ROSInterface(std::string topic)
00074   {
00075     this->topic = topic;
00076   }
00077 
00078   virtual ~ROSInterface()
00079   {
00080   }
00081 
00086   static void setROSTime(const ros::Time& time)
00087   {
00088     current_time_ = time;
00089   }
00090 
00096   static ros::Time getROSTime()
00097   {
00098     return current_time_;
00099   }
00100 };
00101 
00102 class ROSSubscriberInterface : public ROSInterface
00103 {
00104 protected:
00105   ros::Subscriber sub_;
00106 public:
00107   ROSSubscriberInterface(std::string topic);
00108 
00109   virtual void createSubscriber(ros::NodeHandle &nh)=0;
00110 
00111   /* Thread code */
00112   void run();
00113 
00114   ~ROSSubscriberInterface();
00115 };
00116 
00117 class ROSOdomToPAT : public ROSSubscriberInterface
00118 {
00119   osg::ref_ptr<osg::MatrixTransform> transform;
00120   ros::WallTime last;
00121   int started;
00122 
00123 public:
00124   ROSOdomToPAT(osg::Group *rootNode, std::string topic, std::string vehicleName);
00125 
00126   virtual void createSubscriber(ros::NodeHandle &nh);
00127 
00128   virtual void processData(const nav_msgs::Odometry::ConstPtr& odom);
00129   ~ROSOdomToPAT();
00130 };
00131 
00132 class ROSTwistToPAT : public ROSSubscriberInterface
00133 {
00134   osg::ref_ptr<osg::MatrixTransform> transform;
00135   ros::WallTime last;
00136   int started;
00137 public:
00138   ROSTwistToPAT(osg::Group *rootNode, std::string topic, std::string vehicleName);
00139 
00140   virtual void createSubscriber(ros::NodeHandle &nh);
00141 
00142   virtual void processData(const geometry_msgs::TwistStamped::ConstPtr& odom);
00143   ~ROSTwistToPAT();
00144 };
00145 
00146 class ROSPoseToPAT : public ROSSubscriberInterface
00147 {
00148   osg::ref_ptr<osg::MatrixTransform> transform;
00149 public:
00150   ROSPoseToPAT(osg::Group *rootNode, std::string topic, std::string vehicleName);
00151 
00152   virtual void createSubscriber(ros::NodeHandle &nh);
00153 
00154   virtual void processData(const geometry_msgs::Pose::ConstPtr& odom);
00155   ~ROSPoseToPAT();
00156 };
00157 
00158 class ROSPointCloudLoader : public ROSSubscriberInterface
00159 {
00160   osg::ref_ptr<osg::Group> scene_root;
00161   unsigned int nodeMask;
00162   osg::ref_ptr < osg::MatrixTransform > lastPCD;
00163   bool deleteLastPCD;
00164 public:
00165   ROSPointCloudLoader(std::string topic, osg::ref_ptr<osg::Group> root, unsigned int mask,bool del);
00166   virtual void createSubscriber(ros::NodeHandle &nh);
00167   virtual void processData(const pcl::PointCloud<pcl::PointXYZ>::ConstPtr& msg);
00168   ~ROSPointCloudLoader();
00169 };
00170 
00171 /*
00172  class ROSNavigationDataToPAT: public ROSSubscriberInterface {
00173  osg::PositionAttitudeTransform *transform;
00174  public:
00175  ROSNavigationDataToPAT(std::string topic, osg::PositionAttitudeTransform *t): ROSSubscriberInterface(topic) {
00176  transform=t;
00177  }
00178 
00179  virtual void createSubscriber(ros::NodeHandle &nh) {
00180  sub_ = nh.subscribe<cola2_common::NavigationData>(topic, 10, &ROSNavigationDataToPAT::processData, this);
00181  }
00182 
00183  virtual void processData(const cola2_common::NavigationData::ConstPtr& odom) {
00184  //Simulated vehicle frame wrt real vehicle frame
00185  vpHomogeneousMatrix vMsv(0.8,0,0.8,0,M_PI,0);
00186 
00187  vpHomogeneousMatrix sMsv;
00188 
00189 
00190  //Set a position reference
00191  //Pose of the real vehicle wrt to the localization origin
00192  vpRotationMatrix pRv(vpRxyzVector(odom->pose[3],odom->pose[4],odom->pose[5]));
00193  vpTranslationVector pTv(odom->pose[0],odom->pose[1],odom->pose[2]);
00194  vpHomogeneousMatrix pMv;
00195  pMv.buildFrom(pTv, pRv);
00196 
00197  //Localization origin wrt simulator origin
00198  vpRxyzVector sRVp(M_PI,0,-M_PI_2);
00199  vpRotationMatrix sRp(sRVp);
00200  vpTranslationVector sTp(-514921,-4677958,3.4);
00201 
00202  vpHomogeneousMatrix sMp;
00203  sMp.buildFrom(sTp,sRp);
00204 
00205  sMsv=sMp*pMv*vMsv;
00206 
00207  if (transform!=NULL) {
00208  //OSG_DEBUG << "SimulatedVehicle::processData baseTransform not null" << std::endl;
00209  transform->setPosition(osg::Vec3d(sMsv[0][3],sMsv[1][3],sMsv[2][3]));
00210  vpRotationMatrix mr;
00211  sMsv.extract(mr);
00212  vpRxyzVector vr(mr);
00213  osg::Quat sQsv(vr[0],osg::Vec3d(1,0,0), vr[1],osg::Vec3d(0,1,0), vr[2],osg::Vec3d(0,0,1));
00214  transform->setAttitude(sQsv);
00215  //baseTransform->setAttitude(osg::Quat(js->pose.pose.orientation.w,osg::Vec3d(0,0,1)));
00216  }
00217  }
00218 
00219  ~ROSNavigationDataToPAT(){}
00220  };
00221  */
00222 
00223 class ROSJointStateToArm : public ROSSubscriberInterface
00224 {
00225   boost::shared_ptr<SimulatedIAUV> arm;
00226 public:
00227   ROSJointStateToArm(std::string topic, boost::shared_ptr<SimulatedIAUV> arm);
00228   virtual void createSubscriber(ros::NodeHandle &nh);
00229 
00230   virtual void processData(const sensor_msgs::JointState::ConstPtr& js);
00231   ~ROSJointStateToArm();
00232 };
00233 
00234 class ROSImageToHUDCamera : public ROSSubscriberInterface
00235 {
00236   boost::shared_ptr<HUDCamera> cam;
00237   boost::shared_ptr<image_transport::ImageTransport> it;
00238   image_transport::Subscriber image_sub;
00239   std::string image_topic;
00240 public:
00241   ROSImageToHUDCamera(std::string topic, std::string info_topic, boost::shared_ptr<HUDCamera> camera);
00242 
00243   virtual void createSubscriber(ros::NodeHandle &nh);
00244 
00245   virtual void processData(const sensor_msgs::ImageConstPtr& msg);
00246   ~ROSImageToHUDCamera();
00247 };
00248 
00249 class ROSPublisherInterface : public ROSInterface
00250 {
00251 protected:
00252   int publish_rate;
00253   ros::Publisher pub_;
00254 public:
00255   ROSPublisherInterface(std::string topic, int publish_rate);
00256 
00257   virtual void createPublisher(ros::NodeHandle &nh)=0;
00258   virtual void publish()=0;
00259 
00260   /* Thread code */
00261   void run();
00262 
00263   ~ROSPublisherInterface();
00264 };
00265 
00266 class PATToROSOdom : public ROSPublisherInterface
00267 {
00268   osg::ref_ptr<osg::MatrixTransform> transform;
00269 public:
00270   PATToROSOdom(osg::Group *rootNode, std::string vehicleName, std::string topic, int rate);
00271 
00272   void createPublisher(ros::NodeHandle &nh);
00273 
00274   void publish();
00275 
00276   ~PATToROSOdom();
00277 };
00278 
00279 class WorldToROSTF : public ROSPublisherInterface
00280 {
00281   std::vector< osg::ref_ptr<osg::MatrixTransform> > transforms_;
00282   std::vector< boost::shared_ptr<robot_state_publisher::RobotStatePublisher> > robot_pubs_;
00283   boost::shared_ptr<tf::TransformBroadcaster> tfpub_;
00284   std::vector< boost::shared_ptr<SimulatedIAUV> > iauvFile_;
00285   std::vector<osg::ref_ptr<osg::Node> > objects_;
00286   std::string worldRootName_; 
00287   unsigned int enableObjects_;
00288   osg::Group *rootNode_;
00289 public:
00290   WorldToROSTF(osg::Group *rootNode, std::vector<boost::shared_ptr<SimulatedIAUV> > iauvFile,  std::vector<osg::ref_ptr<osg::Node> > objects, std::string worldRootName, unsigned int enableObjects, int rate);
00291 
00292   void createPublisher(ros::NodeHandle &nh);
00293 
00294   void publish();
00295 
00296   ~WorldToROSTF();
00297 };
00298 
00299 class ImuToROSImu : public ROSPublisherInterface
00300 {
00301   InertialMeasurementUnit *imu_;
00302 
00303 public:
00304   ImuToROSImu(InertialMeasurementUnit *imu, std::string topic, int rate);
00305 
00306   void createPublisher(ros::NodeHandle &nh);
00307 
00308   void publish();
00309 
00310   ~ImuToROSImu();
00311 };
00312 
00313 class PressureSensorToROS : public ROSPublisherInterface
00314 {
00315   PressureSensor *sensor_;
00316 
00317 public:
00318   PressureSensorToROS(PressureSensor *sensor, std::string topic, int rate);
00319 
00320   void createPublisher(ros::NodeHandle &nh);
00321 
00322   void publish();
00323 
00324   ~PressureSensorToROS();
00325 };
00326 
00327 class GPSSensorToROS : public ROSPublisherInterface
00328 {
00329   GPSSensor *sensor_;
00330 
00331 public:
00332   GPSSensorToROS(GPSSensor *sensor, std::string topic, int rate) :
00333       ROSPublisherInterface(topic, rate), sensor_(sensor)
00334   {
00335   }
00336 
00337   void createPublisher(ros::NodeHandle &nh);
00338 
00339   void publish();
00340 
00341   ~GPSSensorToROS()
00342   {
00343   }
00344 };
00345 
00346 class DVLSensorToROS : public ROSPublisherInterface
00347 {
00348   DVLSensor *sensor_;
00349 
00350 public:
00351   DVLSensorToROS(DVLSensor *sensor, std::string topic, int rate) :
00352       ROSPublisherInterface(topic, rate), sensor_(sensor)
00353   {
00354   }
00355 
00356   void createPublisher(ros::NodeHandle &nh);
00357 
00358   void publish();
00359 
00360   ~DVLSensorToROS()
00361   {
00362   }
00363 };
00364 
00365 class ArmToROSJointState : public ROSPublisherInterface
00366 {
00367   boost::shared_ptr<URDFRobot> arm;
00368 public:
00369   ArmToROSJointState(SimulatedIAUV *arm, std::string topic, int rate);
00370 
00371   void createPublisher(ros::NodeHandle &nh);
00372 
00373   void publish();
00374 
00375   ~ArmToROSJointState();
00376 };
00377 
00378 class VirtualCameraToROSImage : public ROSPublisherInterface
00379 {
00380 
00381   //Updates camera buffer when publisher is not publishing
00382   class CameraBufferCallback : public osg::Camera::DrawCallback
00383   {
00384     public:
00385       virtual void operator () (const osg::Camera& camera) const;
00386       CameraBufferCallback(VirtualCameraToROSImage * publisher,VirtualCamera *camera,int depth);
00387     private:
00388 
00389       VirtualCameraToROSImage * pub;
00390       VirtualCamera *cam;
00391       int depth;
00392   };
00393 
00394   boost::shared_ptr<image_transport::ImageTransport> it;
00395   image_transport::Publisher img_pub_;
00396   std::string image_topic;
00397   VirtualCamera *cam;
00398   int depth;
00399   int bw;
00400 public:
00401 
00402   VirtualCameraToROSImage(VirtualCamera *camera, std::string topic, std::string info_topic, int rate, int depth);
00403 
00404   void createPublisher(ros::NodeHandle &nh);
00405 
00406   void publish();
00407 
00408   ~VirtualCameraToROSImage();
00409 
00410   osg::ref_ptr < osg::Image > osgimage;
00411   OpenThreads::Mutex mutex; //Mutex to avoid image overwriting
00412 
00413 };
00414 
00415 class RangeSensorToROSRange : public ROSPublisherInterface
00416 {
00417   VirtualRangeSensor *rs;
00418 public:
00419   RangeSensorToROSRange(VirtualRangeSensor *rangesensor, std::string topic, int rate);
00420 
00421   void createPublisher(ros::NodeHandle &nh);
00422 
00423   void publish();
00424 
00425   ~RangeSensorToROSRange();
00426 };
00427 
00428 class MultibeamSensorToROS : public ROSPublisherInterface
00429 {
00430   MultibeamSensor *MB;
00431 public:
00432   MultibeamSensorToROS(MultibeamSensor *multibeamSensor, std::string topic, int rate);
00433 
00434   void createPublisher(ros::NodeHandle &nh);
00435 
00436   void publish();
00437 
00438   ~MultibeamSensorToROS();
00439 };
00440 
00441 class contactSensorToROS : public ROSPublisherInterface
00442 {
00443   BulletPhysics * physics;
00444   std::string target;
00445   osg::Group *rootNode;
00446 public:
00447   contactSensorToROS(osg::Group *rootNode, BulletPhysics * physics, std::string target, std::string topic, int rate);
00448 
00449   void createPublisher(ros::NodeHandle &nh);
00450 
00451   void publish();
00452 
00453   ~contactSensorToROS();
00454 };
00455 #endif
00456 


uwsim
Author(s): Mario Prats , Javier Perez
autogenerated on Fri Aug 28 2015 13:28:58