Skip to content

Commit 429820d

Browse files
authored
Cleanup Resource Manager a bit to increase clarity. (backport #816) (#1191)
1 parent fc7097b commit 429820d

File tree

4 files changed

+178
-186
lines changed

4 files changed

+178
-186
lines changed

hardware_interface/include/hardware_interface/resource_manager.hpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,6 @@ class HARDWARE_INTERFACE_PUBLIC ResourceManager
118118
*/
119119
std::vector<std::string> available_state_interfaces() const;
120120

121-
/// Checks whether a state interface is registered under the given key.
122-
/**
123-
* \return true if interface exist, false otherwise.
124-
*/
125-
bool state_interface_exists(const std::string & key) const;
126-
127121
/// Checks whether a state interface is available under the given key.
128122
/**
129123
* \return true if interface is available, false otherwise.
@@ -239,13 +233,6 @@ class HARDWARE_INTERFACE_PUBLIC ResourceManager
239233
*/
240234
std::vector<std::string> available_command_interfaces() const;
241235

242-
/// Checks whether a command interface is registered under the given key.
243-
/**
244-
* \param[in] key string identifying the interface to check.
245-
* \return true if interface exist, false otherwise.
246-
*/
247-
bool command_interface_exists(const std::string & key) const;
248-
249236
/// Checks whether a command interface is available under the given name.
250237
/**
251238
* \param[in] name string identifying the interface to check.
@@ -401,6 +388,19 @@ class HARDWARE_INTERFACE_PUBLIC ResourceManager
401388
*/
402389
void activate_all_components();
403390

391+
/// Checks whether a command interface is registered under the given key.
392+
/**
393+
* \param[in] key string identifying the interface to check.
394+
* \return true if interface exist, false otherwise.
395+
*/
396+
bool command_interface_exists(const std::string & key) const;
397+
398+
/// Checks whether a state interface is registered under the given key.
399+
/**
400+
* \return true if interface exist, false otherwise.
401+
*/
402+
bool state_interface_exists(const std::string & key) const;
403+
404404
private:
405405
void validate_storage(const std::vector<hardware_interface::HardwareInfo> & hardware_info) const;
406406

@@ -411,6 +411,7 @@ class HARDWARE_INTERFACE_PUBLIC ResourceManager
411411
mutable std::recursive_mutex resource_interfaces_lock_;
412412
mutable std::recursive_mutex claimed_command_interfaces_lock_;
413413
mutable std::recursive_mutex resources_lock_;
414+
414415
std::unique_ptr<ResourceStorage> resource_storage_;
415416

416417
// Structure to store read and write status so it is not initialized in the real-time loop

hardware_interface/src/resource_manager.cpp

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,7 @@ ResourceManager::ResourceManager(
675675
}
676676
}
677677

678+
// CM API: Called in "callback/slow"-thread
678679
void ResourceManager::load_urdf(const std::string & urdf, bool validate_interfaces)
679680
{
680681
is_urdf_loaded__ = true;
@@ -730,6 +731,7 @@ LoanedStateInterface ResourceManager::claim_state_interface(const std::string &
730731
return LoanedStateInterface(resource_storage_->state_interface_map_.at(key));
731732
}
732733

734+
// CM API: Called in "callback/slow"-thread
733735
std::vector<std::string> ResourceManager::state_interface_keys() const
734736
{
735737
std::vector<std::string> keys;
@@ -741,19 +743,14 @@ std::vector<std::string> ResourceManager::state_interface_keys() const
741743
return keys;
742744
}
743745

746+
// CM API: Called in "update"-thread
744747
std::vector<std::string> ResourceManager::available_state_interfaces() const
745748
{
746749
std::lock_guard<std::recursive_mutex> guard(resource_interfaces_lock_);
747750
return resource_storage_->available_state_interfaces_;
748751
}
749752

750-
bool ResourceManager::state_interface_exists(const std::string & key) const
751-
{
752-
std::lock_guard<std::recursive_mutex> guard(resource_interfaces_lock_);
753-
return resource_storage_->state_interface_map_.find(key) !=
754-
resource_storage_->state_interface_map_.end();
755-
}
756-
753+
// CM API: Called in "update"-thread (indirectly through `claim_state_interface`)
757754
bool ResourceManager::state_interface_is_available(const std::string & name) const
758755
{
759756
std::lock_guard<std::recursive_mutex> guard(resource_interfaces_lock_);
@@ -763,6 +760,7 @@ bool ResourceManager::state_interface_is_available(const std::string & name) con
763760
name) != resource_storage_->available_state_interfaces_.end();
764761
}
765762

