00001 #include <sbpl_arm_planner_node/add_objects_to_map.h>
00002
00003
00004 AddObjectsToMap::AddObjectsToMap()
00005 {
00006 object_in_map_pub_ = nh_.advertise<mapping_msgs::CollisionObject>("collision_object", 1024);
00007 }
00008
00009 bool AddObjectsToMap::parseObjectsFile(FILE* fCfg, std::vector<std::vector<double> > &objects, std::vector<std::string> &object_ids)
00010 {
00011 char sTemp[1024];
00012 int num_obs = 0;
00013
00014 ROS_INFO("parsing objects file...");
00015
00016 if(fCfg == NULL)
00017 {
00018 ROS_INFO("ERROR: unable to open objects file. Exiting.\n");
00019 return false;
00020 }
00021
00022
00023 if(fscanf(fCfg,"%s",sTemp) < 1)
00024 printf("Parsed string has length < 1.\n");
00025
00026 num_obs = atoi(sTemp);
00027
00028 ROS_INFO("%i objects in file",num_obs);
00029
00030
00031 objects.resize(num_obs);
00032 object_ids.clear();
00033 for (int i=0; i < num_obs; ++i)
00034 {
00035 if(fscanf(fCfg,"%s",sTemp) < 1)
00036 printf("Parsed string has length < 1.\n");
00037 object_ids.push_back(sTemp);
00038
00039 objects[i].resize(6);
00040 for(int j=0; j < 6; ++j)
00041 {
00042 if(fscanf(fCfg,"%s",sTemp) < 1)
00043 printf("Parsed string has length < 1.\n");
00044 if(!feof(fCfg) && strlen(sTemp) != 0)
00045 objects[i][j] = atof(sTemp);
00046 }
00047 }
00048
00049 printObjects(stdout);
00050 return true;
00051 }
00052
00053 void AddObjectsToMap::addObjectsFromFile(std::string filename)
00054 {
00055 char* file = new char[filename.length()+1];
00056 filename.copy(file, filename.length(),0);
00057 file[filename.length()] = '\0';
00058 FILE* fCfg = fopen(file, "r");
00059
00060 if(!parseObjectsFile(fCfg, objects_, object_ids_))
00061 {
00062 ROS_ERROR("failed to parse object file: %s", file);
00063 return;
00064 }
00065
00066 addBoxes(objects_, object_ids_);
00067 }
00068
00069 void AddObjectsToMap::addBoxes(std::vector<std::vector<double> > &objects)
00070 {
00071 std::vector<std::string> object_ids(objects.size(), "cube");
00072
00073 for(size_t i = 0; i < object_ids.size(); i++)
00074 object_ids[i] = "cube_" + boost::lexical_cast<std::string>(i);
00075
00076 addBoxes(objects, object_ids);
00077 }
00078
00079 void AddObjectsToMap::addBoxes(std::vector<std::vector<double> > &objects, std::vector<std::string> &object_ids)
00080 {
00081 std::vector<double> dims(3,0);
00082 geometry_msgs::Pose pose;
00083 pose.orientation.x = 0;
00084 pose.orientation.y = 0;
00085 pose.orientation.z = 0;
00086 pose.orientation.w = 1;
00087
00088 if(object_ids.size() != objects.size())
00089 {
00090 ROS_INFO("object id list is not same length as object list. exiting.");
00091 return;
00092 }
00093
00094 for(size_t i = 0; i < objects.size(); i++)
00095 {
00096 pose.position.x = objects[i][0];
00097 pose.position.y = objects[i][1];
00098 pose.position.z = objects[i][2];
00099 dims[0] = objects[i][3];
00100 dims[1] = objects[i][4];
00101 dims[2] = objects[i][5];
00102
00103 addBox(pose, dims, object_ids.at(i));
00104 }
00105 }
00106
00107 void AddObjectsToMap::addBox(geometry_msgs::Pose pose, std::vector<double> &dims, std::string id)
00108 {
00109 mapping_msgs::CollisionObject object;
00110 object.id = id;
00111 object.operation.operation = mapping_msgs::CollisionObjectOperation::ADD;
00112 object.header.frame_id = "base_link";
00113 object.header.stamp = ros::Time::now();
00114
00115 geometric_shapes_msgs::Shape box_object;
00116 box_object.type = geometric_shapes_msgs::Shape::BOX;
00117 box_object.dimensions.resize(3);
00118 box_object.dimensions[0] = dims[0];
00119 box_object.dimensions[1] = dims[1];
00120 box_object.dimensions[2] = dims[2];
00121
00122 object.shapes.push_back(box_object);
00123 object.poses.push_back(pose);
00124
00125 sleep(1);
00126 object_in_map_pub_.publish(object);
00127 ROS_INFO("published %s", id.c_str());
00128 }
00129
00130 void AddObjectsToMap::printObjects(FILE * fOut)
00131 {
00132 if(object_ids_.size() != objects_.size())
00133 {
00134 fprintf(fOut,"object_id list and objects list have different sizes\n");
00135 return;
00136 }
00137
00138 for(size_t i = 0; i < objects_.size(); i++)
00139 {
00140 fprintf(fOut,"%s: %0.2f %0.2f %0.2f %0.2f %0.2f %0.2f\n",object_ids_[i].c_str(),objects_[i][0],objects_[i][1],objects_[i][2],objects_[i][3],objects_[i][4],objects_[i][5]);
00141 }
00142 }
00143
00144 int main(int argc, char** argv)
00145 {
00146 ros::init(argc,argv,"add_objects");
00147 sleep(2);
00148 AddObjectsToMap addobj;
00149 while(true)
00150 {
00151 if(argc > 1)
00152 addobj.addObjectsFromFile(std::string(argv[1]));
00153 else
00154 addobj.addObjectsFromFile(std::string("objects.txt"));
00155
00156 ROS_INFO("added objects to collision map");
00157
00158 sleep(5);
00159 }
00160
00161 ros::spin();
00162 return 0;
00163 }
00164