Skip to content

Support service generation on action folders - #308

Merged
apojomovsky merged 9 commits into
masterfrom
hidmic/prepare_for_action_generation
Oct 30, 2018
Merged

apojomovsky merged 9 commits into
masterfrom
hidmic/prepare_for_action_generation

Conversation

@hidmic

@hidmic hidmic commented Oct 23, 2018 •

Copy link
Copy Markdown
Contributor

Connected to #301. This pull request is part of a series of pull requests that make the necessary changes to allow us to build .srv files outside an srv folder.

connects to #301

@tfoote tfoote added the in progress Actively being worked on (Kanban column) label Oct 23, 2018
@hidmic
hidmic requested review from sloretz and sservulo October 23, 2018 21:39
@@ -110,7 +113,7 @@ extern "C"
ROSIDL_TYPESUPPORT_INTROSPECTION_CPP_PUBLIC
const rosidl_service_type_support_t *
ROSIDL_TYPESUPPORT_INTERFACE__SERVICE_SYMBOL_NAME(rosidl_typesupport_introspection_cpp, @(spec.pkg_name), @(spec.srv_name))() {

@sloretz sloretz Oct 23, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the macro ROSIDL_TYPESUPPORT_INTERFACE__SERVICE_SYMBOL_NAME needs a parameter that is either srv or action just like ROSIDL_TYPESUPPORT_INTERFACE__MESSAGE_SYMBOL_NAME has. Otherwise this creates a symbol with srv in the name that conflicts when the same service name is in an action and a srv folder.

There's a branch with a duplicate service and message for testing here https://github.com/ros2/rcl_interfaces/tree/sloretz/msg_srv_in_action/test_msgs

Here's the compiler linker error when trying to build it (when skipping rosidl_generator_py)

CMakeFiles/test_msgs__rosidl_typesupport_fastrtps_cpp.dir/rosidl_typesupport_fastrtps_cpp/test_msgs/action/dds_fastrtps/primitives__type_support.cpp.o: In function `rosidl_typesupport_fastrtps_cpp__get_service_type_support_handle__test_msgs__srv__Primitives':
/home/developer/workspaces/ros2-dev0/build/test_msgs/rosidl_typesupport_fastrtps_cpp/test_msgs/action/dds_fastrtps/primitives__type_support.cpp:62: multiple definition of `rosidl_typesupport_fastrtps_cpp__get_service_type_support_handle__test_msgs__srv__Primitives'
CMakeFiles/test_msgs__rosidl_typesupport_fastrtps_cpp.dir/rosidl_typesupport_fastrtps_cpp/test_msgs/srv/dds_fastrtps/primitives__type_support.cpp.o:/home/developer/workspaces/ros2-dev0/build/test_msgs/rosidl_typesupport_fastrtps_cpp/test_msgs/srv/dds_fastrtps/primitives__type_support.cpp:62: first defined here
collect2: error: ld returned 1 exit status
make[2]: *** [libtest_msgs__rosidl_typesupport_fastrtps_cpp.so] Error 1
make[1]: *** [CMakeFiles/test_msgs__rosidl_typesupport_fastrtps_cpp.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
CMakeFiles/test_msgs__rosidl_typesupport_introspection_cpp.dir/rosidl_typesupport_introspection_cpp/test_msgs/action/primitives__type_support.cpp.o: In function `rosidl_typesupport_introspection_cpp__get_service_type_support_handle__test_msgs__srv__Primitives':
/home/developer/workspaces/ros2-dev0/build/test_msgs/rosidl_typesupport_introspection_cpp/test_msgs/action/primitives__type_support.cpp:103: multiple definition of `rosidl_typesupport_introspection_cpp__get_service_type_support_handle__test_msgs__srv__Primitives'
CMakeFiles/test_msgs__rosidl_typesupport_introspection_cpp.dir/rosidl_typesupport_introspection_cpp/test_msgs/srv/primitives__type_support.cpp.o:/home/developer/workspaces/ros2-dev0/build/test_msgs/rosidl_typesupport_introspection_cpp/test_msgs/srv/primitives__type_support.cpp:103: first defined here
collect2: error: ld returned 1 exit status
make[2]: *** [libtest_msgs__rosidl_typesupport_introspection_cpp.so] Error 1
make[1]: *** [CMakeFiles/test_msgs__rosidl_typesupport_introspection_cpp.dir/all] Error 2
make: *** [all] Error 2

message(FATAL_ERROR "Interface file with unknown extension: ${_idl_file}")
endif()
set(_generated_source_files "_generated_${_parent_folder}_source_files")
set(_generated_header_files "_generated_${_parent_folder}_header_files")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_generated_header_files doesn't appear to be used anywhere. Possible cause of this CMake Error output?

CMake Error at /home/developer/workspaces/ros2-dev0/install/rosidl_typesupport_introspection_c/share/rosidl_typesupport_introspection_c/cmake/rosidl_typesupport_introspection_c_generate_interfaces.cmake:113 (add_library):
  Cannot find source file:

    /home/developer/workspaces/ros2-dev0/build/test_msgs/rosidl_typesupport_introspection_c/test_msgs/action/shane_dynamic_array_primitives_nested__rosidl_typesupport_introspection_c.h

  Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
  .hxx .in .txx
Call Stack (most recent call first):
  /home/developer/workspaces/ros2-dev0/install/ament_cmake_core/share/ament_cmake_core/cmake/core/ament_execute_extensions.cmake:38 (include)
  /home/developer/workspaces/ros2-dev0/install/rosidl_cmake/share/rosidl_cmake/cmake/rosidl_generate_interfaces.cmake:186 (ament_execute_extensions)
  CMakeLists.txt:42 (rosidl_generate_interfaces)


CMake Error: CMake can not determine linker language for target: test_msgs__rosidl_typesupport_introspection_c
CMake Error: Cannot determine link language for target "test_msgs__rosidl_typesupport_introspection_c".

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I misread the code, looks like the issue is _generated_action_source_files is missing from a call to add_custom_comand

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that seems to be the case.

spec.pkg_name, subfolder,
get_header_filename_from_msg_name(spec.srv_name) + '__traits_hpp']
header_guard_variable = '__'.join([x.upper() for x in header_guard_parts]) + '_'
cpp_namespace = '%s::srv::' % (spec.pkg_name)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing `subfolder here

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5e1b0d6

@sloretz sloretz left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested with

Repos file
repositories:
  ros2/rcl_interfaces:
    type: git
    url: https://github.com/ros2/rcl_interfaces.git
    version: sloretz/msg_srv_in_action
  ros2/rosidl:
    type: git
    url: https://github.com/ros2/rosidl.git
    version: hidmic/prepare_for_action_generation
  ros2/rosidl_dds:
    type: git
    url: https://github.com/ros2/rosidl_dds.git
    version: hidmic/prepare_for_action_generation
  ros2/rosidl_python:
    type: git
    url: https://github.com/ros2/rosidl_python.git
    version: apojomovsky/allow_generating_msg_and_srv_in_action
  ros2/rosidl_typesupport:
    type: git
    url: https://github.com/ros2/rosidl_typesupport.git
    version: hidmic/prepare_for_action_generation
  ros2/rosidl_typesupport_connext:
    type: git
    url: https://github.com/ros2/rosidl_typesupport_connext.git
    version: hidmic/subfolder_aware_srv_generation
  ros2/rosidl_typesupport_fastrtps:
    type: git
    url: https://github.com/ros2/rosidl_typesupport_fastrtps.git
    version: apojomovsky/subfolder_aware_srv_generation
  ros2/rosidl_typesupport_opensplice:
    type: git
    url: https://github.com/ros2/rosidl_typesupport_opensplice.git
    version: sservulo/prepare_for_action_generation
  ros2/system_tests:
    type: git
    url: https://github.com/ros2/system_tests.git
    version: hidmic/namespace-messages-with-subfolder
Python Publisher
from time import sleep

import rclpy

from test_msgs.action import Something

# We do not recommend this style as ROS 2 provides timers for this purpose,
# and it is recommended that all nodes call a variation of spin.
# This example is only included for completeness because it is similar to examples in ROS 1.
# For periodic publication please see the other examples using timers.


def main(args=None):
    rclpy.init(args=args)

    node = rclpy.create_node('minimal_publisher')

    publisher = node.create_publisher(Something, 'topic')

    msg = Something()

    i = 0
    while rclpy.ok():
        msg.string_value = 'Hello World: %d' % i
        i += 1
        node.get_logger().info('Publishing: "%s"' % msg.string_value)
        publisher.publish(msg)
        sleep(0.5)  # seconds

    # Destroy the node explicitly
    # (optional - otherwise it will be done automatically
    # when the garbage collector destroys the node object)
    node.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()
Python Subscriber
import rclpy

from test_msgs.action import Something

g_node = None


def chatter_callback(msg):
    global g_node
    g_node.get_logger().info(
        'I heard: "%s"' % msg.string_value)


def main(args=None):
    global g_node
    rclpy.init(args=args)

    g_node = rclpy.create_node('minimal_subscriber')

    subscription = g_node.create_subscription(Something, 'topic', chatter_callback)
    subscription  # prevent unused variable warning

    while rclpy.ok():
        rclpy.spin_once(g_node)

    # Destroy the node explicitly
    # (optional - otherwise it will be done automatically
    # when the garbage collector destroys the node object)
    g_node.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()
Python Client
from test_msgs.action import Primitives

import rclpy


def main(args=None):
    rclpy.init(args=args)
    node = rclpy.create_node('minimal_client')
    cli = node.create_client(Primitives, 'add_two_ints')

    req = Primitives.Request()
    req.int8_value = 41
    req.int16_value = 1
    while not cli.wait_for_service(timeout_sec=1.0):
        node.get_logger().info('service not available, waiting again...')

    future = cli.call_async(req)
    rclpy.spin_until_future_complete(node, future)

    if future.result() is not None:
        node.get_logger().info(
            'Result of add_two_ints: for %d + %d = %d' %
            (req.int8_value, req.int16_value, future.result().int32_value))
    else:
        node.get_logger().info('Service call failed %r' % (future.exception(),))

    node.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()
Python Service
from test_msgs.action import Primitives

import rclpy

g_node = None


def add_two_ints_callback(request, response):
    global g_node
    response.int32_value = request.int8_value + request.int16_value
    g_node.get_logger().info(
        'Incoming request\na: %d b: %d' % (request.int8_value, request.int16_value))

    return response


def main(args=None):
    global g_node
    rclpy.init(args=args)

    g_node = rclpy.create_node('minimal_service')

    srv = g_node.create_service(Primitives, 'add_two_ints', add_two_ints_callback)
    while rclpy.ok():
        rclpy.spin_once(g_node)

    # Destroy the service attached to the node explicitly
    # (optional - otherwise it will be done automatically
    # when the garbage collector destroys the node object)
    g_node.destroy_service(srv)
    rclpy.shutdown()


if __name__ == '__main__':
    main()
C++ Publisher
#include <iostream>
#include <chrono>
#include "rclcpp/rclcpp.hpp"
#include "test_msgs/action/something.hpp"

using namespace std::chrono_literals;

/* We do not recommend this style anymore, because composition of multiple
 * nodes in the same executable is not possible. Please see one of the subclass
 * examples for the "new" recommended styles. This example is only included
 * for completeness because it is similar to "classic" standalone ROS nodes. */

int main(int argc, char * argv[])
{
  rclcpp::init(argc, argv);
  auto node = rclcpp::Node::make_shared("minimal_publisher");
  auto publisher = node->create_publisher<test_msgs::action::Something>("topic");
  auto message = std::make_shared<test_msgs::action::Something>();
  auto publish_count = 0;
  rclcpp::WallRate loop_rate(500ms);

  while (rclcpp::ok()) {
    message->string_value = "Hello, world! " + std::to_string(publish_count++);
    RCLCPP_INFO(node->get_logger(), "Publishing: '%s'", message->string_value.c_str());
    publisher->publish(message);
    rclcpp::spin_some(node);
    loop_rate.sleep();
  }
  rclcpp::shutdown();
  return 0;
}
C++ Subscriber
#include "rclcpp/rclcpp.hpp"
#include "test_msgs/action/something.hpp"

rclcpp::Node::SharedPtr g_node = nullptr;

/* We do not recommend this style anymore, because composition of multiple
 * nodes in the same executable is not possible. Please see one of the subclass
 * examples for the "new" recommended styles. This example is only included
 * for completeness because it is similar to "classic" standalone ROS nodes. */

void topic_callback(const test_msgs::action::Something::SharedPtr action)
{
  RCLCPP_INFO(g_node->get_logger(), "I heard: '%s'", action->string_value.c_str());
}

int main(int argc, char * argv[])
{
  rclcpp::init(argc, argv);
  g_node = rclcpp::Node::make_shared("minimal_subscriber");
  auto subscription = g_node->create_subscription<test_msgs::action::Something>
      ("topic", topic_callback);
  rclcpp::spin(g_node);
  rclcpp::shutdown();
  // TODO(clalancette): It would be better to remove both of these nullptr
  // assignments and let the destructors handle it, but we can't because of
  // https://github.com/eProsima/Fast-RTPS/issues/235 .  Once that is fixed
  // we should probably look at removing these two assignments.
  subscription = nullptr;
  g_node = nullptr;
  return 0;
}
C++ Client
#include <chrono>
#include <cinttypes>
#include <memory>
#include "test_msgs/action/primitives.hpp"
#include "rclcpp/rclcpp.hpp"

using Primitives = test_msgs::action::Primitives;

int main(int argc, char * argv[])
{
  rclcpp::init(argc, argv);
  auto node = rclcpp::Node::make_shared("minimal_client");
  auto client = node->create_client<Primitives>("add_two_ints");
  while (!client->wait_for_service(std::chrono::seconds(1))) {
    if (!rclcpp::ok()) {
      RCLCPP_ERROR(node->get_logger(), "client interrupted while waiting for service to appear.");
      return 1;
    }
    RCLCPP_INFO(node->get_logger(), "waiting for service to appear...");
  }
  auto request = std::make_shared<Primitives::Request>();
  request->int8_value = 41;
  request->int16_value = 1;
  auto result_future = client->async_send_request(request);
  if (rclcpp::spin_until_future_complete(node, result_future) !=
    rclcpp::executor::FutureReturnCode::SUCCESS)
  {
    RCLCPP_ERROR(node->get_logger(), "service call failed :(");
    return 1;
  }
  auto result = result_future.get();
  RCLCPP_INFO(node->get_logger(), "result of %" PRId64 " + %" PRId64 " = %" PRId64,
    request->int8_value, request->int16_value, result->int32_value);
  rclcpp::shutdown();
  return 0;
}
C++ Service
#include <inttypes.h>
#include <memory>
#include "test_msgs/action/primitives.hpp"
#include "rclcpp/rclcpp.hpp"

using Primitives = test_msgs::action::Primitives;
rclcpp::Node::SharedPtr g_node = nullptr;

void handle_service(
  const std::shared_ptr<rmw_request_id_t> request_header,
  const std::shared_ptr<Primitives::Request> request,
  const std::shared_ptr<Primitives::Response> response)
{
  (void)request_header;
  RCLCPP_INFO(
    g_node->get_logger(),
    "request: %" PRId64 " + %" PRId64, request->int8_value, request->int16_value);
  response->int32_value = request->int8_value + request->int16_value;
}

int main(int argc, char ** argv)
{
  rclcpp::init(argc, argv);
  g_node = rclcpp::Node::make_shared("minimal_service");
  auto server = g_node->create_service<Primitives>("add_two_ints", handle_service);
  rclcpp::spin(g_node);
  rclcpp::shutdown();
  g_node = nullptr;
  return 0;
}

@apojomovsky

Copy link
Copy Markdown

Ok, given that we have green light I'm proceeding to merge this and its related PRs

@apojomovsky
apojomovsky merged commit 94082a6 into master Oct 30, 2018
@tfoote tfoote removed the in progress Actively being worked on (Kanban column) label Oct 30, 2018
@hidmic
hidmic deleted the hidmic/prepare_for_action_generation branch November 2, 2018 14:59
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.

5 participants