reach_event_node.cpp
Go to the documentation of this file.
00001 //
00002 // Created by tom on 26/06/16.
00003 //
00004 
00005 #include <cmath>
00006 #include <ros/ros.h>
00007 #include <tf/transform_listener.h>
00008 #include <visualization_msgs/Marker.h>
00009 
00010 int main(int argc, char **argv) {
00011     ros::init(argc, argv, "goal_node");
00012     ros::NodeHandle nodeHandle("~");
00013     tf::TransformListener listener;
00014     tf::StampedTransform transform;
00015 
00016     ros::Publisher marker_pub=nodeHandle.advertise<visualization_msgs::Marker>("goal_marker", 2, true);
00017 
00018     ros::Rate loopRate(10);
00019 
00020     double goal_x,goal_y,goal_z,goal_tol;
00021     std::string moving_frame,sys_cmd;
00022     nodeHandle.getParam("goal_x", goal_x);
00023     nodeHandle.getParam("goal_y", goal_y);
00024     nodeHandle.getParam("goal_z", goal_z);
00025     nodeHandle.getParam("goal_tol", goal_tol);
00026     nodeHandle.getParam("moving_frame", moving_frame);
00027     nodeHandle.getParam("sys_cmd", sys_cmd);
00028 
00029     tf::Vector3 goal(goal_x,goal_y,goal_z);
00030     bool inPose = false;
00031 #ifdef DEBUG_REACH_EVENT
00032     ROS_INFO("[%s]:node is active", ros::this_node::getName().c_str());
00033 #endif
00034 
00035     visualization_msgs::Marker marker;
00036     marker.header.frame_id = "map";
00037     marker.header.stamp = ros::Time();
00038     marker.ns = "/";
00039     marker.id = 0;
00040     marker.type = visualization_msgs::Marker::SPHERE;
00041     marker.action = visualization_msgs::Marker::ADD;
00042     marker.pose.position.x = goal_x;
00043     marker.pose.position.y = goal_y;
00044     marker.pose.position.z = goal_z;
00045     marker.pose.orientation.x = 0.0;
00046     marker.pose.orientation.y = 0.0;
00047     marker.pose.orientation.z = 0.0;
00048     marker.pose.orientation.w = 1.0;
00049     marker.scale.x = 0.05;
00050     marker.scale.y = 0.05;
00051     marker.scale.z = 0.05;
00052     marker.color.a = 1.0; // Don't forget to set the alpha!
00053     marker.color.r = 1.0;
00054     marker.color.g = 0.0;
00055     marker.color.b = 0.0;
00056     marker_pub.publish(marker);
00057 
00058     while (ros::ok()) {
00059 
00060         try {
00061             listener.lookupTransform("map", moving_frame.c_str(), ros::Time(0), transform);
00062 
00063             tf::Vector3 mf=transform.getOrigin();
00064             double dist=mf.distance(goal);
00065             // if (dist<1.0) ROS_INFO("dist: %f",dist);
00066 
00067             if ((dist <= goal_tol)&&(!inPose)) {
00068                 inPose = true;
00069 #ifdef DEBUG_REACH_EVENT
00070                 ROS_INFO("GOAL REACHED");
00071 #endif
00072                 FILE *process=popen(sys_cmd.c_str(),"r");
00073                 if (process!=NULL) ROS_INFO("System command done");
00074                 else ROS_ERROR("System command fail");
00075                 ros::shutdown();
00076             }
00077         }
00078         catch (tf::TransformException ex) {
00079             // ROS_ERROR("Goal node error: %s",ex.what());
00080         }
00081 
00082 
00083         ros::spinOnce();
00084         loopRate.sleep();
00085     }
00086 }


robotican_common
Author(s):
autogenerated on Fri Oct 27 2017 03:02:37