Skip to content

Commit 15ae324

Browse files
committed
add limiting
1 parent 1f825e9 commit 15ae324

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

joint_state_topic_hardware_interface/include/joint_state_topic_hardware_interface/joint_state_topic_hardware_interface.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,25 @@ namespace joint_state_topic_hardware_interface
3333
{
3434
using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;
3535

36+
struct VelocityLimits
37+
{
38+
explicit VelocityLimits() = default;
39+
explicit VelocityLimits(const hardware_interface::InterfaceInfo& info)
40+
{
41+
if (not info.min.empty())
42+
{
43+
min = std::stod(info.min);
44+
}
45+
if (not info.max.empty())
46+
{
47+
max = std::stod(info.max);
48+
}
49+
}
50+
51+
double min = -std::numeric_limits<double>::infinity();
52+
double max = std::numeric_limits<double>::infinity();
53+
};
54+
3655
class JointStateTopicSystem : public hardware_interface::SystemInterface
3756
{
3857
public:
@@ -70,6 +89,11 @@ class JointStateTopicSystem : public hardware_interface::SystemInterface
7089
std::vector<std::vector<double>> joint_commands_;
7190
std::vector<std::vector<double>> joint_states_;
7291

92+
bool enable_command_limiting_ = false;
93+
std::vector<double> nonlimited_velocity_commands_;
94+
std::vector<double> limited_velocity_commands_;
95+
std::vector<VelocityLimits> velocity_limits_;
96+
7397
// If the difference between the current joint state and joint command is less than this value,
7498
// the joint command will not be published.
7599
double trigger_joint_command_threshold_ = 1e-5;

joint_state_topic_hardware_interface/src/joint_state_topic_hardware_interface.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ CallbackReturn JointStateTopicSystem::on_init(const hardware_interface::Hardware
5757
// Initialize storage for all joints' standard interfaces, regardless of their existence and set all values to nan
5858
joint_commands_.resize(standard_interfaces_.size());
5959
joint_states_.resize(standard_interfaces_.size());
60+
velocity_limits_.resize(get_hardware_info().joints.size());
61+
limited_velocity_commands_.resize(get_hardware_info().joints.size());
62+
nonlimited_velocity_commands_.resize(get_hardware_info().joints.size());
6063
for (auto i = 0u; i < standard_interfaces_.size(); i++)
6164
{
6265
joint_commands_[i].resize(get_hardware_info().joints.size(), 0.0);
@@ -67,7 +70,7 @@ CallbackReturn JointStateTopicSystem::on_init(const hardware_interface::Hardware
6770
for (auto i = 0u; i < get_hardware_info().joints.size(); i++)
6871
{
6972
const auto& component = get_hardware_info().joints[i];
70-
for (const auto& interface : component.state_interfaces)
73+
for (const auto& interface : component.command_interfaces)
7174
{
7275
auto it = std::find(standard_interfaces_.begin(), standard_interfaces_.end(), interface.name);
7376
// If interface name is found in the interfaces list
@@ -80,6 +83,7 @@ CallbackReturn JointStateTopicSystem::on_init(const hardware_interface::Hardware
8083
joint_states_[index][i] = std::stod(interface.initial_value);
8184
joint_commands_[index][i] = std::stod(interface.initial_value);
8285
}
86+
velocity_limits_[i] = VelocityLimits(interface);
8387
}
8488
}
8589
}
@@ -135,9 +139,14 @@ CallbackReturn JointStateTopicSystem::on_init(const hardware_interface::Hardware
135139

136140
// if the values on the `joint_states_topic` are wrapped between -2*pi and 2*pi (like they are in Isaac Sim)
137141
// sum the total joint rotation returned on the `joint_states_` interface
138-
if (get_hardware_parameter("sum_wrapped_joint_states", "false") == "true")
142+
sum_wrapped_joint_states_ =
143+
hardware_interface::parse_bool(get_hardware_parameter("sum_wrapped_joint_states", "false"));
144+
// Clamp the command values to the min/max defined in the <ros2_control> tag for each joint
145+
enable_command_limiting_ = hardware_interface::parse_bool(get_hardware_parameter("enable_command_limiting", "false"));
146+
147+
if (enable_command_limiting_)
139148
{
140-
sum_wrapped_joint_states_ = true;
149+
RCLCPP_WARN(get_node()->get_logger(), "** Joint command limiting is enabled **");
141150
}
142151

143152
return CallbackReturn::SUCCESS;
@@ -269,7 +278,17 @@ hardware_interface::return_type JointStateTopicSystem::write(const rclcpp::Time&
269278
}
270279
else if (interface.name == hardware_interface::HW_IF_VELOCITY)
271280
{
272-
joint_state.velocity.push_back(joint_commands_[VELOCITY_INTERFACE_INDEX][i]);
281+
nonlimited_velocity_commands_[i] = joint_commands_[VELOCITY_INTERFACE_INDEX][i];
282+
if (enable_command_limiting_)
283+
{
284+
limited_velocity_commands_[i] =
285+
std::clamp(nonlimited_velocity_commands_[i], velocity_limits_[i].min, velocity_limits_[i].max);
286+
joint_state.velocity.push_back(limited_velocity_commands_[i]);
287+
}
288+
else
289+
{
290+
joint_state.velocity.push_back(nonlimited_velocity_commands_[i]);
291+
}
273292
}
274293
else if (interface.name == hardware_interface::HW_IF_EFFORT)
275294
{

0 commit comments

Comments
 (0)