ROS Resources: Documentation | Support | Discussion Forum | Service Status | Q&A answers.ros.org

ROS2: CMakeList, Command Line Publish, Metapackages [Revised Heading]

I wrote a simple package with class and library to subscribe and publish using rclcpp.

My CMakeLists.txt is:

cmake_minimum_required(VERSION 3.5)

project(testpackage)

if(NOT WIN32)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra")
endif()

find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)

include_directories(include 
					${rclcpp_INCLUDE_DIRS}
                    ${rmw_implementation_INCLUDE_DIRS}
                    ${std_msgs_INCLUDE_DIRS})

add_library(test_lib
src/test.cpp
)

target_link_libraries(test_lib ${rclcpp_LIBRARIES} ${rmw_implementation_LIBRARIES} ${std_msgs_LIBRARIES})

add_executable(test_node src/test_node.cpp)
add_dependencies(test_node test_lib)
target_link_libraries(test_node test_lib)

install(
  DIRECTORY include/${PROJECT_NAME}
  DESTINATION include
)

install(
  TARGETS test_lib test_node 
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin
)

ament_package()

It builds well. However when I try to run the package like this:

ros2 run testpackage test_node
it says no executable found.

Is there something like roscore in ros2 to have the ROS_MASTER and ros services running? I have sourced install/local_setup.bash as given in the tutorials.

I have built using the command = ament build --symlink-install --only-package testpackage
Also i realised, my library is not created inside install/lib/.

Your CMake installs you binary to bin, but ros2 run only looks in the “libexec” folder (actually lib/pkg) for executables. Your binary is just on the PATH, so you don’t need ros2 run to easily execute it.

See our examples for how to install to libexec:

Thanks WIlliam ! could we do something like rostopic pub from the terminal in ros2?

Is it going to be like ros2 ropic pub /test_topic std_msgs/msgs/String "hello there"?? @wjwwood

Also, I realized when I run my executable like ros2 run testpackage test_node … and then do ros2 node list. It shows me the right name. However, when I exit my program (Ctrl+C) I still happen to find my ros2 node if I do a ros2 node list. The instance continues to be there which is sometimes also seen in ROS 1 if a large number of nodes are running however not for just one node.

Another point is my topic names. Even after exiting my running executable ros2 topic list still, shows the topic names. I changed the topic names in my program and built it again and ran it, however, the topic names weren’t changed or I don’t know if it’s the old instance running. Regardless of that, I tried killing the node by ros2 node kill --all however seems like that is not the command to kill anymore.

Please see ros2 topic pub --help for an example.

For me it works for the demo_nodes_cpp / demo_nodes_py talker and listener using FastRTPS on Ubuntu using Beta 3. Maybe you can provide more information what environment / version / nodes you are experiencing it with.

I am using Ubuntu 16.04 Beta 2.
This is my simple package: https://github.com/arunavanag591/test_ros2

When I try this
ros2 topic pub /test_topic std_msgs/String "hello"

It throws me this:

Traceback (most recent call last):
  File "/home/artc/ros2_ws/install/bin/ros2", line 9, in <module>
    load_entry_point('ros2cli==0.0.0', 'console_scripts', 'ros2')()
  File "/home/artc/ros2_ws/install/lib/python3.5/site-packages/ros2cli/cli.py", line 64, in main
    rc = extension.main(parser=parser, args=args)
  File "/home/artc/ros2_ws/install/lib/python3.5/site-packages/ros2topic/command/topic.py", line 38, in main
    return extension.main(args=args)
  File "/home/artc/ros2_ws/install/lib/python3.5/site-packages/ros2topic/verb/pub.py", line 43, in main
    return main(args)
  File "/home/artc/ros2_ws/install/lib/python3.5/site-packages/ros2topic/verb/pub.py", line 47, in main
    return publisher(args.message_type, args.topic_name, args.values)
  File "/home/artc/ros2_ws/install/lib/python3.5/site-packages/ros2topic/verb/pub.py", line 78, in publisher
    set_msg_fields(msg, values_dictionary)
  File "/home/artc/ros2_ws/install/lib/python3.5/site-packages/ros2topic/verb/pub.py", line 92, in set_msg_fields
    for field_name, field_value in values.items():
AttributeError: 'str' object has no attribute 'items'

Did you try passing a yaml dictionnary as the help message suggests?
ros2 topic pub /test_topic std_msgs/String "data: Hello World"

Copied from:

usage: ros2 topic pub [-h] topic_name message_type [values]

Publish a message to a topic

positional arguments:
  topic_name    Name of the ROS topic to publish to (e.g. '/chatter')
  message_type  Type of the ROS message (e.g. 'std_msgs/String')
  values        Values to fill the message with in YAML format (e.g. "data:
                Hello World"), otherwise the message will be published with
                default values

Ah thanks yes I tried earlier, however I totally did not give the space between data: and Hello. Thanks much

Just a quick question, do we create metapackages in ament ros2 the same way as in ros1?

Ah yeah the space is not obvious given that the help message wraps the line just after the :. That tricked me recently as well. The error message has been improved recently to make it clearer that a YAML dictionary is expected.

This has been brought up recently but it hasn’t been decided yet if metapackages should exist as ament packages or if they should be stored in a central place given that they are for producing binaries only and should not be used as dependencies