ROS在rviz中实时显示轨迹(nav_msgs/Path消息的使用)

消息结构说明
nav_msgs/Path.msg结构
#An array of poses that represents a Path for a robot to follow
Header header
geometry_msgs/PoseStamped[] poses
1
2
3
geometry_msgs/PoseStamped.msg结构
# A Pose with reference coordinate frame and timestamp
Header header
Pose pose
1
2
3
geometry_msgs/Pose.msg结构
# A representation of pose in free space, composed of position and orientation.
Point position
Quaternion orientation
1
2
3
geometry_msgs/Point.msg结构
# This contains the position of a point in free space
float64 x
float64 y
float64 z
1
2
3
4
geometry_msgs/Quaternion.msg结构
# This represents an orientation in free space in quaternion form.

float64 x
float64 y
float64 z
float64 w
1
2
3
4
5
6
实现代码:
#include <ros/ros.h>
#include <ros/console.h>
#include <nav_msgs/Path.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>

main (int argc, char **argv)
{
ros::init (argc, argv, "showpath");

ros::NodeHandle ph;
ros::Publisher path_pub = ph.advertise<nav_msgs::Path>("trajectory",1, true);

ros::Time current_time, last_time;
current_time = ros::Time::now();
last_time = ros::Time::now();

nav_msgs::Path path;
//nav_msgs::Path path;
path.header.stamp=current_time;
path.header.frame_id="odom";


double x = 0.0;
double y = 0.0;
double th = 0.0;
double vx = 0.1;
double vy = -0.1;
double vth = 0.1;

ros::Rate loop_rate(1);
while (ros::ok())
{

current_time = ros::Time::now();
//compute odometry in a typical way given the velocities of the robot
double dt = (current_time - last_time).toSec();
double delta_x = (vx * cos(th) - vy * sin(th)) * dt;
double delta_y = (vx * sin(th) + vy * cos(th)) * dt;
double delta_th = vth * dt;

x += delta_x;
y += delta_y;
th += delta_th;


geometry_msgs::PoseStamped this_pose_stamped;
this_pose_stamped.pose.position.x = x;
this_pose_stamped.pose.position.y = y;

geometry_msgs::Quaternion goal_quat = tf::createQuaternionMsgFromYaw(th);
this_pose_stamped.pose.orientation.x = goal_quat.x;
this_pose_stamped.pose.orientation.y = goal_quat.y;
this_pose_stamped.pose.orientation.z = goal_quat.z;
this_pose_stamped.pose.orientation.w = goal_quat.w;

this_pose_stamped.header.stamp=current_time;
this_pose_stamped.header.frame_id="odom";
path.poses.push_back(this_pose_stamped);

path_pub.publish(path);
ros::spinOnce(); // check for incoming messages

last_time = current_time;
loop_rate.sleep();
}

return 0;
}
--------------------- 

转载于:https://www.cnblogs.com/hyhy904/p/11001412.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ROS,我们可以使用tf库来管理坐标系之间的转换关系。要将`nav_msgs/Path`消息转换为tf,需要按照以下步骤进行操作: 1. 首先,我们需要在代码包含必要的头文件: ```cpp #include <ros/ros.h> #include <tf/transform_broadcaster.h> #include <nav_msgs/Path.h> ``` 2. 接下来,我们需要定义一个回调函数来处理`nav_msgs/Path`消息。在这个回调函数,我们可以使用`tf::TransformBroadcaster`类来广播tf变换信息。 ```cpp void pathCallback(const nav_msgs::Path::ConstPtr& msg) { static tf::TransformBroadcaster broadcaster; for (int i = 0; i < msg->poses.size(); ++i) { const geometry_msgs::PoseStamped& pose = msg->poses[i]; tf::Vector3 position(pose.pose.position.x, pose.pose.position.y, pose.pose.position.z); tf::Quaternion orientation(pose.pose.orientation.x, pose.pose.orientation.y, pose.pose.orientation.z, pose.pose.orientation.w); tf::Transform transform(orientation, position); broadcaster.sendTransform(tf::StampedTransform(transform, pose.header.stamp, pose.header.frame_id, "path_frame")); } } ``` 在上面的代码,我们使用`tf::Vector3`和`tf::Quaternion`类来创建位置和旋转信息。然后,我们使用这些信息创建一个`tf::Transform`对象,并使用`tf::TransformBroadcaster`类的`sendTransform`方法将其广播到ROS系统。 3. 最后,我们需要在`main`函数创建一个ROS节点,并订阅`nav_msgs/Path`消息。 ```cpp int main(int argc, char** argv) { ros::init(argc, argv, "path_to_tf"); ros::NodeHandle nh; ros::Subscriber sub = nh.subscribe<nav_msgs::Path>("path_topic", 10, pathCallback); ros::spin(); return 0; } ``` 在上面的代码,我们使用`ros::Subscriber`类订阅`nav_msgs/Path`消息,并将其传递给`pathCallback`回调函数进行处理。`ros::spin()`函数将一直运行,直到节点被关闭。 注意:在广播tf变换信息时,我们将目标框架设置为`"path_frame"`。这意味着我们需要在代码创建一个名为`"path_frame"`的坐标系。如果您还没有创建这个坐标系,请参考ROS文档有关tf库的教程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值