Skip to content

Commit 8148af1

Browse files
committed
Add own qualitiy of service and register command forwarders
1 parent 5b8eeea commit 8148af1

File tree

2 files changed

+52
-40
lines changed

2 files changed

+52
-40
lines changed

controller_manager/src/controller_manager.cpp

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ static const rmw_qos_profile_t rmw_qos_profile_services_hist_keep_all = {
5353
RMW_QOS_LIVELINESS_LEASE_DURATION_DEFAULT,
5454
false};
5555

56+
rclcpp::QoSInitialization qos_profile_services_keep_all_persist_init(
57+
RMW_QOS_POLICY_HISTORY_KEEP_ALL, 1);
58+
rclcpp::QoS qos_profile_services_keep_all(qos_profile_services_keep_all_persist_init);
59+
5660
inline bool is_controller_inactive(const controller_interface::ControllerInterfaceBase & controller)
5761
{
5862
return controller.get_state().id() == lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE;
@@ -344,7 +348,7 @@ void ControllerManager::init_distributed_main_controller_services()
344348
std::bind(
345349
&ControllerManager::register_sub_controller_manager_srv_cb, this, std::placeholders::_1,
346350
std::placeholders::_2),
347-
rmw_qos_profile_services_hist_keep_all, distributed_system_srv_callback_group_);
351+
qos_profile_services_keep_all, distributed_system_srv_callback_group_);
348352
}
349353

