Skip to content

Commit 64d14e3

Browse files
authored
Merge branch 'master' into add/hardware_component/statistics
2 parents d5b83c1 + 3e253fc commit 64d14e3

File tree

2 files changed

+95
-96
lines changed

2 files changed

+95
-96
lines changed

controller_manager/include/controller_manager/controller_manager.hpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,42 @@ class ControllerManager : public rclcpp::Node
604604
};
605605

606606
SwitchParams switch_params_;
607+
608+
struct RTBufferVariables
609+
{
610+
RTBufferVariables()
611+
{
612+
deactivate_controllers_list.reserve(1000);
613+
activate_controllers_using_interfaces_list.reserve(1000);
614+
fallback_controllers_list.reserve(1000);
615+
interfaces_to_start.reserve(1000);
616+
interfaces_to_stop.reserve(1000);
617+
concatenated_string.reserve(5000);
618+
}
619+
620+
const std::string & get_concatenated_string(
621+
const std::vector<std::string> & strings, bool clear_string = true)
622+
{
623+
if (clear_string)
624+
{
625+
concatenated_string.clear();
626+
}
627+
for (const auto & str : strings)
628+
{
629+
concatenated_string.append(str);
630+
concatenated_string.append(" ");
631+
}
632+
return concatenated_string;
633+
}
634+
635+
std::vector<std::string> deactivate_controllers_list;
636+
std::vector<std::string> activate_controllers_using_interfaces_list;
637+
std::vector<std::string> fallback_controllers_list;
638+
std::vector<std::string> interfaces_to_start;
639+
std::vector<std::string> interfaces_to_stop;
640+
std::string concatenated_string;
641+
};
642+
RTBufferVariables rt_buffer_;
607643
};
608644

609645
} // namespace controller_manager

controller_manager/src/controller_manager.cpp

