path_publisher.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 #include <nav_msgs/Path.h>
3 #include <geometry_msgs/PoseStamped.h>
5 #include <vector>
6 #include <string>
7 #include "init_utils.h"
8 
10  double wait_for_transform_duration;
11  ros::param::param<double>("wait_for_transform_duration", wait_for_transform_duration, 3.0);
12 
13  return wait_for_transform_duration;
14 }
15 
16 static std::string tf_base_link_frame_id() {
17  std::string base_link_frame_id;
18  ros::param::param<std::string>("base_link_frame_id", base_link_frame_id, "base_link");
19 
20  return base_link_frame_id;
21 }
22 
23 static int subscriber_queue_size() {
25  ros::param::param<int>("subscribers_queue_size", subscriber_queue_size, 1000);
26 
27  return subscriber_queue_size;
28 }
29 
30 static double publishing_rate() {
31  double publishing_rate;
32  ros::param::param<double>("publishing_rate", publishing_rate, 5.0);
33 
34  return publishing_rate;
35 }
36 
37 static std::string path_topic_name() {
38  std::string path_topic_name;
39  ros::param::param<std::string>("path_topic_name", path_topic_name, "path");
40 
41  return path_topic_name;
42 }
43 
44 // Returns stamped base_link pose.
45 // In case of failure reports an error and does retries.
46 // If ros::shutdown is issued, will return a dummy pose object.
47 static geometry_msgs::PoseStamped get_current_pose() {
48  tf::TransformListener tf_listener;
49  tf::StampedTransform stamped_transform;
50 
51  while (ros::ok()) {
52  try {
53  auto stamp_latest = ros::Time(0);
54  auto map_frame_id = tf_map_frame_id();
55  auto robot_frame_id = tf_base_link_frame_id();
56 
57  tf_listener.waitForTransform(map_frame_id, robot_frame_id, stamp_latest, ros::Duration(tf_wait_for_transform_duration()));
58  tf_listener.lookupTransform(map_frame_id, robot_frame_id, stamp_latest, stamped_transform);
59 
60  geometry_msgs::PoseStamped pose_stamped;
61  pose_stamped.header.frame_id = stamped_transform.frame_id_;
62  pose_stamped.header.stamp = stamped_transform.stamp_;
63  pose_stamped.pose.position.x = stamped_transform.getOrigin().x();
64  pose_stamped.pose.position.y = stamped_transform.getOrigin().y();
65 
66  return pose_stamped;
67  }
68  catch (tf::TransformException &ex) {
69  ROS_ERROR("%s", ex.what());
70  ros::Duration(1.0).sleep();
71  continue;
72  }
73  }
74 
75  return geometry_msgs::PoseStamped();
76 }
77 
78 
79 int main(int argc, char **argv) {
80  ros::init(argc, argv, "path_publisher");
81 
82  auto node_handle = ros::NodeHandle();
83  auto publisher = node_handle.advertise<nav_msgs::Path>(path_topic_name(), subscriber_queue_size());
84  auto loop_rate = ros::Rate(publishing_rate());
85 
86  nav_msgs::Path path;
87  path.header.frame_id = tf_map_frame_id();
88 
89  while(node_handle.ok()) {
90  auto pose_stamped = get_current_pose();
91  path.header.stamp = pose_stamped.header.stamp;
92  path.poses.push_back(pose_stamped);
93 
94  publisher.publish(path);
95 
96  loop_rate.sleep();
97  }
98 
99  return 0;
100 }
bool sleep() const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
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
int main(int argc, char **argv)
static double tf_wait_for_transform_duration()
static std::string tf_base_link_frame_id()
ROSCPP_DECL bool ok()
TFSIMD_FORCE_INLINE const tfScalar & y() const
TFSIMD_FORCE_INLINE Vector3 & getOrigin()
void lookupTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, StampedTransform &transform) const
static std::string path_topic_name()
static geometry_msgs::PoseStamped get_current_pose()
std::string tf_map_frame_id()
Definition: init_utils.h:33
#define ROS_ERROR(...)
static double publishing_rate()
std::string frame_id_
static int subscriber_queue_size()


slam_constructor
Author(s): JetBrains Research, OSLL team
autogenerated on Mon Jun 10 2019 15:08:25