Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of using custom messages in the ros project #10

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mjcarroll
Copy link
Contributor

No description provided.

@mjcarroll mjcarroll marked this pull request as draft September 26, 2023 16:27
@jrutgeer
Copy link
Contributor

jrutgeer commented Oct 16, 2023

There is this part:

gz_msgs_generate_messages(
  # The cmake target to be generated for libraries/executables to link
  TARGET msgs

And then further on:

target_link_libraries(FullSystem PRIVATE
  gz-sim${GZ_SIM_VER}::gz-sim${GZ_SIM_VER}
  ros_gz_example_gazebo-msgs
)

Doest that mean that the TARGET name specified in the gz_msgs_generate_messages() call is combined with the project() name to form the library name?

If so, I think this should be explicitly mentioned.
I suggest to add the following comment:

target_link_libraries(FullSystem PRIVATE
  gz-sim${GZ_SIM_VER}::gz-sim${GZ_SIM_VER}
  # Downstream libraries/executables using custom messages
  # need to link with the custom message library.
  # The name of this library is the concatenation of the project name and
  # the target name as specified in the gz_msgs_generate_messages() call
  # (in this example 'ros_gz_example_gazebo' and 'msgs'):
  ros_gz_example_gazebo-msgs
)

@jrutgeer
Copy link
Contributor

In this example the custom messages and the system plugin that uses them are in the same package.

However in a ROS / Gazebo setting it is typically a more common use case that the messages and systems are in different packages, and the downstream package calls ament_target_dependencies() to load the package dependencies:

ament_target_dependencies(my_system
  PUBLIC
  custom_messages_package
  gz-transport${GZ_TRANSPORT_VER}
  gz-sim${GZ_SIM_VER} 
  rclcpp
  #possible other dependencies
)

I am trying this for my project, and it seems that an extra statement is needed in the CMakeLists.txt of the custom message definition package:

ament_export_include_directories(include)

I am not sure if this is the correct/recommended way to do this, as the documentation states that this should be 'superfluous':

There are two additional functions which are available, but are superfluous for target based installs:
ament_export_include_directories("include/${PROJECT_NAME}")
ament_export_libraries(my_library)

Also note that in my case it seems I need:

  • ament_export_include_directories(include)

as opposed to:

  • ament_export_include_directories(include/${PROJECT_NAME}).

I think it would be valuable to extend this example to also cover this use case.

@jrutgeer
Copy link
Contributor

Above did not work, I did not manage to get it to link.

After another 2h of toiling and plodding, I finally have a working CMakeLists.txt.

I conclude that it is not intended to use ament_export_include_directories(include), but rather:

  • Add following commands to the CMakeLists.txt file of the custom messages package:

    install(
      TARGETS ros_gz_example_gazebo-msgs
      EXPORT arbitrary_name_for_export
    )
    
    target_include_directories(ros_gz_example_gazebo-msgs
      PUBLIC
      "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
      "$<INSTALL_INTERFACE:include>")
    
    ament_export_targets(arbitrary_name_for_export HAS_LIBRARY_TARGET)
  • And add this in the downstream package:

    find_package(ros_gz_example_gazebo REQUIRED COMPONENTS ros_gz_example_gazebo-msgs)
    # Not sure if this could be just 'find_package(ros_gz_example_gazebo REQUIRED)'
    
    add_executable(my_executable src/executable.cc)
    ament_target_dependencies(my_executable ros_gz_example_gazebo)
    
    target_link_libraries(my_executable
      gz-transport${GZ_TRANSPORT_VER}::core
      ros_gz_example_gazebo::ros_gz_example_gazebo-msgs)

@jrutgeer
Copy link
Contributor

It would be great if there were more documentation on the use of CMake / ament for ROS and Gazebo, as currently one has to resort to copy-paste and trial-and-error until it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants