Skip to content

Commit 5b8eeea

Browse files
committed
change commandinterface from obj to shared_ptr
1 parent 5c8a8b4 commit 5b8eeea

File tree

5 files changed

+62
-27
lines changed

5 files changed

+62
-27
lines changed

controller_manager/src/controller_manager.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,12 +516,21 @@ void ControllerManager::register_sub_controller_manager()
516516
{
517517
// TODO(Manuel) we should probably make the keys explicit (add key_generation function to handles)
518518
// send keys with request
519-
for (auto command_state_publisher : result.get()->command_state_publishers)
519+
for (const auto & command_state_publisher : result.get()->command_state_publishers)
520520
{
521521
std::string key = command_state_publisher.name.prefix_name + "/" +
522522
command_state_publisher.name.interface_name;
523-
// auto command_forwarder = resource_manager_->find_command_forwarder(key);
524-
// command_forwarder->subscribe_to_command(command_state_publisher);
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
529+
{
530+
RCLCPP_WARN_STREAM(
531+
get_logger(), "SubControllerManager: Could not find a CommandForwarder for key["
532+
<< key << "]. No subscription to command state possible.");
533+
}
525534
}
526535
RCLCPP_INFO(get_logger(), "SubControllerManager: Successfully registered StatePublishers.");
527536
}

hardware_interface/include/hardware_interface/distributed_control_interface/command_forwarder.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class CommandForwarder final
4545

4646
controller_manager_msgs::msg::PublisherDescription create_publisher_description_msg() const;
4747

48-
void subscribe_to_command(const distributed_control::PublisherDescription & description);
48+
void subscribe_to_command_publisher(const std::string & topic_name);
4949

5050
private:
5151
void publish_value_on_timer();

hardware_interface/include/hardware_interface/loaned_command_interface.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ class LoanedCommandInterface
2828
public:
2929
using Deleter = std::function<void(void)>;
3030

31-
explicit LoanedCommandInterface(CommandInterface & command_interface)
31+
explicit LoanedCommandInterface(std::shared_ptr<ReadWriteHandle> command_interface)
3232
: LoanedCommandInterface(command_interface, nullptr)
3333
{
3434
}
3535

36-
LoanedCommandInterface(CommandInterface & command_interface, Deleter && deleter)
36+
LoanedCommandInterface(std::shared_ptr<ReadWriteHandle> command_interface, Deleter && deleter)
3737
: command_interface_(command_interface), deleter_(std::forward<Deleter>(deleter))
3838
{
3939
}
@@ -50,30 +50,30 @@ class LoanedCommandInterface
5050
}
5151
}
5252

53-
std::string get_name() const { return command_interface_.get_name(); }
53+
std::string get_name() const { return command_interface_->get_name(); }
5454

55-
std::string get_interface_name() const { return command_interface_.get_interface_name(); }
55+
std::string get_interface_name() const { return command_interface_->get_interface_name(); }
5656

5757
[[deprecated(
5858
"Replaced by get_name method, which is semantically more correct")]] const std::string
5959
get_full_name() const
6060
{
61-
return command_interface_.get_name();
61+
return command_interface_->get_name();
6262
}
6363

64-
std::string get_prefix_name() const { return command_interface_.get_prefix_name(); }
64+
std::string get_prefix_name() const { return command_interface_->get_prefix_name(); }
6565

66-
void set_value(double val) { command_interface_.set_value(val); }
66+
void set_value(double val) { command_interface_->set_value(val); }
6767

68-
double get_value() const { return command_interface_.get_value(); }
68+
double get_value() const { return command_interface_->get_value(); }
6969

7070
std::string get_underscore_separated_name() const
7171
{
72-
return command_interface_.get_underscore_separated_name();
72+
return command_interface_->get_underscore_separated_name();
7373
}
7474

7575
protected:
76-
CommandInterface & command_interface_;
76+
std::shared_ptr<ReadWriteHandle> command_interface_;
7777
Deleter deleter_;
7878
};
7979

hardware_interface/src/hardware_interface/distributed_control_interface/command_forwarder.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,9 @@ CommandForwarder::create_publisher_description_msg() const
7878
return msg;
7979
}
8080

