13 #include <std_msgs/String.h> 15 #include <moveit_msgs/ExecuteKnownTrajectory.h> 16 #include <yaml-cpp/exceptions.h> 17 #include <yaml-cpp/mark.h> 19 #include <fanuc_grinding_scanning/ScanningService.h> 20 #include <fanuc_grinding_publish_meshfile/PublishMeshfileService.h> 23 #include <pcl/point_cloud.h> 24 #include <pcl/common/colors.h> 25 #include <pcl/common/transforms.h> 26 #include <pcl/io/davidsdk_grabber.h> 27 #include <pcl/io/ply_io.h> 28 #include <pcl/filters/voxel_grid.h> 50 group->setJointValueTarget(joint_states);
51 if (group->move() ==
false)
62 bool moveRobotScan(fanuc_grinding_scanning::ScanningService::Request &req,
63 fanuc_grinding_scanning::ScanningService::Response &res)
65 std_msgs::String status;
66 status.data =
"Parsing YAML trajectory file";
67 status_pub->publish(status);
70 const std::string
filename= req.YamlFileName;
71 std::string reference_frame;
72 std::string tool_reference;
73 std::vector<double> joint;
74 std::vector<std::vector<double> > joint_list;
83 if(!yamlFile[
"reference"])
86 YAML::Exception yaml_exception(mark,
"Cannot parse 'reference' node");
98 YAML::Exception yaml_exception(mark,
"Cannot parse 'tool' node");
107 if(!yamlFile[
"joint_values"])
110 YAML::Exception yaml_exception(mark,
"Cannot parse 'joint_values' node");
111 throw yaml_exception;
115 for(
unsigned j = 0; j < joint_node.size(); ++ j)
118 joint_list.push_back(joint);
121 catch(
const YAML::Exception &e)
123 res.ReturnStatus =
false;
124 res.ReturnMessage =
"Problem occurred during parsing yaml file for joint values, problem is: " + e.msg;
129 status.data =
"Parsing YAML calibration file";
130 status_pub->publish(status);
131 const std::string filenameCalibration= req.YamlCalibrationFileName;
132 std::vector<double> n_vector;
133 std::vector<double> o_vector;
134 std::vector<double> a_vector;
135 std::vector<double> p_vector;
138 YAML::Node yamlFileCalibration;
143 if(!yamlFileCalibration[
"sls_2_calibration_values"])
146 YAML::Exception yaml_exception(mark,
"Cannot parse 'sls_2_calibration_values'");
147 throw yaml_exception;
154 catch (
const YAML::Exception &e)
156 res.ReturnStatus =
false;
157 res.ReturnMessage =
"Problem parsing YAML calibration file, error:\n" + e.msg;
162 Eigen::Affine3d calib_sls_2;
163 calib_sls_2.matrix() <<
164 n_vector[0], o_vector[0], a_vector[0], p_vector[0],
165 n_vector[1], o_vector[1], a_vector[1], p_vector[1],
166 n_vector[2], o_vector[2], a_vector[2], p_vector[2],
167 n_vector[3], o_vector[3], a_vector[3], p_vector[3];
170 calib_sls_2 = calib_sls_2.inverse();
176 Eigen::Affine3d calibration;
183 status.data =
"Connecting to SLS-2 server...";
184 status_pub->publish(status);
185 std::string davidSDK_ip = req.SLS2IpAddress;
186 std::string davidSDK_server_name = req.SLS2ServerName;
190 ROS_INFO_STREAM(
"scanning: Trying to connect to DavidSDK IP \"" << davidSDK_ip <<
"\" and server name \"" << davidSDK_server_name <<
"\"");
194 ROS_ERROR_STREAM(
"scanning: Cannot connect to the David SLS-2 scanner (IP: " << davidSDK_ip <<
")");
196 res.ReturnStatus =
false;
197 res.ReturnMessage =
"Cannot connect to the David SLS-2 scanner";
200 davidsdk_ptr->setLocalAndRemotePaths(
"/var/tmp/davidsdk/",
"\\\\" + davidSDK_server_name +
"\\davidsdk\\");
203 status.data =
"SLS-2 connected";
204 status_pub->publish(status);
207 PointCloudXYZ::Ptr stacked_point_cloud (
new PointCloudXYZ());
209 for(
unsigned k = 0; k < joint_list.size(); ++k)
213 res.ReturnStatus =
false;
214 res.ReturnMessage =
"Could not move the robot for scanning pose " + boost::lexical_cast<std::string>(k);
224 Eigen::Affine3d matrix_transform;
231 res.ReturnStatus =
false;
232 res.ReturnMessage =
"Could not capture point cloud";
236 status.data =
"Point cloud " + boost::lexical_cast<std::string>(k + 1) +
237 "/" + boost::lexical_cast<std::string>(joint_list.size()) +
" captured";
238 status_pub->publish(status);
241 Eigen::Affine3d resize_pc_meters(Eigen::Affine3d::Identity());
242 resize_pc_meters.matrix() *= 0.001;
244 Eigen::Affine3d transformation_pc(Eigen::Affine3d::Identity());
245 transformation_pc = matrix_transform * calibration * calib_sls_2 * resize_pc_meters;
246 pcl::transformPointCloud(*point_cloud, *point_cloud, transformation_pc);
249 *stacked_point_cloud += *point_cloud;
254 status.data =
"Applying voxel grid on point clouds";
255 status_pub->publish(status);
257 pcl::VoxelGrid<PointXYZ> sor;
258 sor.setLeafSize (req.VoxelGridLeafSize, req.VoxelGridLeafSize, req.VoxelGridLeafSize);
259 sor.setInputCloud (stacked_point_cloud);
260 sor.filter (*stacked_point_cloud);
262 if(stacked_point_cloud->size() == 0)
264 res.ReturnStatus =
false;
265 res.ReturnMessage =
"Filtered point cloud is empty";
270 if (req.CADName.size() < 4)
272 res.ReturnStatus =
false;
273 res.ReturnMessage =
"CAD mesh name is too short!";
277 res.ScanMeshName = req.CADName.substr(0, req.CADName.size() - 4) +
"_scan.ply";
278 pcl::io::savePLYFileBinary(res.ScanMeshName, *stacked_point_cloud);
281 fanuc_grinding_publish_meshfile::PublishMeshfileService srv_publish_meshfile;
283 node->serviceClient<fanuc_grinding_publish_meshfile::PublishMeshfileService>(
"publish_meshfile_service");
286 status.data =
"Publishing final point cloud";
287 status_pub->publish(status);
288 srv_publish_meshfile.request.MeshName = res.ScanMeshName;
289 srv_publish_meshfile.request.MarkerName = req.MarkerName;
290 srv_publish_meshfile.request.PosX =
291 srv_publish_meshfile.request.PosY =
292 srv_publish_meshfile.request.PosZ =
293 srv_publish_meshfile.request.RotX =
294 srv_publish_meshfile.request.RotY =
295 srv_publish_meshfile.request.RotZ = 0.0;
296 srv_publish_meshfile.request.RotW = 1.0;
297 srv_publish_meshfile.request.ColorR = 75 / 255.0;
298 srv_publish_meshfile.request.ColorG = 75 / 255.0;
299 srv_publish_meshfile.request.ColorB = 130 / 255.0;
300 srv_publish_meshfile.request.ColorA = 1.0;
301 publish_meshfile_service.
call(srv_publish_meshfile);
303 res.ReturnStatus =
true;
304 boost::filesystem::path p(res.ScanMeshName);
305 res.ReturnMessage =
"Scan succeeded, file:\n" + p.filename().string();
309 int main(
int argc,
char **argv)
316 group->setPlannerId(
"RRTConnectkConfigDefault");
317 group->setPoseReferenceFrame(
"/base");
318 group->setPlanningTime(2);
321 *status_pub = node->advertise<std_msgs::String>(
"scanning_status", 1);
pcl::PointCloud< PointXYZ > PointCloudXYZ
int main(int argc, char **argv)
bool parseVectorD(const YAML::Node &node, char const *var_name, std::vector< double > &var_value)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
bool moveRobotScan(fanuc_grinding_scanning::ScanningService::Request &req, fanuc_grinding_scanning::ScanningService::Response &res)
pcl::DavidSDKGrabber::Ptr davidsdk_ptr
ROSCPP_DECL void spin(Spinner &spinner)
const YAML::Node parseNode(const YAML::Node &node, char const *var_name)
boost::shared_ptr< ros::NodeHandle > node
bool parseString(const YAML::Node &node, char const *var_name, std::string &var_value)
#define ROS_INFO_STREAM(args)
boost::shared_ptr< ros::Publisher > status_pub
void transformTFToEigen(const tf::Transform &t, Eigen::Isometry3d &e)
#define ROS_ERROR_STREAM(args)
bool executeJointState(const std::vector< double > joint_states)
bool yamlNodeFromFileName(std::string filename, YAML::Node &ynode)
boost::shared_ptr< moveit::planning_interface::MoveGroupInterface > group