发布时间:2022-09-15 10:00
有时为了验证程序可以运行,需要给程序输入一个里程计信息;有时为了在实车实验前验证程序,需要修改不同的里程计数据(可以在下面代码中增加一个odom_tmp的数组,或者用循环来使odom_tmp值变化),下面这个c++代码可以实现这个功能:
pub_odom.cpp
#include "ros/ros.h"
#include "std_msgs/String.h"
#include
#include
#include
#include "nav_msgs/OccupancyGrid.h"
#include "nav_msgs/Odometry.h"
#include
#include
#include
ros::Publisher pubCarpositiopn;
void odom_pub(){
nav_msgs::Odometry odom_tmp;
odom_tmp.header.frame_id = "/camera_init";
odom_tmp.child_frame_id ="/aft_mapped";
odom_tmp.header.stamp = ros::Time::now();
//参数可以改动
odom_tmp.pose.pose.position.x=-0.1;
odom_tmp.pose.pose.position.y=9;
odom_tmp.pose.pose.position.z=0;
// odom_tmp.pose.pose.orientation.x=0;
// odom_tmp.pose.pose.orientation.y=0;
// odom_tmp.pose.pose.orientation.z=0;
// odom_tmp.pose.pose.orientation.w=1;
// 绕z轴旋转90°
//参数可以改动
odom_tmp.pose.pose.orientation.x=0.707;
odom_tmp.pose.pose.orientation.y=0;
odom_tmp.pose.pose.orientation.z=0;
odom_tmp.pose.pose.orientation.w=0.707;
pubCarpositiopn.publish(odom_tmp);
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "pub_odom");
ros::NodeHandle nh;
pubCarpositiopn = nh.advertise("/aft_mapped_to_init", 1);//显示车辆/雷达在地图中的位置
ros::Rate rate_10hz(10);
while(ros::ok())
{
odom_pub();
rate_10hz.sleep();
}
return 0;
}
rviz效果: