Skip to content

Commit b7eaf35

Browse files
committed
Tool contact controller is functional
Removed tool contact functionality from GPIO controller.
1 parent 4e4a24b commit b7eaf35

File tree

9 files changed

+295
-86
lines changed

9 files changed

+295
-86
lines changed

ur_controllers/include/ur_controllers/gpio_controller.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,6 @@ class GPIOController : public controller_interface::ControllerInterface
145145

146146
bool zeroFTSensor(std_srvs::srv::Trigger::Request::SharedPtr req, std_srvs::srv::Trigger::Response::SharedPtr resp);
147147

148-
bool startToolContact(std_srvs::srv::Trigger::Request::SharedPtr req,
149-
std_srvs::srv::Trigger::Response::SharedPtr resp);
150-
151-
bool endToolContact(std_srvs::srv::Trigger::Request::SharedPtr req, std_srvs::srv::Trigger::Response::SharedPtr resp);
152-
153148
void publishIO();
154149

155150
void publishToolData();
@@ -178,8 +173,6 @@ class GPIOController : public controller_interface::ControllerInterface
178173
rclcpp::Service<ur_msgs::srv::SetAnalogOutput>::SharedPtr set_analog_output_srv_;
179174
rclcpp::Service<ur_msgs::srv::SetPayload>::SharedPtr set_payload_srv_;
180175
rclcpp::Service<std_srvs::srv::Trigger>::SharedPtr tare_sensor_srv_;
181-
rclcpp::Service<std_srvs::srv::Trigger>::SharedPtr start_tool_contact_srv_;
182-
rclcpp::Service<std_srvs::srv::Trigger>::SharedPtr end_tool_contact_srv_;
183176

184177
std::shared_ptr<rclcpp::Publisher<ur_msgs::msg::IOStates>> io_pub_;
185178
std::shared_ptr<rclcpp::Publisher<ur_msgs::msg::ToolDataMsg>> tool_data_pub_;

ur_controllers/include/ur_controllers/tool_contact_controller.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
#define UR_CONTROLLERS__TOOL_CONTACT_CONTROLLER_HPP_
4343

4444
#include <controller_interface/chainable_controller_interface.hpp>
45+
#include "std_msgs/msg/bool.hpp"
46+
47+
#include "tool_contact_controller_parameters.hpp"
4548

4649
namespace ur_controllers
4750
{
@@ -120,6 +123,29 @@ class ToolContactController : public controller_interface::ChainableControllerIn
120123
*/
121124
controller_interface::return_type update_and_write_commands(const rclcpp::Time& time,
122125
const rclcpp::Duration& period) override;
126+
127+
private:
128+
double tool_contact_enable;
129+
double tool_contact_active;
130+
131+
std::atomic<bool> tool_contact_active_ = false;
132+
std::atomic<bool> change_requested_ = false;
133+
134+
double old_reference_val = 0.0;
135+
136+
std::optional<std::reference_wrapper<hardware_interface::LoanedCommandInterface>> async_success_interface_;
137+
std::optional<std::reference_wrapper<hardware_interface::LoanedCommandInterface>> enable_command_interface_;
138+
std::optional<std::reference_wrapper<hardware_interface::LoanedCommandInterface>> reference_interface_;
139+
140+
rclcpp::Subscription<std_msgs::msg::Bool>::SharedPtr tool_contact_enable_sub_;
141+
142+
std::shared_ptr<tool_contact_controller::ParamListener> tool_contact_param_listener_;
143+
tool_contact_controller::Params tool_contact_params_;
144+
145+
void tool_contact_sub_callback(const std_msgs::msg::Bool::SharedPtr msg);
146+
147+
static constexpr double ASYNC_WAITING = 2.0;
148+
static constexpr double ASYNC_STANDBY_ = std::numeric_limits<double>::quiet_NaN();
123149
};
124150
} // namespace ur_controllers
125151

ur_controllers/src/gpio_controller.cpp

Lines changed: 53 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,6 @@ controller_interface::InterfaceConfiguration GPIOController::command_interface_c
9999

100100
config.names.emplace_back(tf_prefix + "gpio/analog_output_domain_cmd");
101101

