|
| 1 | +// Copyright (c) 2021 PickNik LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "ur_controllers/force_torque_sensor_controller.h" |
| 16 | + |
| 17 | +namespace ur_controllers |
| 18 | +{ |
| 19 | +ForceTorqueStateController::ForceTorqueStateController() |
| 20 | + : controller_interface::ControllerInterface(), wrench_state_publisher_(nullptr) |
| 21 | +{ |
| 22 | +} |
| 23 | + |
| 24 | +controller_interface::return_type ForceTorqueStateController::init(const std::string& controller_name) |
| 25 | +{ |
| 26 | + auto ret = ControllerInterface::init(controller_name); |
| 27 | + if (ret != controller_interface::return_type::SUCCESS) |
| 28 | + { |
| 29 | + return ret; |
| 30 | + } |
| 31 | + |
| 32 | + try |
| 33 | + { |
| 34 | + auto node = get_node(); |
| 35 | + node->declare_parameter<std::vector<std::string>>("state_interface_names", {}); |
| 36 | + node->declare_parameter<std::string>("sensor_name", ""); |
| 37 | + node->declare_parameter<std::string>("topic_name", ""); |
| 38 | + node->declare_parameter<std::string>("frame_id", ""); |
| 39 | + } |
| 40 | + catch (const std::exception& e) |
| 41 | + { |
| 42 | + fprintf(stderr, "Exception thrown during init stage with message: %s \n", e.what()); |
| 43 | + return controller_interface::return_type::ERROR; |
| 44 | + } |
| 45 | + |
| 46 | + return controller_interface::return_type::SUCCESS; |
| 47 | +} |
| 48 | + |
| 49 | +controller_interface::InterfaceConfiguration ForceTorqueStateController::command_interface_configuration() const |
| 50 | +{ |
| 51 | + controller_interface::InterfaceConfiguration config; |
| 52 | + config.type = controller_interface::interface_configuration_type::NONE; |
| 53 | + return config; |
| 54 | +} |
| 55 | + |
| 56 | +controller_interface::InterfaceConfiguration ForceTorqueStateController::state_interface_configuration() const |
| 57 | +{ |
| 58 | + controller_interface::InterfaceConfiguration config; |
| 59 | + config.type = controller_interface::interface_configuration_type::INDIVIDUAL; |
| 60 | + for (const auto& si : fts_params_.state_interfaces_names_) |
| 61 | + { |
| 62 | + config.names.push_back(fts_params_.sensor_name_ + "/" + si); |
| 63 | + } |
| 64 | + |
| 65 | + return config; |
| 66 | +} |
| 67 | + |
| 68 | +controller_interface::return_type ur_controllers::ForceTorqueStateController::update() |
| 69 | +{ |
| 70 | + geometry_msgs::msg::Vector3 f_vec; |
| 71 | + geometry_msgs::msg::Vector3 t_vec; |
| 72 | + |
| 73 | + if (state_interfaces_.size() != fts_params_.state_interfaces_names_.size()) |
| 74 | + return controller_interface::return_type::ERROR; |
| 75 | + |
| 76 | + for (auto index = 0ul; index < state_interfaces_.size(); ++index) |
| 77 | + { |
| 78 | + switch (index) |
| 79 | + { |
| 80 | + case 0: |
| 81 | + f_vec.set__x(state_interfaces_[index].get_value()); |
| 82 | + break; |
| 83 | + case 1: |
| 84 | + f_vec.set__y(state_interfaces_[index].get_value()); |
| 85 | + break; |
| 86 | + case 2: |
| 87 | + f_vec.set__z(state_interfaces_[index].get_value()); |
| 88 | + break; |
| 89 | + case 3: |
| 90 | + t_vec.set__x(state_interfaces_[index].get_value()); |
| 91 | + break; |
| 92 | + case 4: |
| 93 | + t_vec.set__y(state_interfaces_[index].get_value()); |
| 94 | + break; |
| 95 | + case 5: |
| 96 | + t_vec.set__z(state_interfaces_[index].get_value()); |
| 97 | + break; |
| 98 | + default: |
| 99 | + break; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + wrench_state_msg_.header.stamp = get_node()->get_clock()->now(); |
| 104 | + wrench_state_msg_.header.frame_id = fts_params_.frame_id; |
| 105 | + |
| 106 | + // update wrench state message |
| 107 | + wrench_state_msg_.wrench.set__force(f_vec); |
| 108 | + wrench_state_msg_.wrench.set__torque(t_vec); |
| 109 | + |
| 110 | + // publish |
| 111 | + wrench_state_publisher_->publish(wrench_state_msg_); |
| 112 | + |
| 113 | + return controller_interface::return_type::SUCCESS; |
| 114 | +} |
| 115 | + |
| 116 | +rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn |
| 117 | +ForceTorqueStateController::on_configure(const rclcpp_lifecycle::State& /*previous_state*/) |
| 118 | +{ |
| 119 | + fts_params_.state_interfaces_names_ = node_->get_parameter("state_interface_names").as_string_array(); |
| 120 | + |
| 121 | + if (fts_params_.state_interfaces_names_.empty()) |
| 122 | + { |
| 123 | + RCLCPP_ERROR(get_node()->get_logger(), "'state_interface_names' parameter was empty"); |
| 124 | + return CallbackReturn::ERROR; |
| 125 | + } |
| 126 | + |
| 127 | + fts_params_.sensor_name_ = node_->get_parameter("sensor_name").as_string(); |
| 128 | + if (fts_params_.sensor_name_.empty()) |
| 129 | + { |
| 130 | + RCLCPP_ERROR(get_node()->get_logger(), "'sensor_name' parameter was empty"); |
| 131 | + return CallbackReturn::ERROR; |
| 132 | + } |
| 133 | + |
| 134 | + fts_params_.topic_name = node_->get_parameter("topic_name").as_string(); |
| 135 | + if (fts_params_.topic_name.empty()) |
| 136 | + { |
| 137 | + RCLCPP_ERROR(get_node()->get_logger(), "'topic_name' parameter was empty"); |
| 138 | + return CallbackReturn::ERROR; |
| 139 | + } |
| 140 | + |
| 141 | + fts_params_.frame_id = node_->get_parameter("frame_id").as_string(); |
| 142 | + if (fts_params_.frame_id.empty()) |
| 143 | + { |
| 144 | + RCLCPP_ERROR(get_node()->get_logger(), "'frame_id' parameter was empty"); |
| 145 | + return CallbackReturn::ERROR; |
| 146 | + } |
| 147 | + |
| 148 | + try |
| 149 | + { |
| 150 | + // register ft sensor data publisher |
| 151 | + wrench_state_publisher_ = get_node()->create_publisher<geometry_msgs::msg::WrenchStamped>( |
| 152 | + fts_params_.topic_name, rclcpp::SystemDefaultsQoS()); |
| 153 | + } |
| 154 | + catch (...) |
| 155 | + { |
| 156 | + return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::ERROR; |
| 157 | + } |
| 158 | + |
| 159 | + return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS; |
| 160 | +} |
| 161 | + |
| 162 | +rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn |
| 163 | +ForceTorqueStateController::on_activate(const rclcpp_lifecycle::State& /*previous_state*/) |
| 164 | +{ |
| 165 | + return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS; |
| 166 | +} |
| 167 | + |
| 168 | +rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn |
| 169 | +ForceTorqueStateController::on_deactivate(const rclcpp_lifecycle::State& /*previous_state*/) |
| 170 | +{ |
| 171 | + return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS; |
| 172 | +} |
| 173 | + |
| 174 | +} // namespace ur_controllers |
| 175 | + |
| 176 | +#include "pluginlib/class_list_macros.hpp" |
| 177 | + |
| 178 | +PLUGINLIB_EXPORT_CLASS(ur_controllers::ForceTorqueStateController, controller_interface::ControllerInterface) |
0 commit comments