fake_odom_node.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 #include <tf/transform_broadcaster.h>
00003 #include <nav_msgs/Odometry.h>
00004 #include <sensor_msgs/JointState.h>
00005 #include <geometry_msgs/Twist.h>
00006 
00007 double vx = 0.0;
00008 double vth = 0.0;
00009 
00010 
00011 void odomCallback( const geometry_msgs::TwistConstPtr& cmd_vel) {
00012         vx = cmd_vel->linear.x;
00013         vth = cmd_vel->angular.z;
00014 }
00015 
00016 
00017 int main( int argc, char** argv) {
00018 
00019         ros::init(argc, argv, "fake_odom");
00020 
00021         ros::NodeHandle n;
00022         ros::Publisher odom_pub = n.advertise<nav_msgs::Odometry>("odom", 50);
00023         ros::Publisher joint_state_pub = n.advertise<sensor_msgs::JointState>("joint_states", 50);
00024         ros::Subscriber sub = n.subscribe("cmd_vel", 1000, odomCallback);
00025         tf::TransformBroadcaster transform;
00026 
00027         double x = 0.0;
00028         double y = 0.0;
00029         double th = 0.0;
00030 
00031         ros::Time current_time, last_time;
00032         current_time = ros::Time::now();
00033         last_time = ros::Time::now();
00034 
00035         ros::Rate rate (500);
00036   
00037         while (n.ok()) {
00038                 current_time = ros::Time::now();
00039 
00040                 double dt = (current_time - last_time).toSec();
00041                 double delta_x = vx * cos(th) * dt;
00042                 double delta_y = vx * sin(th) * dt;
00043                 double delta_th = vth * dt;
00044 
00045                 x += delta_x ;
00046                 y += delta_y ;
00047                 th += delta_th;
00048 
00049                 geometry_msgs::Quaternion odom_quat = tf::createQuaternionMsgFromYaw(th);
00050 
00051                 // publish the transform odom --> base_footprint
00052                 geometry_msgs::TransformStamped odom_trans;
00053                 odom_trans.header.stamp = current_time;
00054                 odom_trans.header.frame_id = "odom";
00055                 odom_trans.child_frame_id = "base_footprint";
00056                 odom_trans.transform.translation.x = x;
00057                 odom_trans.transform.translation.y = y;
00058                 odom_trans.transform.translation.z = 0.0;
00059                 odom_trans.transform.rotation = odom_quat;
00060                 transform.sendTransform(odom_trans);
00061 
00062                 // publish the transform base_footprint --> base_link
00063                 geometry_msgs::Quaternion base_quat = tf::createQuaternionMsgFromRollPitchYaw(0,vx*0.5,0);
00064                 geometry_msgs::TransformStamped base_trans;
00065                 base_trans.header.stamp = current_time;
00066                 base_trans.header.frame_id = "base_footprint";
00067                 base_trans.child_frame_id = "base_link";
00068                 base_trans.transform.translation.x = 0;
00069                 base_trans.transform.translation.y = 0;
00070                 base_trans.transform.translation.z = 0.055;
00071                 base_trans.transform.rotation = base_quat;
00072                 transform.sendTransform(base_trans);
00073 
00074                 // publis /odom
00075                 nav_msgs::Odometry odom;
00076                 odom.header.stamp = current_time;
00077                 odom.header.frame_id = "odom";
00078                 odom.child_frame_id = "base_link";
00079                 odom.pose.pose.position.x = x;
00080                 odom.pose.pose.position.y = y;
00081                 odom.pose.pose.position.z = 0.0;
00082                 odom.pose.pose.orientation = odom_quat;
00083                 odom.twist.twist.linear.x = vx;
00084                 odom.twist.twist.angular.z = vth;
00085                 odom_pub.publish(odom);
00086                 
00087                 // publish joint states
00088                 sensor_msgs::JointState joint_state;
00089                 joint_state.header.stamp = current_time;
00090                 joint_state.name.push_back("left_wheel_joint");
00091                 joint_state.position.push_back(0);
00092                 joint_state.velocity.push_back(0);
00093                 joint_state.effort.push_back(0);
00094                 joint_state.name.push_back("right_wheel_joint");
00095                 joint_state.position.push_back(0);
00096                 joint_state.velocity.push_back(0);
00097                 joint_state.effort.push_back(0);
00098                 joint_state_pub.publish(joint_state);
00099                 
00100                 last_time = current_time;
00101                 
00102                 ros::spinOnce();
00103                 rate.sleep();
00104         }
00105 }


fake_odom
Author(s): Johannes Mayr , Klemens Springer
autogenerated on Wed Aug 26 2015 16:46:12