|
19 | 19 | //---------------------------------------------------------------------- |
20 | 20 | /*!\file |
21 | 21 | * |
22 | | - * \author Felix Exner exner@fzi.de |
23 | | - * \date 2019-04-17 |
| 22 | + * \author Marvin Große Besselmann grosse@fzi.de |
| 23 | + * \date 2021-02-10 |
24 | 24 | * |
25 | 25 | */ |
26 | 26 | //---------------------------------------------------------------------- |
27 | 27 |
|
28 | 28 | #include "ur_controllers/speed_scaling_state_controller.h" |
| 29 | + |
| 30 | +#include "hardware_interface/types/hardware_interface_return_values.hpp" |
| 31 | +#include "hardware_interface/types/hardware_interface_type_values.hpp" |
| 32 | +#include "rclcpp/clock.hpp" |
| 33 | +#include "rclcpp/qos.hpp" |
| 34 | +#include "rclcpp/qos_event.hpp" |
| 35 | +#include "rclcpp/time.hpp" |
| 36 | +#include "rclcpp_lifecycle/lifecycle_node.hpp" |
| 37 | +#include "rcpputils/split.hpp" |
| 38 | +#include "rcutils/logging_macros.h" |
| 39 | + |
| 40 | +namespace rclcpp_lifecycle |
| 41 | +{ |
| 42 | +class State; |
| 43 | +} // namespace rclcpp_lifecycle |
| 44 | + |
| 45 | +namespace ur_controllers |
| 46 | +{ |
| 47 | +SpeedScalingStateController::SpeedScalingStateController() |
| 48 | +{ |
| 49 | +} |
| 50 | + |
| 51 | +controller_interface::InterfaceConfiguration SpeedScalingStateController::command_interface_configuration() const |
| 52 | +{ |
| 53 | + return controller_interface::InterfaceConfiguration{ controller_interface::interface_configuration_type::NONE }; |
| 54 | +} |
| 55 | + |
| 56 | +controller_interface::InterfaceConfiguration SpeedScalingStateController::state_interface_configuration() const |
| 57 | +{ |
| 58 | + controller_interface::InterfaceConfiguration config; |
| 59 | + config.type = controller_interface::interface_configuration_type::INDIVIDUAL; |
| 60 | + config.names.push_back("speed_scaling/speed_scaling_factor"); |
| 61 | + return config; |
| 62 | +} |
| 63 | + |
| 64 | +rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn |
| 65 | +SpeedScalingStateController::on_configure(const rclcpp_lifecycle::State& /*previous_state*/) |
| 66 | +{ |
| 67 | + node_->declare_parameter("state_publish_rate"); |
| 68 | + |
| 69 | + if (!node_->get_parameter("state_publish_rate", publish_rate_)) |
| 70 | + { |
| 71 | + RCLCPP_INFO(get_node()->get_logger(), "Parameter 'state_publish_rate' not set"); |
| 72 | + return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::ERROR; |
| 73 | + } |
| 74 | + else |
| 75 | + { |
| 76 | + RCLCPP_INFO(get_node()->get_logger(), "Publisher rate set to : %.1f Hz", publish_rate_); |
| 77 | + } |
| 78 | + |
| 79 | + try |
| 80 | + { |
| 81 | + speed_scaling_state_publisher_ = |
| 82 | + get_node()->create_publisher<std_msgs::msg::Float64>("speed_scaling_factor", rclcpp::SystemDefaultsQoS()); |
| 83 | + } |
| 84 | + catch (const std::exception& e) |
| 85 | + { |
| 86 | + // get_node() may throw, logging raw here |
| 87 | + fprintf(stderr, "Exception thrown during init stage with message: %s \n", e.what()); |
| 88 | + return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::ERROR; |
| 89 | + } |
| 90 | + return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS; |
| 91 | +} |
| 92 | + |
| 93 | +rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn |
| 94 | +SpeedScalingStateController::on_activate(const rclcpp_lifecycle::State& /*previous_state*/) |
| 95 | +{ |
| 96 | + last_publish_time_ = node_->now(); |
| 97 | + return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS; |
| 98 | +} |
| 99 | + |
| 100 | +rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn |
| 101 | +SpeedScalingStateController::on_deactivate(const rclcpp_lifecycle::State& /*previous_state*/) |
| 102 | +{ |
| 103 | + return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS; |
| 104 | +} |
| 105 | + |
| 106 | +controller_interface::return_type SpeedScalingStateController::update() |
| 107 | +{ |
| 108 | + if (publish_rate_ > 0.0 && (node_->now() - last_publish_time_) > rclcpp::Duration(1.0 / publish_rate_, 0.0)) |
| 109 | + { |
| 110 | + // Speed scaling is the only interface of the controller |
| 111 | + speed_scaling_state_msg_.data = state_interfaces_[0].get_value(); |
| 112 | + |
| 113 | + // publish |
| 114 | + speed_scaling_state_publisher_->publish(speed_scaling_state_msg_); |
| 115 | + last_publish_time_ = node_->now(); |
| 116 | + } |
| 117 | + return controller_interface::return_type::SUCCESS; |
| 118 | +} |
| 119 | + |
| 120 | +} // namespace ur_controllers |
| 121 | + |
| 122 | +#include "pluginlib/class_list_macros.hpp" |
| 123 | + |
| 124 | +PLUGINLIB_EXPORT_CLASS(ur_controllers::SpeedScalingStateController, controller_interface::ControllerInterface) |
0 commit comments