Lines changed: 59 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,14 @@ void get_active_controllers_using_command_interfaces_of_controller(
217217

218218
void extract_command_interfaces_for_controller(
219219
const controller_manager::ControllerSpec & ctrl,
220-
const hardware_interface::ResourceManager & resource_manager,
220+
const std::unique_ptr<hardware_interface::ResourceManager> & resource_manager,
221221
std::vector<std::string> & request_interface_list)
222222
{
223223
auto command_interface_config = ctrl.c->command_interface_configuration();
224224
std::vector<std::string> command_interface_names = {};
225225
if (command_interface_config.type == controller_interface::interface_configuration_type::ALL)
226226
{
227-
command_interface_names = resource_manager.available_command_interfaces();
227+
command_interface_names = resource_manager->available_command_interfaces();
228228
}
229229
if (
230230
command_interface_config.type == controller_interface::interface_configuration_type::INDIVIDUAL)
@@ -238,7 +238,7 @@ void extract_command_interfaces_for_controller(
238238
void get_controller_list_command_interfaces(
239239
const std::vector<std::string> & controllers_list,
240240
const std::vector<controller_manager::ControllerSpec> & controllers_spec,
241-
const hardware_interface::ResourceManager & resource_manager,
241+
const std::unique_ptr<hardware_interface::ResourceManager> & resource_manager,
242242
std::vector<std::string> & request_interface_list)
243243
{
244244
for (const auto & controller_name : controllers_list)
@@ -1459,12 +1459,12 @@ controller_interface::return_type ControllerManager::switch_controller(
14591459
if (in_activate_list)
14601460
{
14611461
extract_command_interfaces_for_controller(
1462-
controller, *resource_manager_, activate_command_interface_request_);
1462+
controller, resource_manager_, activate_command_interface_request_);
14631463
}
14641464
if (in_deactivate_list)
14651465
{
14661466
extract_command_interfaces_for_controller(
1467-
controller, *resource_manager_, deactivate_command_interface_request_);
1467+
controller, resource_manager_, deactivate_command_interface_request_);
14681468
}
14691469

14701470
// cache mapping between hardware and controllers for stopping when read/write error happens
@@ -2430,36 +2430,26 @@ void ControllerManager::read(const rclcpp::Time & time, const rclcpp::Duration &
24302430

24312431
if (!ok)
24322432
{
2433-
std::vector<std::string> stop_request = {};
2434-
std::string failed_hardware_string;
2435-
failed_hardware_string.reserve(500);
2433+
rt_buffer_.deactivate_controllers_list.clear();
24362434
// Determine controllers to stop
24372435
for (const auto & hardware_name : failed_hardware_names)
24382436
{
2439-
failed_hardware_string.append(hardware_name);
2440-
failed_hardware_string.append(" ");
24412437
auto controllers = resource_manager_->get_cached_controllers_to_hardware(hardware_name);
2442-
stop_request.insert(stop_request.end(), controllers.begin(), controllers.end());
2443-
}
2444-
std::string stop_request_string;
2445-
stop_request_string.reserve(500);
2446-
for (const auto & controller : stop_request)
2447-
{
2448-
stop_request_string.append(controller);
2449-
stop_request_string.append(" ");
2438+
rt_buffer_.deactivate_controllers_list.insert(
2439+
rt_buffer_.deactivate_controllers_list.end(), controllers.begin(), controllers.end());
24502440
}
24512441
RCLCPP_ERROR(
24522442
get_logger(),
24532443
"Deactivating following hardware components as their read cycle resulted in an error: [ %s]",
2454-
failed_hardware_string.c_str());
2444+
rt_buffer_.get_concatenated_string(failed_hardware_names).c_str());
24552445
RCLCPP_ERROR_EXPRESSION(
2456-
get_logger(), !stop_request_string.empty(),
2446+
get_logger(), !rt_buffer_.deactivate_controllers_list.empty(),
24572447
"Deactivating following controllers as their hardware components read cycle resulted in an "
24582448
"error: [ %s]",
2459-
stop_request_string.c_str());
2449+
rt_buffer_.get_concatenated_string(rt_buffer_.deactivate_controllers_list).c_str());
24602450
std::vector<ControllerSpec> & rt_controller_list =
24612451
rt_controllers_wrapper_.update_and_get_used_by_rt_list();
2462-
deactivate_controllers(rt_controller_list, stop_request);
2452+
deactivate_controllers(rt_controller_list, rt_buffer_.deactivate_controllers_list);
24632453
// TODO(destogl): do auto-start of broadcasters
24642454
}
24652455
}
@@ -2532,7 +2522,7 @@ controller_interface::return_type ControllerManager::update(
25322522
"configuration (use_sim_time parameter) and if a valid clock source is available");
25332523
}
25342524

