Skip to content

Commit dfad818

Browse files
committed
PoC: Add effort command interface to hardware interface
This is just a proof of concept to make the robot work. This still requires some work.
1 parent 9dae45a commit dfad818

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed

ur_robot_driver/config/ur_controllers.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ controller_manager:
2121
forward_velocity_controller:
2222
type: velocity_controllers/JointGroupVelocityController
2323

24+
forward_effort_controller:
25+
type: effort_controllers/JointGroupEffortController
26+
2427
forward_position_controller:
2528
type: position_controllers/JointGroupPositionController
2629

@@ -159,6 +162,17 @@ forward_velocity_controller:
159162
- $(var tf_prefix)wrist_3_joint
160163
interface_name: velocity
161164

165+
forward_effort_controller:
166+
ros__parameters:
167+
joints:
168+
- $(var tf_prefix)shoulder_pan_joint
169+
- $(var tf_prefix)shoulder_lift_joint
170+
- $(var tf_prefix)elbow_joint
171+
- $(var tf_prefix)wrist_1_joint
172+
- $(var tf_prefix)wrist_2_joint
173+
- $(var tf_prefix)wrist_3_joint
174+
interface_name: effort
175+
162176
forward_position_controller:
163177
ros__parameters:
164178
joints:

ur_robot_driver/include/ur_robot_driver/hardware_interface.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ enum StoppingInterface
8181
STOP_FORCE_MODE,
8282
STOP_FREEDRIVE,
8383
STOP_TOOL_CONTACT,
84+
STOP_TORQUE,
8485
};
8586

8687
// We define our own quaternion to use it as a buffer, since we need to pass pointers to the state
@@ -175,6 +176,7 @@ class URPositionHardwareInterface : public hardware_interface::SystemInterface
175176
urcl::vector6d_t urcl_position_commands_;
176177
urcl::vector6d_t urcl_position_commands_old_;
177178
urcl::vector6d_t urcl_velocity_commands_;
179+
urcl::vector6d_t urcl_torque_commands_;
178180
urcl::vector6d_t urcl_joint_positions_;
179181
urcl::vector6d_t urcl_joint_velocities_;
180182
urcl::vector6d_t urcl_joint_efforts_;
@@ -303,6 +305,7 @@ class URPositionHardwareInterface : public hardware_interface::SystemInterface
303305
std::vector<std::vector<std::string>> start_modes_;
304306
bool position_controller_running_;
305307
bool velocity_controller_running_;
308+
bool torque_controller_running_;
306309
bool force_mode_controller_running_ = false;
307310

308311
std::unique_ptr<urcl::UrDriver> ur_driver_;

ur_robot_driver/launch/ur_control.launch.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ def controller_spawner(controllers, active=True):
187187
"joint_trajectory_controller",
188188
"forward_velocity_controller",
189189
"forward_position_controller",
190+
"forward_effort_controller",
190191
"force_mode_controller",
191192
"passthrough_trajectory_controller",
192193
"freedrive_mode_controller",

