Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add force and velocity hardware interfaces #44

Merged
merged 7 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion robotiq_description/config/robotiq_controllers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ controller_manager:
joint_state_broadcaster:
type: joint_state_broadcaster/JointStateBroadcaster
robotiq_gripper_controller:
type: position_controllers/GripperActionController
type: antipodal_gripper_action_controller/GripperActionController
robotiq_activation_controller:
type: robotiq_controllers/RobotiqActivationController

robotiq_gripper_controller:
ros__parameters:
default: true
joint: robotiq_85_left_knuckle_joint
use_effort_interface: true
use_speed_interface: true

robotiq_activation_controller:
ros__parameters:
Expand Down
4 changes: 4 additions & 0 deletions robotiq_driver/include/robotiq_driver/hardware_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,15 @@ class RobotiqGripperHardwareInterface : public hardware_interface::SystemInterfa
double gripper_position_command_;

std::atomic<uint8_t> write_command_;
std::atomic<uint8_t> write_force_;
std::atomic<uint8_t> write_speed_;
std::atomic<uint8_t> gripper_current_state_;

double reactivate_gripper_cmd_;
std::atomic<bool> reactivate_gripper_async_cmd_;
double reactivate_gripper_response_;
double gripper_force_;
double gripper_speed_;
std::atomic<std::optional<bool>> reactivate_gripper_async_response_;
};

Expand Down
21 changes: 21 additions & 0 deletions robotiq_driver/src/hardware_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

constexpr uint8_t kGripperMinPos = 3;
constexpr uint8_t kGripperMaxPos = 230;
constexpr double kGripperMaxSpeed = 0.150; // mm/s
constexpr double kGripperMaxforce = 235; // N
constexpr uint8_t kGripperRange = kGripperMaxPos - kGripperMinPos;

constexpr auto kGripperCommsLoopPeriod = std::chrono::milliseconds{ 10 };
Expand All @@ -60,15 +62,15 @@
communication_thread_is_running_.store(false);
if (communication_thread_.joinable())
{
communication_thread_.join();

Check warning on line 65 in robotiq_driver/src/hardware_interface.cpp

View check run for this annotation

Codecov / codecov/patch

robotiq_driver/src/hardware_interface.cpp#L65

Added line #L65 was not covered by tests
}
}

// This constructor is use for testing only.
RobotiqGripperHardwareInterface::RobotiqGripperHardwareInterface(std::unique_ptr<DriverFactory> driver_factory)
: driver_factory_{ std::move(driver_factory) }

Check warning on line 71 in robotiq_driver/src/hardware_interface.cpp

View check run for this annotation

Codecov / codecov/patch

robotiq_driver/src/hardware_interface.cpp#L70-L71

Added lines #L70 - L71 were not covered by tests
{
}

Check warning on line 73 in robotiq_driver/src/hardware_interface.cpp

View check run for this annotation

Codecov / codecov/patch

robotiq_driver/src/hardware_interface.cpp#L73

Added line #L73 was not covered by tests

hardware_interface::CallbackReturn RobotiqGripperHardwareInterface::on_init(const hardware_interface::HardwareInfo& info)
{
Expand Down Expand Up @@ -139,14 +141,14 @@
}

rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
RobotiqGripperHardwareInterface::on_configure(const rclcpp_lifecycle::State& previous_state)

Check warning on line 144 in robotiq_driver/src/hardware_interface.cpp

View check run for this annotation

Codecov / codecov/patch

robotiq_driver/src/hardware_interface.cpp#L144

Added line #L144 was not covered by tests
{
RCLCPP_DEBUG(kLogger, "on_configure");
try
{
if (hardware_interface::SystemInterface::on_configure(previous_state) != CallbackReturn::SUCCESS)
{
return CallbackReturn::ERROR;

Check warning on line 151 in robotiq_driver/src/hardware_interface.cpp

View check run for this annotation

Codecov / codecov/patch

robotiq_driver/src/hardware_interface.cpp#L151

Added line #L151 was not covered by tests
}

// Open the serial port and handshake.
Expand All @@ -154,15 +156,15 @@
if (!connected)
{
RCLCPP_ERROR(kLogger, "Cannot connect to the Robotiq gripper");
return CallbackReturn::ERROR;

Check warning on line 159 in robotiq_driver/src/hardware_interface.cpp

View check run for this annotation

Codecov / codecov/patch

robotiq_driver/src/hardware_interface.cpp#L159

Added line #L159 was not covered by tests
}
}
catch (const std::exception& e)
{
RCLCPP_ERROR(kLogger, "Cannot configure the Robotiq gripper: %s", e.what());
return CallbackReturn::ERROR;
}
return CallbackReturn::SUCCESS;

Check warning on line 167 in robotiq_driver/src/hardware_interface.cpp

View check run for this annotation

Codecov / codecov/patch

robotiq_driver/src/hardware_interface.cpp#L165-L167

Added lines #L165 - L167 were not covered by tests
}