102-
// Start or stop tool contact functionality
103-
config.names.emplace_back(tf_prefix + "start_tool_contact/start_tool_contact_cmd");
104-
config.names.emplace_back(tf_prefix + "start_tool_contact/start_tool_contact_async_success");
105-
config.names.emplace_back(tf_prefix + "end_tool_contact/end_tool_contact_cmd");
106-
config.names.emplace_back(tf_prefix + "end_tool_contact/end_tool_contact_async_success");
107-
108102
return config;
109103
}
110104

@@ -323,14 +317,6 @@ ur_controllers::GPIOController::on_activate(const rclcpp_lifecycle::State& /*pre
323317
tare_sensor_srv_ = get_node()->create_service<std_srvs::srv::Trigger>(
324318
"~/zero_ftsensor",
325319
std::bind(&GPIOController::zeroFTSensor, this, std::placeholders::_1, std::placeholders::_2));
326-
327-
start_tool_contact_srv_ = get_node()->create_service<std_srvs::srv::Trigger>(
328-
"~/start_tool_contact",
329-
std::bind(&GPIOController::startToolContact, this, std::placeholders::_1, std::placeholders::_2));
330-
331-
end_tool_contact_srv_ = get_node()->create_service<std_srvs::srv::Trigger>(
332-
"~/end_tool_contact",
333-
std::bind(&GPIOController::endToolContact, this, std::placeholders::_1, std::placeholders::_2));
334320
} catch (...) {
335321
return LifecycleNodeInterface::CallbackReturn::ERROR;
336322
}
@@ -579,58 +565,59 @@ bool GPIOController::zeroFTSensor(std_srvs::srv::Trigger::Request::SharedPtr /*r
579565
return true;
580566
}
581567

582-
bool GPIOController::startToolContact(std_srvs::srv::Trigger::Request::SharedPtr /*req*/,
583-
std_srvs::srv::Trigger::Response::SharedPtr resp)
584-
{
585-
// reset success flag
586-
command_interfaces_[CommandInterfaces::START_TOOL_CONTACT_ASYNC_SUCCESS].set_value(ASYNC_WAITING);
587-
588-
// call the service in the hardware
589-
command_interfaces_[CommandInterfaces::START_TOOL_CONTACT_CMD].set_value(1.0);
590-
591-
if (!waitForAsyncCommand(
592-
[&]() { return command_interfaces_[CommandInterfaces::START_TOOL_CONTACT_ASYNC_SUCCESS].get_value(); })) {
593-
RCLCPP_WARN(get_node()->get_logger(), "Could not verify that tool contact was started.");
594-
}
595-
596-
resp->success =
597-
static_cast<bool>(command_interfaces_[CommandInterfaces::START_TOOL_CONTACT_ASYNC_SUCCESS].get_value());
598-
599-
if (resp->success) {
600-
RCLCPP_INFO(get_node()->get_logger(), "Successfully started tool contact");
601-
} else {
602-
RCLCPP_ERROR(get_node()->get_logger(), "Could not start tool contact");
603-
return false;
604-
}
605-
606-
return true;
607-
}
608-
609-
bool GPIOController::endToolContact(std_srvs::srv::Trigger::Request::SharedPtr /*req*/,
610-
std_srvs::srv::Trigger::Response::SharedPtr resp)
611-
{
612-
// reset success flag
613-
command_interfaces_[CommandInterfaces::END_TOOL_CONTACT_ASYNC_SUCCESS].set_value(ASYNC_WAITING);
614-
615-
// call the service in the hardware
616-
command_interfaces_[CommandInterfaces::END_TOOL_CONTACT_CMD].set_value(1.0);
617-
618-
if (!waitForAsyncCommand(
619-
[&]() { return command_interfaces_[CommandInterfaces::END_TOOL_CONTACT_ASYNC_SUCCESS].get_value(); })) {
620-
RCLCPP_WARN(get_node()->get_logger(), "Could not verify that tool contact was stopped.");
621-
}
622-
623-
resp->success = static_cast<bool>(command_interfaces_[CommandInterfaces::END_TOOL_CONTACT_ASYNC_SUCCESS].get_value());
624-
625-
if (resp->success) {
626-
RCLCPP_INFO(get_node()->get_logger(), "Successfully stopped tool contact");
627-
} else {
628-
RCLCPP_ERROR(get_node()->get_logger(), "Could not stop tool contact");
629-
return false;
630-
}
631-
632-
return true;
633-
}
568+
// bool GPIOController::startToolContact(std_srvs::srv::Trigger::Request::SharedPtr /*req*/,
569+
// std_srvs::srv::Trigger::Response::SharedPtr resp)
570+
// {
571+
// // reset success flag
572+
// command_interfaces_[CommandInterfaces::START_TOOL_CONTACT_ASYNC_SUCCESS].set_value(ASYNC_WAITING);
573+
574+
// // call the service in the hardware
575+
// command_interfaces_[CommandInterfaces::START_TOOL_CONTACT_CMD].set_value(1.0);
576+
577+
// if (!waitForAsyncCommand(
578+
// [&]() { return command_interfaces_[CommandInterfaces::START_TOOL_CONTACT_ASYNC_SUCCESS].get_value(); })) {
579+
// RCLCPP_WARN(get_node()->get_logger(), "Could not verify that tool contact was started.");
580+
// }
581+
582+
// resp->success =
583+
// static_cast<bool>(command_interfaces_[CommandInterfaces::START_TOOL_CONTACT_ASYNC_SUCCESS].get_value());
584+
585+
// if (resp->success) {
586+
// RCLCPP_INFO(get_node()->get_logger(), "Successfully started tool contact");
587+
// } else {
588+
// RCLCPP_ERROR(get_node()->get_logger(), "Could not start tool contact");
589+
// return false;
590+
// }
591+
592+
// return true;
593+
// }
594+
595+
// bool GPIOController::endToolContact(std_srvs::srv::Trigger::Request::SharedPtr /*req*/,
596+
// std_srvs::srv::Trigger::Response::SharedPtr resp)
597+
// {
598+
// // reset success flag
599+
// command_interfaces_[CommandInterfaces::END_TOOL_CONTACT_ASYNC_SUCCESS].set_value(ASYNC_WAITING);
600+
601+
// // call the service in the hardware
602+
// command_interfaces_[CommandInterfaces::END_TOOL_CONTACT_CMD].set_value(1.0);
603+
604+
// if (!waitForAsyncCommand(
605+
// [&]() { return command_interfaces_[CommandInterfaces::END_TOOL_CONTACT_ASYNC_SUCCESS].get_value(); })) {
606+
// RCLCPP_WARN(get_node()->get_logger(), "Could not verify that tool contact was stopped.");
607+
// }
608+
609+
// resp->success =
610+
// static_cast<bool>(command_interfaces_[CommandInterfaces::END_TOOL_CONTACT_ASYNC_SUCCESS].get_value());
611+
612+
// if (resp->success) {
613+
// RCLCPP_INFO(get_node()->get_logger(), "Successfully stopped tool contact");
614+
// } else {
615+
// RCLCPP_ERROR(get_node()->get_logger(), "Could not stop tool contact");
616+
// return false;
617+
// }
618+
619+
// return true;
620+
// }
634621