ur_robot_driver/src/hardware_interface.cpp

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ URPositionHardwareInterface::on_init(const hardware_interface::HardwareInfo& sys
8181
urcl_velocity_commands_ = { { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 } };
8282
position_controller_running_ = false;
8383
velocity_controller_running_ = false;
84+
torque_controller_running_ = false;
8485
freedrive_mode_controller_running_ = false;
8586
passthrough_trajectory_controller_running_ = false;
8687
tool_contact_controller_running_ = false;
@@ -104,9 +105,9 @@ URPositionHardwareInterface::on_init(const hardware_interface::HardwareInfo& sys
104105
trajectory_joint_accelerations_.reserve(32768);
105106

106107
for (const hardware_interface::ComponentInfo& joint : info_.joints) {
107-
if (joint.command_interfaces.size() != 2) {
108+
if (joint.command_interfaces.size() != 3) {
108109
RCLCPP_FATAL(rclcpp::get_logger("URPositionHardwareInterface"),
109-
"Joint '%s' has %zu command interfaces found. 2 expected.", joint.name.c_str(),
110+
"Joint '%s' has %zu command interfaces found. 3 expected.", joint.name.c_str(),
110111
joint.command_interfaces.size());
111112
return hardware_interface::CallbackReturn::ERROR;
112113
}
@@ -289,6 +290,9 @@ std::vector<hardware_interface::CommandInterface> URPositionHardwareInterface::e
289290

290291
command_interfaces.emplace_back(hardware_interface::CommandInterface(
291292
info_.joints[i].name, hardware_interface::HW_IF_VELOCITY, &urcl_velocity_commands_[i]));
293+
294+
command_interfaces.emplace_back(hardware_interface::CommandInterface(
295+
info_.joints[i].name, hardware_interface::HW_IF_EFFORT, &urcl_torque_commands_[i]));
292296
}
293297
// Obtain the tf_prefix from the urdf so that we can have the general interface multiple times
294298
// NOTE using the tf_prefix at this point is some kind of workaround. One should actually go through the list of gpio
@@ -771,6 +775,7 @@ hardware_interface::return_type URPositionHardwareInterface::read(const rclcpp::
771775
// initialize commands
772776
urcl_position_commands_ = urcl_position_commands_old_ = urcl_joint_positions_;
773777
urcl_velocity_commands_ = { { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 } };
778+
urcl_torque_commands_ = { { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 } };
774779
target_speed_fraction_cmd_ = NO_NEW_CMD_;
775780
resend_robot_program_cmd_ = NO_NEW_CMD_;
776781
zero_ftsensor_cmd_ = NO_NEW_CMD_;
@@ -805,7 +810,8 @@ hardware_interface::return_type URPositionHardwareInterface::write(const rclcpp:
805810

806811
} else if (velocity_controller_running_) {
807812
ur_driver_->writeJointCommand(urcl_velocity_commands_, urcl::comm::ControlMode::MODE_SPEEDJ, receive_timeout_);
808-
813+
} else if (torque_controller_running_) {
814+
ur_driver_->writeJointCommand(urcl_torque_commands_, urcl::comm::ControlMode::MODE_TORQUE, receive_timeout_);
809815
} else if (freedrive_mode_controller_running_ && freedrive_activated_) {
810816
ur_driver_->writeFreedriveControlMessage(urcl::control::FreedriveControlMessage::FREEDRIVE_NOOP);
811817

@@ -1085,6 +1091,9 @@ hardware_interface::return_type URPositionHardwareInterface::prepare_command_mod
10851091
if (velocity_controller_running_) {
10861092
control_modes[i] = { hardware_interface::HW_IF_VELOCITY };
10871093
}
1094+
if (torque_controller_running_) {
1095+
control_modes[i] = { hardware_interface::HW_IF_EFFORT };
1096+
}
10881097
if (force_mode_controller_running_) {
10891098
control_modes[i].push_back(FORCE_MODE_GPIO);
10901099
}
@@ -1124,6 +1133,16 @@ hardware_interface::return_type URPositionHardwareInterface::prepare_command_mod
11241133
return hardware_interface::return_type::ERROR;
11251134
}
11261135
start_modes_[i].push_back(hardware_interface::HW_IF_VELOCITY);
1136+
} else if (key == info_.joints[i].name + "/" + hardware_interface::HW_IF_EFFORT) {
1137+
if (std::any_of(start_modes_[i].begin(), start_modes_[i].end(), [&](const std::string& item) {
1138+
return item == hardware_interface::HW_IF_POSITION || item == PASSTHROUGH_GPIO ||
1139+
item == FORCE_MODE_GPIO || item == FREEDRIVE_MODE_GPIO;
1140+
})) {
1141+
RCLCPP_ERROR(get_logger(), "Attempting to start effort control while there is another control mode already "
1142+
"requested.");
1143+
return hardware_interface::return_type::ERROR;
1144+
}
1145+
start_modes_[i].push_back(hardware_interface::HW_IF_EFFORT);
11271146
} else if (key == tf_prefix + FORCE_MODE_GPIO + "/type") {
11281147
if (std::any_of(start_modes_[i].begin(), start_modes_[i].end(), [&](const std::string& item) {
11291148
return item == hardware_interface::HW_IF_POSITION || item == hardware_interface::HW_IF_VELOCITY;
@@ -1187,6 +1206,13 @@ hardware_interface::return_type URPositionHardwareInterface::prepare_command_mod
11871206
[](const std::string& item) { return item == hardware_interface::HW_IF_VELOCITY; }),
11881207
control_modes[i].end());
11891208
}
1209+
if (key == info_.joints[i].name + "/" + hardware_interface::HW_IF_EFFORT) {
1210+
stop_modes_[i].push_back(StoppingInterface::STOP_TORQUE);
1211+
control_modes[i].erase(
1212+
std::remove_if(control_modes[i].begin(), control_modes[i].end(),
1213+
[](const std::string& item) { return item == hardware_interface::HW_IF_EFFORT; }),
1214+
control_modes[i].end());
1215+
}
11901216
if (key == tf_prefix + FORCE_MODE_GPIO + "/disable_cmd") {
11911217
stop_modes_[i].push_back(StoppingInterface::STOP_FORCE_MODE);
11921218
control_modes[i].erase(std::remove_if(control_modes[i].begin(), control_modes[i].end(),
@@ -1312,6 +1338,24 @@ hardware_interface::return_type URPositionHardwareInterface::prepare_command_mod
13121338
ret_val = hardware_interface::return_type::ERROR;
13131339
}
13141340

1341+
// Effort mode requested to start
1342+
if (std::any_of(start_modes_[0].begin(), start_modes_[0].end(),
1343+
[](auto& item) { return (item == hardware_interface::HW_IF_EFFORT); }) &&
1344+
(std::any_of(start_modes_[0].begin(), start_modes_[0].end(),
1345+
[this](auto& item) {
1346+
return (item == hardware_interface::HW_IF_POSITION || item == hardware_interface::HW_IF_VELOCITY ||
1347+
item == PASSTHROUGH_GPIO || item == FORCE_MODE_GPIO || item == FREEDRIVE_MODE_GPIO);
1348+
}) ||
1349+
std::any_of(control_modes[0].begin(), control_modes[0].end(), [this](auto& item) {
1350+
return (item == hardware_interface::HW_IF_EFFORT || item == hardware_interface::HW_IF_VELOCITY ||
1351+
item == hardware_interface::HW_IF_POSITION || item == PASSTHROUGH_GPIO || item == FORCE_MODE_GPIO ||
1352+
item == FREEDRIVE_MODE_GPIO);
1353+
}))) {
1354+
RCLCPP_ERROR(get_logger(), "Attempting to start velosity control while there is either trajectory passthrough or "
1355+
"position mode or force_mode or freedrive mode running.");
1356+
ret_val = hardware_interface::return_type::ERROR;
1357+
}
1358+
13151359
controllers_initialized_ = true;
13161360
return ret_val;
13171361
}
@@ -1331,6 +1375,11 @@ hardware_interface::return_type URPositionHardwareInterface::perform_command_mod
13311375
velocity_controller_running_ = false;
13321376
urcl_velocity_commands_ = { { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 } };
13331377
}
1378+
if (stop_modes_[0].size() != 0 &&
1379+
std::find(stop_modes_[0].begin(), stop_modes_[0].end(), StoppingInterface::STOP_TORQUE) != stop_modes_[0].end()) {
1380+
torque_controller_running_ = false;
1381+
urcl_torque_commands_ = { { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 } };
1382+
}
13341383
if (stop_modes_[0].size() != 0 && std::find(stop_modes_[0].begin(), stop_modes_[0].end(),
13351384
StoppingInterface::STOP_FORCE_MODE) != stop_modes_[0].end()) {
13361385
force_mode_controller_running_ = false;
@@ -1361,16 +1410,25 @@ hardware_interface::return_type URPositionHardwareInterface::perform_command_mod
13611410
if (start_modes_.size() != 0 && std::find(start_modes_[0].begin(), start_modes_[0].end(),
13621411
hardware_interface::HW_IF_POSITION) != start_modes_[0].end()) {
13631412
velocity_controller_running_ = false;
1413+
torque_controller_running_ = false;
13641414
passthrough_trajectory_controller_running_ = false;
13651415
urcl_position_commands_ = urcl_position_commands_old_ = urcl_joint_positions_;
13661416
position_controller_running_ = true;
13671417

13681418
} else if (start_modes_[0].size() != 0 && std::find(start_modes_[0].begin(), start_modes_[0].end(),
13691419
hardware_interface::HW_IF_VELOCITY) != start_modes_[0].end()) {
13701420
position_controller_running_ = false;
1421+
torque_controller_running_ = false;
13711422
passthrough_trajectory_controller_running_ = false;
13721423
urcl_velocity_commands_ = { { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 } };
13731424
velocity_controller_running_ = true;
1425+
} else if (start_modes_[0].size() != 0 && std::find(start_modes_[0].begin(), start_modes_[0].end(),
1426+
hardware_interface::HW_IF_EFFORT) != start_modes_[0].end()) {
1427+
position_controller_running_ = false;
1428+
velocity_controller_running_ = false;
1429+
torque_controller_running_ = true;
1430+
passthrough_trajectory_controller_running_ = false;
1431+
urcl_torque_commands_ = { { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 } };
13741432
}
13751433
if (start_modes_[0].size() != 0 &&
13761434
std::find(start_modes_[0].begin(), start_modes_[0].end(), FORCE_MODE_GPIO) != start_modes_[0].end()) {
@@ -1380,13 +1438,15 @@ hardware_interface::return_type URPositionHardwareInterface::perform_command_mod
13801438
std::find(start_modes_[0].begin(), start_modes_[0].end(), PASSTHROUGH_GPIO) != start_modes_[0].end()) {
13811439
velocity_controller_running_ = false;
13821440
position_controller_running_ = false;
1441+
torque_controller_running_ = false;
13831442
passthrough_trajectory_controller_running_ = true;
13841443
passthrough_trajectory_abort_ = 0.0;
13851444
}
13861445
if (start_modes_[0].size() != 0 &&
13871446
std::find(start_modes_[0].begin(), start_modes_[0].end(), FREEDRIVE_MODE_GPIO) != start_modes_[0].end()) {
13881447
velocity_controller_running_ = false;
13891448
position_controller_running_ = false;
1449+
torque_controller_running_ = false;
13901450
freedrive_mode_controller_running_ = true;
13911451
freedrive_activated_ = false;
13921452
}

0 commit comments

Comments
 (0)