robot_pose_publisher.cpp
Go to the documentation of this file.
1 
12 #include <geometry_msgs/PoseStamped.h>
13 #include <geometry_msgs/Pose.h>
14 #include <ros/ros.h>
15 #include <tf/transform_listener.h>
16 
24 int main(int argc, char ** argv)
25 {
26  // initialize ROS and the node
27  ros::init(argc, argv, "robot_pose_publisher");
28  ros::NodeHandle nh;
29  ros::NodeHandle nh_priv("~");
30 
31  // configuring parameters
32  std::string map_frame, base_frame;
33  double publish_frequency;
34  bool is_stamped;
35  ros::Publisher p_pub;
36 
37  nh_priv.param<std::string>("map_frame",map_frame,"/map");
38  nh_priv.param<std::string>("base_frame",base_frame,"/base_link");
39  nh_priv.param<double>("publish_frequency",publish_frequency,10);
40  nh_priv.param<bool>("is_stamped", is_stamped, false);
41 
42  if(is_stamped)
43  p_pub = nh.advertise<geometry_msgs::PoseStamped>("robot_pose", 1);
44  else
45  p_pub = nh.advertise<geometry_msgs::Pose>("robot_pose", 1);
46 
47  // create the listener
48  tf::TransformListener listener;
49  listener.waitForTransform(map_frame, base_frame, ros::Time(), ros::Duration(1.0));
50 
51  ros::Rate rate(publish_frequency);
52  while (nh.ok())
53  {
54  tf::StampedTransform transform;
55  try
56  {
57  listener.lookupTransform(map_frame, base_frame, ros::Time(0), transform);
58 
59  // construct a pose message
60  geometry_msgs::PoseStamped pose_stamped;
61  pose_stamped.header.frame_id = map_frame;
62  pose_stamped.header.stamp = ros::Time::now();
63 
64  pose_stamped.pose.orientation.x = transform.getRotation().getX();
65  pose_stamped.pose.orientation.y = transform.getRotation().getY();
66  pose_stamped.pose.orientation.z = transform.getRotation().getZ();
67  pose_stamped.pose.orientation.w = transform.getRotation().getW();
68 
69  pose_stamped.pose.position.x = transform.getOrigin().getX();
70  pose_stamped.pose.position.y = transform.getOrigin().getY();
71  pose_stamped.pose.position.z = transform.getOrigin().getZ();
72 
73  if(is_stamped)
74  p_pub.publish(pose_stamped);
75  else
76  p_pub.publish(pose_stamped.pose);
77  }
78  catch (tf::TransformException &ex)
79  {
80  // just continue on
81  }
82 
83  rate.sleep();
84  }
85 
86  return EXIT_SUCCESS;
87 }
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
TFSIMD_FORCE_INLINE const tfScalar & getY() const
int main(int argc, char **argv)
TFSIMD_FORCE_INLINE const tfScalar & getW() const
TFSIMD_FORCE_INLINE const tfScalar & getZ() const
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
bool param(const std::string &param_name, T &param_val, const T &default_val) const
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
TFSIMD_FORCE_INLINE Vector3 & getOrigin()
bool sleep()
Quaternion getRotation() const
TFSIMD_FORCE_INLINE const tfScalar & getX() const
static Time now()
bool ok() const


robot_pose_publisher
Author(s): Russell Toris
autogenerated on Mon Jun 10 2019 14:34:48