odom.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 #include <tf/transform_broadcaster.h>
00003 #include <geometry_msgs/TransformStamped.h>
00004 #include <nav_msgs/Odometry.h>
00005 
00006 double m_vel_x, m_vel_y, m_vel_th;
00007 geometry_msgs::TransformStamped odom_tf;
00008 nav_msgs::Odometry odom_new;
00009 
00010 void callback(const nav_msgs::Odometry::ConstPtr& odom_old)
00011 {
00012         m_vel_x = odom_old->twist.twist.linear.x;
00013         m_vel_y = odom_old->twist.twist.linear.y;
00014         m_vel_th = odom_old->twist.twist.angular.z;
00015         //odom_new.pose.pose.orientation = odom_old->pose.pose.orientation;
00016         //odom_tf.transform.rotation = odom_old->pose.pose.orientation;
00017         //odom_new.header = odom_old->header;
00018   //odom_new.pose   = odom_old->pose;
00019         //odom_new.twist  = odom_old->twist;
00020         //odom_new.child_frame_id = "base_footprint";
00021         //odom_new.pose.pose.position.x = odom_new.pose.pose.position.x * 1.307;
00022         //odom_new.pose.pose.position.y = odom_new.pose.pose.position.y * 1.0;
00023         //pub.publish(odom_new);
00024 }
00025 
00026 int main(int argc, char **argv)
00027 {
00028         ros::init(argc, argv, "odom_fix");
00029         ros::NodeHandle n;
00030 
00031         ros::Time m_currentTime;
00032         ros::Time m_lastTime;
00033         double m_pos_x, m_pos_y, m_pos_th;
00034         
00035         tf::TransformBroadcaster odom_bro;
00036         
00037         //geometry_msgs::Vector3 rpy;
00038 
00039         ros::Publisher pub = n.advertise<nav_msgs::Odometry>("odom", 10);
00040         ros::Subscriber sub = n.subscribe("odom_old",10,callback);
00041         ros::Rate loop_rate(5);
00042         
00043         while(ros::ok())
00044         {
00045                 m_currentTime = ros::Time::now();
00046                 double dt = (m_currentTime - m_lastTime).toSec();//查询的时间间隔
00047                 double delta_x = (m_vel_x * cos(m_pos_th) - m_vel_y * sin(m_pos_th)) * dt;//x方向增量
00048                 double delta_y = (m_vel_x * sin(m_pos_th) + m_vel_y * cos(m_pos_th)) * dt;//y方向增量
00049                 double delta_th = m_vel_th * dt;//角度增量
00050                 m_pos_x += delta_x;
00051                 m_pos_y += delta_y;
00052                 m_pos_th += delta_th;
00053                 //double roll, pitch, yaw;
00054     //tf::Matrix3x3(quat).getRPY(roll, pitch, yaw);
00055                 geometry_msgs::Quaternion odom_quat = tf::createQuaternionMsgFromRollPitchYaw(0,0,m_pos_th*1.01);//四元数表达姿态
00056                 odom_new.header.stamp = m_currentTime;
00057                 odom_new.header.frame_id = "odom";
00058                 odom_new.child_frame_id = "base_footprint";
00059                 odom_new.pose.pose.position.x = m_pos_x*1.304;
00060                 odom_new.pose.pose.position.y = m_pos_y;
00061                 odom_new.pose.pose.position.z = 0.0;
00062                 odom_new.twist.twist.linear.x = m_vel_x;
00063                 odom_new.twist.twist.linear.y = m_vel_y;
00064                 odom_new.twist.twist.linear.z = 0.0;
00065                 odom_new.twist.twist.angular.x = 0.0;
00066                 odom_new.twist.twist.angular.y = 0.0;
00067                 odom_new.twist.twist.angular.z = m_vel_th;
00068           odom_new.pose.pose.orientation = odom_quat;
00069         
00070                 pub.publish(odom_new);
00071 
00072                 odom_tf.header.stamp = m_currentTime;
00073                 odom_tf.header.frame_id = "odom";
00074                 odom_tf.child_frame_id = "base_footprint";
00075                 odom_tf.transform.translation.x = m_pos_x*1.304;
00076                 odom_tf.transform.translation.y = m_pos_y;
00077                 odom_tf.transform.translation.z = 0.0;
00078                 odom_tf.transform.rotation = odom_quat;
00079                 odom_bro.sendTransform(odom_tf);
00080                 m_vel_x = 0;
00081                 m_vel_y = 0;
00082                 m_vel_th = 0;
00083                 loop_rate.sleep();
00084                 m_lastTime = m_currentTime;
00085                 ros::spinOnce();
00086                 
00087         }
00088         
00089 }


roch_base
Author(s): Mike Purvis , Paul Bovbel , Carl
autogenerated on Sat Jun 8 2019 20:32:33