2535-
std::vector<std::string> failed_controllers_list;
2525+
rt_buffer_.deactivate_controllers_list.clear();
25362526
for (const auto & loaded_controller : rt_controller_list)
25372527
{
25382528
// TODO(v-lopez) we could cache this information
@@ -2630,21 +2620,18 @@ controller_interface::return_type ControllerManager::update(
26302620

26312621
if (controller_ret != controller_interface::return_type::OK)
26322622
{
2633-
failed_controllers_list.push_back(loaded_controller.info.name);
2623+
rt_buffer_.deactivate_controllers_list.push_back(loaded_controller.info.name);
26342624
ret = controller_ret;
26352625
}
26362626
}
26372627
}
26382628
}
2639-
if (!failed_controllers_list.empty())
2629+
if (!rt_buffer_.deactivate_controllers_list.empty())
26402630
{
2641-
const auto FALLBACK_STACK_MAX_SIZE = 500;
2642-
std::vector<std::string> active_controllers_using_interfaces(failed_controllers_list);
2643-
active_controllers_using_interfaces.reserve(FALLBACK_STACK_MAX_SIZE);
2644-
std::vector<std::string> cumulative_fallback_controllers;
2645-
cumulative_fallback_controllers.reserve(FALLBACK_STACK_MAX_SIZE);
2631+
rt_buffer_.fallback_controllers_list.clear();
2632+
rt_buffer_.activate_controllers_using_interfaces_list.clear();
26462633

2647-
for (const auto & failed_ctrl : failed_controllers_list)
2634+
for (const auto & failed_ctrl : rt_buffer_.deactivate_controllers_list)
26482635
{
26492636
auto ctrl_it = std::find_if(
26502637
rt_controller_list.begin(), rt_controller_list.end(),
@@ -2653,72 +2640,58 @@ controller_interface::return_type ControllerManager::update(
26532640
{
26542641
for (const auto & fallback_controller : ctrl_it->info.fallback_controllers_names)
26552642
{
2656-
cumulative_fallback_controllers.push_back(fallback_controller);
2643+
rt_buffer_.fallback_controllers_list.push_back(fallback_controller);
26572644
get_active_controllers_using_command_interfaces_of_controller(
2658-
fallback_controller, rt_controller_list, active_controllers_using_interfaces);
2645+
fallback_controller, rt_controller_list,
2646+
rt_buffer_.activate_controllers_using_interfaces_list);
26592647
}
26602648
}
26612649
}
2662-
std::string controllers_string;
2663-
controllers_string.reserve(500);
2664-
for (const auto & controller : failed_controllers_list)
2665-
{
2666-
controllers_string.append(controller);
2667-
controllers_string.append(" ");
2668-
}
2650+
26692651
RCLCPP_ERROR(
26702652
get_logger(), "Deactivating controllers : [ %s] as their update resulted in an error!",
2671-
controllers_string.c_str());
2672-
if (active_controllers_using_interfaces.size() > failed_controllers_list.size())
2673-
{
2674-
controllers_string.clear();
2675-
for (size_t i = failed_controllers_list.size();
2676-
i < active_controllers_using_interfaces.size(); i++)
2677-
{
2678-
controllers_string.append(active_controllers_using_interfaces[i]);
2679-
controllers_string.append(" ");
2680-
}
2681-
RCLCPP_ERROR_EXPRESSION(
2682-
get_logger(), !controllers_string.empty(),
2683-
"Deactivating controllers : [ %s] using the command interfaces needed for the fallback "
2684-
"controllers to activate.",
2685-
controllers_string.c_str());
2686-
}
2687-
if (!cumulative_fallback_controllers.empty())
2688-
{
2689-
controllers_string.clear();
2690-
for (const auto & controller : cumulative_fallback_controllers)
2691-
{
2692-
controllers_string.append(controller);
2693-
controllers_string.append(" ");
2694-
}
2695-
RCLCPP_ERROR(
2696-
get_logger(), "Activating fallback controllers : [ %s]", controllers_string.c_str());
2697-
}
2698-
std::vector<std::string> failed_controller_interfaces, fallback_controller_interfaces;
2699-
failed_controller_interfaces.reserve(500);
2653+
rt_buffer_.get_concatenated_string(rt_buffer_.deactivate_controllers_list).c_str());
2654+
RCLCPP_ERROR_EXPRESSION(
2655+
get_logger(), !rt_buffer_.activate_controllers_using_interfaces_list.empty(),
2656+
"Deactivating controllers : [ %s] using the command interfaces needed for the fallback "
2657+
"controllers to activate.",
2658+
rt_buffer_.get_concatenated_string(rt_buffer_.activate_controllers_using_interfaces_list)
2659+
.c_str());
2660+
RCLCPP_ERROR_EXPRESSION(
2661+
get_logger(), !rt_buffer_.fallback_controllers_list.empty(),
2662+
"Activating fallback controllers : [ %s]",
2663+
rt_buffer_.get_concatenated_string(rt_buffer_.fallback_controllers_list).c_str());
2664+
std::for_each(
2665+
rt_buffer_.activate_controllers_using_interfaces_list.begin(),
2666+
rt_buffer_.activate_controllers_using_interfaces_list.end(),
2667+
[this](const std::string & controller)
2668+
{ add_element_to_list(rt_buffer_.deactivate_controllers_list, controller); });
2669+
2670+
// Retrieve the interfaces to start and stop from the hardware end
2671+
rt_buffer_.interfaces_to_start.clear();
2672+
rt_buffer_.interfaces_to_stop.clear();
27002673
get_controller_list_command_interfaces(
2701-
failed_controllers_list, rt_controller_list, *resource_manager_,
2702-
failed_controller_interfaces);
2674+
rt_buffer_.deactivate_controllers_list, rt_controller_list, resource_manager_,
2675+
rt_buffer_.interfaces_to_stop);
27032676
get_controller_list_command_interfaces(
2704-
cumulative_fallback_controllers, rt_controller_list, *resource_manager_,
2705-
fallback_controller_interfaces);
2706-
if (!failed_controller_interfaces.empty())
2677+
rt_buffer_.fallback_controllers_list, rt_controller_list, resource_manager_,
2678+
rt_buffer_.interfaces_to_start);
2679+
if (!rt_buffer_.interfaces_to_stop.empty() || !rt_buffer_.interfaces_to_start.empty())
27072680
{
27082681
if (!(resource_manager_->prepare_command_mode_switch(
2709-
fallback_controller_interfaces, failed_controller_interfaces) &&
2682+
rt_buffer_.interfaces_to_start, rt_buffer_.interfaces_to_stop) &&
27102683
resource_manager_->perform_command_mode_switch(
2711-
fallback_controller_interfaces, failed_controller_interfaces)))
2684+
rt_buffer_.interfaces_to_start, rt_buffer_.interfaces_to_stop)))
27122685
{
27132686
RCLCPP_ERROR(
27142687
get_logger(),
27152688
"Error while attempting mode switch when deactivating controllers in update cycle!");
27162689
}
27172690
}
2718-
deactivate_controllers(rt_controller_list, active_controllers_using_interfaces);
2719-
if (!cumulative_fallback_controllers.empty())
2691+
deactivate_controllers(rt_controller_list, rt_buffer_.deactivate_controllers_list);
2692+
if (!rt_buffer_.fallback_controllers_list.empty())
27202693
{
2721-
activate_controllers(rt_controller_list, cumulative_fallback_controllers);
2694+
activate_controllers(rt_controller_list, rt_buffer_.fallback_controllers_list);
27222695
}
27232696
}
27242697

