Skip to content

Commit 6792f6f

Browse files
Felix ExnerRobertWilbrandt
authored andcommitted
Improve scaling exchange with hardware
1 parent ef0738b commit 6792f6f

File tree

2 files changed

+53
-21
lines changed

2 files changed

+53
-21
lines changed

joint_trajectory_controller/src/joint_trajectory_controller.cpp

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

122126
controller_interface::InterfaceConfiguration
123127
JointTrajectoryController::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

142150
controller_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
}

joint_trajectory_controller/src/joint_trajectory_controller_parameters.yaml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,22 @@ joint_trajectory_controller:
3939
"joint_trajectory_controller::state_interface_type_combinations": null,
4040
}
4141
}
42-
exchange_scaling_factor_with_hardware: {
43-
type: bool,
44-
default_value: false,
45-
description: "Flag that mark is hardware supports setting and reading of scaling factor.\n
46-
If set to 'true' corresponding command and state interfaces are created."
47-
}
4842
scaling_factor_initial_default: {
4943
type: double,
5044
default_value: 1.0,
5145
description: "The initial value of the scaling factor if not exchange with hardware takes place."
5246
}
53-
speed_scaling_interface_name: {
47+
speed_scaling_state_interface_name: {
5448
type: string,
55-
default_value: "speed_scaling/speed_scaling_factor",
56-
description: "Fully qualified name of the speed scaling interface name"
49+
default_value: "",
50+
read_only: true,
51+
description: "Fully qualified name of the speed scaling state interface name"
52+
}
53+
speed_scaling_command_interface_name: {
54+
type: string,
55+
default_value: "",
56+
read_only: true,
57+
description: "Command interface name used for setting the speed scaling factor on the hardware."
5758
}
5859
allow_partial_joints_goal: {
5960
type: bool,

0 commit comments

Comments
 (0)