odom_frame_publisher.cpp
Go to the documentation of this file.
1 
12 // headers in this package
14 
20 {
22  current_pose_recieved_ = false;
23  nh_ = nh;
24  pnh_ = pnh;
25  pnh_.param<std::string>("current_pose_topic", current_pose_topic_, "current_pose");
26  pnh_.param<std::string>("current_twist_topic", current_twist_topic_, "current_twist");
27  pnh_.param<std::string>("odom_frame_id", odom_frame_id_, "odom");
28  pnh_.param<std::string>("map_frame_id", map_frame_id_, "map");
29  pnh_.param<std::string>("robot_frame_id", robot_frame_id_, "base_link");
30  data_ = boost::circular_buffer<geometry_msgs::TwistStamped>(2);
31  current_odom_pose_.header.frame_id = odom_frame_id_;
32  current_odom_pose_.header.stamp = ros::Time::now();
33  current_odom_pose_.pose.position.x = 0;
34  current_odom_pose_.pose.position.y = 0;
35  current_odom_pose_.pose.position.z = 0;
36  current_odom_pose_.pose.orientation.x = 0;
37  current_odom_pose_.pose.orientation.y = 0;
38  current_odom_pose_.pose.orientation.z = 0;
39  current_odom_pose_.pose.orientation.w = 1;
40  odom_pose_pub_ = pnh_.advertise<geometry_msgs::PoseStamped>("odom_pose",1);
43 }
44 
50 {
51 
52 }
53 
58 void OdomFramePublisher::currentPoseCallback(const geometry_msgs::PoseStamped::ConstPtr msg)
59 {
61  current_pose_ = *msg;
62  return;
63 }
64 
69 void OdomFramePublisher::currentTwistCallback(const geometry_msgs::TwistStamped::ConstPtr msg)
70 {
71  data_.push_back(*msg);
72  if(data_.size()==2 && current_pose_recieved_)
73  {
74  current_odom_pose_.header.stamp = current_pose_.header.stamp;
75  double duration = (current_pose_.header.stamp - data_[0].header.stamp).toSec();
76  geometry_msgs::Vector3 orientation;
77  orientation.x = (data_[0].twist.angular.x+data_[1].twist.angular.x) * duration * 0.5;
78  orientation.y = (data_[0].twist.angular.y+data_[1].twist.angular.y) * duration * 0.5;
79  orientation.z = (data_[0].twist.angular.z+data_[1].twist.angular.z) * duration * 0.5;
80  geometry_msgs::Quaternion twist_angular_quat =
82  current_odom_pose_.pose.orientation = quaternion_operation::rotation(current_odom_pose_.pose.orientation,twist_angular_quat);
83  Eigen::Vector3d trans_vec;
84  trans_vec(0) = (data_[0].twist.linear.x+data_[1].twist.linear.x) * duration * 0.5;
85  trans_vec(1) = (data_[0].twist.linear.y+data_[1].twist.linear.y) * duration * 0.5;
86  trans_vec(2) = (data_[0].twist.linear.z+data_[1].twist.linear.z) * duration * 0.5;
87  Eigen::Matrix3d rotation_mat = quaternion_operation::getRotationMatrix(current_odom_pose_.pose.orientation);
88  trans_vec = rotation_mat*trans_vec;
89  current_odom_pose_.pose.position.x = current_odom_pose_.pose.position.x + trans_vec(0);
90  current_odom_pose_.pose.position.x = current_odom_pose_.pose.position.x + trans_vec(1);
91  current_odom_pose_.pose.position.x = current_odom_pose_.pose.position.x + trans_vec(2);
93 
94  geometry_msgs::TransformStamped transform_stamped;
95  transform_stamped.header.frame_id = odom_frame_id_;
96  transform_stamped.header.stamp = current_pose_.header.stamp;
97  transform_stamped.child_frame_id = robot_frame_id_;
98  transform_stamped.transform.translation.x = current_odom_pose_.pose.position.x;
99  transform_stamped.transform.translation.y = current_odom_pose_.pose.position.y;
100  transform_stamped.transform.translation.z = current_odom_pose_.pose.position.z;
101  transform_stamped.transform.rotation = current_odom_pose_.pose.orientation;
102  broadcaster_.sendTransform(transform_stamped);
103 
104  tf2::Transform latest_tf;
105  try
106  {
107  tf2::Transform odom_pose_tf2;
108  tf2::convert(current_odom_pose_.pose, odom_pose_tf2);
109  tf2::Quaternion q(current_pose_.pose.orientation.x,current_pose_.pose.orientation.y,current_pose_.pose.orientation.z,current_pose_.pose.orientation.w);
110  tf2::Transform tmp_tf(q, tf2::Vector3(current_pose_.pose.position.x,current_pose_.pose.position.y,current_pose_.pose.position.z));
111  geometry_msgs::PoseStamped tmp_tf_stamped;
112  tmp_tf_stamped.header.frame_id = robot_frame_id_;
113  tmp_tf_stamped.header.stamp = current_pose_.header.stamp;
114  tf2::toMsg(tmp_tf.inverse(), tmp_tf_stamped.pose);
115  geometry_msgs::PoseStamped odom_to_map;
116  buffer_.transform(tmp_tf_stamped, odom_to_map, odom_frame_id_, ros::Duration(0.1));
117  tf2::convert(odom_to_map.pose, latest_tf);
118  }
119  catch(...)
120  {
121  ROS_WARN("Failed to subtract base to odom transform");
122  return;
123  }
124  geometry_msgs::TransformStamped tmp_tf_stamped;
125  tmp_tf_stamped.header.frame_id = map_frame_id_;
126  tmp_tf_stamped.header.stamp = current_pose_.header.stamp;
127  tmp_tf_stamped.child_frame_id = odom_frame_id_;
128  tf2::convert(latest_tf.inverse(), tmp_tf_stamped.transform);
129  broadcaster_.sendTransform(tmp_tf_stamped);
130  }
131  return;
132 }
Definition of the OdomFramePublisher Class.
geometry_msgs::Quaternion convertEulerAngleToQuaternion(geometry_msgs::Vector3 euler)
void publish(const boost::shared_ptr< M > &message) const
tf2_ros::TransformBroadcaster broadcaster_
void setUsingDedicatedThread(bool value)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ros::Subscriber current_pose_sub_
geometry_msgs::PoseStamped current_pose_
#define ROS_WARN(...)
ros::Publisher odom_pose_pub_
OdomFramePublisher(ros::NodeHandle nh, ros::NodeHandle pnh)
Constructor of the OdomFramePublisher Class.
bool param(const std::string &param_name, T &param_val, const T &default_val) const
std::string current_twist_topic_
geometry_msgs::Quaternion rotation(geometry_msgs::Quaternion from, geometry_msgs::Quaternion rotation)
Transform inverse() const
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
boost::circular_buffer< geometry_msgs::TwistStamped > data_
void sendTransform(const geometry_msgs::TransformStamped &transform)
B toMsg(const A &a)
ros::Subscriber current_twist_sub_
void currentPoseCallback(const geometry_msgs::PoseStamped::ConstPtr msg)
ROS callback function for the current pose topic.
tf2_ros::Buffer buffer_
void currentTwistCallback(const geometry_msgs::TwistStamped::ConstPtr msg)
ROS callback function for the current tiwst topic.
static Time now()
void convert(const A &a, B &b)
T & transform(const T &in, T &out, const std::string &target_frame, ros::Duration timeout=ros::Duration(0.0)) const
~OdomFramePublisher()
Destructor of the OdomFramePublisher class.
geometry_msgs::PoseStamped current_odom_pose_
std::string current_pose_topic_
Eigen::Matrix3d getRotationMatrix(geometry_msgs::Quaternion quat)


odom_frame_publisher
Author(s):
autogenerated on Tue Jun 2 2020 03:53:06