|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <thread> |
| 4 | + |
| 5 | +#include <tinyxml2.h> |
| 6 | +#include <rclcpp/rclcpp.hpp> |
| 7 | +#include <rclcpp_action/rclcpp_action.hpp> |
| 8 | + |
| 9 | +#include <capabilities2_runner/service_runner.hpp> |
| 10 | +#include <capabilities2_msgs/srv/get_capability_specs.hpp> |
| 11 | + |
| 12 | +namespace capabilities2_runner |
| 13 | +{ |
| 14 | + |
| 15 | +/** |
| 16 | + * @brief Executor runner class |
| 17 | + * |
| 18 | + * Class to run capabilities2 executor action based capability |
| 19 | + * |
| 20 | + */ |
| 21 | +class CapabilityGetRunner : public ServiceRunner<capabilities2_msgs::srv::GetCapabilitySpecs> |
| 22 | +{ |
| 23 | +public: |
| 24 | + CapabilityGetRunner() : ServiceRunner() |
| 25 | + { |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @brief Starter function for starting the action runner |
| 30 | + * |
| 31 | + * @param node shared pointer to the capabilities node. Allows to use ros node related functionalities |
| 32 | + * @param run_config runner configuration loaded from the yaml file |
| 33 | + */ |
| 34 | + virtual void start(rclcpp::Node::SharedPtr node, const runner_opts& run_config) override |
| 35 | + { |
| 36 | + init_service(node, run_config, "/capabilities/get_capability_specs"); |
| 37 | + } |
| 38 | + |
| 39 | +protected: |
| 40 | + /** |
| 41 | + * @brief This generate goal function overrides the generate_goal() function from ActionRunner() |
| 42 | + * @param parameters XMLElement that contains parameters in the format |
| 43 | + '<Event name=follow_waypoints provider=WaypointRunner x='$value' y='$value' />' |
| 44 | + * @return ActionT::Goal the generated goal |
| 45 | + */ |
| 46 | + virtual capabilities2_msgs::srv::GetCapabilitySpecs::Request generate_request(tinyxml2::XMLElement* parameters, |
| 47 | + int id) override |
| 48 | + { |
| 49 | + capabilities2_msgs::srv::GetCapabilitySpecs::Request request; |
| 50 | + return request; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @brief Update on_success event parameters with new data from an CapabilitySpecs message if avaible. |
| 55 | + * |
| 56 | + * This function is used to inject new data into the XMLElement containing |
| 57 | + * parameters related to the on_success trigger event |
| 58 | + * |
| 59 | + * <CapabilitySpecs> |
| 60 | + * <CapabilitySpec package="robot_navigation" type="capability_interface" |
| 61 | + * default_provider="robot_navigation/navigation_provider"> <content> name: Navigation dependencies: |
| 62 | + * - MapServer |
| 63 | + * - PathPlanner |
| 64 | + * </content> |
| 65 | + * </CapabilitySpec> |
| 66 | + * <CapabilitySpec package="robot_perception" type="capability_interface"> |
| 67 | + * <content> |
| 68 | + * name: ObjectDetection |
| 69 | + * dependencies: |
| 70 | + * - Camera |
| 71 | + * - ImageProcessor |
| 72 | + * </content> |
| 73 | + * </CapabilitySpec> |
| 74 | + * </CapabilitySpecs> |
| 75 | + * |
| 76 | + * @param parameters |
| 77 | + * @return std::string |
| 78 | + */ |
| 79 | + |
| 80 | + virtual std::string update_on_success(std::string& parameters) |
| 81 | + { |
| 82 | + tinyxml2::XMLElement* element = convert_to_xml(parameters); |
| 83 | + |
| 84 | + // Create the OccupancyGrid element as a child of the existing parameters element |
| 85 | + tinyxml2::XMLElement* capabilitySpecsElement = element->GetDocument()->NewElement("CapabilitySpecs"); |
| 86 | + element->InsertEndChild(capabilitySpecsElement); |
| 87 | + |
| 88 | + for (const auto& spec : response_->capability_specs) |
| 89 | + { |
| 90 | + // Create a <CapabilitySpec> element |
| 91 | + tinyxml2::XMLElement* specElement = element->GetDocument()->NewElement("CapabilitySpec"); |
| 92 | + |
| 93 | + // Set attributes |
| 94 | + specElement->SetAttribute("package", spec.package.c_str()); |
| 95 | + specElement->SetAttribute("type", spec.type.c_str()); |
| 96 | + |
| 97 | + if (!spec.default_provider.empty()) |
| 98 | + { |
| 99 | + specElement->SetAttribute("default_provider", spec.default_provider.c_str()); |
| 100 | + } |
| 101 | + |
| 102 | + // Add content as a child element inside <CapabilitySpec> |
| 103 | + tinyxml2::XMLElement* contentElement = element->GetDocument()->NewElement("content"); |
| 104 | + contentElement->SetText(spec.content.c_str()); |
| 105 | + specElement->InsertEndChild(contentElement); |
| 106 | + |
| 107 | + // Append <CapabilitySpec> to <CapabilitySpecs> |
| 108 | + capabilitySpecsElement->InsertEndChild(specElement); |
| 109 | + } |
| 110 | + |
| 111 | + // Convert updated XML back to string and return |
| 112 | + std::string result = convert_to_string(element); |
| 113 | + return result; |
| 114 | + } |
| 115 | +}; |
| 116 | + |
| 117 | +} // namespace capabilities2_runner |
0 commit comments