Skip to content

Commit efe7f89

Browse files
fix: controllers for rolling
1 parent 4e67d51 commit efe7f89

4 files changed

Lines changed: 42 additions & 25 deletions

File tree

src/cartesian_controller.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ CartesianController::update(const rclcpp::Time &time,
6565

6666
/*q[i] = exponential_moving_average(q[i], state_interfaces_[i].get_value(),*/
6767
/* params_.filter.q);*/
68-
q[i] = state_interfaces_[i].get_value();
68+
auto q_opt = state_interfaces_[i].get_optional();
69+
q[i] = q_opt.value_or(0.0);
6970
if (continous_joint_types.count(
7071
joint.shortname())) { // Then we are handling a continous
7172
// joint that is SO(2)
@@ -77,7 +78,8 @@ CartesianController::update(const rclcpp::Time &time,
7778
/*dq[i] = exponential_moving_average(*/
7879
/* dq[i], state_interfaces_[num_joints + i].get_value(),*/
7980
/* params_.filter.dq);*/
80-
dq[i] = state_interfaces_[num_joints + i].get_value();
81+
auto dq_opt = state_interfaces_[num_joints + i].get_optional();
82+
dq[i] = dq_opt.value_or(0.0);
8183
}
8284

8385
if (new_target_pose_) {parse_target_pose_(); new_target_pose_ = false;}
@@ -202,7 +204,9 @@ CartesianController::update(const rclcpp::Time &time,
202204

203205
if (not params_.stop_commands) {
204206
for (size_t i = 0; i < num_joints; ++i) {
205-
command_interfaces_[i].set_value(tau_d[i]);
207+
if (!command_interfaces_[i].set_value(tau_d[i])) {
208+
RCLCPP_WARN(get_node()->get_logger(), "Failed to set command value for joint %d", i);
209+
}
206210
}
207211
}
208212

@@ -456,7 +460,9 @@ CallbackReturn CartesianController::on_activate(
456460
model_.getJointId(joint_name); // pinocchio joind id might be different
457461
auto joint = model_.joints[joint_id];
458462

459-
q[i] = state_interfaces_[i].get_value();
463+
auto q_opt = state_interfaces_[i].get_optional();
464+
auto dq_opt = state_interfaces_[num_joints + i].get_optional();
465+
q[i] = q_opt.value_or(0.0);
460466
if (joint.shortname() == "JointModelRZ") { // simple revolute joint case
461467
q_pin[joint.idx_q()] = q[i];
462468
} else if (continous_joint_types.count(
@@ -466,10 +472,10 @@ CallbackReturn CartesianController::on_activate(
466472
q_pin[joint.idx_q() + 1] = std::sin(q[i]);
467473
}
468474

469-
q_ref[i] = state_interfaces_[i].get_value();
475+
q_ref[i] = q_opt.value_or(0.0);
470476

471-
dq[i] = state_interfaces_[num_joints + i].get_value();
472-
dq_ref[i] = state_interfaces_[num_joints + i].get_value();
477+
dq[i] = dq_opt.value_or(0.0);
478+
dq_ref[i] = dq_opt.value_or(0.0);
473479
}
474480

475481
pinocchio::forwardKinematics(model_, data_, q_pin, dq);

src/pose_broadcaster.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ PoseBroadcaster::update(const rclcpp::Time &time,
4646
auto joint_id = model_.getJointId(joint_name);
4747
auto joint = model_.joints[joint_id];
4848

49-
q[i] = state_interfaces_[i].get_value();
49+
auto q_opt = state_interfaces_[i].get_optional();
50+
q[i] = q_opt.value_or(0.0);
5051
if (continous_joint_types.count(joint.shortname())) { // Then we are handling a continous joint that is SO(2)
5152
q_pin[joint.idx_q()] = std::cos(q[i]);
5253
q_pin[joint.idx_q()+1] = std::sin(q[i]);
@@ -70,10 +71,9 @@ PoseBroadcaster::update(const rclcpp::Time &time,
7071
should_publish = time_since_last >= min_interval;
7172
}
7273

73-
if (should_publish && realtime_pose_publisher_ && realtime_pose_publisher_->trylock())
74+
if (should_publish && realtime_pose_publisher_)
7475
{
75-
auto & pose_msg = realtime_pose_publisher_->msg_;
76-
76+
geometry_msgs::msg::PoseStamped pose_msg;
7777
pose_msg.header.stamp = time;
7878
pose_msg.header.frame_id = params_.base_frame;
7979
pose_msg.pose.position.x = current_pose.translation()[0];
@@ -83,8 +83,10 @@ PoseBroadcaster::update(const rclcpp::Time &time,
8383
pose_msg.pose.orientation.y = current_quaternion.y();
8484
pose_msg.pose.orientation.z = current_quaternion.z();
8585
pose_msg.pose.orientation.w = current_quaternion.w();
86-
realtime_pose_publisher_->unlockAndPublish();
87-
last_publish_time_ = time;
86+
87+
if (realtime_pose_publisher_->try_publish(pose_msg)) {
88+
last_publish_time_ = time;
89+
}
8890
}
8991

9092
return controller_interface::return_type::OK;

src/torque_feedback_controller.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ TorqueFeedbackController::update(const rclcpp::Time & /*time*/,
4848
const rclcpp::Duration & /*period*/) {
4949
// Update joint states
5050
for (int i = 0; i < num_joints_; i++) {
51-
q_[i] = state_interfaces_[i].get_value();
52-
dq_[i] = state_interfaces_[num_joints_ + i].get_value();
51+
auto q_opt = state_interfaces_[i].get_optional();
52+
auto dq_opt = state_interfaces_[num_joints_ + i].get_optional();
53+
q_[i] = q_opt.value_or(0.0);
54+
dq_[i] = dq_opt.value_or(0.0);
5355
}
5456

5557
// Compute forward kinematics and jacobian
@@ -109,7 +111,9 @@ TorqueFeedbackController::update(const rclcpp::Time & /*time*/,
109111
tau_commanded_ = tau_d + tau_f + tau_nullspace;
110112

111113
for (int i = 0; i < num_joints_; i++) {
112-
command_interfaces_[i].set_value(tau_commanded_[i]);
114+
if (!command_interfaces_[i].set_value(tau_commanded_[i])) {
115+
RCLCPP_WARN(get_node()->get_logger(), "Failed to set command value for joint %d", i);
116+
}
113117
}
114118

115119
params_listener_->refresh_dynamic_parameters();
@@ -248,8 +252,10 @@ CallbackReturn TorqueFeedbackController::on_configure(
248252
CallbackReturn TorqueFeedbackController::on_activate(
249253
const rclcpp_lifecycle::State & /*previous_state*/) {
250254
for (int i = 0; i < num_joints_; i++) {
251-
q_[i] = state_interfaces_[i].get_value();
252-
dq_[i] = state_interfaces_[num_joints_ + i].get_value();
255+
auto q_opt = state_interfaces_[i].get_optional();
256+
auto dq_opt = state_interfaces_[num_joints_ + i].get_optional();
257+
q_[i] = q_opt.value_or(0.0);
258+
dq_[i] = dq_opt.value_or(0.0);
253259
tau_ext_[i] = 0.0;
254260
q_init_[i] = q_[i];
255261
}

src/twist_broadcaster.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ TwistBroadcaster::update(const rclcpp::Time &time,
4848
auto joint_id = model_.getJointId(joint_name);
4949
auto joint = model_.joints[joint_id];
5050

51-
q[i] = state_interfaces_[i*2].get_value();
52-
q_dot[i] = state_interfaces_[i*2+1].get_value();
51+
auto q_opt = state_interfaces_[i*2].get_optional();
52+
auto q_dot_opt = state_interfaces_[i*2+1].get_optional();
53+
q[i] = q_opt.value_or(0.0);
54+
q_dot[i] = q_dot_opt.value_or(0.0);
5355

5456
if (continous_joint_types.count(joint.shortname())) { // Then we are handling a continous joint that is SO(2)
5557
q_pin[joint.idx_q()] = std::cos(q[i]);
@@ -74,10 +76,9 @@ TwistBroadcaster::update(const rclcpp::Time &time,
7476
should_publish = time_since_last >= min_interval;
7577
}
7678

77-
if (should_publish && realtime_twist_publisher_ && realtime_twist_publisher_->trylock())
79+
if (should_publish && realtime_twist_publisher_)
7880
{
79-
auto & twist_msg = realtime_twist_publisher_->msg_;
80-
81+
geometry_msgs::msg::TwistStamped twist_msg;
8182
twist_msg.header.stamp = time;
8283
twist_msg.header.frame_id = params_.end_effector_frame;
8384
twist_msg.twist.linear.x = current_velocity.linear()[0];
@@ -86,8 +87,10 @@ TwistBroadcaster::update(const rclcpp::Time &time,
8687
twist_msg.twist.angular.x = current_velocity.angular()[0];
8788
twist_msg.twist.angular.y = current_velocity.angular()[1];
8889
twist_msg.twist.angular.z = current_velocity.angular()[2];
89-
realtime_twist_publisher_->unlockAndPublish();
90-
last_publish_time_ = time;
90+
91+
if (realtime_twist_publisher_->try_publish(twist_msg)) {
92+
last_publish_time_ = time;
93+
}
9194
}
9295

9396
return controller_interface::return_type::OK;

0 commit comments

Comments
 (0)