00001
00061 #include <ros/ros.h>
00062 #include <stdio.h>
00063 #include <vector>
00064
00065 #include <mapping_msgs/CollisionObject.h>
00066 #include <geometric_shapes_msgs/Shape.h>
00067 #include <tinyxml/tinyxml.h>
00068 #include <urdf/model.h>
00069 #include <planning_models/kinematic_model.h>
00070 #include <tf/tf.h>
00071 #include <planning_environment/util/construct_object.h>
00072 #include <gazebo/GetModelState.h>
00073
00074
00075 shapes::Shape* constructShape(const urdf::Geometry *geom)
00076 {
00077 ROS_ASSERT(geom);
00078
00079 shapes::Shape *result = NULL;
00080 if(geom->type == urdf::Geometry::BOX)
00081 {
00082 ROS_INFO("BOX");
00083 urdf::Vector3 dim = dynamic_cast<const urdf::Box*>(geom)->dim;
00084 result = new shapes::Box(dim.x, dim.y, dim.z);
00085 }
00086 else if(geom->type == urdf::Geometry::SPHERE)
00087 {
00088 ROS_INFO("SPHERE");
00089 result = new shapes::Sphere(dynamic_cast<const urdf::Sphere*>(geom)->radius);
00090 }
00091 else if(geom->type == urdf::Geometry::CYLINDER)
00092 {
00093 ROS_INFO("CYLINDER");
00094 result = new shapes::Cylinder(dynamic_cast<const urdf::Cylinder*>(geom)->radius, dynamic_cast<const urdf::Cylinder*>(geom)->length);
00095 }
00096 else if(geom->type == urdf::Geometry::MESH)
00097 {
00098
00099 ROS_INFO("MESH --- currently not supported");
00100 }
00101 else
00102 {
00103 ROS_ERROR("Unknown geometry type: %d", (int)geom->type);
00104 }
00105
00106 return result;
00107 }
00108
00109
00110 int main(int argc, char** argv) {
00111
00112 ros::init(argc, argv, "addURDF");
00113
00114 const std::string frame_id = "/map";
00115
00116 ros::NodeHandle nh;
00117
00118 ros::Publisher object_in_map_pub_;
00119 object_in_map_pub_ = nh.advertise<mapping_msgs::CollisionObject>("collision_object", 20);
00120
00121
00122 std::string parameter_name = "world_description";
00123 std::string model_name = "urdf_world_model";
00124
00125
00126
00127 while(!nh.hasParam(parameter_name))
00128 {
00129 ROS_WARN("waiting for parameter \"world_description\"... ");
00130 ros::Duration(0.5).sleep();
00131 }
00132
00133
00134 urdf::Model model;
00135 if (!model.initParam(parameter_name))
00136 {
00137 ROS_ERROR("Failed to parse %s from parameter server", parameter_name.c_str());
00138 return -1;
00139 }
00140
00141 ROS_INFO("Successfully parsed urdf file");
00142
00143 std::vector< boost::shared_ptr< urdf::Link > > URDF_links;
00144 model.getLinks(URDF_links);
00145 std::map< std::string, boost::shared_ptr< urdf::Joint > > URDF_joints = model.joints_;
00146 std::map< std::string, boost::shared_ptr< urdf::Joint > >::iterator joints_it;
00147
00148
00149
00150
00151 for(joints_it=URDF_joints.begin() ; joints_it != URDF_joints.end(); joints_it++)
00152 {
00153 ROS_INFO("Joint name: %s", (*joints_it).first.c_str());
00154 ROS_INFO("\t origin: %f,%f,%f", (*joints_it).second->parent_to_joint_origin_transform.position.x, (*joints_it).second->parent_to_joint_origin_transform.position.y, (*joints_it).second->parent_to_joint_origin_transform.position.z);
00155 }
00156
00157 ros::service::waitForService("/gazebo/get_model_state");
00158 ros::service::waitForService("/cob3_environment_server/get_state_validity");
00159
00160
00161 ros::ServiceClient client = nh.serviceClient<gazebo::GetModelState>("/gazebo/get_model_state");
00162 gazebo::GetModelState srv;
00163
00164 srv.request.model_name = model_name;
00165 if (client.call(srv))
00166 {
00167 ROS_INFO("URDFPose (x,y,z): (%f,%f,%f)", srv.response.pose.position.x, srv.response.pose.position.y, srv.response.pose.position.z);
00168 }
00169 else
00170 {
00171 ROS_ERROR("Failed to call service get_model_state");
00172 ros::shutdown();
00173 }
00174
00175 mapping_msgs::CollisionObject collision_object;
00176 collision_object.id = model_name + "_object";
00177 collision_object.operation.operation = mapping_msgs::CollisionObjectOperation::ADD;
00178
00179 collision_object.header.frame_id = frame_id;
00180 collision_object.header.stamp = ros::Time::now();
00181 collision_object.shapes.resize(URDF_links.size());
00182 collision_object.poses.resize(URDF_links.size());
00183
00184
00185 joints_it=URDF_joints.begin();
00186 for(unsigned int i=0; i<URDF_links.size(); i++)
00187 {
00188 urdf::Link current_link = *URDF_links[i];
00189 ROS_INFO("Current Link: %s", current_link.name.c_str());
00190
00191 if(current_link.name == "dummy_link")
00192 {
00193 ROS_INFO("Dealing with dummy_link...");
00194 continue;
00195 }
00196
00197 boost::shared_ptr< urdf::Joint > current_parent_joint = current_link.parent_joint;
00198
00199
00200
00201
00202 shapes::Shape *current_shape;
00203 current_shape = constructShape(current_link.collision->geometry.get());
00204 ROS_INFO("shape.type: %d", current_shape->type);
00205
00206
00207
00208 tf::Pose pose;
00209 tf::Transform world2dummy;
00210 tf::Transform dummy2link;
00211 world2dummy = tf::Transform(tf::Quaternion(srv.response.pose.orientation.x, srv.response.pose.orientation.y, srv.response.pose.orientation.z, srv.response.pose.orientation.w), tf::Vector3(srv.response.pose.position.x, srv.response.pose.position.y, srv.response.pose.position.z));
00212 dummy2link = tf::Transform(tf::Quaternion(current_parent_joint->parent_to_joint_origin_transform.rotation.x,
00213 current_parent_joint->parent_to_joint_origin_transform.rotation.y,
00214 current_parent_joint->parent_to_joint_origin_transform.rotation.z,
00215 current_parent_joint->parent_to_joint_origin_transform.rotation.w),
00216 tf::Vector3(current_parent_joint->parent_to_joint_origin_transform.position.x,
00217 current_parent_joint->parent_to_joint_origin_transform.position.y,
00218 current_parent_joint->parent_to_joint_origin_transform.position.z));
00219 tf::Pose pose2;
00220 pose2.mult(world2dummy, dummy2link);
00221
00222 tf::Stamped<tf::Pose> stamped_pose_in;
00223 stamped_pose_in.stamp_ = ros::Time::now();
00224 stamped_pose_in.frame_id_ = frame_id;
00225 stamped_pose_in.setData(pose2);
00226
00227
00228
00230 geometric_shapes_msgs::Shape msg_shape;
00231 planning_environment::constructObjectMsg(current_shape, msg_shape);
00232
00233 geometry_msgs::PoseStamped msg_pose_stamped;
00234
00235 tf::poseStampedTFToMsg (stamped_pose_in, msg_pose_stamped);
00236
00237 collision_object.shapes[i] = msg_shape;
00238 collision_object.poses[i] = msg_pose_stamped.pose;
00239
00240 object_in_map_pub_.publish(collision_object);
00241
00242 ROS_INFO("Should have published");
00243 }
00244
00245 ros::shutdown();
00246 }
00247
00248
00249