81-
void CommandForwarder::subscribe_to_command(
82-
const distributed_control::PublisherDescription & description)
81+
void CommandForwarder::subscribe_to_command_publisher(const std::string & topic_name)
8382
{
84-
subscription_topic_name_ = description.topic_name();
83+
subscription_topic_name_ = topic_name;
8584
command_subscription_ = node_->create_subscription<std_msgs::msg::Float64>(
8685
subscription_topic_name_, 10,
8786
std::bind(&CommandForwarder::forward_command, this, std::placeholders::_1));

hardware_interface/src/resource_manager.cpp

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,33 @@ class ResourceStorage
460460
hardware_info_map_[hardware.get_name()].command_interfaces = add_command_interfaces(interfaces);
461461
}
462462

463+
void add_command_interface(std::shared_ptr<ReadWriteHandle> command_interface)
464+
{
465+
const auto [it, success] = command_interface_map_.insert(
466+
std::make_pair(command_interface->get_name(), command_interface));
467+
if (!success)
468+
{
469+
std::string msg(
470+
"ResourceStorage: Tried to insert CommandInterface with already existing key. Insert[" +
471+
command_interface->get_name() + "]");
472+
throw std::runtime_error(msg);
473+
}
474+
}
475+
476+
void add_command_interface(CommandInterface && command_interface)
477+
{
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))));
481+
if (!success)
482+
{
483+
std::string msg(
484+
"ResourceStorage: Tried to insert CommandInterface with already existing key. Insert[" +
485+
command_interface.get_name() + "]");
486+
throw std::runtime_error(msg);
487+
}
488+
}
489+
463490
/// Adds exported command interfaces into internal storage.
464491
/**
465492
* Add command interfaces to the internal storage. Command interfaces exported from hardware or
@@ -478,7 +505,7 @@ class ResourceStorage
478505
for (auto & interface : interfaces)
479506
{
480507
auto key = interface.get_name();
481-
command_interface_map_.emplace(std::make_pair(key, std::move(interface)));
508+
add_command_interface(std::move(interface));
482509
claimed_command_interface_map_.emplace(std::make_pair(key, false));
483510
interface_names.push_back(key);
484511
}
@@ -563,13 +590,13 @@ class ResourceStorage
563590
std::pair<bool, std::shared_ptr<distributed_control::CommandForwarder>> find_command_forwarder(
564591
const std::string & key)
565592
{
566-
// auto command_forwarder = state_interface_state_publisher_map_.find(key);
567-
// // we could not find a command forwarder for the provided key
568-
// if (command_forwarder == state_interface_state_publisher_map_.end())
569-
// {
570-
// return std::make_pair(false, nullptr);
571-
// }
572-
// return std::make_pair(true, command_forwarder->second);
593+
auto command_forwarder = command_interface_command_forwarder_map_.find(key);
594+
// we could not find a command forwarder for the provided key
595+
if (command_forwarder == command_interface_command_forwarder_map_.end())
596+
{
597+
return std::make_pair(false, nullptr);
598+
}
599+
return std::make_pair(true, command_forwarder->second);
573600
}
574601

575602
void check_for_duplicates(const HardwareInfo & hardware_info)
@@ -708,7 +735,7 @@ class ResourceStorage
708735
/// Storage of all available state interfaces
709736
std::map<std::string, std::shared_ptr<ReadOnlyHandle>> state_interface_map_;
710737
/// Storage of all available command interfaces
711-
std::map<std::string, CommandInterface> command_interface_map_;
738+
std::map<std::string, std::shared_ptr<ReadWriteHandle>> command_interface_map_;
712739

713740
/// Vectors with interfaces available to controllers (depending on hardware component state)
714741
std::vector<std::string> available_state_interfaces_;
@@ -919,7 +946,7 @@ ResourceManager::get_command_forwarders() const
919946
std::pair<bool, std::shared_ptr<distributed_control::CommandForwarder>>
920947
ResourceManager::find_command_forwarder(const std::string & key)
921948
{
922-
// return resource_storage_->find_command_forwarder(key);
949+
return resource_storage_->find_command_forwarder(key);
923950
}
924951

925952
// CM API: Called in "callback/slow"-thread

0 commit comments

Comments
 (0)