std::vector<hardware_interface::StateInterface> RobotiqGripperHardwareInterface::export_state_interfaces()
Expand All @@ -187,6 +189,19 @@

command_interfaces.emplace_back(hardware_interface::CommandInterface(
info_.joints[0].name, hardware_interface::HW_IF_POSITION, &gripper_position_command_));

command_interfaces.emplace_back(
hardware_interface::CommandInterface(info_.joints[0].name, "set_gripper_max_velocity", &gripper_speed_));
gripper_speed_ = info_.hardware_parameters.count("gripper_speed_multiplier") ?
info_.hardware_parameters.count("gripper_speed_multiplier") :
1.0;

command_interfaces.emplace_back(
hardware_interface::CommandInterface(info_.joints[0].name, "set_gripper_max_effort", &gripper_force_));
gripper_force_ = info_.hardware_parameters.count("gripper_force_multiplier") ?
info_.hardware_parameters.count("gripper_force_multiplier") :
1.0;

command_interfaces.emplace_back(
hardware_interface::CommandInterface("reactivate_gripper", "reactivate_gripper_cmd", &reactivate_gripper_cmd_));
command_interfaces.emplace_back(hardware_interface::CommandInterface(
Expand All @@ -196,7 +211,7 @@
}

hardware_interface::CallbackReturn
RobotiqGripperHardwareInterface::on_activate(const rclcpp_lifecycle::State& /*previous_state*/)

Check warning on line 214 in robotiq_driver/src/hardware_interface.cpp

View check run for this annotation

Codecov / codecov/patch

robotiq_driver/src/hardware_interface.cpp#L214

Added line #L214 was not covered by tests
{
RCLCPP_DEBUG(kLogger, "on_activate");

Expand All @@ -214,8 +229,8 @@
driver_->deactivate();
driver_->activate();

communication_thread_is_running_.store(true);
communication_thread_ = std::thread([this] { this->background_task(); });

Check warning on line 233 in robotiq_driver/src/hardware_interface.cpp

View check run for this annotation

Codecov / codecov/patch

robotiq_driver/src/hardware_interface.cpp#L232-L233

Added lines #L232 - L233 were not covered by tests
}
catch (const std::exception& e)
{
Expand All @@ -228,15 +243,15 @@
}

hardware_interface::CallbackReturn
RobotiqGripperHardwareInterface::on_deactivate(const rclcpp_lifecycle::State& /*previous_state*/)

Check warning on line 246 in robotiq_driver/src/hardware_interface.cpp

View check run for this annotation

Codecov / codecov/patch

robotiq_driver/src/hardware_interface.cpp#L246

Added line #L246 was not covered by tests
{
RCLCPP_DEBUG(kLogger, "on_deactivate");

communication_thread_is_running_.store(false);
communication_thread_.join();

Check warning on line 251 in robotiq_driver/src/hardware_interface.cpp

View check run for this annotation

Codecov / codecov/patch

robotiq_driver/src/hardware_interface.cpp#L250-L251

Added lines #L250 - L251 were not covered by tests
if (communication_thread_.joinable())
{
communication_thread_.join();

Check warning on line 254 in robotiq_driver/src/hardware_interface.cpp

View check run for this annotation

Codecov / codecov/patch

robotiq_driver/src/hardware_interface.cpp#L254

Added line #L254 was not covered by tests
}

try
Expand All @@ -246,16 +261,16 @@
catch (const std::exception& e)
{
RCLCPP_ERROR(kLogger, "Failed to deactivate the Robotiq gripper: %s", e.what());
return CallbackReturn::ERROR;
}

Check warning on line 265 in robotiq_driver/src/hardware_interface.cpp

View check run for this annotation

Codecov / codecov/patch

robotiq_driver/src/hardware_interface.cpp#L264-L265

Added lines #L264 - L265 were not covered by tests
RCLCPP_INFO(kLogger, "Robotiq Gripper successfully deactivated!");
return CallbackReturn::SUCCESS;
}

hardware_interface::return_type RobotiqGripperHardwareInterface::read(const rclcpp::Time& /*time*/,

Check warning on line 270 in robotiq_driver/src/hardware_interface.cpp

View check run for this annotation

Codecov / codecov/patch

robotiq_driver/src/hardware_interface.cpp#L270

Added line #L270 was not covered by tests
const rclcpp::Duration& /*period*/)
{
gripper_position_ = gripper_closed_pos_ * (gripper_current_state_.load() - kGripperMinPos) / kGripperRange;

Check warning on line 273 in robotiq_driver/src/hardware_interface.cpp

View check run for this annotation

Codecov / codecov/patch

robotiq_driver/src/hardware_interface.cpp#L273

Added line #L273 was not covered by tests

if (!std::isnan(reactivate_gripper_cmd_))
{
Expand All @@ -273,17 +288,21 @@
return hardware_interface::return_type::OK;
}

hardware_interface::return_type RobotiqGripperHardwareInterface::write(const rclcpp::Time& /*time*/,

Check warning on line 291 in robotiq_driver/src/hardware_interface.cpp

View check run for this annotation

Codecov / codecov/patch

robotiq_driver/src/hardware_interface.cpp#L291

Added line #L291 was not covered by tests
const rclcpp::Duration& /*period*/)
{
double gripper_pos = (gripper_position_command_ / gripper_closed_pos_) * kGripperRange + kGripperMinPos;

Check warning on line 294 in robotiq_driver/src/hardware_interface.cpp

View check run for this annotation

Codecov / codecov/patch

robotiq_driver/src/hardware_interface.cpp#L294

Added line #L294 was not covered by tests
gripper_pos = std::max(std::min(gripper_pos, 255.0), 0.0);
write_command_.store(uint8_t(gripper_pos));
gripper_speed_ = kGripperMaxSpeed * std::clamp(fabs(gripper_speed_) / kGripperMaxSpeed, 0.0, 1.0);
write_speed_.store(uint8_t(gripper_speed_ * 0xFF));
gripper_force_ = kGripperMaxforce * std::clamp(fabs(gripper_force_) / kGripperMaxforce, 0.0, 1.0);
write_force_.store(uint8_t(gripper_force_ * 0xFF));

Check warning on line 300 in robotiq_driver/src/hardware_interface.cpp

View check run for this annotation

Codecov / codecov/patch

robotiq_driver/src/hardware_interface.cpp#L297-L300

Added lines #L297 - L300 were not covered by tests

return hardware_interface::return_type::OK;
}

void RobotiqGripperHardwareInterface::background_task()

Check warning on line 305 in robotiq_driver/src/hardware_interface.cpp

View check run for this annotation

Codecov / codecov/patch

robotiq_driver/src/hardware_interface.cpp#L305

Added line #L305 was not covered by tests
{
while (communication_thread_is_running_.load())
{
Expand All @@ -295,12 +314,14 @@
{
this->driver_->deactivate();
this->driver_->activate();
reactivate_gripper_async_cmd_.store(false);
reactivate_gripper_async_response_.store(true);

Check warning on line 318 in robotiq_driver/src/hardware_interface.cpp

View check run for this annotation

Codecov / codecov/patch

robotiq_driver/src/hardware_interface.cpp#L317-L318

Added lines #L317 - L318 were not covered by tests
}

// Write the latest command to the gripper.
this->driver_->set_gripper_position(write_command_.load());
this->driver_->set_speed(write_speed_.load());
this->driver_->set_force(write_force_.load());

// Read the state of the gripper.
gripper_current_state_.store(this->driver_->get_gripper_position());
Expand All @@ -308,11 +329,11 @@
catch (std::exception& e)
{
RCLCPP_ERROR(kLogger, "Error: %s", e.what());
}

Check warning on line 332 in robotiq_driver/src/hardware_interface.cpp

View check run for this annotation

Codecov / codecov/patch

robotiq_driver/src/hardware_interface.cpp#L332

Added line #L332 was not covered by tests

std::this_thread::sleep_for(kGripperCommsLoopPeriod);

Check warning on line 334 in robotiq_driver/src/hardware_interface.cpp

View check run for this annotation

Codecov / codecov/patch

robotiq_driver/src/hardware_interface.cpp#L334

Added line #L334 was not covered by tests
}
}

Check warning on line 336 in robotiq_driver/src/hardware_interface.cpp

View check run for this annotation

Codecov / codecov/patch

robotiq_driver/src/hardware_interface.cpp#L336

Added line #L336 was not covered by tests

} // namespace robotiq_driver

Expand Down
Loading