Keep having issues with custom message

Hello like many this exercise for creating a custom message is a bit hard.

To be honest I have done this before but while I’m following all the steps I keep having an import error. But when I do rosmsg list | grep Age I get the correct information as shown in the examples. Now when I even copy the code form the solution and only change the package name it still won’t work.
Than I removed the Build and Devel folders but still issues, So I don’t know what to do anymore actually.

The package.xml file

<?xml version="1.0"?>
<package format="2">
  <name>read_odometry_pkg</name>
  <version>0.0.0</version>
  <description>The read_odometry_pkg package</description>


  <maintainer email="user@todo.todo">user</maintainer>

  <license>TODO</license>

  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>rospy</build_depend>
  <build_depend>std_msgs</build_depend>
  <build_depend>message_generation</build_depend>
  <build_export_depend>rospy</build_export_depend>
  <exec_depend>rospy</exec_depend>
  <build_export_depend>std_msgs</build_export_depend>
  <exec_depend>std_msgs</exec_depend>
  <build_export_depend>message_runtime</build_export_depend>
  <exec_depend>message_runtime</exec_depend>
  <export>

  </export>
</package>

CMakeLists

cmake_minimum_required(VERSION 2.8.3)
project(read_odometry_pkg)

## Here go all the packages needed to COMPILE the messages of topic, services and actions.
## Its only geting its paths, and not really importing them to be used in the compilation.
## Its only for further functions in CMakeLists.txt to be able to find those packages.
## In package.xml you have to state them as build
find_package(catkin REQUIRED COMPONENTS
  std_msgs
  message_generation
)

## Generate topic messages in the 'msg' folder
## In this function will be placed all the topic messages files of this package ( in the msg folder ) to be compiled.
add_message_files(
  FILES
  Age.msg
)

## Here is where the packages needed for the topic messages compilation are imported.
generate_messages(
  DEPENDENCIES
  std_msgs
)

## State here all the packages that will be needed by someone that executes something from your package.
## All the packages stated here must be in the package.xml as exec_depend
catkin_package(
  CATKIN_DEPENDS rospy message_runtime
)

include_directories(
  ${catkin_INCLUDE_DIRS}
)

And the scripts I wrote

#! /usr/bin/env python

import rospy
from read_odometry_pkg import Age

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

age = Age()
age.years = 5
age.monts = 6
age.days = 15

while not rospy.is_shutdown():
    pub.publish(age)
    rate.sleep()
    

Obviously the message file I created is called Age.msg and contains the three float32’s. I don’t see where I make any mistake here. I started first to make adjust CMakeLists and the package.xml files but when that didn’t work out well I copied it from the solution but I keep having issues.

Maybe this information helps, it is the error info:
ImportError: cannot import name ‘Age’ from ‘read_odometry_pkg’ (/home/user/catkin_ws/devel/lib/python3/dist-packages/read_odometry_pkg/init.py)

Anybody knows why?

Perhaps this is your scenario.

Did you run source devel/setup.bash on the same terminal where you are attempting to run your package? After (re)compiling your package, you need to source devel/setup.bash on each terminal, otherwise they will not know about your changes.

Additionally, since it appears that you modified your package a number of times, you might need to remove build and devel folder every time you are recompiling (after every change):

cd ~/catkin_ws
rm -rf build/ devel/
catkin_make
source devel/setup.bash # run this line only on the other terminals too

@bayodesegun

Yes I did both of your suggestions, thats why I posted the question cause I don’t know where to look anymore.

Wait a second…!

Your import line for the message is incorrect. It should be:

from read_odometry_pkg.msg import Age

Ai ai ai I think you are right it seems I made a mistake which I completely overlooked. I will try it asap when I have better internet. I’m working offshore at the moment so I have limited access to the internet.

But again I think you have seen my error correctly

thanks

GRTZ

Ray

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