There is no age_info topic in exercise 4.3

Hi there,

exercise 4.3

In the solution, we published the newly defined message Age.msg to the age_info topic but this customized topic is not defined…Did I miss anything???

pub = rospy.Publisher('/age_info', Age, queue_size=1) #Create a Publisher that will publish in the /age_info topic

Here is the solution that the course provided:

#! /usr/bin/env python

import rospy
from exercise_33.msg import Age #Import Age message from the exercise_33 package

rospy.init_node('publish_age_node')
pub = rospy.Publisher('/age_info', Age, queue_size=1) #Create a Publisher that will publish in the /age_info topic
rate = rospy.Rate(2)
age = Age() #Create an Age message object
age.years = 5 #Fill the values of the message
age.months = 10 #Fill the values of the message
age.days = 21 #Fill the values of the message

while not rospy.is_shutdown(): 
  pub.publish(age) #Publish the message into the defined topic /age_info
  rate.sleep()

Hi @min.wang.1997.01.13,

Can you try running ‘rostopic list’ on your CLI?

Also, make sure to build the package and source your workspace after before running the node again.

Regards,
Christian

Hi @min.wang.1997.01.13 ,

I think you forgot to source your workspace after compilation in the terminal that you are running this program. You must source the workspace in all the terminals that you will be using, otherwise you will face these issues.

I am assuming that you are running this program with publisher in one terminal and trying to see the message printed in another terminal. Just make sure you source your catkin workspace on both the terminals.

Regards,
Girish

1 Like

I did source my workspace and use rostopic list but I cannot find this topic



In addition, it cannot find the read_odemetry package…Could you please tell me why?

It is also strange that if I tried to run the .py file directly. It shows that the package cannot be found when I import the message.


I am sure the message definition works:

This is the structure of the code:

I paste the code here:

#! /usr/bin/env python

import rospy
from read_odometry.msg import Age #Import Age message from the our package


rospy.init_node('pub_age_node')
pub = rospy.Publisher("/age_info", Age, queue_size=1)
rate = rospy.Rate(2)

age = Age()
age.years = 2024
age.months = 5
age.days = 15

# Create a loop that will go until someone stops the program execution
while not rospy.is_shutdown():
  # Publish the message 
  pub.publish(age)

  # Make sure the publish rate maintains at 2 Hz
  rate.sleep()     

@girishkumar.kannan @CCAnabeza

Hi @min.wang.1997.01.13,

I believe we have to restructure your code.

First, we have to create a function for your node as follows:

def age_publisher_node():
      # Place your ROS Node initializations here
if __name__ == "__main__":
     age_publisher_node()
     except rospy.ROSInterruptException:
          pass

And then, your current code until the last line, except for the imports and environmet declarations, will be placed inside this function.

For the code structures, you may also refer to the roswiki here: ROS/Tutorials/WritingPublisherSubscriber(python) - ROS Wiki

Let me know if it works!

Regards,
Christian

Hi Christian, the error is a module cannot be found error, which is not related to the code structure. Nevertheless, I updated the structure but it did not help.

For this kind of error, it could mean two things. Please try them in this order:

  1. You have properly built and sourced the module on one web shell, but it is not working on another web shell.
  • You need to source ~/catkin_ws/devel/setup.bash on the shell where you are running the program
  1. You have not properly built and sourced the package:
    # On web shell 1
    cd ~/catkin_ws
    rm -rf build/ devel/
    catkin_make
    source devel/setup.bash
    
    # On any other web shell
    cd ~/catkin_ws
    source devel/setup.bash    
    
1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.