Skip to content

Commit d29784e

Browse files
committed
Code cleanup
Only handle speed scaling interface Removed all therefore useless helper functions
1 parent 00727bf commit d29784e

File tree

2 files changed

+2
-96
lines changed

2 files changed

+2
-96
lines changed

ur_controllers/include/ur_controllers/speed_scaling_state_controller.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@
2828
#ifndef UR_CONTROLLERS_SPEED_SCALING_STATE_CONTROLLER_H_INCLUDED
2929
#define UR_CONTROLLERS_SPEED_SCALING_STATE_CONTROLLER_H_INCLUDED
3030

31-
#include <memory>
32-
#include <string>
33-
#include <unordered_map>
34-
#include <vector>
35-
3631
#include "controller_interface/controller_interface.hpp"
3732

3833
#include "rclcpp_lifecycle/lifecycle_publisher.hpp"
@@ -61,18 +56,13 @@ class SpeedScalingStateController : public controller_interface::ControllerInter
6156
rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
6257
on_deactivate(const rclcpp_lifecycle::State& previous_state) override;
6358

64-
protected:
65-
bool init_sensor_data();
66-
6759
protected:
6860
std::vector<std::string> sensor_names_;
6961
rclcpp::Time last_publish_time_;
7062
double publish_rate_;
7163

7264
std::shared_ptr<rclcpp::Publisher<std_msgs::msg::Float64>> speed_scaling_state_publisher_;
7365
std_msgs::msg::Float64 speed_scaling_state_msg_;
74-
75-
std::unordered_map<std::string, std::unordered_map<std::string, double>> name_if_value_mapping_;
7666
};
7767
} // namespace ur_controllers
7868
#endif // ifndef UR_CONTROLLERS_SPEED_SCALING_STATE_CONTROLLER_H_INCLUDED

ur_controllers/src/speed_scaling_state_controller.cpp

Lines changed: 2 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@
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"
@@ -43,7 +36,6 @@
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

4840
namespace rclcpp_lifecycle
4941
{
@@ -52,8 +44,6 @@ class State;
5244

5345
namespace ur_controllers
5446
{
55-
const auto kUninitializedValue = std::numeric_limits<double>::quiet_NaN();
56-
5747
SpeedScalingStateController::SpeedScalingStateController()
5848
{
5949
}
@@ -103,11 +93,6 @@ SpeedScalingStateController::on_configure(const rclcpp_lifecycle::State& /*previ
10393
rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
10494
SpeedScalingStateController::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-
180106
controller_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

Comments
 (0)