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()
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.
#! /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()
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.
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:
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
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