@@ -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