@@ -74,6 +74,18 @@ controller_interface::InterfaceConfiguration SpeedScalingStateController::state_
7474rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
7575SpeedScalingStateController::on_configure (const rclcpp_lifecycle::State& /* previous_state*/ )
7676{
77+ node_->declare_parameter (" state_publish_rate" );
78+
79+ if (!node_->get_parameter (" state_publish_rate" , publish_rate_))
80+ {
81+ RCLCPP_INFO (get_node ()->get_logger (), " Parameter 'state_publish_rate' not set" );
82+ return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::ERROR;
83+ }
84+ else
85+ {
86+ RCLCPP_INFO (get_node ()->get_logger (), " Publisher rate set to : %.1f Hz" , publish_rate_);
87+ }
88+
7789 try
7890 {
7991 speed_scaling_state_publisher_ =
@@ -96,6 +108,7 @@ SpeedScalingStateController::on_activate(const rclcpp_lifecycle::State& /*previo
96108 return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::ERROR;
97109 }
98110
111+ last_publish_time_ = node_->now ();
99112 return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS;
100113}
101114
@@ -166,22 +179,25 @@ double get_value(const std::unordered_map<std::string, std::unordered_map<std::s
166179
167180controller_interface::return_type SpeedScalingStateController::update ()
168181{
169- for ( const auto & state_interface : state_interfaces_ )
182+ if (publish_rate_ > 0.0 && (node_-> now () - last_publish_time_) > rclcpp::Duration ( 1.0 / publish_rate_, 0.0 ) )
170183 {
171- name_if_value_mapping_[state_interface.get_name ()][state_interface.get_interface_name ()] =
172- state_interface.get_value ();
173- RCLCPP_DEBUG (get_node ()->get_logger (), " %s/%s: %f\n " , state_interface.get_name ().c_str (),
174- state_interface.get_interface_name ().c_str (), state_interface.get_value ());
175- }
176-
177- for (auto i = 0ul ; i < joint_names_.size (); ++i)
178- {
179- speed_scaling_state_msg_.data = get_value (name_if_value_mapping_, joint_names_[i], " speed_scaling_factor" );
180- }
184+ for (const auto & state_interface : state_interfaces_)
185+ {
186+ name_if_value_mapping_[state_interface.get_name ()][state_interface.get_interface_name ()] =
187+ state_interface.get_value ();
188+ RCLCPP_DEBUG (get_node ()->get_logger (), " %s/%s: %f\n " , state_interface.get_name ().c_str (),
189+ state_interface.get_interface_name ().c_str (), state_interface.get_value ());
190+ }
181191
182- // publish
183- speed_scaling_state_publisher_->publish (speed_scaling_state_msg_);
192+ for (auto i = 0ul ; i < joint_names_.size (); ++i)
193+ {
194+ speed_scaling_state_msg_.data = get_value (name_if_value_mapping_, joint_names_[i], " speed_scaling_factor" );
195+ }
184196
197+ // publish
198+ speed_scaling_state_publisher_->publish (speed_scaling_state_msg_);
199+ last_publish_time_ = node_->now ();
200+ }
185201 return controller_interface::return_type::SUCCESS;
186202}
187203
0 commit comments