0

have navigation Odometry, but its some how it is not in the robot body frame. The odometry is being published in world frame, so its not correct. So i need to transform it in the robot body frame as that how should be in the correct way. So I tried to republish the linear velocity in the x axis in the robot body frame, just to check that Im on the correct way, but the code is not working. Here is the ROS node

#include <ros/ros.h>
#include <tf/transform_broadcaster.h>
#include <nav_msgs/Odometry.h>

float linear_x;
ros::Publisher odom_pub;

void poseCallback(const nav_msgs::OdometryConstPtr& msg){

    linear_x = (msg->twist.twist.linear.x );
    nav_msgs::Odometry pose_gt_frame;

        pose_gt_frame.header.frame_id = "world";

    //set the velocity
    pose_gt_frame.child_frame_id = "rexrov2/base_link";
    pose_gt_frame.twist.twist.linear.x = linear_x;

    //publish the message
    odom_pub.publish(pose_gt_frame);

}

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

  ros::NodeHandle node;
  ros::Subscriber sub = node.subscribe("/rexrov2/pose_gt", 10, &poseCallback);
  ros::spin();
  return 0;
};

When run the code I got error

[FATAL] [1635340917.678039503, 15.652000000]: ASSERTION FAILED
    file = /opt/ros/melodic/include/ros/publisher.h
    line = 106
    cond = false
    message = 
[FATAL] [1635340917.680256176, 15.654000000]: Call to publish() on an invalid Publisher
[FATAL] [1635340917.680299807, 15.654000000]:

And also a static frame in the launch file didnt help. What can be wrong? Any help? Thanks

Bob9710
  • 205
  • 3
  • 15

1 Answers1

0

You're getting the error because while you declared a publisher variable you have not initialized it yet.

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

  ros::NodeHandle node;
  odom_pub = node.advertise<nav_msgs::Odometry>("some_odom_topic", 5);
  ros::Subscriber sub = node.subscribe("/rexrov2/pose_gt", 10, &poseCallback);
  ros::spin();
  return 0;
};
BTables
  • 4,413
  • 2
  • 11
  • 30
  • I change taking into account your suggestions still same problem – Bob9710 Oct 27 '21 at 16:53
  • @Bob9710 that was my fault. I had a typo in my code. It has been edited now – BTables Oct 27 '21 at 16:54
  • ok. no error now but its still in the world frame not in the robot body frame. WhY? Whats wrong? – Bob9710 Oct 27 '21 at 17:17
  • @Bob9710 Because when you create the outgoing message you set `head.frame_id` to `world`. Also changing the `frame_id` field is fairly arbitrary; it's just a string. It doesn't apply any transform, which needs to be done to the actual data. – BTables Oct 27 '21 at 17:24
  • so head.frame id should be what? so how to apply transform to the actual data? – Bob9710 Oct 27 '21 at 17:32
  • @Bob9710 `header.frame_id` should be whatever frame the data is in. Again, it's arbitrary though and won't effect the actual data stored in a message. If you want to apply a transform manually you should use [the tf2 package](http://wiki.ros.org/tf2/Tutorials/Writing%20a%20tf2%20listener%20%28C%2B%2B%29) – BTables Oct 27 '21 at 17:34
  • so I need a tf2 listener? Please can you help with the manually transform? – Bob9710 Oct 27 '21 at 17:43
  • sorry , Im not clear doe I need tf2 broadcaster to publish the pose to tf2, or a tf2 listener to start using tf2, or both broadcaster and listener?Please, can you clarify this? – Bob9710 Oct 31 '21 at 08:29
  • @Bob9710 you’ll need a listener. It will listen for transforms to be broadcast. Once broadcast you can use the listener to get the transform and apply it to your data. – BTables Oct 31 '21 at 21:04
  • should I start a new question regarding the listener? as im not sure if I coded correct or just edit it here in the same question? – Bob9710 Nov 01 '21 at 07:02