635622
void GPIOController::initMsgs()
636623
{

ur_controllers/src/tool_contact_controller.cpp

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,186 @@
3939
//----------------------------------------------------------------------
4040

4141
#include <ur_controllers/tool_contact_controller.hpp>
42+
#include "controller_interface/helpers.hpp"
43+
#include <rclcpp/rclcpp.hpp>
44+
#include "std_msgs/msg/bool.hpp"
45+
#include <iostream>
4246

4347
namespace ur_controllers
4448
{
4549

50+
controller_interface::CallbackReturn ToolContactController::on_init()
51+
{
52+
tool_contact_param_listener_ = std::make_shared<tool_contact_controller::ParamListener>(get_node());
53+
tool_contact_params_ = tool_contact_param_listener_->get_params();
54+
55+
// Resize this value depending on reference interfaces to be sent
56+
reference_interfaces_.resize(1, std::numeric_limits<double>::quiet_NaN());
57+
return controller_interface::CallbackReturn::SUCCESS;
58+
}
59+
60+
controller_interface::CallbackReturn ToolContactController::on_configure(const rclcpp_lifecycle::State& previous_state)
61+
{
62+
return controller_interface::CallbackReturn::SUCCESS;
63+
}
64+
65+
controller_interface::InterfaceConfiguration ToolContactController::command_interface_configuration() const
66+
{
67+
controller_interface::InterfaceConfiguration config;
68+
config.type = controller_interface::interface_configuration_type::INDIVIDUAL;
69+
const std::string tf_prefix = tool_contact_params_.tf_prefix;
70+
71+
// Start or stop tool contact functionality
72+
config.names.emplace_back(tf_prefix + "tool_contact/enable_cmd");
73+
config.names.emplace_back(tf_prefix + "tool_contact/async_success");
74+
config.names.emplace_back(tf_prefix + "end_tool_contact/end_tool_contact_cmd");
75+
config.names.emplace_back(tf_prefix + "end_tool_contact/end_tool_contact_async_success");
76+
return config;
77+
}
78+
79+
controller_interface::InterfaceConfiguration ToolContactController::state_interface_configuration() const
80+
{
81+
controller_interface::InterfaceConfiguration config;
82+
config.type = controller_interface::interface_configuration_type::INDIVIDUAL;
83+
return config;
84+
}
85+
86+
controller_interface::CallbackReturn ToolContactController::on_activate(const rclcpp_lifecycle::State& previous_state)
87+
{
88+
tool_contact_enable_sub_ = get_node()->create_subscription<std_msgs::msg::Bool>(
89+
"~/enable_tool_contact", 10,
90+
std::bind(&ToolContactController::tool_contact_sub_callback, this, std::placeholders::_1));
91+
92+
/*Figure out how to get reference interface by name*/
93+
// const std::string interface_name = tool_contact_params_.tf_prefix + get_node()->get_name() + "tool_contact/enable";
94+
// auto it = std::find_if(reference_interfaces_.begin(), reference_interfaces_.end(),
95+
// [&](auto& interface) { return (interface.get_name() == interface_name); });
96+
// if (it != reference_interfaces_.end()) {
97+
// reference_interface_ = *it;
98+
// reference_interface_->get().set_value(0.0);
99+
// } else {
100+
// RCLCPP_ERROR(get_node()->get_logger(), "Did not find '%s' in command interfaces.", interface_name.c_str());
101+
// return controller_interface::CallbackReturn::ERROR;
102+
// }
103+
reference_interfaces_[0] = 0.0;
104+
{
105+
const std::string interface_name = tool_contact_params_.tf_prefix + "tool_contact/enable_cmd";
106+
auto it = std::find_if(command_interfaces_.begin(), command_interfaces_.end(),
107+
[&](auto& interface) { return (interface.get_name() == interface_name); });
108+
if (it != command_interfaces_.end()) {
109+
enable_command_interface_ = *it;
110+
enable_command_interface_->get().set_value(ASYNC_STANDBY_);
111+
} else {
112+
RCLCPP_ERROR(get_node()->get_logger(), "Did not find '%s' in command interfaces.", interface_name.c_str());
113+
return controller_interface::CallbackReturn::ERROR;
114+
}
115+
}
116+
{
117+
const std::string interface_name = tool_contact_params_.tf_prefix + "tool_contact/async_success";
118+
auto it = std::find_if(command_interfaces_.begin(), command_interfaces_.end(),
119+
[&](auto& interface) { return (interface.get_name() == interface_name); });
120+
if (it != command_interfaces_.end()) {
121+
async_success_interface_ = *it;
122+
async_success_interface_->get().set_value(ASYNC_STANDBY_);
123+
} else {
124+
RCLCPP_ERROR(get_node()->get_logger(), "Did not find '%s' in command interfaces.", interface_name.c_str());
125+
return controller_interface::CallbackReturn::ERROR;
126+
}
127+
}
128+
129+
return controller_interface::CallbackReturn::SUCCESS;
130+
}
131+
132+
controller_interface::CallbackReturn ToolContactController::on_deactivate(const rclcpp_lifecycle::State& previous_state)
133+
{
134+
tool_contact_enable_sub_.reset();
135+
136+
return controller_interface::CallbackReturn::SUCCESS;
137+
}
138+
139+
void ToolContactController::tool_contact_sub_callback(const std_msgs::msg::Bool::SharedPtr msg)
140+
{
141+
std::cout << "data received: " << msg->data << std::endl;
142+
if (msg->data) {
143+
if (!tool_contact_active_ && !change_requested_) {
144+
tool_contact_active_ = true;
145+
change_requested_ = true;
146+
}
147+
} else {
148+
if (tool_contact_active_ && !change_requested_) {
149+
tool_contact_active_ = false;
150+
change_requested_ = true;
151+
}
152+
}
153+
}
154+
155+
std::vector<hardware_interface::CommandInterface> ToolContactController::on_export_reference_interfaces()
156+
{
157+
std::vector<hardware_interface::CommandInterface> reference_interfaces;
158+
reference_interfaces.reserve(1);
159+
reference_interfaces.push_back(
160+
hardware_interface::CommandInterface(get_node()->get_name(), "tool_contact/enable", &tool_contact_enable));
161+
162+
return reference_interfaces;
163+
}
164+
165+
std::vector<hardware_interface::StateInterface> ToolContactController::on_export_state_interfaces()
166+
{
167+
std::vector<hardware_interface::StateInterface> state_interfaces;
168+
state_interfaces.push_back(
169+
hardware_interface::StateInterface(get_node()->get_name(), "tool_contact/active", &tool_contact_active));
170+
171+
return state_interfaces;
172+
}
173+
174+
bool ToolContactController::on_set_chained_mode(bool chained_mode)
175+
{
176+
if (chained_mode) {
177+
tool_contact_enable_sub_.reset();
178+
} else {
179+
tool_contact_enable_sub_ = get_node()->create_subscription<std_msgs::msg::Bool>(
180+
"~/enable_tool_contact", 10,
181+
std::bind(&ToolContactController::tool_contact_sub_callback, this, std::placeholders::_1));
182+
}
183+
change_requested_ = false;
184+
tool_contact_active_ = false;
185+
return true;
186+
}
187+
188+
controller_interface::return_type
189+
ToolContactController::update_reference_from_subscribers(const rclcpp::Time& time, const rclcpp::Duration& period)
190+
{
191+
if (change_requested_) {
192+
reference_interfaces_[0] = static_cast<double>(tool_contact_active_);
193+
change_requested_ = false;
194+
}
195+
return controller_interface::return_type::OK;
196+
}
197+
198+
controller_interface::return_type ToolContactController::update_and_write_commands(const rclcpp::Time& time,
199+
const rclcpp::Duration& period)
200+
{
201+
if (reference_interfaces_[0] != old_reference_val) {
202+
enable_command_interface_->get().set_value(reference_interfaces_[0]);
203+
async_success_interface_->get().set_value(ASYNC_WAITING);
204+
std::cout << "Tool contact interface set to: " << reference_interfaces_[0] << std::endl;
205+
}
206+
207+
if (async_success_interface_->get().get_value() == 0.0) {
208+
RCLCPP_ERROR(get_node()->get_logger(), "Failed to enable tool contact mode.");
209+
async_success_interface_->get().set_value(ASYNC_STANDBY_);
210+
211+
} else if (async_success_interface_->get().get_value() == 1.0) {
212+
RCLCPP_INFO(get_node()->get_logger(), "Tool contact enabled successfully ");
213+
async_success_interface_->get().set_value(ASYNC_STANDBY_);
214+
}
215+
216+
old_reference_val = reference_interfaces_[0];
217+
return controller_interface::return_type::OK;
218+
}
219+
46220
} // namespace ur_controllers
221+
222+
#include "pluginlib/class_list_macros.hpp"
223+
224+
PLUGINLIB_EXPORT_CLASS(ur_controllers::ToolContactController, controller_interface::ChainableControllerInterface)

0 commit comments

Comments
 (0)