-
Notifications
You must be signed in to change notification settings - Fork 334
/
Copy pathresource_manager.cpp
2052 lines (1879 loc) · 70 KB
/
resource_manager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "hardware_interface/resource_manager.hpp"
#include <functional>
#include <map>
#include <memory>
#include <stdexcept>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#include "hardware_interface/actuator.hpp"
#include "hardware_interface/actuator_interface.hpp"
#include "hardware_interface/component_parser.hpp"
#include "hardware_interface/hardware_component_info.hpp"
#include "hardware_interface/sensor.hpp"
#include "hardware_interface/sensor_interface.hpp"
#include "hardware_interface/system.hpp"
#include "hardware_interface/system_interface.hpp"
#include "lifecycle_msgs/msg/state.hpp"
#include "pluginlib/class_loader.hpp"
#include "rclcpp/logging.hpp"
#include "rcutils/logging_macros.h"
namespace hardware_interface
{
auto trigger_and_print_hardware_state_transition =
[](
auto transition, const std::string transition_name, const std::string & hardware_name,
const lifecycle_msgs::msg::State::_id_type & target_state)
{
RCUTILS_LOG_INFO_NAMED(
"resource_manager", "'%s' hardware '%s' ", transition_name.c_str(), hardware_name.c_str());
const rclcpp_lifecycle::State new_state = transition();
bool result = new_state.id() == target_state;
if (result)
{
RCUTILS_LOG_INFO_NAMED(
"resource_manager", "Successful '%s' of hardware '%s'", transition_name.c_str(),
hardware_name.c_str());
}
else
{
RCUTILS_LOG_ERROR_NAMED(
"resource_manager", "Failed to '%s' hardware '%s'", transition_name.c_str(),
hardware_name.c_str());
}
return result;
};
std::string interfaces_to_string(
const std::vector<std::string> & start_interfaces,
const std::vector<std::string> & stop_interfaces)
{
std::stringstream ss;
ss << "Start interfaces: " << std::endl << "[" << std::endl;
for (const auto & start_if : start_interfaces)
{
ss << " " << start_if << std::endl;
}
ss << "]" << std::endl;
ss << "Stop interfaces: " << std::endl << "[" << std::endl;
for (const auto & stop_if : stop_interfaces)
{
ss << " " << stop_if << std::endl;
}
ss << "]" << std::endl;
return ss.str();
};
class ResourceStorage
{
static constexpr const char * pkg_name = "hardware_interface";
static constexpr const char * actuator_interface_name = "hardware_interface::ActuatorInterface";
static constexpr const char * sensor_interface_name = "hardware_interface::SensorInterface";
static constexpr const char * system_interface_name = "hardware_interface::SystemInterface";
public:
// TODO(VX792): Change this when HW ifs get their own update rate,
// because the ResourceStorage really shouldn't know about the cm's parameters
explicit ResourceStorage(
rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface,
rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr logger_interface)
: actuator_loader_(pkg_name, actuator_interface_name),
sensor_loader_(pkg_name, sensor_interface_name),
system_loader_(pkg_name, system_interface_name),
clock_interface_(clock_interface),
rm_logger_(rclcpp::get_logger("resource_manager"))
{
if (!clock_interface_)
{
throw std::invalid_argument(
"Clock interface is nullptr. ResourceManager needs a valid clock interface.");
}
if (logger_interface)
{
rm_logger_ = logger_interface->get_logger().get_child("resource_manager");
}
}
template <class HardwareT, class HardwareInterfaceT>
[[nodiscard]] bool load_hardware(
const HardwareInfo & hardware_info, pluginlib::ClassLoader<HardwareInterfaceT> & loader,
std::vector<HardwareT> & container)
{
bool is_loaded = false;
try
{
RCLCPP_INFO(get_logger(), "Loading hardware '%s' ", hardware_info.name.c_str());
// hardware_plugin_name has to match class name in plugin xml description
auto interface = std::unique_ptr<HardwareInterfaceT>(
loader.createUnmanagedInstance(hardware_info.hardware_plugin_name));
if (interface)
{
RCLCPP_INFO(
get_logger(), "Loaded hardware '%s' from plugin '%s'", hardware_info.name.c_str(),
hardware_info.hardware_plugin_name.c_str());
HardwareT hardware(std::move(interface));
container.emplace_back(std::move(hardware));
// initialize static data about hardware component to reduce later calls
HardwareComponentInfo component_info;
component_info.name = hardware_info.name;
component_info.type = hardware_info.type;
component_info.group = hardware_info.group;
component_info.rw_rate = hardware_info.rw_rate;
component_info.plugin_name = hardware_info.hardware_plugin_name;
component_info.is_async = hardware_info.is_async;
hardware_info_map_.insert(std::make_pair(component_info.name, component_info));
hw_group_state_.insert(std::make_pair(component_info.group, return_type::OK));
hardware_used_by_controllers_.insert(
std::make_pair(component_info.name, std::vector<std::string>()));
is_loaded = true;
}
else
{
RCLCPP_ERROR(
get_logger(), "Failed to load hardware '%s' from plugin '%s'", hardware_info.name.c_str(),
hardware_info.hardware_plugin_name.c_str());
}
}
catch (const pluginlib::PluginlibException & ex)
{
RCLCPP_ERROR(
get_logger(), "Caught exception of type : %s while loading hardware: %s", typeid(ex).name(),
ex.what());
}
catch (const std::exception & ex)
{
RCLCPP_ERROR(
get_logger(), "Exception of type : %s occurred while loading hardware '%s': %s",
typeid(ex).name(), hardware_info.name.c_str(), ex.what());
}
catch (...)
{
RCLCPP_ERROR(
get_logger(), "Unknown exception occurred while loading hardware '%s'",
hardware_info.name.c_str());
}
return is_loaded;
}
template <class HardwareT>
bool initialize_hardware(const HardwareInfo & hardware_info, HardwareT & hardware)
{
RCLCPP_INFO(get_logger(), "Initialize hardware '%s' ", hardware_info.name.c_str());
bool result = false;
try
{
const rclcpp_lifecycle::State new_state =
hardware.initialize(hardware_info, rm_logger_, clock_interface_);
result = new_state.id() == lifecycle_msgs::msg::State::PRIMARY_STATE_UNCONFIGURED;
if (result)
{
RCLCPP_INFO(
get_logger(), "Successful initialization of hardware '%s'", hardware_info.name.c_str());
}
else
{
RCLCPP_ERROR(
get_logger(), "Failed to initialize hardware '%s'", hardware_info.name.c_str());
}
}
catch (const std::exception & ex)
{
RCLCPP_ERROR(
get_logger(), "Exception of type : %s occurred while initializing hardware '%s': %s",
typeid(ex).name(), hardware_info.name.c_str(), ex.what());
}
catch (...)
{
RCLCPP_ERROR(
get_logger(), "Unknown exception occurred while initializing hardware '%s'",
hardware_info.name.c_str());
}
return result;
}
template <class HardwareT>
bool configure_hardware(HardwareT & hardware)
{
bool result = false;
try
{
result = trigger_and_print_hardware_state_transition(
std::bind(&HardwareT::configure, &hardware), "configure", hardware.get_name(),
lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE);
}
catch (const std::exception & ex)
{
RCLCPP_ERROR(
get_logger(), "Exception of type : %s occurred while configuring hardware '%s': %s",
typeid(ex).name(), hardware.get_name().c_str(), ex.what());
}
catch (...)
{
RCLCPP_ERROR(
get_logger(), "Unknown exception occurred while configuring hardware '%s'",
hardware.get_name().c_str());
}
if (result)
{
// TODO(destogl): is it better to check here if previous state was unconfigured instead of
// checking if each state already exists? Or we should somehow know that transition has
// happened and only then trigger this part of the code?
// On the other side this part of the code should never be executed in real-time critical
// thread, so it could be also OK as it is...
for (const auto & interface : hardware_info_map_[hardware.get_name()].state_interfaces)
{
// add all state interfaces to available list
auto found_it = std::find(
available_state_interfaces_.begin(), available_state_interfaces_.end(), interface);
if (found_it == available_state_interfaces_.end())
{
available_state_interfaces_.emplace_back(interface);
RCLCPP_DEBUG(
get_logger(), "(hardware '%s'): '%s' state interface added into available list",
hardware.get_name().c_str(), interface.c_str());
}
else
{
// TODO(destogl): do here error management if interfaces are only partially added into
// "available" list - this should never be the case!
RCLCPP_WARN(
get_logger(),
"(hardware '%s'): '%s' state interface already in available list."
" This can happen due to multiple calls to 'configure'",
hardware.get_name().c_str(), interface.c_str());
}
}
// add command interfaces to available list
for (const auto & interface : hardware_info_map_[hardware.get_name()].command_interfaces)
{
// TODO(destogl): check if interface should be available on configure
auto found_it = std::find(
available_command_interfaces_.begin(), available_command_interfaces_.end(), interface);
if (found_it == available_command_interfaces_.end())
{
available_command_interfaces_.emplace_back(interface);
RCLCPP_DEBUG(
get_logger(), "(hardware '%s'): '%s' command interface added into available list",
hardware.get_name().c_str(), interface.c_str());
}
else
{
// TODO(destogl): do here error management if interfaces are only partially added into
// "available" list - this should never be the case!
RCLCPP_WARN(
get_logger(),
"(hardware '%s'): '%s' command interface already in available list."
" This can happen due to multiple calls to 'configure'",
hardware.get_name().c_str(), interface.c_str());
}
}
}
if (!hardware.get_group_name().empty())
{
hw_group_state_[hardware.get_group_name()] = return_type::OK;
}
return result;
}
void remove_all_hardware_interfaces_from_available_list(const std::string & hardware_name)
{
// remove all command interfaces from available list
for (const auto & interface : hardware_info_map_[hardware_name].command_interfaces)
{
auto found_it = std::find(
available_command_interfaces_.begin(), available_command_interfaces_.end(), interface);
if (found_it != available_command_interfaces_.end())
{
available_command_interfaces_.erase(found_it);
RCLCPP_DEBUG(
get_logger(), "(hardware '%s'): '%s' command interface removed from available list",
hardware_name.c_str(), interface.c_str());
}
else
{
// TODO(destogl): do here error management if interfaces are only partially added into
// "available" list - this should never be the case!
RCLCPP_WARN(
get_logger(),
"(hardware '%s'): '%s' command interface not in available list. "
"This should not happen (hint: multiple cleanup calls).",
hardware_name.c_str(), interface.c_str());
}
}
// remove all state interfaces from available list
for (const auto & interface : hardware_info_map_[hardware_name].state_interfaces)
{
auto found_it = std::find(
available_state_interfaces_.begin(), available_state_interfaces_.end(), interface);
if (found_it != available_state_interfaces_.end())
{
available_state_interfaces_.erase(found_it);
RCLCPP_DEBUG(
get_logger(), "(hardware '%s'): '%s' state interface removed from available list",
hardware_name.c_str(), interface.c_str());
}
else
{
// TODO(destogl): do here error management if interfaces are only partially added into
// "available" list - this should never be the case!
RCLCPP_WARN(
get_logger(),
"(hardware '%s'): '%s' state interface not in available list. "
"This should not happen (hint: multiple cleanup calls).",
hardware_name.c_str(), interface.c_str());
}
}
}
template <class HardwareT>
bool cleanup_hardware(HardwareT & hardware)
{
bool result = false;
try
{
result = trigger_and_print_hardware_state_transition(
std::bind(&HardwareT::cleanup, &hardware), "cleanup", hardware.get_name(),
lifecycle_msgs::msg::State::PRIMARY_STATE_UNCONFIGURED);
}
catch (const std::exception & ex)
{
RCLCPP_ERROR(
get_logger(), "Exception of type : %s occurred while cleaning up hardware '%s': %s",
typeid(ex).name(), hardware.get_name().c_str(), ex.what());
}
catch (...)
{
RCLCPP_ERROR(
get_logger(), "Unknown exception occurred while cleaning up hardware '%s'",
hardware.get_name().c_str());
}
if (result)
{
remove_all_hardware_interfaces_from_available_list(hardware.get_name());
}
if (!hardware.get_group_name().empty())
{
hw_group_state_[hardware.get_group_name()] = return_type::OK;
}
return result;
}
template <class HardwareT>
bool shutdown_hardware(HardwareT & hardware)
{
bool result = false;
try
{
result = trigger_and_print_hardware_state_transition(
std::bind(&HardwareT::shutdown, &hardware), "shutdown", hardware.get_name(),
lifecycle_msgs::msg::State::PRIMARY_STATE_FINALIZED);
}
catch (const std::exception & ex)
{
RCLCPP_ERROR(
get_logger(), "Exception of type : %s occurred while shutting down hardware '%s': %s",
typeid(ex).name(), hardware.get_name().c_str(), ex.what());
}
catch (...)
{
RCLCPP_ERROR(
get_logger(), "Unknown exception occurred while shutting down hardware '%s'",
hardware.get_name().c_str());
}
if (result)
{
remove_all_hardware_interfaces_from_available_list(hardware.get_name());
// TODO(destogl): change this - deimport all things if there is there are interfaces there
// deimport_non_movement_command_interfaces(hardware);
// deimport_state_interfaces(hardware);
// use remove_command_interfaces(hardware);
if (!hardware.get_group_name().empty())
{
hw_group_state_[hardware.get_group_name()] = return_type::OK;
}
}
return result;
}
template <class HardwareT>
bool activate_hardware(HardwareT & hardware)
{
bool result = false;
try
{
result = trigger_and_print_hardware_state_transition(
std::bind(&HardwareT::activate, &hardware), "activate", hardware.get_name(),
lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE);
}
catch (const std::exception & ex)
{
RCLCPP_ERROR(
get_logger(), "Exception of type : %s occurred while activating hardware '%s': %s",
typeid(ex).name(), hardware.get_name().c_str(), ex.what());
}
catch (...)
{
RCLCPP_ERROR(
get_logger(), "Unknown exception occurred while activating hardware '%s'",
hardware.get_name().c_str());
}
return result;
}
template <class HardwareT>
bool deactivate_hardware(HardwareT & hardware)
{
bool result = false;
try
{
result = trigger_and_print_hardware_state_transition(
std::bind(&HardwareT::deactivate, &hardware), "deactivate", hardware.get_name(),
lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE);
}
catch (const std::exception & ex)
{
RCLCPP_ERROR(
get_logger(), "Exception of type : %s occurred while deactivating hardware '%s': %s",
typeid(ex).name(), hardware.get_name().c_str(), ex.what());
}
catch (...)
{
RCLCPP_ERROR(
get_logger(), "Unknown exception occurred while deactivating hardware '%s'",
hardware.get_name().c_str());
}
if (result)
{
// TODO(destogl): make all command interfaces unavailable that should be present only
// when active (currently are all available) also at inactive
}
return result;
}
template <class HardwareT>
bool set_component_state(HardwareT & hardware, const rclcpp_lifecycle::State & target_state)
{
using lifecycle_msgs::msg::State;
bool result = false;
switch (target_state.id())
{
case State::PRIMARY_STATE_UNCONFIGURED:
switch (hardware.get_lifecycle_state().id())
{
case State::PRIMARY_STATE_UNCONFIGURED:
result = true;
break;
case State::PRIMARY_STATE_INACTIVE:
result = cleanup_hardware(hardware);
break;
case State::PRIMARY_STATE_ACTIVE:
result = deactivate_hardware(hardware);
if (result)
{
result = cleanup_hardware(hardware);
}
break;
case State::PRIMARY_STATE_FINALIZED:
result = false;
RCLCPP_WARN(
get_logger(), "hardware '%s' is in finalized state and can be only destroyed.",
hardware.get_name().c_str());
break;
}
break;
case State::PRIMARY_STATE_INACTIVE:
switch (hardware.get_lifecycle_state().id())
{
case State::PRIMARY_STATE_UNCONFIGURED:
result = configure_hardware(hardware);
break;
case State::PRIMARY_STATE_INACTIVE:
result = true;
break;
case State::PRIMARY_STATE_ACTIVE:
result = deactivate_hardware(hardware);
break;
case State::PRIMARY_STATE_FINALIZED:
result = false;
RCLCPP_WARN(
get_logger(), "hardware '%s' is in finalized state and can be only destroyed.",
hardware.get_name().c_str());
break;
}
break;
case State::PRIMARY_STATE_ACTIVE:
switch (hardware.get_lifecycle_state().id())
{
case State::PRIMARY_STATE_UNCONFIGURED:
result = configure_hardware(hardware);
if (result)
{
result = activate_hardware(hardware);
}
break;
case State::PRIMARY_STATE_INACTIVE:
result = activate_hardware(hardware);
break;
case State::PRIMARY_STATE_ACTIVE:
result = true;
break;
case State::PRIMARY_STATE_FINALIZED:
result = false;
RCLCPP_WARN(
get_logger(), "hardware '%s' is in finalized state and can be only destroyed.",
hardware.get_name().c_str());
break;
}
break;
case State::PRIMARY_STATE_FINALIZED:
switch (hardware.get_lifecycle_state().id())
{
case State::PRIMARY_STATE_UNCONFIGURED:
result = shutdown_hardware(hardware);
break;
case State::PRIMARY_STATE_INACTIVE:
result = shutdown_hardware(hardware);
break;
case State::PRIMARY_STATE_ACTIVE:
result = deactivate_hardware(hardware) && shutdown_hardware(hardware);
break;
case State::PRIMARY_STATE_FINALIZED:
result = true;
break;
}
break;
}
return result;
}
template <class HardwareT>
void import_state_interfaces(HardwareT & hardware)
{
auto interfaces = hardware.export_state_interfaces();
const auto interface_names = add_state_interfaces(interfaces);
RCLCPP_WARN_EXPRESSION(
get_logger(), interface_names.empty(),
"Importing state interfaces for the hardware '%s' returned no state interfaces.",
hardware.get_name().c_str());
hardware_info_map_[hardware.get_name()].state_interfaces = interface_names;
available_state_interfaces_.reserve(
available_state_interfaces_.capacity() + interface_names.size());
}
void insert_command_interface(const CommandInterface::SharedPtr command_interface)
{
const auto [it, success] = command_interface_map_.insert(
std::make_pair(command_interface->get_name(), command_interface));
if (!success)
{
std::string msg(
"ResourceStorage: Tried to insert CommandInterface with already existing key. Insert[" +
command_interface->get_name() + "]");
throw std::runtime_error(msg);
}
}
// BEGIN (Handle export change): for backward compatibility, can be removed if
// export_command_interfaces() method is removed
void insert_command_interface(CommandInterface && command_interface)
{
std::string key = command_interface.get_name();
const auto [it, success] = command_interface_map_.emplace(
std::make_pair(key, std::make_shared<CommandInterface>(std::move(command_interface))));
if (!success)
{
std::string msg(
"ResourceStorage: Tried to insert CommandInterface with already existing key. Insert[" +
key + "]");
throw std::runtime_error(msg);
}
}
// END: for backward compatibility
template <class HardwareT>
void import_command_interfaces(HardwareT & hardware)
{
try
{
auto interfaces = hardware.export_command_interfaces();
hardware_info_map_[hardware.get_name()].command_interfaces =
add_command_interfaces(interfaces);
// TODO(Manuel) END: for backward compatibility
}
catch (const std::exception & ex)
{
RCLCPP_ERROR(
get_logger(),
"Exception of type : %s occurred while importing command interfaces for the hardware '%s' "
": %s",
typeid(ex).name(), hardware.get_name().c_str(), ex.what());
}
catch (...)
{
RCLCPP_ERROR(
get_logger(),
"Unknown exception occurred while importing command interfaces for the hardware '%s'",
hardware.get_name().c_str());
}
}
std::string add_state_interface(StateInterface::ConstSharedPtr interface)
{
auto interface_name = interface->get_name();
const auto [it, success] = state_interface_map_.emplace(interface_name, interface);
if (!success)
{
std::string msg(
"ResourceStorage: Tried to insert StateInterface with already existing key. Insert[" +
interface->get_name() + "]");
throw std::runtime_error(msg);
}
return interface_name;
}
/// Adds exported state interfaces into internal storage.
/**
* Adds state interfaces to the internal storage. State interfaces exported from hardware or
* chainable controllers are moved to the map with name-interface pairs and available list's
* size is increased to reserve storage when interface change theirs status in real-time
* control loop.
*
* \param[interfaces] list of state interface to add into storage.
* \returns list of interface names that are added into internal storage. The output is used to
* avoid additional iterations to cache interface names, e.g., for initializing info structures.
*/
std::vector<std::string> add_state_interfaces(
std::vector<StateInterface::ConstSharedPtr> & interfaces)
{
std::vector<std::string> interface_names;
interface_names.reserve(interfaces.size());
for (auto & interface : interfaces)
{
try
{
interface_names.push_back(add_state_interface(interface));
}
// We don't want to crash during runtime because a StateInterface could not be added
catch (const std::exception & e)
{
RCLCPP_WARN(
get_logger(), "Exception occurred while importing state interfaces: %s", e.what());
}
}
available_state_interfaces_.reserve(
available_state_interfaces_.capacity() + interface_names.size());
return interface_names;
}
/// Removes state interfaces from internal storage.
/**
* State interface are removed from the maps with theirs storage and their claimed status.
*
* \param[interface_names] list of state interface names to remove from storage.
*/
void remove_state_interfaces(const std::vector<std::string> & interface_names)
{
for (const auto & interface : interface_names)
{
state_interface_map_.erase(interface);
}
}
/// Adds exported command interfaces into internal storage.
/**
* Add command interfaces to the internal storage. Command interfaces exported from hardware or
* chainable controllers are moved to the map with name-interface pairs, the interface names are
* added to the claimed map and available list's size is increased to reserve storage when
* interface change theirs status in real-time control loop.
*
* \param[interfaces] list of command interface to add into storage.
* \returns list of interface names that are added into internal storage. The output is used to
* avoid additional iterations to cache interface names, e.g., for initializing info structures.
*/
std::vector<std::string> add_command_interfaces(std::vector<CommandInterface> & interfaces)
{
std::vector<std::string> interface_names;
interface_names.reserve(interfaces.size());
for (auto & interface : interfaces)
{
auto key = interface.get_name();
insert_command_interface(std::move(interface));
claimed_command_interface_map_.emplace(std::make_pair(key, false));
interface_names.push_back(key);
}
available_command_interfaces_.reserve(
available_command_interfaces_.capacity() + interface_names.size());
return interface_names;
}
std::vector<std::string> add_command_interfaces(
const std::vector<CommandInterface::SharedPtr> & interfaces)
{
std::vector<std::string> interface_names;
interface_names.reserve(interfaces.size());
for (const auto & interface : interfaces)
{
auto key = interface->get_name();
insert_command_interface(interface);
claimed_command_interface_map_.emplace(std::make_pair(key, false));
interface_names.push_back(key);
}
available_command_interfaces_.reserve(
available_command_interfaces_.capacity() + interface_names.size());
return interface_names;
}
/// Removes command interfaces from internal storage.
/**
* Command interface are removed from the maps with theirs storage and their claimed status.
*
* \param[interface_names] list of command interface names to remove from storage.
*/
void remove_command_interfaces(const std::vector<std::string> & interface_names)
{
for (const auto & interface : interface_names)
{
command_interface_map_.erase(interface);
claimed_command_interface_map_.erase(interface);
}
}
// TODO(destogl): Propagate "false" up, if happens in initialize_hardware
bool load_and_initialize_actuator(const HardwareInfo & hardware_info)
{
auto load_and_init_actuators = [&](auto & container)
{
if (!load_hardware<Actuator, ActuatorInterface>(hardware_info, actuator_loader_, container))
{
return false;
}
if (initialize_hardware(hardware_info, container.back()))
{
import_state_interfaces(container.back());
import_command_interfaces(container.back());
}
else
{
RCLCPP_WARN(
get_logger(), "Actuator hardware component '%s' from plugin '%s' failed to initialize.",
hardware_info.name.c_str(), hardware_info.hardware_plugin_name.c_str());
return false;
}
return true;
};
return load_and_init_actuators(actuators_);
}
bool load_and_initialize_sensor(const HardwareInfo & hardware_info)
{
auto load_and_init_sensors = [&](auto & container)
{
if (!load_hardware<Sensor, SensorInterface>(hardware_info, sensor_loader_, container))
{
return false;
}
if (initialize_hardware(hardware_info, container.back()))
{
import_state_interfaces(container.back());
}
else
{
RCLCPP_WARN(
get_logger(), "Sensor hardware component '%s' from plugin '%s' failed to initialize.",
hardware_info.name.c_str(), hardware_info.hardware_plugin_name.c_str());
return false;
}
return true;
};
return load_and_init_sensors(sensors_);
}
bool load_and_initialize_system(const HardwareInfo & hardware_info)
{
auto load_and_init_systems = [&](auto & container)
{
if (!load_hardware<System, SystemInterface>(hardware_info, system_loader_, container))
{
return false;
}
if (initialize_hardware(hardware_info, container.back()))
{
import_state_interfaces(container.back());
import_command_interfaces(container.back());
}
else
{
RCLCPP_WARN(
get_logger(), "System hardware component '%s' from plugin '%s' failed to initialize.",
hardware_info.name.c_str(), hardware_info.hardware_plugin_name.c_str());
return false;
}
return true;
};
return load_and_init_systems(systems_);
}
void initialize_actuator(
std::unique_ptr<ActuatorInterface> actuator, const HardwareInfo & hardware_info)
{
auto init_actuators = [&](auto & container)
{
container.emplace_back(Actuator(std::move(actuator)));
if (initialize_hardware(hardware_info, container.back()))
{
import_state_interfaces(container.back());
import_command_interfaces(container.back());
}
else
{
RCLCPP_WARN(
get_logger(), "Actuator hardware component '%s' from plugin '%s' failed to initialize.",
hardware_info.name.c_str(), hardware_info.hardware_plugin_name.c_str());
}
};
init_actuators(actuators_);
}
void initialize_sensor(
std::unique_ptr<SensorInterface> sensor, const HardwareInfo & hardware_info)
{
auto init_sensors = [&](auto & container)
{
container.emplace_back(Sensor(std::move(sensor)));
if (initialize_hardware(hardware_info, container.back()))
{
import_state_interfaces(container.back());
}
else
{
RCLCPP_WARN(
get_logger(), "Sensor hardware component '%s' from plugin '%s' failed to initialize.",
hardware_info.name.c_str(), hardware_info.hardware_plugin_name.c_str());
}
};
init_sensors(sensors_);
}
void initialize_system(
std::unique_ptr<SystemInterface> system, const HardwareInfo & hardware_info)
{
auto init_systems = [&](auto & container)
{
container.emplace_back(System(std::move(system)));
if (initialize_hardware(hardware_info, container.back()))
{
import_state_interfaces(container.back());
import_command_interfaces(container.back());
}
else
{
RCLCPP_WARN(
get_logger(), "System hardware component '%s' from plugin '%s' failed to initialize.",
hardware_info.name.c_str(), hardware_info.hardware_plugin_name.c_str());
}
};
init_systems(systems_);
}
void clear()
{
actuators_.clear();
sensors_.clear();
systems_.clear();
hardware_info_map_.clear();
state_interface_map_.clear();
command_interface_map_.clear();
available_state_interfaces_.clear();
available_command_interfaces_.clear();
claimed_command_interface_map_.clear();
}
/**
* Returns the return type of the hardware component group state, if the return type is other
* than OK, then updates the return type of the group to the respective one
*/
return_type update_hardware_component_group_state(
const std::string & group_name, const return_type & value)
{
// This is for the components that has no configured group
if (group_name.empty())
{
return value;
}
// If it is anything other than OK, change the return type of the hardware group state
// to the respective return type
if (value != return_type::OK)
{
hw_group_state_.at(group_name) = value;
}
return hw_group_state_.at(group_name);
}
/// Gets the logger for the resource storage
/**
* \return logger of the resource storage
*/
const rclcpp::Logger & get_logger() const { return rm_logger_; }
/// Gets the clock for the resource storage
/**
* \return clock of the resource storage
*/
rclcpp::Clock::SharedPtr get_clock() const { return clock_interface_->get_clock(); }
// hardware plugins
pluginlib::ClassLoader<ActuatorInterface> actuator_loader_;
pluginlib::ClassLoader<SensorInterface> sensor_loader_;
pluginlib::ClassLoader<SystemInterface> system_loader_;
// Logger and Clock interfaces
rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface_;
rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr logger_interface_;
rclcpp::Logger rm_logger_;
std::vector<Actuator> actuators_;
std::vector<Sensor> sensors_;
std::vector<System> systems_;
std::unordered_map<std::string, HardwareComponentInfo> hardware_info_map_;
std::unordered_map<std::string, hardware_interface::return_type> hw_group_state_;
/// Mapping between hardware and controllers that are using it (accessing data from it)
std::unordered_map<std::string, std::vector<std::string>> hardware_used_by_controllers_;
/// Mapping between controllers and list of interfaces they are using
std::unordered_map<std::string, std::vector<std::string>>
controllers_exported_state_interfaces_map_;
std::unordered_map<std::string, std::vector<std::string>> controllers_reference_interfaces_map_;
/// Storage of all available state interfaces
std::map<std::string, StateInterface::ConstSharedPtr> state_interface_map_;
/// Storage of all available command interfaces