SetGazeboPhysicsClient.cpp
Go to the documentation of this file.
00001 #ifdef DOXYGEN_SHOULD_SKIP_THIS
00002 
00021 #endif
00022 
00023 
00024 #include <ros/ros.h>
00025 
00026 #include <gazebo_msgs/SetPhysicsProperties.h>
00027 
00028 #include <string>
00029 
00030 #define DEFAULT_MAX_STEP_SIZE 0.001
00031 #define DEFAULT_MAX_UPDATE_RATE 1000
00032 #define DEFAULT_PRECON_ITERS 0
00033 #define DEFAULT_ITERS 500
00034 #define DEFAULT_W 1.3
00035 #define DEFAULT_RMS_ERROR_TOL 0
00036 #define DEFAULT_CONTACT_SURFACE_LAYER 0.001
00037 #define DEFAULT_CONTACT_MAX_CORRECTING_VEL 100
00038 #define DEFAULT_CFM 0
00039 #define DEFAULT_ERP 0.2
00040 #define DEFAULT_MAX_CONTACTS 20
00041 
00042 bool SetPhysicsProperties(ros::NodeHandle& nh,
00043     const std::string& gazeboPhysicsServiceTopic = "/gazebo/set_physics_properties")
00044 {
00045     float time_step = DEFAULT_MAX_STEP_SIZE;
00046     nh.param("gazebo_physics/max_step_size", time_step, time_step);
00047     ROS_INFO_STREAM("Setting gazebo physics max_step_size " << time_step);
00048 
00049     float max_update_rate = DEFAULT_MAX_UPDATE_RATE;
00050     nh.param("gazebo_physics/max_update_rate", max_update_rate, max_update_rate);
00051     ROS_INFO_STREAM("Setting gazebo physics max_update_rate " << max_update_rate);
00052 
00053     float ode_slv_precon_iters = DEFAULT_PRECON_ITERS;
00054     nh.param("gazebo_physics/ode_slv_precon_iters", ode_slv_precon_iters, ode_slv_precon_iters);
00055     ROS_INFO_STREAM("Setting gazebo physics ode_slv_precon_iters " << ode_slv_precon_iters);
00056 
00057     float ode_slv_iters = DEFAULT_ITERS;
00058     nh.param("gazebo_physics/ode_slv_iters", ode_slv_iters, ode_slv_iters);
00059     ROS_INFO_STREAM("Setting gazebo physics ode_slv_iters " << ode_slv_iters);
00060 
00061     // gazebo displays this as <sor>
00062     float ode_slv_w = DEFAULT_W;
00063     nh.param("gazebo_physics/ode_slv_w", ode_slv_w, ode_slv_w);
00064     ROS_INFO_STREAM("Setting gazebo physics ode_slv_w " << ode_slv_w);
00065 
00066     float ode_slv_rms_error_tol = DEFAULT_RMS_ERROR_TOL;
00067 //    nh.param("gazebo_physics/ode_slv_rms_error_tol",ode_slv_rms_error_tol,ode_slv_rms_error_tol);
00068 //    ROS_INFO_STREAM("Setting gazebo physics ode_slv_rms_error_tol "<<ode_slv_rms_error_tol);
00069 
00070     float contact_surface_layer = DEFAULT_CONTACT_SURFACE_LAYER;
00071     nh.param("gazebo_physics/contact_surface_layer", contact_surface_layer, contact_surface_layer);
00072     ROS_INFO_STREAM("Setting gazebo physics contact_surface_layer " << contact_surface_layer);
00073 
00074     float contact_max_correcting_vel = DEFAULT_CONTACT_MAX_CORRECTING_VEL;
00075     nh.param("gazebo_physics/contact_max_correcting_vel", contact_max_correcting_vel, contact_max_correcting_vel);
00076     ROS_INFO_STREAM("Setting gazebo physics contact_max_correcting_vel " << contact_max_correcting_vel);
00077 
00078     float cfm = DEFAULT_CFM;
00079     nh.param("gazebo_physics/cfm", cfm, cfm);
00080     ROS_INFO_STREAM("Setting gazebo physics cfm " << cfm);
00081 
00082     float erp = DEFAULT_ERP;
00083     nh.param("gazebo_physics/erp", erp, erp);
00084     ROS_INFO_STREAM("Setting gazebo physics erp " << erp);
00085 
00086     float max_contacts = DEFAULT_MAX_CONTACTS;
00087     nh.param("gazebo_physics/max_contacts", max_contacts, max_contacts);
00088     ROS_INFO_STREAM("Setting gazebo physics max_contacts " << max_contacts);
00089 
00090     gazebo_msgs::SetPhysicsProperties srv;
00091     srv.request.time_step = time_step;  // dt in seconds
00092     srv.request.max_update_rate = max_update_rate;  //  throttle maximum physics update rate
00093 
00094     geometry_msgs::Vector3 gravity;  // gravity vector (e.g. earth ~[0,0,-9.81])
00095     gravity.x = 0;
00096     gravity.y = 0;
00097     gravity.z = -9.81;
00098     srv.request.gravity = gravity;
00099 
00100     gazebo_msgs::ODEPhysics ode_config;  // configurations for ODE
00101 
00102     ode_config.auto_disable_bodies = false;  // enable auto disabling of bodies, default false
00103     ode_config.sor_pgs_precon_iters = ode_slv_precon_iters;  // preconditioning inner iterations when uisng projected Gauss Seidel
00104     ode_config.sor_pgs_iters = ode_slv_iters;  // inner iterations when uisng projected Gauss Seidel
00105     ode_config.sor_pgs_w = ode_slv_w;  // relaxation parameter when using projected Gauss Seidel, 1 = no relaxation
00106     ode_config.sor_pgs_rms_error_tol = ode_slv_rms_error_tol;  // rms error tolerance before stopping inner iterations
00107 
00108     ode_config.contact_surface_layer = contact_surface_layer;  // contact "dead-band" width
00109     ode_config.contact_max_correcting_vel = contact_max_correcting_vel;  // contact maximum correction velocity
00110 
00111     ode_config.cfm = cfm;  // global constraint force mixing
00112     ode_config.erp = erp;  // global error reduction parameter
00113 
00114     ode_config.max_contacts = max_contacts;  // maximum contact joints between two geoms
00115 
00116     srv.request.ode_config = ode_config;
00117 
00118     ros::ServiceClient client = nh.serviceClient<gazebo_msgs::SetPhysicsProperties>(gazeboPhysicsServiceTopic);
00119 
00120     if (!client.call(srv))
00121     {
00122         ROS_ERROR("Failed to call gazebo set physics service");
00123         return false;
00124     }
00125 
00126     if (!srv.response.success)    // return true if set wrench successful
00127     {
00128         ROS_ERROR_STREAM("Could not set gazebo physics properties. Error: '" << srv.response.status_message << "'");
00129         return false;
00130     }
00131     return true;
00132 }
00133 
00136 int main(int argc, char **argv)
00137 {
00138     ros::init(argc, argv, "gazebo_set_physics_client");
00139     std::string gazeboPhysicsServiceTopic =  "/gazebo/set_physics_properties";
00140     
00141     if (argc > 1)
00142     {
00143         gazeboPhysicsServiceTopic = argv[1];
00144     }
00145 
00146     ros::NodeHandle nh;
00147     if (!SetPhysicsProperties(nh, gazeboPhysicsServiceTopic))
00148     {
00149         ROS_ERROR("Could not set physics properties.");
00150         return 1;
00151     }
00152     return 0;
00153 }


gazebo_test_tools
Author(s): Jennifer Buehler
autogenerated on Tue May 7 2019 03:29:23