Skip to content

Commit a97511b

Browse files
Use command joints instead of joints for gains structure
1 parent 581f7b2 commit a97511b

File tree

7 files changed

+44
-23
lines changed

7 files changed

+44
-23
lines changed

joint_trajectory_controller/include/joint_trajectory_controller/joint_trajectory_controller.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ class JointTrajectoryController : public controller_interface::ControllerInterfa
134134

135135
// Storing command joint names for interfaces
136136
std::vector<std::string> command_joint_names_;
137+
// TODO(anyone) remove this if there is another way to lock command_joints parameter
138+
rclcpp_lifecycle::LifecycleNode::PreSetParametersCallbackHandle::SharedPtr lock_cmd_joint_names;
137139

138140
// Parameters from ROS for joint_trajectory_controller
139141
std::shared_ptr<ParamListener> param_listener_;

joint_trajectory_controller/src/joint_trajectory_controller.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,36 @@ controller_interface::CallbackReturn JointTrajectoryController::on_configure(
690690
command_joint_names_ = params_.joints;
691691
RCLCPP_INFO(
692692
logger, "No specific joint names are used for command interfaces. Using 'joints' parameter.");
693+
694+
// set the parameter for the controller plugin
695+
auto result =
696+
get_node()->set_parameter(rclcpp::Parameter("command_joints", command_joint_names_));
697+
if (result.successful == false)
698+
{
699+
RCLCPP_ERROR(logger, "Failed to set 'command_joints' parameter");
700+
return CallbackReturn::FAILURE;
701+
}
702+
// TODO(christophfroehlich) how to lock the parameter (set read_only to false)?
703+
// Setting it to read_only but override is not supported
704+
// https://github.com/ros2/rclcpp/issues/1762 get_node()->undeclare_parameter("command_joints");
705+
// rcl_interfaces::msg::ParameterDescriptor parameter_descriptor;
706+
// parameter_descriptor.read_only = true;
707+
// get_node()->declare_parameter("command_joints",
708+
// rclcpp::ParameterValue(command_joint_names_), parameter_descriptor);
709+
lock_cmd_joint_names = get_node()->add_pre_set_parameters_callback(
710+
[this](std::vector<rclcpp::Parameter> & parameters)
711+
{
712+
for (auto & parameter : parameters)
713+
{
714+
if (parameter.get_name() == "command_joints")
715+
{
716+
RCLCPP_ERROR(
717+
get_node()->get_logger(),
718+
"The parameter 'command_joints' is read-only. You can't change it.");
719+
parameter = rclcpp::Parameter("command_joints", command_joint_names_);
720+
}
721+
}
722+
});
693723
}
694724
else if (command_joint_names_.size() != params_.joints.size())
695725
{

joint_trajectory_controller/src/joint_trajectory_controller_parameters.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ joint_trajectory_controller:
1212
type: string_array,
1313
default_value: [],
1414
description: "Names of the commanding joints used by the controller",
15-
read_only: true,
15+
read_only: false, # should be set to true after configuration of the controller
1616
validation: {
1717
unique<>: null,
1818
}

joint_trajectory_controller/test/test_trajectory_controller_utils.hpp

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,32 +86,18 @@ class TestableJointTrajectoryController
8686
return success;
8787
}
8888

89-
void set_joint_names(const std::vector<std::string> & joint_names)
90-
{
91-
params_.joints = joint_names;
92-
}
93-
9489
void set_command_joint_names(const std::vector<std::string> & command_joint_names)
9590
{
9691
command_joint_names_ = command_joint_names;
9792
}
9893

99-
void set_command_interfaces(const std::vector<std::string> & command_interfaces)
100-
{
101-
params_.command_interfaces = command_interfaces;
102-
}
103-
104-
void set_state_interfaces(const std::vector<std::string> & state_interfaces)
105-
{
106-
params_.state_interfaces = state_interfaces;
107-
}
108-
10994
void trigger_declare_parameters() { param_listener_->declare_params(); }
11095

11196
trajectory_msgs::msg::JointTrajectoryPoint get_current_state_when_offset() const
11297
{
11398
return last_commanded_state_;
11499
}
100+
115101
bool has_position_state_interface() const { return has_position_state_interface_; }
116102

117103
bool has_velocity_state_interface() const { return has_velocity_state_interface_; }
@@ -227,12 +213,15 @@ class TrajectoryControllerTest : public ::testing::Test
227213
}
228214
}
229215

216+
/**
217+
* @brief set PIDs for every entry in joint_names_
218+
* be aware to update if PIDs should be configured for different command_joints than joint_names
219+
*/
230220
void SetPidParameters(double p_default = 0.0, double ff_default = 1.0)
231221
{
232222
traj_controller_->trigger_declare_parameters();
233223
auto node = traj_controller_->get_node();
234224

235-
// TODO(christophfroehlich): use command joints instead of joint_names
236225
for (size_t i = 0; i < joint_names_.size(); ++i)
237226
{
238227
const std::string prefix = "gains." + joint_names_[i];

joint_trajectory_controller_plugins/src/pid_trajectory_plugin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void PidTrajectoryPlugin::updateGains()
7575
node_->get_logger(), "[PidTrajectoryPlugin] command_joint_names_ %lu : %s", i,
7676
command_joint_names_[i].c_str());
7777

78-
const auto & gains = params_.gains.joints_map.at(command_joint_names_[i]);
78+
const auto & gains = params_.gains.command_joints_map.at(command_joint_names_[i]);
7979
pids_[i] = std::make_shared<control_toolbox::Pid>(
8080
gains.p, gains.i, gains.d, gains.i_clamp, -gains.i_clamp);
8181
ff_velocity_scale_[i] = gains.ff_velocity_scale;

joint_trajectory_controller_plugins/src/pid_trajectory_plugin_parameters.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
joint_trajectory_controller_plugins:
2-
joints: {
2+
command_joints: {
33
type: string_array,
44
default_value: [],
5-
description: "Names of joints used by the controller",
5+
description: "Names of joints used by the controller plugin",
66
read_only: true,
77
validation: {
88
size_gt<>: [0],
99
}
1010
}
1111
gains:
12-
__map_joints:
12+
__map_command_joints:
1313
p: {
1414
type: double,
1515
default_value: 0.0,

joint_trajectory_controller_plugins/test/test_pid_trajectory.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ TEST_F(PidTrajectoryTest, TestSingleJoint)
3131
auto joint_names_paramv = rclcpp::ParameterValue(joint_names);
3232

3333
// override read_only parameter
34-
node_->declare_parameter("joints", joint_names_paramv);
34+
node_->declare_parameter("command_joints", joint_names_paramv);
3535

3636
ASSERT_TRUE(traj_contr->initialize(node_, joint_names));
3737

@@ -63,7 +63,7 @@ TEST_F(PidTrajectoryTest, TestMultipleJoints)
6363
auto joint_names_paramv = rclcpp::ParameterValue(joint_names);
6464

6565
// override read_only parameter
66-
node_->declare_parameter("joints", joint_names_paramv);
66+
node_->declare_parameter("command_joints", joint_names_paramv);
6767

6868
ASSERT_TRUE(traj_contr->initialize(node_, joint_names));
6969

0 commit comments

Comments
 (0)