@@ -116,12 +116,17 @@ JointTrajectoryController::command_interface_configuration() const
116116 conf.names .push_back (joint_name + " /" + interface_type);
117117 }
118118 }
119+ if (!params_.speed_scaling_command_interface_name .empty ())
120+ {
121+ conf.names .push_back (params_.speed_scaling_command_interface_name );
122+ }
119123 return conf;
120124}
121125
122126controller_interface::InterfaceConfiguration
123127JointTrajectoryController::state_interface_configuration () const
124128{
129+ const auto logger = get_node ()->get_logger ();
125130 controller_interface::InterfaceConfiguration conf;
126131 conf.type = controller_interface::interface_configuration_type::INDIVIDUAL;
127132 conf.names .reserve (dof_ * params_.state_interfaces .size ());
@@ -132,33 +137,36 @@ JointTrajectoryController::state_interface_configuration() const
132137 conf.names .push_back (joint_name + " /" + interface_type);
133138 }
134139 }
135- if (params_.exchange_scaling_factor_with_hardware )
140+ if (! params_.speed_scaling_state_interface_name . empty () )
136141 {
137- conf.names .push_back (params_.speed_scaling_interface_name );
142+ RCLCPP_INFO (
143+ logger, " Using scaling state from the hardware from interface %s." ,
144+ params_.speed_scaling_state_interface_name .c_str ());
145+ conf.names .push_back (params_.speed_scaling_state_interface_name );
138146 }
139147 return conf;
140148}
141149
142150controller_interface::return_type JointTrajectoryController::update (
143151 const rclcpp::Time & time, const rclcpp::Duration & period)
144152{
145- if (params_.exchange_scaling_factor_with_hardware )
153+ if (params_.speed_scaling_state_interface_name .empty ())
154+ {
155+ scaling_factor_ = *(scaling_factor_rt_buff_.readFromRT ());
156+ }
157+ else
146158 {
147- if (state_interfaces_.back ().get_name () == params_.speed_scaling_interface_name )
159+ if (state_interfaces_.back ().get_name () == params_.speed_scaling_state_interface_name )
148160 {
149161 scaling_factor_ = state_interfaces_.back ().get_value ();
150162 }
151163 else
152164 {
153165 RCLCPP_ERROR (
154166 get_node ()->get_logger (), " Speed scaling interface (%s) not found in hardware interface." ,
155- params_.speed_scaling_interface_name .c_str ());
167+ params_.speed_scaling_state_interface_name .c_str ());
156168 }
157169 }
158- else
159- {
160- scaling_factor_ = *(scaling_factor_rt_buff_.readFromRT ());
161- }
162170
163171 if (get_lifecycle_state ().id () == lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE)
164172 {
@@ -494,7 +502,8 @@ bool JointTrajectoryController::read_state_from_command_interfaces(JointTrajecto
494502 auto interface_has_values = [](const auto & joint_interface)
495503 {
496504 return std::find_if (
497- joint_interface.begin (), joint_interface.end (), [](const auto & interface)
505+ joint_interface.begin (), joint_interface.end (),
506+ [](const auto & interface)
498507 { return std::isnan (interface.get ().get_value ()); }) == joint_interface.end ();
499508 };
500509
@@ -564,7 +573,8 @@ bool JointTrajectoryController::read_commands_from_command_interfaces(
564573 auto interface_has_values = [](const auto & joint_interface)
565574 {
566575 return std::find_if (
567- joint_interface.begin (), joint_interface.end (), [](const auto & interface)
576+ joint_interface.begin (), joint_interface.end (),
577+ [](const auto & interface)
568578 { return std::isnan (interface.get ().get_value ()); }) == joint_interface.end ();
569579 };
570580
@@ -908,6 +918,8 @@ controller_interface::CallbackReturn JointTrajectoryController::on_configure(
908918 std::placeholders::_1, std::placeholders::_2));
909919
910920 // set scaling factor to low value default
921+ RCLCPP_INFO (
922+ logger, " Setting initial scaling factor to %2f" , params_.scaling_factor_initial_default );
911923 scaling_factor_rt_buff_.writeFromNonRT (params_.scaling_factor_initial_default );
912924
913925 return CallbackReturn::SUCCESS;
@@ -1624,7 +1636,26 @@ bool JointTrajectoryController::set_scaling_factor(
16241636 }
16251637
16261638 RCLCPP_INFO (get_node ()->get_logger (), " New scaling factor will be %f" , req->scaling_factor );
1627- scaling_factor_rt_buff_.writeFromNonRT (req->scaling_factor );
1639+ if (params_.speed_scaling_command_interface_name .empty ())
1640+ {
1641+ if (!params_.speed_scaling_state_interface_name .empty ())
1642+ {
1643+ RCLCPP_WARN (
1644+ get_node ()->get_logger (),
1645+ " Setting the scaling factor while only one-way communication with the hardware is setup. "
1646+ " This will likely get overwritten by the hardware again. If available, please also setup "
1647+ " the speed_scaling_command_interface_name" );
1648+ }
1649+ scaling_factor_rt_buff_.writeFromNonRT (req->scaling_factor );
1650+ }
1651+ else
1652+ {
1653+ if (get_state ().id () == lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE)
1654+ {
1655+ // TODO(Felix): Use proper adressing here.
1656+ command_interfaces_.back ().set_value (static_cast <double >(req->scaling_factor ));
1657+ }
1658+ }
16281659 resp->success = true ;
16291660 return true ;
16301661}
0 commit comments