ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

ROS2 TF publisher not publishing anything

asked 2023-02-02 13:28:23 -0600

chm007 gravatar image

Hi All,

I'm new to ROS2 I coded a publisher in C++ colcon build ran successfully and the topic publishes but I don't see any TF messages my cod is below. Any idea why it wont work?

#include "rclcpp/rclcpp.hpp"

#include "tf2/LinearMath/Quaternion.h"

#include "tf2_ros/transform_broadcaster.h"

#include "geometry_msgs/msg/transform_stamped.hpp"


class OmniBroadcastnode : public rclcpp::Node
{
public: 
    OmniBroadcastnode() 
    : Node("omni_tf2_broadcaster_node")
    {
        tf2_broadcaster_ = std::make_unique<tf2_ros::TransformBroadcaster>(this);

    }

private:

    void spin()
    {
    geometry_msgs::msg::TransformStamped transform_;
        rclcpp::Rate loop_rate(100);
        while (rclcpp::ok())
        {
            transform_.header.stamp = this->get_clock()->now();
      transform_.header.frame_id = "base_link";
          transform_.child_frame_id = "base_laser";
          transform_.transform.translation.x = 0.0;
          transform_.transform.translation.y = 0.025;
          transform_.transform.translation.z = 0.2;
          tf2::Quaternion q;
          q.setRPY(0, 0, 0);
          transform_.transform.rotation.x = q.x();
          transform_.transform.rotation.y = q.y();
          transform_.transform.rotation.z = q.z();
          transform_.transform.rotation.w = q.w();
            tf2_broadcaster_->sendTransform(transform_);
            loop_rate.sleep();
        }
    }

    std::unique_ptr<tf2_ros::TransformBroadcaster> tf2_broadcaster_;

};

int main(int argc, char * argv[]){
  rclcpp::init(argc, argv);
  //rclcpp::spin(std::make_shared<OmniBroadcastnode>());
  auto omni_tf2_broadcaster_node = std::make_shared<OmniBroadcastnode>();
  rclcpp::spin(omni_tf2_broadcaster_node);
  rclcpp::shutdown();
  return 0;
}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-02-03 20:19:19 -0600

chm007 gravatar image

updated 2023-02-13 13:11:45 -0600

I solved this there were errors in my code. See below for the example i used :

#include <rclcpp/rclcpp.hpp>
#include <tf2_ros/transform_broadcaster.h>
#include <geometry_msgs/msg/transform_stamped.hpp>

class TFPublisher : public rclcpp::Node
{
  public:
    TFPublisher()
        : Node("tf_publisher")
    {
      broadcaster_ = std::make_unique<tf2_ros::TransformBroadcaster>(this);
    }

    void spin()
    {
      rclcpp::Rate loop_rate(10);
      while (rclcpp::ok())
      {
        geometry_msgs::msg::TransformStamped laser_transform;
    laser_transform.header.frame_id = "base_link";
    laser_transform.child_frame_id = "base_laser";
    laser_transform.header.stamp = rclcpp::Clock().now();
    laser_transform.transform.translation.x = 0.0;
    laser_transform.transform.translation.y = 0.0;
    laser_transform.transform.translation.z = 0.0;
    laser_transform.transform.rotation.x = 0.0;
    laser_transform.transform.rotation.y = 0.0;
    laser_transform.transform.rotation.z = 0.0;
    laser_transform.transform.rotation.w = 1.0;

    geometry_msgs::msg::TransformStamped imu_transform;
    imu_transform.header.frame_id = "base_link";
    imu_transform.child_frame_id = "base_imu";
    imu_transform.header.stamp = rclcpp::Clock().now();
    imu_transform.transform.translation.x = 0.0;
    imu_transform.transform.translation.y = 0.0;
    imu_transform.transform.translation.z = 0.0;
    imu_transform.transform.rotation.x = 0.0;
    imu_transform.transform.rotation.y = 0.0;
    imu_transform.transform.rotation.z = 0.0;
    imu_transform.transform.rotation.w = 1.0;

    broadcaster_->sendTransform(laser_transform);
    broadcaster_->sendTransform(imu_transform);

    rclcpp::spin_some(this->get_node_base_interface());
    loop_rate.sleep();
  }
}



 private:
    std::unique_ptr<tf2_ros::TransformBroadcaster> broadcaster_;
};

int main(int argc, char *argv[])
{
  rclcpp::init(argc, argv);
  auto node = std::make_shared<TFPublisher>();
  node->spin();
  rclcpp::shutdown();
  return 0;
}
edit flag offensive delete link more

Comments

And what were those errors in [your] code?

gvdhoorn gravatar image gvdhoorn  ( 2023-02-04 02:36:21 -0600 )edit

kindly tell us the errors.

Davies Ogunsina gravatar image Davies Ogunsina  ( 2023-02-04 14:52:16 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2023-02-02 13:28:23 -0600

Seen: 443 times

Last updated: Feb 13 '23