Skip to content

Commit 3e1f979

Browse files
authored
Add approximate gripper position (#49)
- Main change: Add approximate gripper position - Minor change: Add more comments about TODOs and appropriate waits
1 parent 444a998 commit 3e1f979

File tree

2 files changed

+32
-27
lines changed

2 files changed

+32
-27
lines changed

openarm_hardware/include/openarm_hardware/v10_simple_hardware.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#pragma once
1616

17+
#include <chrono>
1718
#include <memory>
1819
#include <openarm/can/socket/openarm.hpp>
1920
#include <openarm/damiao_motor/dm_motor_constants.hpp>
@@ -105,6 +106,13 @@ class OpenArm_v10HW : public hardware_interface::SystemInterface {
105106
const std::vector<double> DEFAULT_KD = {2.75, 2.5, 0.7, 0.4,
106107
0.7, 0.6, 0.5, 0.1};
107108

109+
const double GRIPPER_JOINT_0_POSITION = 0.044;
110+
const double GRIPPER_JOINT_1_POSITION = 0.0;
111+
const double GRIPPER_MOTOR_0_RADIANS = 0.0;
112+
const double GRIPPER_MOTOR_1_RADIANS = -1.0472;
113+
const double GRIPPER_DEFAULT_KP = 5.0;
114+
const double GRIPPER_DEFAULT_KD = 0.1;
115+
108116
// Configuration
109117
std::string can_interface_;
110118
std::string arm_prefix_;

openarm_hardware/src/v10_simple_hardware.cpp

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ bool OpenArm_v10HW::parse_config(const hardware_interface::HardwareInfo& info) {
6464
"Configuration: CAN=%s, arm_prefix=%s, hand=%s, can_fd=%s",
6565
can_interface_.c_str(), arm_prefix_.c_str(),
6666
hand_ ? "enabled" : "disabled", can_fd_ ? "enabled" : "disabled");
67-
6867
return true;
6968
}
7069

7170
void OpenArm_v10HW::generate_joint_names() {
7271
joint_names_.clear();
73-
// TODO: read from urdf properly in the future
74-
// Generate arm joint names: openarm_{arm_prefix}joint{N}
72+
// TODO: read from urdf properly and sort in the future.
73+
// Currently, the joint names are hardcoded for order consistency to align
74+
// with hardware. Generate arm joint names: openarm_{arm_prefix}joint{N}
7575
for (size_t i = 1; i <= ARM_DOF; ++i) {
7676
std::string joint_name =
7777
"openarm_" + arm_prefix_ + "joint" + std::to_string(i);
@@ -154,7 +154,6 @@ hardware_interface::CallbackReturn OpenArm_v10HW::on_init(
154154
hardware_interface::CallbackReturn OpenArm_v10HW::on_configure(
155155
const rclcpp_lifecycle::State& /*previous_state*/) {
156156
// Set callback mode to ignore during configuration
157-
openarm_->set_callback_mode_all(openarm::damiao_motor::CallbackMode::IGNORE);
158157
openarm_->refresh_all();
159158
std::this_thread::sleep_for(std::chrono::milliseconds(100));
160159
openarm_->recv_all();
@@ -165,7 +164,6 @@ hardware_interface::CallbackReturn OpenArm_v10HW::on_configure(
165164
std::vector<hardware_interface::StateInterface>
166165
OpenArm_v10HW::export_state_interfaces() {
167166
std::vector<hardware_interface::StateInterface> state_interfaces;
168-
169167
for (size_t i = 0; i < joint_names_.size(); ++i) {
170168
state_interfaces.emplace_back(hardware_interface::StateInterface(
171169
joint_names_[i], hardware_interface::HW_IF_POSITION, &pos_states_[i]));
@@ -181,7 +179,7 @@ OpenArm_v10HW::export_state_interfaces() {
181179
std::vector<hardware_interface::CommandInterface>
182180
OpenArm_v10HW::export_command_interfaces() {
183181
std::vector<hardware_interface::CommandInterface> command_interfaces;
184-
182+
// TODO: consider exposing only needed interfaces to avoid undefined behavior.
185183
for (size_t i = 0; i < joint_names_.size(); ++i) {
186184
command_interfaces.emplace_back(hardware_interface::CommandInterface(
187185
joint_names_[i], hardware_interface::HW_IF_POSITION,
@@ -199,14 +197,12 @@ OpenArm_v10HW::export_command_interfaces() {
199197
hardware_interface::CallbackReturn OpenArm_v10HW::on_activate(
200198
const rclcpp_lifecycle::State& /*previous_state*/) {
201199
RCLCPP_INFO(rclcpp::get_logger("OpenArm_v10HW"), "Activating OpenArm V10...");
202-
203-
// Set callback mode to state and enable all motors (like full_arm.cpp)
204200
openarm_->set_callback_mode_all(openarm::damiao_motor::CallbackMode::STATE);
205201
openarm_->enable_all();
206202
std::this_thread::sleep_for(std::chrono::milliseconds(100));
207203
openarm_->recv_all();
208204

209-
// Return to zero position smoothly
205+
// Return to zero position
210206
return_to_zero();
211207

212208
RCLCPP_INFO(rclcpp::get_logger("OpenArm_v10HW"), "OpenArm V10 activated");
@@ -220,6 +216,7 @@ hardware_interface::CallbackReturn OpenArm_v10HW::on_deactivate(
220216

221217
// Disable all motors (like full_arm.cpp exit)
222218
openarm_->disable_all();
219+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
223220
openarm_->recv_all();
224221

225222
RCLCPP_INFO(rclcpp::get_logger("OpenArm_v10HW"), "OpenArm V10 deactivated");
@@ -229,7 +226,6 @@ hardware_interface::CallbackReturn OpenArm_v10HW::on_deactivate(
229226
hardware_interface::return_type OpenArm_v10HW::read(
230227
const rclcpp::Time& /*time*/, const rclcpp::Duration& /*period*/) {
231228
// Receive all motor states
232-
openarm_->set_callback_mode_all(openarm::damiao_motor::CallbackMode::STATE);
233229
openarm_->refresh_all();
234230
openarm_->recv_all();
235231

@@ -245,13 +241,12 @@ hardware_interface::return_type OpenArm_v10HW::read(
245241
if (hand_ && joint_names_.size() > ARM_DOF) {
246242
const auto& gripper_motors = openarm_->get_gripper().get_motors();
247243
if (!gripper_motors.empty()) {
248-
// TODO the mappings are unimplemented, need to actually implement the
249-
// mappings for pos, vel, tau Convert motor position (radians) to joint
250-
// value (0-1)
244+
// TODO the mappings are approximates
245+
// Convert motor position (radians) to joint value (0-0.044m)
251246
double motor_pos = gripper_motors[0].get_position();
252247
pos_states_[ARM_DOF] = motor_radians_to_joint(motor_pos);
253248

254-
// Velocity and torque can be passed through directly for now
249+
// Unimplemented: Velocity and torque mapping
255250
vel_states_[ARM_DOF] = 0; // gripper_motors[0].get_velocity();
256251
tau_states_[ARM_DOF] = 0; // gripper_motors[0].get_torque();
257252
}
@@ -271,13 +266,12 @@ hardware_interface::return_type OpenArm_v10HW::write(
271266
openarm_->get_arm().mit_control_all(arm_params);
272267
// Control gripper if enabled
273268
if (hand_ && joint_names_.size() > ARM_DOF) {
274-
// TODO the mappings are unimplemented.
275-
// Convert joint value (0-1) to motor position (radians)
269+
// TODO the true mappings are unimplemented.
276270
double motor_command = joint_to_motor_radians(pos_commands_[ARM_DOF]);
277-
openarm_->get_gripper().mit_control_all({{5.0, 1.0, motor_command, 0, 0}});
271+
openarm_->get_gripper().mit_control_all(
272+
{{GRIPPER_DEFAULT_KP, GRIPPER_DEFAULT_KD, motor_command, 0, 0}});
278273
}
279-
std::this_thread::sleep_for(std::chrono::microseconds(300));
280-
openarm_->recv_all();
274+
openarm_->recv_all(1000);
281275
return hardware_interface::return_type::OK;
282276
}
283277

@@ -294,23 +288,26 @@ void OpenArm_v10HW::return_to_zero() {
294288

295289
// Return gripper to zero if enabled
296290
if (hand_) {
297-
openarm_->get_gripper().mit_control_all({{5.0, 1.0, 0.0, 0.0, 0.0}});
291+
openarm_->get_gripper().mit_control_all(
292+
{{GRIPPER_DEFAULT_KP, GRIPPER_DEFAULT_KD, GRIPPER_JOINT_0_POSITION, 0.0,
293+
0.0}});
298294
}
299-
300-
// // Wait for motors to settle
301-
std::this_thread::sleep_for(std::chrono::milliseconds(100));
295+
std::this_thread::sleep_for(std::chrono::microseconds(1000));
302296
openarm_->recv_all();
303297
}
304298

305299
// Gripper mapping helper functions
306300
double OpenArm_v10HW::joint_to_motor_radians(double joint_value) {
307-
// Joint 0=closed -> motor 0 rad, Joint 1=open -> motor -1.0472 rad
308-
return joint_value * (-1.0472); // Scale from 0-1 to 0 to -1.0472
301+
// Joint 0=closed -> motor 0 rad, Joint 0.044=open -> motor -1.0472 rad
302+
return (joint_value / GRIPPER_JOINT_0_POSITION) *
303+
GRIPPER_MOTOR_1_RADIANS; // Scale from 0-0.044 to 0 to -1.0472
309304
}
310305

311306
double OpenArm_v10HW::motor_radians_to_joint(double motor_radians) {
312-
// Motor 0 rad=closed -> joint 0, Motor -1.0472 rad=open -> joint 1
313-
return motor_radians / (-1.0472); // Scale from 0 to -1.0472 to 0-1
307+
// Motor 0 rad=closed -> joint 0, Motor -1.0472 rad=open -> joint 0.044
308+
return GRIPPER_JOINT_0_POSITION *
309+
(motor_radians /
310+
GRIPPER_MOTOR_1_RADIANS); // Scale from 0 to -1.0472 to 0-0.044
314311
}
315312

316313
} // namespace openarm_hardware

0 commit comments

Comments
 (0)