【问题标题】:Cannot show the published path in rviz ROS无法在 rviz ROS 中显示发布的路径
【发布时间】:2023-02-21 16:49:05
【问题描述】:

我是 ROS 的新手,正在尝试在 ROS 中发布路径。我检查过主题“/路径”已发布,但路径似乎未在 rviz 中显示 [我可以选择 /path,它似​​乎有效但未显示轨迹。我确定这条线不是透明的]。我不知道为什么?如果有人能帮助我,非常感谢。以下是我的代码:

广播路径.cpp

"""

#include "control_turtle/BroadcastPath.hpp"

namespace broadcast_path {

BroadcastPath::BroadcastPath(ros::NodeHandle& nodeHandle)
    : nodeHandle_(nodeHandle)
{
    publisher_ = nodeHandle_.advertise<nav_msgs::Path>("/path",1);
    subscriber_ = nodeHandle_.subscribe("/odom", 2, &BroadcastPath::topicCallback, this);

}

BroadcastPath::~BroadcastPath()
{
}

void BroadcastPath::topicCallback(const nav_msgs::Odometry& msg)
{
    nav_msgs::Path path;
    geometry_msgs::PoseStamped pose;

    pose.pose.position = msg.pose.pose.position;
    pose.pose.orientation = msg.pose.pose.orientation;

    path.header.stamp = ros::Time::now();
    path.header.frame_id = "map";
    path.poses.push_back(pose);

    publisher_.publish(path);

}

} /* namespace */

"""

广播路径节点.cpp

"""

#include <ros/ros.h>
#include "control_turtle/BroadcastPath.hpp"

int main(int argc, char** argv)
{
  ros::init(argc, argv, "broadcast_path");
  ros::NodeHandle nodeHandle("~");
  ros::Rate loopRate(10);

  broadcast_path::BroadcastPath broadcastPath(nodeHandle);

  ros::spin();
  return 0;
}

"""

广播路径.hpp

"""

#pragma once


// ROS
#include <iostream>
#include <ros/ros.h>
#include <stdio.h>
#include <nav_msgs/Odometry.h>
#include <nav_msgs/Path.h>

namespace broadcast_path {

/*!
 * Main class for the node to handle the ROS interfacing.
 */
class BroadcastPath
{
 public:
  /*!
   * Constructor.
   * @param nodeHandle the ROS node handle.
   */
    BroadcastPath(ros::NodeHandle& nodeHandle);

  /*!
   * Destructor.
   */
  virtual ~BroadcastPath();

 private:

  /*!
   * ROS topic callback method.
   * @param message the received message.
   */
  void topicCallback(const nav_msgs::Odometry& msg);


  /*!
   * ROS service server callback.
   * @param request the request of the service.
   * @param response the provided response.
   * @return true if successful, false otherwise.
   */

  //! ROS node handle.
  ros::NodeHandle& nodeHandle_;

  //! ROS publisher
  ros::Publisher publisher_;
  ros::Subscriber subscriber_;

  int count = 0;

};

} /* namespace */

"""

【问题讨论】:

    标签: c++ ros


    【解决方案1】:

    在 BroadcastPath.cpp 中,您应该在将姿势推送到路径之前为姿势分配一个标题。 您还应该将路径的 frame_id 设置为 parentId(如果您使用的是 ros 标准,则为“odom”),并将姿势的 frame_id 设置为 childId(如果您使用的是 ros 标准,则为“base_link”)。 您的代码应如下所示:

    #include "control_turtle/BroadcastPath.hpp"
    
    namespace broadcast_path {
    
    BroadcastPath::BroadcastPath(ros::NodeHandle& nodeHandle)
        : nodeHandle_(nodeHandle)
    {
        publisher_ = nodeHandle_.advertise<nav_msgs::Path>("/path",1);
        subscriber_ = nodeHandle_.subscribe("/odom", 2, &BroadcastPath::topicCallback, this);
    
    }
    
    BroadcastPath::~BroadcastPath()
    {
    }
    
    void BroadcastPath::topicCallback(const nav_msgs::Odometry& msg)
    {
        nav_msgs::Path path;
        geometry_msgs::PoseStamped pose;
    
    
        pose.header.stamp = ros::Time::now();
        path.header.frame_id = "odom";
        pose.pose.position = msg.pose.pose.position;
        pose.pose.orientation = msg.pose.pose.orientation;
    
        path.header.stamp = ros::Time::now();
        path.header.frame_id = "base_link";
        path.poses.push_back(pose);
    
        publisher_.publish(path);
    
    }
    
    } /* namespace */
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-20
      • 2021-12-23
      • 2022-06-29
      • 2022-07-19
      • 1970-01-01
      相关资源
      最近更新 更多