Skip to content

Commit 990f002

Browse files
committed
fix: dq assignment and num_joint usage
1 parent b682642 commit 990f002

2 files changed

Lines changed: 19 additions & 19 deletions

File tree

include/crisp_controllers/cartesian_controller.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class CartesianController : public controller_interface::ControllerInterface {
118118
* @brief Get the current state of the robot from hardware interfaces and update internal variables
119119
* @param filter_measurements Whether to apply exponential moving average filtering to the measurements
120120
*/
121-
void updateCurrentState(bool filter_measurements = true);
121+
void updateCurrentState(bool initialize = false);
122122

123123
/**
124124
* @brief Reads the target pose in realtime loop from the buffer and parses it to be used in the controller.
@@ -135,8 +135,6 @@ class CartesianController : public controller_interface::ControllerInterface {
135135
*/
136136
void parse_target_wrench_();
137137

138-
size_t num_joints_;
139-
140138
bool new_target_pose_;
141139
bool new_target_joint_;
142140
bool new_target_wrench_;

src/cartesian_controller.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ CartesianController::update(const rclcpp::Time & time, const rclcpp::Duration &
189189
tau_d = exponential_moving_average(tau_d, tau_previous, params_.filter.output_torque);
190190

191191
if (!params_.stop_commands) {
192-
for (size_t i = 0; i < num_joints_; ++i) {
192+
for (size_t i = 0; i < params_.joints.size(); ++i) {
193193
#if ROS2_VERSION_ABOVE_HUMBLE
194194
(void)command_interfaces_[i].set_value(tau_d[i]);
195195
#else
@@ -293,7 +293,6 @@ CartesianController::on_configure(const rclcpp_lifecycle::State & /*previous_sta
293293
}
294294

295295
// Preallocate the matrices and vectors that will be used in the control loop
296-
num_joints_ = params_.joints.size();
297296
end_effector_frame_id = model_.getFrameId(params_.end_effector_frame);
298297
q = Eigen::VectorXd::Zero(model_.nv);
299298
q_pin = Eigen::VectorXd::Zero(model_.nq);
@@ -463,27 +462,28 @@ void CartesianController::setStiffnessAndDamping() {
463462
}
464463
}
465464

466-
void CartesianController::updateCurrentState(bool filter_measurements) {
467-
for (size_t i = 0; i < num_joints_; i++) {
465+
void CartesianController::updateCurrentState(bool initialize) {
466+
auto num_joints = params_.joints.size();
467+
for (size_t i = 0; i < num_joints; i++) {
468468
auto joint_name = params_.joints[i];
469469
auto joint_id = model_.getJointId(joint_name); // pinocchio joind id might be different
470470
auto joint = model_.joints[joint_id];
471471

472472
#if ROS2_VERSION_ABOVE_HUMBLE
473473
double q_meas = state_interfaces_[i].get_optional().value_or(q[i]);
474-
double dq_meas = state_interfaces_[num_joints_ + i].get_optional().value_or(dq[i]);
474+
double dq_meas = state_interfaces_[num_joints + i].get_optional().value_or(dq[i]);
475475
#else
476476
double q_meas = state_interfaces_[i].get_value();
477-
double dq_meas = state_interfaces_[num_joints_ + i].get_value();
477+
double dq_meas = state_interfaces_[num_joints + i].get_value();
478478
#endif
479479

480-
q_ref[i] = filter_measurements
481-
? exponential_moving_average(q_ref[i], q_target[i], params_.filter.q_ref)
482-
: q_meas;
480+
q_ref[i] = initialize
481+
? q_meas
482+
: exponential_moving_average(q_ref[i], q_target[i], params_.filter.q_ref);
483483

484-
q[i] = filter_measurements
485-
? exponential_moving_average(q[i], q_meas, params_.filter.q)
486-
: q_meas;
484+
q[i] = initialize
485+
? q_meas
486+
: exponential_moving_average(q[i], q_meas, params_.filter.q);
487487

488488
if (continous_joint_types.count(joint.shortname())) { // Then we are handling a continous
489489
// joint that is SO(2)
@@ -493,9 +493,11 @@ void CartesianController::updateCurrentState(bool filter_measurements) {
493493
q_pin[joint.idx_q()] = q[i];
494494
}
495495

496-
dq[i] = filter_measurements
497-
? exponential_moving_average(dq[i], dq_meas, params_.filter.dq)
498-
: dq_meas;
496+
dq[i] = initialize
497+
? dq_meas
498+
: exponential_moving_average(dq[i], dq_meas, params_.filter.dq);
499+
500+
q_target[i] = initialize ? q_meas : q_target[i];
499501
}
500502
}
501503

@@ -504,7 +506,7 @@ CartesianController::on_activate(const rclcpp_lifecycle::State & /*previous_stat
504506

505507
// Update the current state with initial measurements (no EMA filtering)
506508
// to avoid large initial errors
507-
updateCurrentState(false);
509+
updateCurrentState(true);
508510

509511
pinocchio::forwardKinematics(model_, data_, q_pin, dq);
510512
pinocchio::updateFramePlacements(model_, data_);

0 commit comments

Comments
 (0)