reach_event_node.cpp
Go to the documentation of this file.
1 //
2 // Created by tom on 26/06/16.
3 //
4 
5 #include <cmath>
6 #include <ros/ros.h>
8 #include <visualization_msgs/Marker.h>
9 
10 int main(int argc, char **argv) {
11  ros::init(argc, argv, "goal_node");
12  ros::NodeHandle nodeHandle("~");
13  tf::TransformListener listener;
14  tf::StampedTransform transform;
15 
16  ros::Publisher marker_pub=nodeHandle.advertise<visualization_msgs::Marker>("goal_marker", 2, true);
17 
18  ros::Rate loopRate(10);
19 
20  double goal_x,goal_y,goal_z,goal_tol;
21  std::string moving_frame,sys_cmd;
22  nodeHandle.getParam("goal_x", goal_x);
23  nodeHandle.getParam("goal_y", goal_y);
24  nodeHandle.getParam("goal_z", goal_z);
25  nodeHandle.getParam("goal_tol", goal_tol);
26  nodeHandle.getParam("moving_frame", moving_frame);
27  nodeHandle.getParam("sys_cmd", sys_cmd);
28 
29  tf::Vector3 goal(goal_x,goal_y,goal_z);
30  bool inPose = false;
31 #ifdef DEBUG_REACH_EVENT
32  ROS_INFO("[%s]:node is active", ros::this_node::getName().c_str());
33 #endif
34 
35  visualization_msgs::Marker marker;
36  marker.header.frame_id = "map";
37  marker.header.stamp = ros::Time();
38  marker.ns = "/";
39  marker.id = 0;
40  marker.type = visualization_msgs::Marker::SPHERE;
41  marker.action = visualization_msgs::Marker::ADD;
42  marker.pose.position.x = goal_x;
43  marker.pose.position.y = goal_y;
44  marker.pose.position.z = goal_z;
45  marker.pose.orientation.x = 0.0;
46  marker.pose.orientation.y = 0.0;
47  marker.pose.orientation.z = 0.0;
48  marker.pose.orientation.w = 1.0;
49  marker.scale.x = 0.05;
50  marker.scale.y = 0.05;
51  marker.scale.z = 0.05;
52  marker.color.a = 1.0; // Don't forget to set the alpha!
53  marker.color.r = 1.0;
54  marker.color.g = 0.0;
55  marker.color.b = 0.0;
56  marker_pub.publish(marker);
57 
58  while (ros::ok()) {
59 
60  try {
61  listener.lookupTransform("map", moving_frame.c_str(), ros::Time(0), transform);
62 
63  tf::Vector3 mf=transform.getOrigin();
64  double dist=mf.distance(goal);
65  // if (dist<1.0) ROS_INFO("dist: %f",dist);
66 
67  if ((dist <= goal_tol)&&(!inPose)) {
68  inPose = true;
69 #ifdef DEBUG_REACH_EVENT
70  ROS_INFO("GOAL REACHED");
71 #endif
72  FILE *process=popen(sys_cmd.c_str(),"r");
73  if (process!=NULL) ROS_INFO("System command done");
74  else ROS_ERROR("System command fail");
75  ros::shutdown();
76  }
77  }
78  catch (tf::TransformException ex) {
79  // ROS_ERROR("Goal node error: %s",ex.what());
80  }
81 
82 
83  ros::spinOnce();
84  loopRate.sleep();
85  }
86 }
void publish(const boost::shared_ptr< M > &message) const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ROSCPP_DECL const std::string & getName()
TFSIMD_FORCE_INLINE tfScalar distance(const Vector3 &v) const
#define ROS_INFO(...)
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
TFSIMD_FORCE_INLINE Vector3 & getOrigin()
void lookupTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, StampedTransform &transform) const
int main(int argc, char **argv)
bool getParam(const std::string &key, std::string &s) const
ROSCPP_DECL void shutdown()
ROSCPP_DECL void spinOnce()
#define ROS_ERROR(...)


robotican_common
Author(s):
autogenerated on Wed Jan 3 2018 03:48:33