debug_trajectory_marker_node.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #include <string>
19 
20 #include <ros/ros.h>
21 #include <tf/tf.h>
22 #include <tf/transform_listener.h>
23 #include <std_msgs/ColorRGBA.h>
24 #include <visualization_msgs/MarkerArray.h>
25 #include <cob_twist_controller/TwistControllerConfig.h>
26 
28 {
32 
34 
35  std::string root_frame_;
36  std::string base_link_;
37  std::string chain_tip_link_;
38  std::string target_frame_;
39 
40  visualization_msgs::Marker tip_marker_;
41  visualization_msgs::Marker target_marker_;
42  visualization_msgs::Marker base_marker_;
43 
44 public:
45  int init()
46  {
47  if (!nh_.getParam("root_frame", this->root_frame_))
48  {
49  ROS_ERROR("Failed to get parameter \"root_frame\".");
50  return -1;
51  }
52 
53  this->base_link_ = "base_link";
54 
55  if (!nh_.getParam("chain_tip_link", this->chain_tip_link_))
56  {
57  ROS_ERROR("Failed to get parameter \"chain_tip_link\".");
58  return -2;
59  }
60 
61  if (!nh_.getParam("frame_tracker/target_frame", this->target_frame_))
62  {
63  ROS_ERROR_STREAM("Please provide a 'frame_tracker/target_frame' parameter in this node's namespace.");
64  return -3;
65  }
66 
67  marker_pub_ = this->nh_.advertise<visualization_msgs::MarkerArray>("trajectory_marker", 1, true);
68  marker_timer_ = this->nh_.createTimer(ros::Duration(0.1), &DebugTrajectoryMarker::publishMarker, this);
69 
70  while (marker_pub_.getNumSubscribers() < 1)
71  {
72  if (ros::isShuttingDown())
73  {
74  return -4;
75  }
76  ROS_WARN_STREAM_ONCE("Please create a subscriber to '" + this->nh_.getNamespace() + "/trajectory_marker' topic (Type: visualization_msgs/MarkerArray)");
77  ros::Duration(1.0).sleep();
78  }
79 
80  std_msgs::ColorRGBA green;
81  green.g = green.a = 1.0;
82  tip_marker_ = getMarker(green);
83  tip_marker_.ns = "tip_marker";
84 
85  std_msgs::ColorRGBA red;
86  red.r = red.a = 1.0;
87  target_marker_ = getMarker(red);
88  target_marker_.ns = "target_marker";
89 
90  std_msgs::ColorRGBA blue;
91  blue.b = blue.a = 1.0;
92  base_marker_ = getMarker(blue);
93  base_marker_.ns = "base_marker";
94 
95  marker_timer_.start();
96 
97  return 0;
98  }
99 
100  void publishMarker(const ros::TimerEvent& event)
101  {
102  visualization_msgs::MarkerArray marker_array;
103  geometry_msgs::Point point;
104 
105  if (tf_listener_.frameExists(this->chain_tip_link_))
106  {
107  if (tip_marker_.points.size() > 10000)
108  {
109  tip_marker_.points.clear();
110  }
111  point = getPoint(this->root_frame_, this->chain_tip_link_);
112  tip_marker_.points.push_back(point);
113  marker_array.markers.push_back(tip_marker_);
114  }
115 
116  if (tf_listener_.frameExists(this->target_frame_))
117  {
118  if (target_marker_.points.size() > 10000)
119  {
120  target_marker_.points.clear();
121  }
122  point = getPoint(this->root_frame_, this->target_frame_);
123  target_marker_.points.push_back(point);
124  marker_array.markers.push_back(target_marker_);
125  }
126 
127  if (tf_listener_.frameExists(this->base_link_))
128  {
129  if (nh_.param("twist_controller/kinematic_extension", 0) == cob_twist_controller::TwistController_BASE_ACTIVE)
130  {
131  if (base_marker_.points.size() > 10000)
132  {
133  base_marker_.points.clear();
134  }
135  point = getPoint(this->root_frame_, this->base_link_);
136  base_marker_.points.push_back(point);
137  marker_array.markers.push_back(base_marker_);
138  }
139  }
140 
141  if (!marker_array.markers.empty())
142  {
143  this->marker_pub_.publish(marker_array);
144  }
145  }
146 
147  visualization_msgs::Marker getMarker(std_msgs::ColorRGBA color)
148  {
149  visualization_msgs::Marker box_marker;
150  box_marker.type = visualization_msgs::Marker::LINE_STRIP;
151  box_marker.lifetime = ros::Duration();
152  box_marker.action = visualization_msgs::Marker::ADD;
153  box_marker.id = 42;
154  box_marker.header.stamp = ros::Time::now();
155  box_marker.header.frame_id = this->root_frame_;
156 
157  box_marker.scale.x = 0.01;
158  box_marker.color = color;
159  box_marker.pose.orientation.w = 1.0;
160 
161  return box_marker;
162  }
163 
164  geometry_msgs::Point getPoint(std::string from, std::string to)
165  {
166  geometry_msgs::Point point;
167  tf::StampedTransform transform;
168  try
169  {
171  tf_listener_.waitForTransform(from, to, now, ros::Duration(0.5));
172  tf_listener_.lookupTransform(from, to, now, transform);
173 
174  point.x = transform.getOrigin().x();
175  point.y = transform.getOrigin().y();
176  point.z = transform.getOrigin().z();
177  }
178  catch (tf::TransformException& ex)
179  {
180  ROS_ERROR("%s", ex.what());
181  }
182 
183  return point;
184  }
185 };
186 
187 
188 int main(int argc, char** argv)
189 {
190  ros::init(argc, argv, "debug_trajectory_marker_node");
191 
193  if (dtm.init() != 0)
194  {
195  ROS_ERROR("Failed to initialize DebugTrajectoryMarker.");
196  return -1;
197  }
198 
199  ros::spin();
200 }
geometry_msgs::Point getPoint(std::string from, std::string to)
double now()
void publish(const boost::shared_ptr< M > &message) const
void publishMarker(const ros::TimerEvent &event)
bool sleep() const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
visualization_msgs::Marker base_marker_
ROSCPP_DECL void spin(Spinner &spinner)
bool waitForTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, const ros::Duration &timeout, const ros::Duration &polling_sleep_duration=ros::Duration(0.01), std::string *error_msg=NULL) const
TFSIMD_FORCE_INLINE const tfScalar & x() const
visualization_msgs::Marker target_marker_
TFSIMD_FORCE_INLINE const tfScalar & z() const
bool param(const std::string &param_name, T &param_val, const T &default_val) const
TFSIMD_FORCE_INLINE const tfScalar & y() const
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
const std::string & getNamespace() const
ROSCPP_DECL bool isShuttingDown()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
int main(int argc, char **argv)
TFSIMD_FORCE_INLINE Vector3 & getOrigin()
void lookupTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, StampedTransform &transform) const
visualization_msgs::Marker tip_marker_
uint32_t getNumSubscribers() const
#define ROS_WARN_STREAM_ONCE(args)
bool getParam(const std::string &key, std::string &s) const
static Time now()
#define ROS_ERROR_STREAM(args)
visualization_msgs::Marker getMarker(std_msgs::ColorRGBA color)
#define ROS_ERROR(...)
bool frameExists(const std::string &frame_id_str) const


cob_twist_controller
Author(s): Felix Messmer , Marco Bezzon , Christoph Mark , Francisco Moreno
autogenerated on Thu Apr 8 2021 02:40:00