Can't publish or echo topics with custom message types from command line
Hello. I have starting to migrate from ROS1 to ROS2 and have encountered some issues trying to use the topic tools in the command line. I can use the "ros2 topic" tools just fine with message types that came installed with ROS2 humble (std_msgs, etc.). However, I discovered that some of these tools are not working for me for a package I've been porting to ROS2 that contains and uses custom-defined messages. The package which includes the custom messages builds just fine and runs, but I can't call "ros2 topic echo" or "pub" for the topics with the types defined in that package (but work fine on other types).
I've tried to follow this example as closely as possible, and present a minimal package below which replicates this issue for me. The error text when trying to call "ros2 topic pub/echo" (after sourcing the workspace) is displayed at the bottom of my question.
test_msgs/msg/TestMessage.msg
std_msgs/Int16 data
test_msgs/CMakeLists.txt
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
project(test_msgs)
set(CMAKE_CXX_STANDARD 17)
find_package(ament_cmake REQUIRED)
find_package(rosidl_default_generators REQUIRED)
find_package(std_msgs REQUIRED)
set(MSG_FILES "msg/TestMessage.msg")
rosidl_generate_interfaces(${PROJECT_NAME} ${MSG_FILES} DEPENDENCIES std_msgs)
ament_export_dependencies(rosidl_default_runtime)
ament_package()
test_msgs/package.xml
<package format="3">
<name>test_msgs</name>
<version>0.0.0</version>
<description>No</description>
<maintainer email="no@no.no">No</maintainer>
<license>Test</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<buildtool_depend>rosidl_default_generators</buildtool_depend>
<exec_depend>rosidl_default_runtime</exec_depend>
<test_depend>ament_lint_common</test_depend>
<member_of_group>rosidl_interface_packages</member_of_group>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>
After building, the built interface shows up using the ros2 interface tool:
ros2 interface list | grep test
returns:
pendulum_msgs/msg/RttestResults
test_msgs/msg/TestMessage
as expected. I can also show the fields of the message from the command line without error:
ros2 interface show test_msgs/msg/TestMessage
returns:
int16 data
as expected.
I can publish a topic with a type from the nominal ROS2 distribution:
ros2 topic pub /test std_msgs/Int16 "data: 0"
publisher: beginning loop
publishing #1: std_msgs.msg.Int16(data=0)
However, I get this error when trying to publish a topic with my custom message (after sourcing the workspace, of course):
ros2 topic pub /test test_msgs/msg/TestMessage "data: {data: 0}"
Traceback (most recent call last):
File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_generator_py/import_type_support_impl.py", line 46, in import_type_support
return importlib.import_module(module_name, package=pkg_name)
File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'test_msgs.test_msgs_s__rosidl_typesupport_c'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/opt/ros/humble/bin/ros2", line 33, in <module>
sys.exit(load_entry_point('ros2cli==0.18.6', 'console_scripts', 'ros2')())
File "/opt/ros/humble/lib/python3.10/site-packages/ros2cli/cli.py", line 89, in main
rc = extension.main(parser=parser, args=args)
File "/opt/ros/humble/lib/python3.10/site-packages/ros2topic/command/topic.py", line 41, in ...
Quick comment: I've seen this happen whenever I forget to
source
thesetup.bash
(orlocal_setup.bash
) after building a new msg package. Just something to check.Yeah, I have triple checked this, and unfortunately is not the issue.