350354
void ControllerManager::register_sub_controller_manager_srv_cb(
@@ -362,6 +366,7 @@ void ControllerManager::register_sub_controller_manager_srv_cb(
362366

363367
std::vector<std::shared_ptr<hardware_interface::DistributedReadOnlyHandle>>
364368
distributed_state_interfaces;
369+
distributed_state_interfaces.reserve(sub_ctrl_mng_wrapper->get_state_publisher_count());
365370
distributed_state_interfaces =
366371
resource_manager_->import_state_interfaces_of_sub_controller_manager(sub_ctrl_mng_wrapper);
367372

@@ -384,6 +389,7 @@ void ControllerManager::register_sub_controller_manager_srv_cb(
384389

385390
std::vector<std::shared_ptr<hardware_interface::DistributedReadWriteHandle>>
386391
distributed_command_interfaces;
392+
distributed_command_interfaces.reserve(sub_ctrl_mng_wrapper->get_command_forwarder_count());
387393
distributed_command_interfaces =
388394
resource_manager_->import_command_interfaces_of_sub_controller_manager(sub_ctrl_mng_wrapper);
389395

@@ -392,14 +398,6 @@ void ControllerManager::register_sub_controller_manager_srv_cb(
392398
try
393399
{
394400
executor_->add_node(command_interface->get_node()->get_node_base_interface());
395-
auto msg = controller_manager_msgs::msg::PublisherDescription();
396-
msg.ns = get_namespace();
397-
msg.name.prefix_name = command_interface->get_prefix_name();
398-
msg.name.interface_name = command_interface->get_interface_name();
399-
// want topic name relative to namespace
400-
msg.publisher_topic = std::string(get_namespace()) + std::string("/") +
401-
command_interface->forward_command_topic_name();
402-
response->command_state_publishers.push_back(msg);
403401
}
404402
catch (const std::runtime_error & e)
405403
{
@@ -410,6 +408,13 @@ void ControllerManager::register_sub_controller_manager_srv_cb(
410408
"Exception:"
411409
<< e.what());
412410
}
411+
auto msg = controller_manager_msgs::msg::PublisherDescription();
412+
msg.ns = get_namespace();
413+
msg.name.prefix_name = command_interface->get_prefix_name();
414+
msg.name.interface_name = command_interface->get_interface_name();
415+
// TODO(Manuel) want topic name relative to namespace, but have to treat "root" namespace separate
416+
msg.publisher_topic = std::string("/") + command_interface->forward_command_topic_name();
417+
response->command_state_publishers.push_back(msg);
413418
}
414419

415420
response->ok = true;
@@ -462,7 +467,7 @@ void ControllerManager::add_hardware_command_forwarders()
462467

463468
void ControllerManager::register_sub_controller_manager()
464469
{
465-
RCLCPP_INFO(get_logger(), "SubControllerManager:Trying to register StatePublishers.");
470+
RCLCPP_INFO(get_logger(), "SubControllerManager: Trying to register StatePublishers.");
466471
rclcpp::Client<controller_manager_msgs::srv::RegisterSubControllerManager>::SharedPtr client =
467472
create_client<controller_manager_msgs::srv::RegisterSubControllerManager>(
468473
"/register_sub_controller_manager");
@@ -510,29 +515,42 @@ void ControllerManager::register_sub_controller_manager()
510515

511516
auto result = client->async_send_request(request);
512517
if (
513-
(rclcpp::spin_until_future_complete(this->get_node_base_interface(), result) ==
514-
rclcpp::FutureReturnCode::SUCCESS) &&
515-
result.get()->ok)
516-
{
517-
// TODO(Manuel) we should probably make the keys explicit (add key_generation function to handles)
518-
// send keys with request
519-
for (const auto & command_state_publisher : result.get()->command_state_publishers)
520-
{
521-
std::string key = command_state_publisher.name.prefix_name + "/" +
522-
command_state_publisher.name.interface_name;
523-
auto [found, command_forwarder] = resource_manager_->find_command_forwarder(key);
524-
if (found)
525-
{
526-
command_forwarder->subscribe_to_command_publisher(command_state_publisher.publisher_topic);
527-
}
528-
else
518+
rclcpp::spin_until_future_complete(this->get_node_base_interface(), result) ==
519+
rclcpp::FutureReturnCode::SUCCESS)
520+
{
521+
// can call get only once
522+
auto res = result.get();
523+
if (res->ok)
524+
{
525+
auto command_state_publishers = res->command_state_publishers;
526+
// TODO(Manuel) we should probably make the keys explicit (add key_generation function to handles)
527+
// send keys with request
528+
for (const auto & command_state_publisher : command_state_publishers)
529529
{
530-
RCLCPP_WARN_STREAM(
531-
get_logger(), "SubControllerManager: Could not find a CommandForwarder for key["
532-
<< key << "]. No subscription to command state possible.");
530+
std::string key = command_state_publisher.name.prefix_name + "/" +
531+
command_state_publisher.name.interface_name;
532+
auto [found, command_forwarder] = resource_manager_->find_command_forwarder(key);
533+
if (found)
534+
{
535+
command_forwarder->subscribe_to_command_publisher(
536+
command_state_publisher.publisher_topic);
537+
}
538+
else
539+
{
540+
RCLCPP_WARN_STREAM(
541+
get_logger(), "SubControllerManager: Could not find a CommandForwarder for key["
542+
<< key << "]. No subscription to command state possible.");
543+
}
533544
}
545+
RCLCPP_INFO(get_logger(), "SubControllerManager: Successfully registered StatePublishers.");
546+
}
547+
else
548+
{
549+
RCLCPP_WARN(
550+
get_logger(),
551+
"SubControllerManager: Registration of StatePublishers failed. Central ControllerManager "
552+
"returned error code.");
534553
}
535-
RCLCPP_INFO(get_logger(), "SubControllerManager: Successfully registered StatePublishers.");
536554
}
537555
else
538556
{

hardware_interface/src/resource_manager.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -475,14 +475,14 @@ class ResourceStorage
475475

476476
void add_command_interface(CommandInterface && command_interface)
477477
{
478-
const auto [it, success] = command_interface_map_.emplace(std::make_pair(
479-
command_interface.get_name(),
480-
std::make_shared<CommandInterface>(std::move(command_interface))));
478+
std::string key = command_interface.get_name();
479+
const auto [it, success] = command_interface_map_.emplace(
480+
std::make_pair(key, std::make_shared<CommandInterface>(std::move(command_interface))));
481481
if (!success)
482482
{
483483
std::string msg(
484484
"ResourceStorage: Tried to insert CommandInterface with already existing key. Insert[" +
485-
command_interface.get_name() + "]");
485+
key + "]");
486486
throw std::runtime_error(msg);
487487
}
488488
}
@@ -890,9 +890,6 @@ ResourceManager::create_hardware_state_publishers(const std::string & ns)
890890

891891
for (const auto & state_interface : available_state_interfaces())
892892
{
893-
RCLCPP_INFO(
894-
rclcpp::get_logger("ResourceManager"), "Creating StatePublisher for interface:<%s>.",
895-
state_interface.c_str());
896893
auto state_publisher = std::make_shared<distributed_control::StatePublisher>(
897894
std::move(std::make_unique<hardware_interface::LoanedStateInterface>(
898895
claim_state_interface(state_interface))),
@@ -914,9 +911,6 @@ ResourceManager::create_hardware_command_forwarders(const std::string & ns)
914911

915912
for (const auto & command_interface : available_command_interfaces())
916913
{
917-
RCLCPP_INFO(
918-
rclcpp::get_logger("ResourceManager"), "Creating CommandForwarder for interface:<%s>.",
919-
command_interface.c_str());
920914
auto command_forwarder = std::make_shared<distributed_control::CommandForwarder>(
921915
std::move(std::make_unique<hardware_interface::LoanedCommandInterface>(
922916
claim_command_interface(command_interface))),

0 commit comments

Comments
 (0)