|
| 1 | +// |
| 2 | +// Created by livanov93 on 12/12/20. |
| 3 | +// |
| 4 | + |
| 5 | +#include "ur_controllers/force_torque_sensor_controller.h" |
| 6 | + |
| 7 | +namespace ur_controllers |
| 8 | +{ |
| 9 | + ForceTorqueStateController::ForceTorqueStateController() : controller_interface::ControllerInterface() |
| 10 | + { |
| 11 | + } |
| 12 | + |
| 13 | + controller_interface::InterfaceConfiguration ForceTorqueStateController::command_interface_configuration() const |
| 14 | + { |
| 15 | + controller_interface::InterfaceConfiguration config; |
| 16 | + config.type = controller_interface::interface_configuration_type::NONE; |
| 17 | + return config; |
| 18 | + } |
| 19 | + |
| 20 | + controller_interface::InterfaceConfiguration ForceTorqueStateController::state_interface_configuration() const |
| 21 | + { |
| 22 | + controller_interface::InterfaceConfiguration config; |
| 23 | + config.type = controller_interface::interface_configuration_type::ALL; |
| 24 | + return config; |
| 25 | + } |
| 26 | + |
| 27 | + controller_interface::return_type ur_controllers::ForceTorqueStateController::update() |
| 28 | + { |
| 29 | + geometry_msgs::msg::Vector3 fVec; |
| 30 | + geometry_msgs::msg::Vector3 tVec; |
| 31 | + |
| 32 | + // TODO remove these hardcoded names and create better string filtering within state_interfaces_ |
| 33 | + for (const auto& state_interface : state_interfaces_) |
| 34 | + { |
| 35 | + if (state_interface.get_name() != "tcp_fts_sensor") |
| 36 | + continue; |
| 37 | + |
| 38 | + if (state_interface.get_interface_name() == "fx") |
| 39 | + { |
| 40 | + fVec.set__x(state_interface.get_value()); |
| 41 | + continue; |
| 42 | + } |
| 43 | + |
| 44 | + if (state_interface.get_interface_name() == "fy") |
| 45 | + { |
| 46 | + fVec.set__y(state_interface.get_value()); |
| 47 | + continue; |
| 48 | + } |
| 49 | + |
| 50 | + if (state_interface.get_interface_name() == "fz") |
| 51 | + { |
| 52 | + fVec.set__z(state_interface.get_value()); |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + if (state_interface.get_interface_name() == "tx") |
| 57 | + { |
| 58 | + tVec.set__x(state_interface.get_value()); |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + if (state_interface.get_interface_name() == "ty") |
| 63 | + { |
| 64 | + tVec.set__y(state_interface.get_value()); |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + if (state_interface.get_interface_name() == "tz") |
| 69 | + { |
| 70 | + tVec.set__z(state_interface.get_value()); |
| 71 | + continue; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if (!wrench_state_publisher_->is_activated()) |
| 76 | + { |
| 77 | + RCUTILS_LOG_WARN_ONCE_NAMED("publisher", "wrench state publisher is not activated"); |
| 78 | + return controller_interface::return_type::ERROR; |
| 79 | + } |
| 80 | + |
| 81 | + // TODO set frame_id as parameter --> it includes tf listener within controller |
| 82 | + wrench_state_msg_.header.stamp = lifecycle_node_->get_clock()->now(); |
| 83 | + wrench_state_msg_.header.frame_id = "tool0"; |
| 84 | + |
| 85 | + // update wrench state message |
| 86 | + wrench_state_msg_.wrench.set__force(fVec); |
| 87 | + wrench_state_msg_.wrench.set__torque(tVec); |
| 88 | + |
| 89 | + // publish |
| 90 | + wrench_state_publisher_->publish(wrench_state_msg_); |
| 91 | + |
| 92 | + return controller_interface::return_type::SUCCESS; |
| 93 | + } |
| 94 | + |
| 95 | + rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn |
| 96 | + ForceTorqueStateController::on_configure(const rclcpp_lifecycle::State& /*previous_state*/) |
| 97 | + { |
| 98 | + try |
| 99 | + { |
| 100 | + // TODO make topic name a parameter |
| 101 | + wrench_state_publisher_ = |
| 102 | + lifecycle_node_->create_publisher<geometry_msgs::msg::WrenchStamped>("ft_data", rclcpp::SystemDefaultsQoS()); |
| 103 | + } |
| 104 | + catch (...) |
| 105 | + { |
| 106 | + return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::ERROR; |
| 107 | + } |
| 108 | + return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS; |
| 109 | + } |
| 110 | + |
| 111 | + rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn |
| 112 | + ForceTorqueStateController::on_activate(const rclcpp_lifecycle::State& /*previous_state*/) |
| 113 | + { |
| 114 | + if (!init_sensor_data()) |
| 115 | + { |
| 116 | + return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::ERROR; |
| 117 | + } |
| 118 | + |
| 119 | + init_sensor_state_msg(); |
| 120 | + |
| 121 | + wrench_state_publisher_->on_activate(); |
| 122 | + |
| 123 | + return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS; |
| 124 | + } |
| 125 | + |
| 126 | + rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn |
| 127 | + ForceTorqueStateController::on_deactivate(const rclcpp_lifecycle::State& /*previous_state*/) |
| 128 | + { |
| 129 | + wrench_state_publisher_->on_deactivate(); |
| 130 | + |
| 131 | + return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS; |
| 132 | + } |
| 133 | + |
| 134 | + template <typename T> |
| 135 | + bool has_any_key(const std::unordered_map<std::string, T>& map, const std::vector<std::string>& keys) |
| 136 | + { |
| 137 | + bool found_key = false; |
| 138 | + for (const auto& key_item : map) |
| 139 | + { |
| 140 | + const auto& key = key_item.first; |
| 141 | + if (std::find(keys.cbegin(), keys.cend(), key) != keys.cend()) |
| 142 | + { |
| 143 | + found_key = true; |
| 144 | + break; |
| 145 | + } |
| 146 | + } |
| 147 | + return found_key; |
| 148 | + } |
| 149 | + |
| 150 | + bool ForceTorqueStateController::init_sensor_data() |
| 151 | + { |
| 152 | + bool has_fts_sensor = false; |
| 153 | + // loop in reverse order, this maintains the order of values at retrieval time |
| 154 | + for (auto si = state_interfaces_.crbegin(); si != state_interfaces_.crend(); si++) |
| 155 | + { |
| 156 | + // TODO remove hardcoded naming |
| 157 | + if (si->get_name() == "tcp_fts_sensor") |
| 158 | + { |
| 159 | + has_fts_sensor = true; |
| 160 | + axis_val_names_.push_back(si->get_interface_name()); |
| 161 | + } |
| 162 | + } |
| 163 | + return has_fts_sensor; |
| 164 | + } |
| 165 | + |
| 166 | + void ForceTorqueStateController::init_sensor_state_msg() |
| 167 | + { |
| 168 | + // default initialization for wrenchstamped state message |
| 169 | + } |
| 170 | + |
| 171 | +} // namespace ur_controllers |
| 172 | + |
| 173 | +#include "pluginlib/class_list_macros.hpp" |
| 174 | + |
| 175 | +PLUGINLIB_EXPORT_CLASS(ur_controllers::ForceTorqueStateController, controller_interface::ControllerInterface) |
0 commit comments