763+
// CM API: Called in "callback/slow"-thread
766764
void ResourceManager::import_controller_reference_interfaces(
767765
const std::string & controller_name, std::vector<CommandInterface> & interfaces)
768766
{
@@ -772,12 +770,14 @@ void ResourceManager::import_controller_reference_interfaces(
772770
resource_storage_->controllers_reference_interfaces_map_[controller_name] = interface_names;
773771
}
774772

773+
// CM API: Called in "callback/slow"-thread
775774
std::vector<std::string> ResourceManager::get_controller_reference_interface_names(
776775
const std::string & controller_name)
777776
{
778777
return resource_storage_->controllers_reference_interfaces_map_.at(controller_name);
779778
}
780779

780+
// CM API: Called in "update"-thread
781781
void ResourceManager::make_controller_reference_interfaces_available(
782782
const std::string & controller_name)
783783
{
@@ -789,6 +789,7 @@ void ResourceManager::make_controller_reference_interfaces_available(
789789
interface_names.end());
790790
}
791791

792+
// CM API: Called in "update"-thread
792793
void ResourceManager::make_controller_reference_interfaces_unavailable(
793794
const std::string & controller_name)
794795
{
@@ -811,6 +812,7 @@ void ResourceManager::make_controller_reference_interfaces_unavailable(
811812
}
812813
}
813814

815+
// CM API: Called in "callback/slow"-thread
814816
void ResourceManager::remove_controller_reference_interfaces(const std::string & controller_name)
815817
{
816818
auto interface_names =
@@ -822,6 +824,7 @@ void ResourceManager::remove_controller_reference_interfaces(const std::string &
822824
resource_storage_->remove_command_interfaces(interface_names);
823825
}
824826

827+
// CM API: Called in "callback/slow"-thread
825828
void ResourceManager::cache_controller_to_hardware(
826829
const std::string & controller_name, const std::vector<std::string> & interfaces)
827830
{
@@ -861,6 +864,7 @@ void ResourceManager::cache_controller_to_hardware(
861864
}
862865
}
863866

867+
// CM API: Called in "update"-thread
864868
std::vector<std::string> ResourceManager::get_cached_controllers_to_hardware(
865869
const std::string & hardware_name)
866870
{
@@ -908,6 +912,7 @@ void ResourceManager::release_command_interface(const std::string & key)
908912
resource_storage_->claimed_command_interface_map_[key] = false;
909913
}
910914

915+
// CM API: Called in "callback/slow"-thread
911916
std::vector<std::string> ResourceManager::command_interface_keys() const
912917
{
913918
std::vector<std::string> keys;
@@ -919,20 +924,14 @@ std::vector<std::string> ResourceManager::command_interface_keys() const
919924
return keys;
920925
}
921926

927+
// CM API: Called in "update"-thread
922928
std::vector<std::string> ResourceManager::available_command_interfaces() const
923929
{
924930
std::lock_guard<std::recursive_mutex> guard(resource_interfaces_lock_);
925931
return resource_storage_->available_command_interfaces_;
926932
}
927933

928-
bool ResourceManager::command_interface_exists(const std::string & key) const
929-
{
930-
std::lock_guard<std::recursive_mutex> guard(resource_interfaces_lock_);
931-
return resource_storage_->command_interface_map_.find(key) !=
932-
resource_storage_->command_interface_map_.end();
933-
}
934-
935-
// CM API
934+
// CM API: Called in "callback/slow"-thread
936935
bool ResourceManager::command_interface_is_available(const std::string & name) const
937936
{
938937
std::lock_guard<std::recursive_mutex> guard(resource_interfaces_lock_);
@@ -942,16 +941,6 @@ bool ResourceManager::command_interface_is_available(const std::string & name) c
942941
name) != resource_storage_->available_command_interfaces_.end();
943942
}
944943

945-
size_t ResourceManager::actuator_components_size() const
946-
{
947-
return resource_storage_->actuators_.size();
948-
}
949-
950-
size_t ResourceManager::sensor_components_size() const
951-
{
952-
return resource_storage_->sensors_.size();
953-
}
954-
955944
void ResourceManager::import_component(
956945
std::unique_ptr<ActuatorInterface> actuator, const HardwareInfo & hardware_info)
957946
{
@@ -982,12 +971,7 @@ void ResourceManager::import_component(
982971
resource_storage_->systems_.size());
983972
}
984973

985-
size_t ResourceManager::system_components_size() const
986-
{
987-
return resource_storage_->systems_.size();
988-
}
989-
// End of "used only in tests"
990-
974+
// CM API: Called in "callback/slow"-thread
991975
std::unordered_map<std::string, HardwareComponentInfo> ResourceManager::get_components_status()
992976
{
993977
for (auto & component : resource_storage_->actuators_)
@@ -1006,6 +990,7 @@ std::unordered_map<std::string, HardwareComponentInfo> ResourceManager::get_comp
1006990
return resource_storage_->hardware_info_map_;
1007991
}
1008992

993+
// CM API: Called in "callback/slow"-thread
1009994
bool ResourceManager::prepare_command_mode_switch(
1010995
const std::vector<std::string> & start_interfaces,
1011996
const std::vector<std::string> & stop_interfaces)
@@ -1051,6 +1036,7 @@ bool ResourceManager::prepare_command_mode_switch(
10511036
return true;
10521037
}
10531038

1039+
// CM API: Called in "update"-thread
10541040
bool ResourceManager::perform_command_mode_switch(
10551041
const std::vector<std::string> & start_interfaces,
10561042
const std::vector<std::string> & stop_interfaces)
@@ -1078,6 +1064,7 @@ bool ResourceManager::perform_command_mode_switch(
10781064
return true;
10791065
}
10801066

1067+
// CM API: Called in "callback/slow"-thread
10811068
return_type ResourceManager::set_component_state(
10821069
const std::string & component_name, rclcpp_lifecycle::State & target_state)
10831070
{
@@ -1161,6 +1148,7 @@ return_type ResourceManager::set_component_state(
11611148
return result;
11621149
}
11631150

1151+
// CM API: Called in "update"-thread
11641152
HardwareReadWriteStatus ResourceManager::read(
11651153
const rclcpp::Time & time, const rclcpp::Duration & period)
11661154
{
@@ -1188,6 +1176,7 @@ HardwareReadWriteStatus ResourceManager::read(
11881176
return read_write_status;
11891177
}
11901178

1179+
// CM API: Called in "update"-thread
11911180
HardwareReadWriteStatus ResourceManager::write(
11921181
const rclcpp::Time & time, const rclcpp::Duration & period)
11931182
{
@@ -1214,6 +1203,39 @@ HardwareReadWriteStatus ResourceManager::write(
12141203
return read_write_status;
12151204
}
12161205

1206+
// BEGIN: "used only in tests and locally"
1207+
size_t ResourceManager::actuator_components_size() const
1208+
{
1209+
return resource_storage_->actuators_.size();
1210+
}
1211+
1212+
size_t ResourceManager::sensor_components_size() const
1213+
{
1214+
return resource_storage_->sensors_.size();
1215+
}
1216+
1217+
size_t ResourceManager::system_components_size() const
1218+
{
1219+
return resource_storage_->systems_.size();
1220+
}
1221+
1222+
bool ResourceManager::command_interface_exists(const std::string & key) const
1223+
{
1224+
std::lock_guard<std::recursive_mutex> guard(resource_interfaces_lock_);
1225+
return resource_storage_->command_interface_map_.find(key) !=
1226+
resource_storage_->command_interface_map_.end();
1227+
}
1228+
1229+
bool ResourceManager::state_interface_exists(const std::string & key) const
1230+
{
1231+
std::lock_guard<std::recursive_mutex> guard(resource_interfaces_lock_);
1232+
return resource_storage_->state_interface_map_.find(key) !=
1233+
resource_storage_->state_interface_map_.end();
1234+
}
1235+
// END: "used only in tests and locally"
1236+
1237+
// BEGIN: private methods
1238+
12171239
void ResourceManager::validate_storage(
12181240
const std::vector<hardware_interface::HardwareInfo> & hardware_info) const
12191241
{
@@ -1286,7 +1308,9 @@ void ResourceManager::validate_storage(
12861308
}
12871309
}
12881310

1289-
// Temporary method to keep old interface and reduce framework changes in PRs
1311+
// END: private methods
1312+
1313+
// Temporary method to keep old interface and reduce framework changes in the PRs
12901314
void ResourceManager::activate_all_components()
12911315
{
12921316
using lifecycle_msgs::msg::State;

0 commit comments

Comments
 (0)