2727
2828#include " ur_controllers/speed_scaling_state_controller.h"
2929
30- #include < stddef.h>
31- #include < limits>
32- #include < memory>
33- #include < string>
34- #include < unordered_map>
35- #include < vector>
36-
3730#include " hardware_interface/types/hardware_interface_return_values.hpp"
3831#include " hardware_interface/types/hardware_interface_type_values.hpp"
3932#include " rclcpp/clock.hpp"
4336#include " rclcpp_lifecycle/lifecycle_node.hpp"
4437#include " rcpputils/split.hpp"
4538#include " rcutils/logging_macros.h"
46- #include " std_msgs/msg/header.hpp"
4739
4840namespace rclcpp_lifecycle
4941{
@@ -52,8 +44,6 @@ class State;
5244
5345namespace ur_controllers
5446{
55- const auto kUninitializedValue = std::numeric_limits<double >::quiet_NaN();
56-
5747SpeedScalingStateController::SpeedScalingStateController ()
5848{
5949}
@@ -103,11 +93,6 @@ SpeedScalingStateController::on_configure(const rclcpp_lifecycle::State& /*previ
10393rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
10494SpeedScalingStateController::on_activate (const rclcpp_lifecycle::State& /* previous_state*/ )
10595{
106- if (!init_sensor_data ())
107- {
108- return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::ERROR;
109- }
110-
11196 last_publish_time_ = node_->now ();
11297 return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS;
11398}
@@ -118,81 +103,12 @@ SpeedScalingStateController::on_deactivate(const rclcpp_lifecycle::State& /*prev
118103 return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS;
119104}
120105
121- template <typename T>
122- bool has_any_key (const std::unordered_map<std::string, T>& map, const std::vector<std::string>& keys)
123- {
124- bool found_key = false ;
125- for (const auto & key_item : map)
126- {
127- const auto & key = key_item.first ;
128- if (std::find (keys.cbegin (), keys.cend (), key) != keys.cend ())
129- {
130- found_key = true ;
131- break ;
132- }
133- }
134- return found_key;
135- }
136-
137- bool SpeedScalingStateController::init_sensor_data ()
138- {
139- // loop in reverse order, this maintains the order of values at retrieval time
140- for (auto si = state_interfaces_.crbegin (); si != state_interfaces_.crend (); si++)
141- {
142- // initialize map if name is new
143- if (name_if_value_mapping_.count (si->get_name ()) == 0 )
144- {
145- name_if_value_mapping_[si->get_name ()] = {};
146- }
147- // add interface name
148- name_if_value_mapping_[si->get_name ()][si->get_interface_name ()] = kUninitializedValue ;
149- }
150-
151- // filter state interfaces that have a speed scaling factor
152- // the rest will be ignored for this message
153- for (const auto & name_ifv : name_if_value_mapping_)
154- {
155- const auto & interfaces_and_values = name_ifv.second ;
156- if (has_any_key (interfaces_and_values, { " speed_scaling_factor" }))
157- {
158- sensor_names_.push_back (name_ifv.first );
159- }
160- }
161-
162- return true ;
163- }
164-
165- double get_value (const std::unordered_map<std::string, std::unordered_map<std::string, double >>& map,
166- const std::string& name, const std::string& interface_name)
167- {
168- const auto & interfaces_and_values = map.at (name);
169- const auto interface_and_value = interfaces_and_values.find (interface_name);
170- if (interface_and_value != interfaces_and_values.cend ())
171- {
172- return interface_and_value->second ;
173- }
174- else
175- {
176- return kUninitializedValue ;
177- }
178- }
179-
180106controller_interface::return_type SpeedScalingStateController::update ()
181107{
182108 if (publish_rate_ > 0.0 && (node_->now () - last_publish_time_) > rclcpp::Duration (1.0 / publish_rate_, 0.0 ))
183109 {
184- for (const auto & state_interface : state_interfaces_)
185- {
186- name_if_value_mapping_[state_interface.get_name ()][state_interface.get_interface_name ()] =
187- state_interface.get_value ();
188- RCLCPP_DEBUG (get_node ()->get_logger (), " %s/%s: %f\n " , state_interface.get_name ().c_str (),
189- state_interface.get_interface_name ().c_str (), state_interface.get_value ());
190- }
191-
192- for (auto i = 0ul ; i < sensor_names_.size (); ++i)
193- {
194- speed_scaling_state_msg_.data = get_value (name_if_value_mapping_, sensor_names_[i], " speed_scaling_factor" );
195- }
110+ // Speed scaling is the only interface of the controller
111+ speed_scaling_state_msg_.data = state_interfaces_[0 ].get_value ();
196112
197113 // publish
198114 speed_scaling_state_publisher_->publish (speed_scaling_state_msg_);
0 commit comments