gazebo_ros_api_plugin.h
Go to the documentation of this file.
00001 #ifndef __GAZEBO_ROS_API_PLUGIN_HH__
00002 #define __GAZEBO_ROS_API_PLUGIN_HH__
00003 
00004 /*
00005  *  Gazebo - Outdoor Multi-Robot Simulator
00006  *  Copyright (C) 2003
00007  *     Nate Koenig & Andrew Howard
00008  *
00009  *  This program is free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  This program is distributed in the hope that it will be useful,
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *  GNU General Public License for more details.
00018  *
00019  *  You should have received a copy of the GNU General Public License
00020  *  along with this program; if not, write to the Free Software
00021  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  *
00023  */
00024 
00025 /* Desc: External interfaces for Gazebo
00026  * Author: John Hsu adapted original gazebo main.cc by Nate Koenig
00027  * Date: 25 Apr 2010
00028  * SVN: $Id: main.cc 8598 2010-03-22 21:59:24Z hsujohnhsu $
00029  */
00030 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <signal.h>
00034 #include <errno.h>
00035 #include <iostream>
00036 
00037 #include <tinyxml.h>
00038 
00039 #include "Server.hh"
00040 #include "physics/Physics.hh"
00041 #include "physics/PhysicsEngine.hh"
00042 #include "physics/PhysicsTypes.hh"
00043 #include "physics/Entity.hh"
00044 #include "physics/Collision.hh"
00045 #include "physics/Inertial.hh"
00046 #include "physics/Base.hh"
00047 #include "physics/Link.hh"
00048 #include "physics/Model.hh"
00049 #include "physics/Joint.hh"
00050 #include "common/CommonTypes.hh"
00051 #include "common/Exception.hh"
00052 #include "common/SystemPaths.hh"
00053 #include "common/Plugin.hh"
00054 #include "transport/Node.hh"
00055 //#include "msgs/MessageTypes.hh" // implicitly included from CommonTypes.hh
00056 
00057 // ros
00058 #include <ros/ros.h>
00059 #include <ros/callback_queue.h>
00060 #include <ros/subscribe_options.h>
00061 #include <ros/package.h>
00062 #include <rosgraph_msgs/Clock.h>
00063 
00064 // Services
00065 #include "std_srvs/Empty.h"
00066 
00067 #include "gazebo_msgs/JointRequest.h"
00068 #include "gazebo_msgs/BodyRequest.h"
00069 
00070 #include "gazebo_msgs/SpawnModel.h"
00071 #include "gazebo_msgs/DeleteModel.h"
00072 
00073 #include "gazebo_msgs/ApplyBodyWrench.h"
00074 
00075 #include "gazebo_msgs/SetPhysicsProperties.h"
00076 #include "gazebo_msgs/GetPhysicsProperties.h"
00077 
00078 #include "gazebo_msgs/SetJointProperties.h"
00079 
00080 #include "gazebo_msgs/GetWorldProperties.h"
00081 
00082 #include "gazebo_msgs/GetModelProperties.h"
00083 #include "gazebo_msgs/GetModelState.h"
00084 #include "gazebo_msgs/SetModelState.h"
00085 
00086 #include "gazebo_msgs/GetJointProperties.h"
00087 #include "gazebo_msgs/ApplyJointEffort.h"
00088 
00089 #include "gazebo_msgs/GetLinkProperties.h"
00090 #include "gazebo_msgs/SetLinkProperties.h"
00091 #include "gazebo_msgs/SetLinkState.h"
00092 #include "gazebo_msgs/GetLinkState.h"
00093 
00094 // Topics
00095 #include "gazebo_msgs/ModelState.h"
00096 #include "gazebo_msgs/LinkState.h"
00097 #include "gazebo_msgs/ModelStates.h"
00098 #include "gazebo_msgs/LinkStates.h"
00099 
00100 #include "geometry_msgs/Vector3.h"
00101 #include "geometry_msgs/Wrench.h"
00102 #include "geometry_msgs/Pose.h"
00103 #include "geometry_msgs/Twist.h"
00104 
00105 // For model pose transform to set custom joint angles
00106 #include <ros/ros.h>
00107 #include "LinearMath/btTransform.h"
00108 #include "LinearMath/btVector3.h"
00109 #include <gazebo_msgs/SetModelConfiguration.h>
00110 #include <boost/shared_ptr.hpp>
00111 
00112 // For physics dynamics reconfigure
00113 #include <dynamic_reconfigure/server.h>
00114 #include <gazebo/PhysicsConfig.h>
00115 #include "gazebo_msgs/SetPhysicsProperties.h"
00116 #include "gazebo_msgs/GetPhysicsProperties.h"
00117 
00118 #include <boost/algorithm/string.hpp>
00119 
00120 //#include <tf/transform_broadcaster.h>
00121 
00122 namespace gazebo
00123 {
00124 
00125 class GazeboRosApiPlugin : public SystemPlugin
00126 {
00127   public:
00128     GazeboRosApiPlugin();
00129     ~GazeboRosApiPlugin();
00130 
00131     void Load(int argc, char** argv);
00132 
00134     void gazeboQueueThread();
00135 
00137     void AdvertiseServices();
00138 
00139     void onLinkStatesConnect();
00140     void onModelStatesConnect();
00141     void onLinkStatesDisconnect();
00142     void onModelStatesDisconnect();
00143 
00144     bool spawnURDFModel(gazebo_msgs::SpawnModel::Request &req,gazebo_msgs::SpawnModel::Response &res);
00145     bool spawnGazeboModel(gazebo_msgs::SpawnModel::Request &req,gazebo_msgs::SpawnModel::Response &res);
00146 
00149     bool deleteModel(gazebo_msgs::DeleteModel::Request &req,gazebo_msgs::DeleteModel::Response &res);
00150 
00153     bool getModelState(gazebo_msgs::GetModelState::Request &req,gazebo_msgs::GetModelState::Response &res);
00154 
00157     bool getModelProperties(gazebo_msgs::GetModelProperties::Request &req,gazebo_msgs::GetModelProperties::Response &res);
00158 
00161     bool getWorldProperties(gazebo_msgs::GetWorldProperties::Request &req,gazebo_msgs::GetWorldProperties::Response &res);
00162 
00165     bool getJointProperties(gazebo_msgs::GetJointProperties::Request &req,gazebo_msgs::GetJointProperties::Response &res);
00166 
00169     bool getLinkProperties(gazebo_msgs::GetLinkProperties::Request &req,gazebo_msgs::GetLinkProperties::Response &res);
00170 
00173     bool getLinkState(gazebo_msgs::GetLinkState::Request &req,gazebo_msgs::GetLinkState::Response &res);
00174 
00177     bool setLinkProperties(gazebo_msgs::SetLinkProperties::Request &req,gazebo_msgs::SetLinkProperties::Response &res);
00178 
00181     bool setPhysicsProperties(gazebo_msgs::SetPhysicsProperties::Request &req,gazebo_msgs::SetPhysicsProperties::Response &res);
00182 
00185     bool getPhysicsProperties(gazebo_msgs::GetPhysicsProperties::Request &req,gazebo_msgs::GetPhysicsProperties::Response &res);
00186 
00189     bool setJointProperties(gazebo_msgs::SetJointProperties::Request &req,gazebo_msgs::SetJointProperties::Response &res);
00190 
00193     bool setModelState(gazebo_msgs::SetModelState::Request &req,gazebo_msgs::SetModelState::Response &res);
00194 
00197     void updateModelState(const gazebo_msgs::ModelState::ConstPtr& model_state);
00198 
00201     bool applyJointEffort(gazebo_msgs::ApplyJointEffort::Request &req,gazebo_msgs::ApplyJointEffort::Response &res);
00202 
00205     bool resetSimulation(std_srvs::Empty::Request &req,std_srvs::Empty::Response &res);
00206 
00209     bool resetWorld(std_srvs::Empty::Request &req,std_srvs::Empty::Response &res);
00210 
00213     bool pausePhysics(std_srvs::Empty::Request &req,std_srvs::Empty::Response &res);
00214 
00217     bool unpausePhysics(std_srvs::Empty::Request &req,std_srvs::Empty::Response &res);
00218 
00221     bool clearJointForces(gazebo_msgs::JointRequest::Request &req,gazebo_msgs::JointRequest::Response &res);
00222     bool clearJointForces(std::string joint_name);
00223 
00226     bool clearBodyWrenches(gazebo_msgs::BodyRequest::Request &req,gazebo_msgs::BodyRequest::Response &res);
00227     bool clearBodyWrenches(std::string body_name);
00228 
00231     bool setModelConfiguration(gazebo_msgs::SetModelConfiguration::Request &req,gazebo_msgs::SetModelConfiguration::Response &res);
00232 
00235     bool setLinkState(gazebo_msgs::SetLinkState::Request &req,gazebo_msgs::SetLinkState::Response &res);
00236 
00239     void updateLinkState(const gazebo_msgs::LinkState::ConstPtr& link_state);
00240 
00243     bool applyBodyWrench(gazebo_msgs::ApplyBodyWrench::Request &req,gazebo_msgs::ApplyBodyWrench::Response &res);
00244 
00245     void spin();
00246 
00247   private:
00248     // helper function for applyBodyWrench
00249     void transformWrench(gazebo::math::Vector3 &target_force, gazebo::math::Vector3 &target_torque,
00250                          gazebo::math::Vector3 reference_force, gazebo::math::Vector3 reference_torque,
00251                          gazebo::math::Pose target_to_reference );
00252     gazebo::transport::NodePtr gazebonode_;
00253     gazebo::transport::SubscriberPtr stat_sub_;
00254     gazebo::transport::PublisherPtr factory_pub_;
00255     gazebo::transport::PublisherPtr request_pub_;
00256     gazebo::transport::SubscriberPtr response_sub_;
00257 
00258     ros::NodeHandle* rosnode_;
00259     ros::CallbackQueue gazebo_queue_;
00260     boost::thread* gazebo_callback_queue_thread_;
00261 
00262     gazebo::physics::WorldPtr world;
00263     gazebo::event::ConnectionPtr wrench_update_event_;
00264     gazebo::event::ConnectionPtr force_update_event_;
00265     gazebo::event::ConnectionPtr time_update_event_;
00266     gazebo::event::ConnectionPtr pub_link_states_event_;
00267     gazebo::event::ConnectionPtr pub_model_states_event_;
00268     gazebo::event::ConnectionPtr load_gazebo_ros_api_plugin_event_;
00269 
00270     ros::ServiceServer spawn_urdf_gazebo_service_;
00271     ros::ServiceServer spawn_urdf_model_service_;
00272     ros::ServiceServer delete_model_service_;
00273     ros::ServiceServer get_model_state_service_;
00274     ros::ServiceServer get_model_properties_service_;
00275     ros::ServiceServer get_world_properties_service_;
00276     ros::ServiceServer get_joint_properties_service_;
00277     ros::ServiceServer get_link_properties_service_;
00278     ros::ServiceServer get_link_state_service_;
00279     ros::ServiceServer set_link_properties_service_;
00280     ros::ServiceServer set_physics_properties_service_;
00281     ros::ServiceServer get_physics_properties_service_;
00282     ros::ServiceServer apply_body_wrench_service_;
00283     ros::ServiceServer set_joint_properties_service_;
00284     ros::ServiceServer set_model_state_service_;
00285     ros::ServiceServer apply_joint_effort_service_;
00286     ros::ServiceServer set_model_configuration_service_;
00287     ros::ServiceServer set_link_state_service_;
00288     ros::ServiceServer reset_simulation_service_;
00289     ros::ServiceServer reset_world_service_;
00290     ros::ServiceServer pause_physics_service_;
00291     ros::ServiceServer unpause_physics_service_;
00292     ros::ServiceServer clear_joint_forces_service_;
00293     ros::ServiceServer clear_body_wrenches_service_;
00294     ros::Subscriber    set_link_state_topic_;
00295     ros::Subscriber    set_model_state_topic_;
00296     ros::Publisher     pub_link_states_;
00297     ros::Publisher     pub_model_states_;
00298     int                pub_link_states_connection_count_;
00299     int                pub_model_states_connection_count_;
00300 
00301     // for internal gazebo xml use
00302     std::string xmlPrefix_;
00303     std::string xmlSuffix_;
00304 
00305     // physics dynamic reconfigure
00306     boost::thread* ros_spin_thread_;
00307     boost::thread* physics_reconfigure_thread_;
00308     bool physics_reconfigure_initialized_;
00309     ros::ServiceClient physics_reconfigure_set_client_;
00310     ros::ServiceClient physics_reconfigure_get_client_;
00311     void PhysicsReconfigureCallback(gazebo::PhysicsConfig &config, uint32_t level);
00312     void PhysicsReconfigureNode();
00313 
00314     void OnResponse(ConstResponsePtr &_response);
00315 
00316     ros::Publisher     pub_clock_;
00317 
00319     private: boost::mutex lock_;
00320 
00322     bool IsURDF(std::string model_xml);
00323     bool IsGazeboModelXML(std::string model_xml);
00324     bool IsSDF(std::string model_xml);
00325     void LoadGazeboRosApiPlugin(std::string _worldName);
00326     bool world_created_;
00327 
00328     class WrenchBodyJob
00329     {
00330       public:
00331         gazebo::physics::LinkPtr body;
00332         gazebo::math::Vector3 force;
00333         gazebo::math::Vector3 torque;
00334         ros::Time start_time;
00335         ros::Duration duration;
00336     };
00337 
00338     class ForceJointJob
00339     {
00340       public:
00341         gazebo::physics::JointPtr joint;
00342         double force; // should this be a array?
00343         ros::Time start_time;
00344         ros::Duration duration;
00345     };
00346 
00347     std::vector<GazeboRosApiPlugin::WrenchBodyJob*> wrench_body_jobs;
00348     std::vector<GazeboRosApiPlugin::ForceJointJob*> force_joint_jobs;
00349 
00352     void wrenchBodySchedulerSlot();
00353 
00356     void forceJointSchedulerSlot();
00357 
00360     void publishSimTime(const boost::shared_ptr<gazebo::msgs::WorldStatistics const> &msg);
00361     void publishSimTime();
00362 
00365     void publishLinkStates();
00366 
00369     void publishModelStates();
00370 
00373     void stripXmlDeclaration(std::string &model_xml);
00374 
00377     void updateGazeboXmlModelPose(TiXmlDocument &gazebo_model_xml, gazebo::math::Vector3 initial_xyz, gazebo::math::Quaternion initial_q);
00378 
00381     void updateGazeboXmlName(TiXmlDocument &gazebo_model_xml, std::string model_name);
00382 
00385     void updateGazeboSDFModelPose(TiXmlDocument &gazebo_model_xml, gazebo::math::Vector3 initial_xyz, gazebo::math::Quaternion initial_q);
00386 
00389     void updateGazeboSDFName(TiXmlDocument &gazebo_model_xml, std::string model_name);
00390 
00393     void updateURDFModelPose(TiXmlDocument &gazebo_model_xml, gazebo::math::Vector3 initial_xyz, gazebo::math::Quaternion initial_q);
00394 
00397     void updateURDFName(TiXmlDocument &gazebo_model_xml, std::string model_name);
00398 
00401     void walkChildAddRobotNamespace(TiXmlNode* robot_xml);
00402 
00403     std::string robot_namespace_;
00404 
00407     bool spawnAndConform(TiXmlDocument &gazebo_model_xml, std::string model_name, gazebo_msgs::SpawnModel::Response &res);
00408 };
00409 }
00410 #endif


gazebo
Author(s): Nate Koenig, Andrew Howard, with contributions from many others. See web page for a full credits llist.
autogenerated on Mon Oct 6 2014 12:15:20