一、實現(xiàn)效果
紅色為行駛過的軌跡二、實現(xiàn)方法
1、導(dǎo)航包中創(chuàng)建.cpp文件,并將以下代碼復(fù)制進(jìn)去
2、CMakeLists當(dāng)中添加可執(zhí)行文件及鏈接庫3、啟動導(dǎo)航的launch文件中添加啟動該cpp文件
文章來源:http://www.zghlxwxcb.cn/news/detail-616175.html
三、代碼文章來源地址http://www.zghlxwxcb.cn/news/detail-616175.html
#include <ros/ros.h>
#include <ros/console.h>
#include <nav_msgs/Path.h>
#include <nav_msgs/Odometry.h>
#include <std_msgs/String.h>
#include <geometry_msgs/Quaternion.h>
#include <geometry_msgs/PoseStamped.h>
#include <tf/transform_broadcaster.h>
#include <tf/tf.h>
nav_msgs::Path path;
ros::Publisher path_pub;
void odomCallback(const nav_msgs::Odometry::ConstPtr& odom)
{
geometry_msgs::PoseStamped this_pose_stamped;
this_pose_stamped.pose.position.x = odom -> pose.pose.position.x;
this_pose_stamped.pose.position.y = odom -> pose.pose.position.y;
this_pose_stamped.pose.position.z = odom -> pose.pose.position.z;
this_pose_stamped.pose.orientation = odom -> pose.pose.orientation;
this_pose_stamped.header.stamp = ros::Time::now();
this_pose_stamped.header.frame_id = "map";
path.poses.push_back(this_pose_stamped);
path.header.stamp = ros::Time::now();
path.header.frame_id = "map";
path_pub.publish(path);
printf("path_pub:");
printf("odom %.3lf %.3lf %.3lf\n", odom->pose.pose.position.x, odom->pose.pose.position.y, odom->pose.pose.position.z);
}
int main (int argc, char **argv)
{
ros::init (argc, argv, "showpath_odom");
ros::NodeHandle ph;
path_pub = ph.advertise<nav_msgs::Path>("trajectory_odom", 10, true);
ros::Subscriber odomSub = ph.subscribe<nav_msgs::Odometry>("/odom", 10, odomCallback); //訂閱里程計話題信息
ros::Rate loop_rate(50);
while(ros::ok())
{
ros::spinOnce();
loop_rate.sleep();
}
return 0;
}
到了這里,關(guān)于ROS仿真機(jī)器人實現(xiàn)Rviz軌跡顯示的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!