@@ -2739,37 +2712,27 @@ void ControllerManager::write(const rclcpp::Time & time, const rclcpp::Duration
27392712

27402713
if (!ok)
27412714
{
2742-
std::vector<std::string> stop_request = {};
2743-
std::string failed_hardware_string;
2744-
failed_hardware_string.reserve(500);
2715+
rt_buffer_.deactivate_controllers_list.clear();
27452716
// Determine controllers to stop
27462717
for (const auto & hardware_name : failed_hardware_names)
27472718
{
2748-
failed_hardware_string.append(hardware_name);
2749-
failed_hardware_string.append(" ");
27502719
auto controllers = resource_manager_->get_cached_controllers_to_hardware(hardware_name);
2751-
stop_request.insert(stop_request.end(), controllers.begin(), controllers.end());
2752-
}
2753-
std::string stop_request_string;
2754-
stop_request_string.reserve(500);
2755-
for (const auto & controller : stop_request)
2756-
{
2757-
stop_request_string.append(controller);
2758-
stop_request_string.append(" ");
2720+
rt_buffer_.deactivate_controllers_list.insert(
2721+
rt_buffer_.deactivate_controllers_list.end(), controllers.begin(), controllers.end());
27592722
}
27602723
RCLCPP_ERROR(
27612724
get_logger(),
27622725
"Deactivating following hardware components as their write cycle resulted in an error: [ "
27632726
"%s]",
2764-
failed_hardware_string.c_str());
2727+
rt_buffer_.get_concatenated_string(failed_hardware_names).c_str());
27652728
RCLCPP_ERROR_EXPRESSION(
2766-
get_logger(), !stop_request_string.empty(),
2729+
get_logger(), !rt_buffer_.deactivate_controllers_list.empty(),
27672730
"Deactivating following controllers as their hardware components write cycle resulted in an "
27682731
"error: [ %s]",
2769-
stop_request_string.c_str());
2732+
rt_buffer_.get_concatenated_string(rt_buffer_.deactivate_controllers_list).c_str());
27702733
std::vector<ControllerSpec> & rt_controller_list =
27712734
rt_controllers_wrapper_.update_and_get_used_by_rt_list();
2772-
deactivate_controllers(rt_controller_list, stop_request);
2735+
deactivate_controllers(rt_controller_list, rt_buffer_.deactivate_controllers_list);
27732736
// TODO(destogl): do auto-start of broadcasters
27742737
}
27752738
}

0 commit comments

Comments
 (0)