Ex4.4 subscribe Odom (ROS Basics in 5 days C++)

Issue: No /odom info on terminal even it is subscribed

  • Compiled successfully
  • /odom is subscribed by the created cpp
  • /odom is publishing

Code for my subscriber cpp

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

void counterCallback(const nav_msgs::Odometry::ConstPtr& msg) {
  ROS_INFO("%s", msg->header.frame_id.c_str());
}

int main(int argc, char** argv) {
  ros::init(argc, argv, "odom_topic_subscriber");
  ros::NodeHandle nh;
  ros::Subscriber sub = nh.subscribe("odom", 1000, counterCallback);
  ros::spin();
  return 0;
}

Code for my launch


<launch>
    <node pkg="topic_subscriber_pkg" type="simple_odom_subscriber" name="odom_topic_subscriber" output="screen"></node>
</launch>

CMakeLists.txt content

cmake_minimum_required(VERSION 3.0.2)
project(topic_subscriber_pkg)


find_package(catkin REQUIRED COMPONENTS
  roscpp
  std_msgs
  message_generation
)



## Build ##
 #odom
add_executable(simple_odom_subscriber src/simple_odom_subscriber.cpp)
add_dependencies(simple_odom_subscriber ${simple_odom_subscriber_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(simple_odom_subscriber
   ${catkin_LIBRARIES}
 )


#catkin_package()
catkin_package(
    CATKIN_DEPENDS roscpp std_msgs message_runtime   # This will NOT be the only thing here
)



include_directories(
  ${catkin_INCLUDE_DIRS}
)

Hi @RC1 ,

Welcome to this Community!

I went through your code and the related file contents. Everything seems to be fine.
You will not require message_generation in find_package(...) and message_runtime in catkin_package(...) of your CMakeLists.txt file - since you are not using any custom messages I believe.

Otherwise, this would be my guess - as to why you are not seeing any messages - your Odometry message frame_id might be a blank string. Try printing out any of the pose / orientation data from /odom to check if Odometry messages are actually working properly.

In case you have issues with /odom at some point, try shutting down the gazebo simulation and restarting it.
I remember experiencing a similar issue when I worked on this project.

Regards,
Girish

1 Like

Thank you very much Girish, I think there’s something wrong in my ws.

I’ve relaunched the gazebo, the result remains unchanged. I’ve tried pose.pose.position.x as well, no output on terminal.

The content of /odom topic:
odom-frame_id

I’ve posted the issue here: ERROR: cannot launch node of type


I've solved the issue:

Step 1: rosrun pkgname nodename to verify the node
Step 2: Confirm that catkin_package(...) & include_directories(...) are placed before any executables
Step 3: catkin_make and source



Thanks for the reminder as well, the message_generation and catkin_package in CMakeLists.txt are intended for my custom message in another project.