cube_spawner.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 #include <geometry_msgs/Pose.h>
00003 #include <gazebo_msgs/SpawnModel.h>
00004 #include <gazebo_msgs/SpawnModelRequest.h>
00005 #include <gazebo_msgs/SpawnModelResponse.h>
00006 
00007 #include <gazebo_test_tools/gazebo_cube_spawner.h>
00008 
00009 #include <sstream>
00010 
00011 using gazebo_test_tools::GazeboCubeSpawner;
00012 using ros::NodeHandle;
00013 
00014 
00015 #define SPAWN_OBJECT_TOPIC "gazebo/spawn_sdf_model"
00016 
00017 GazeboCubeSpawner::GazeboCubeSpawner(NodeHandle &n) : nh(n){
00018     spawn_object = n.serviceClient<gazebo_msgs::SpawnModel>(SPAWN_OBJECT_TOPIC);
00019 }
00020 
00021 void GazeboCubeSpawner::spawnCube(const std::string& name, const std::string& frame_id,
00022     float x, float y, float z, float qx, float qy, float qz, float qw,
00023     float width, float height, float depth, float mass)
00024 {
00025     spawnPrimitive(name, true, frame_id, x, y, z, qx, qy, qz, qw, width, height, depth, mass);
00026 }
00027 
00028 void GazeboCubeSpawner::spawnPrimitive(const std::string& name, const bool doCube,
00029     const std::string& frame_id,
00030     float x, float y, float z, float qx, float qy, float qz, float qw,
00031     float widthOrRadius, float height, float depth, float _mass)
00032 {
00033 
00034     geometry_msgs::Pose pose;
00035     pose.position.x=x;
00036     pose.position.y=y;
00037     pose.position.z=z;
00038     pose.orientation.x=qx;
00039     pose.orientation.y=qy;
00040     pose.orientation.z=qz;
00041     pose.orientation.w=qw;
00042 
00043     gazebo_msgs::SpawnModel spawn;
00044     spawn.request.model_name=name;
00045 
00046     // just so the variable names are shorter..
00047     float w=widthOrRadius;
00048     float h=height;
00049     float d=depth;
00050 
00051     std::stringstream _s;
00052     if (doCube)
00053     {
00054           _s<<"<box>\
00055             <size>"<<w<<" "<<h<<" "<<d<<"</size>\
00056           </box>";
00057     }else{
00058 
00059           _s<<"<cylinder>\
00060                 <length>"<<h<<"</length>\
00061                 <radius>"<<w<<"</radius>\
00062             </cylinder>";
00063     }
00064     std::string geometryString = _s.str();
00065 
00066 
00067     float mass=_mass;
00068     float mass12=mass/12.0;
00069 
00070     double mu1=500; //500 for PR2 finger tip. In first experiment had it on 1000000
00071     double mu2=mu1;
00072     double kp=10000000; //10000000 for PR2 finger tip
00073     double kd=1; //100 for rubber? 1 fir OR2 finger tip
00074 
00075     bool do_surface=false;
00076     bool do_inertia=true;
00077 
00078     std::stringstream s;\
00079     s<<"<?xml version='1.0'?>\
00080     <sdf version='1.4'>\
00081     <model name='"<<name<<"'>\
00082         <static>false</static>\
00083         <link name='link'>";
00084 
00085     // inertia according to https://en.wikipedia.org/wiki/List_of_moments_of_inertia
00086     if (do_inertia) 
00087     {
00088         double xx, yy, zz;
00089         if (doCube)
00090         {
00091             xx=mass12*(h*h+d*d);
00092             yy=mass12*(w*w+d*d);
00093             zz=mass12*(w*w+h*h);
00094         }
00095         else
00096         {
00097             xx=mass12*(3*w*w + h*h);
00098             yy=mass12*(3*w*w + h*h);
00099             zz=0.5*mass*w*w;
00100         }
00101         s<<"<inertial>\
00102         <mass>"<<mass<<"</mass>\
00103         <inertia>\
00104           <ixx>"<<xx<<"</ixx>\
00105           <ixy>0.0</ixy>\
00106           <ixz>0.0</ixz>\
00107           <iyy>"<<yy<<"</iyy>\
00108           <iyz>0.0</iyz>\
00109           <izz>"<<zz<<"</izz>\
00110         </inertia>\
00111           </inertial>";
00112     }    
00113     s<<"<collision name='collision'>\
00114         <geometry>"<<geometryString;
00115     s<<"</geometry>";
00116     if (do_surface)
00117         s<<"<surface>\
00118             <friction>\
00119               <ode>\
00120             <mu>"<<mu1<<"</mu>\
00121             <mu2>"<<mu2<<"</mu2>\
00122             <fdir1>0.000000 0.000000 0.000000</fdir1>\
00123             <slip1>0.000000</slip1>\
00124             <slip2>0.000000</slip2>\
00125               </ode>\
00126             </friction>\
00127             <bounce>\
00128               <restitution_coefficient>0.000000</restitution_coefficient>\
00129               <threshold>100000.000000</threshold>\
00130             </bounce>\
00131             <contact>\
00132               <ode>\
00133             <soft_cfm>0.000000</soft_cfm>\
00134             <soft_erp>0.200000</soft_erp>\
00135             <kp>"<<kp<<"</kp>\
00136             <kd>"<<kd<<"</kd>\
00137             <max_vel>100.000000</max_vel>\
00138             <min_depth>0.001000</min_depth>\
00139               </ode>\
00140             </contact>\
00141         </surface>";
00142       s<<"</collision>\
00143           <visual name='visual'>";
00144       s<<"<geometry>"<<geometryString;
00145       s<<"</geometry>\
00146         <material>\
00147             <script>\
00148                 <uri>file://media/materials/scripts/gazebo.material</uri> \
00149                 <name>Gazebo/Blue</name>\
00150             </script>\
00151         </material>\
00152           </visual>\
00153         </link>\
00154       </model>\
00155     </sdf>";
00156     
00157     spawn.request.model_xml=s.str();
00158     spawn.request.robot_namespace="cube_spawner";
00159     spawn.request.initial_pose=pose;
00160     spawn.request.reference_frame=frame_id;
00161 
00162     //ROS_INFO("Resulting model: \n %s",s.str().c_str());
00163 
00164     //ROS_INFO("Waiting for service");
00165     spawn_object.waitForExistence();
00166     //ROS_INFO("Calling service");
00167 
00168     //std::cout<<spawn.request<<std::endl;
00169 
00170     if (!spawn_object.call(spawn)) {
00171         ROS_ERROR("Failed to call service %s",SPAWN_OBJECT_TOPIC);
00172     }
00173     ROS_INFO("Result: %s, code %u",spawn.response.status_message.c_str(), spawn.response.success);
00174 }


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