Skip to content

Commit a9d90c4

Browse files
committed
Refactor JointSaturationLimiter to use member variables
Move temporary calculation buffers and limit status vectors from local scope to class members to avoid repeated heap allocations and simplify data access across refactored methods. Split logic into dedicated methods for clamping and braking. Signed-off-by: Sachin Kumar <sachinkum123567@gmail.com>
1 parent 2296fd8 commit a9d90c4

2 files changed

Lines changed: 255 additions & 165 deletions

File tree

joint_limits/include/joint_limits/joint_saturation_limiter.hpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ class JointSaturationLimiter : public JointLimiterInterface<JointLimitsStateData
5050
bool on_configure(const JointLimitsStateDataType & current_joint_states) override
5151
{
5252
prev_command_ = current_joint_states;
53+
const size_t num_joints = this->number_of_joints_;
54+
55+
desired_pos_.assign(num_joints, 0.0);
56+
desired_vel_.assign(num_joints, 0.0);
57+
desired_acc_.assign(num_joints, 0.0);
58+
expected_pos_.assign(num_joints, 0.0);
59+
expected_vel_.assign(num_joints, 0.0);
60+
61+
pos_limit_hit_.assign(num_joints, false);
62+
vel_limit_hit_.assign(num_joints, false);
63+
acc_limit_hit_.assign(num_joints, false);
64+
dec_limit_hit_.assign(num_joints, false);
5365
return true;
5466
}
5567

@@ -91,6 +103,41 @@ class JointSaturationLimiter : public JointLimiterInterface<JointLimitsStateData
91103
rclcpp::Clock::SharedPtr clock_;
92104
JointLimitsStateDataType prev_command_;
93105
std::mutex mutex_;
106+
107+
private:
108+
// Cached vectors to eliminate dynamic memory allocation (malloc) in the real-time execution loop
109+
std::vector<double> desired_pos_;
110+
std::vector<double> desired_vel_;
111+
std::vector<double> desired_acc_;
112+
std::vector<double> expected_vel_;
113+
std::vector<double> expected_pos_;
114+
115+
// Pre-allocation boolean flags for tracking limits
116+
std::vector<bool> pos_limit_hit_;
117+
std::vector<bool> vel_limit_hit_;
118+
std::vector<bool> acc_limit_hit_;
119+
std::vector<bool> dec_limit_hit_;
120+
121+
/**
122+
* @brief
123+
* Clamps the joint limits
124+
*/
125+
void clamp_joint_limits(
126+
const bool has_desired_position, const bool has_desired_velocity,
127+
const bool has_desired_acceleration, const bool has_current_velocity,
128+
const trajectory_msgs::msg::JointTrajectoryPoint & current_joint_states,
129+
trajectory_msgs::msg::JointTrajectoryPoint & desired_joint_states, bool & limits_enforced,
130+
const std::vector<double> & current_joint_velocities,
131+
bool & braking_near_position_limit_triggered, const double dt_seconds);
132+
133+
/**
134+
* @brief
135+
* Handles the braking near position limit
136+
*/
137+
void handle_braking_near_position_limit(
138+
const std::vector<double> & current_joint_velocities, double dt_seconds,
139+
bool has_desired_position, bool has_desired_velocity,
140+
const trajectory_msgs::msg::JointTrajectoryPoint & current_joint_states);
94141
};
95142

96143
template <typename JointLimitsStateDataType>
@@ -114,6 +161,22 @@ bool JointSaturationLimiter<JointLimitsStateDataType>::on_init()
114161
template <>
115162
bool JointSaturationLimiter<JointControlInterfacesData>::on_init();
116163

164+
template <>
165+
void JointSaturationLimiter<trajectory_msgs::msg::JointTrajectoryPoint>::clamp_joint_limits(
166+
const bool has_desired_position, const bool has_desired_velocity,
167+
const bool has_desired_acceleration, const bool has_current_velocity,
168+
const trajectory_msgs::msg::JointTrajectoryPoint & current_joint_states,
169+
trajectory_msgs::msg::JointTrajectoryPoint & desired_joint_states, bool & limits_enforced,
170+
const std::vector<double> & current_joint_velocities,
171+
bool & braking_near_position_limit_triggered, const double dt_seconds);
172+
173+
template <>
174+
void JointSaturationLimiter<trajectory_msgs::msg::JointTrajectoryPoint>::
175+
handle_braking_near_position_limit(
176+
const std::vector<double> & current_joint_velocities, double dt_seconds,
177+
bool has_desired_position, bool has_desired_velocity,
178+
const trajectory_msgs::msg::JointTrajectoryPoint & current_joint_states);
179+
117180
} // namespace joint_limits
118181

119182
#endif // JOINT_LIMITS__JOINT_SATURATION_LIMITER_HPP_

0 commit comments

Comments
 (0)