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