Skip to content

Commit 732dfaf

Browse files
Rename pid_adapter variable and update gains with every new trajectory
1 parent fb57496 commit 732dfaf

File tree

8 files changed

+34
-32
lines changed

8 files changed

+34
-32
lines changed

joint_trajectory_controller/include/joint_trajectory_controller/joint_trajectory_controller.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class JointTrajectoryController : public controller_interface::ControllerInterfa
162162
bool has_effort_command_interface_ = false;
163163

164164
/// If true, a velocity feedforward term plus corrective PID term is used
165-
bool use_closed_loop_pid_adapter_ = false;
165+
bool use_closed_loop_control_law_ = false;
166166
// class loader for actual trajectory controller
167167
std::shared_ptr<
168168
pluginlib::ClassLoader<joint_trajectory_controller_plugins::TrajectoryControllerBase>>

joint_trajectory_controller/src/joint_trajectory_controller.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ controller_interface::return_type JointTrajectoryController::update(
283283
// set values for next hardware write() if tolerance is met
284284
if (!tolerance_violated_while_moving && within_goal_time)
285285
{
286-
if (use_closed_loop_pid_adapter_)
286+
if (use_closed_loop_control_law_)
287287
{
288288
traj_contr_->computeCommands(
289289
tmp_command_, state_current_, state_error_, state_desired_, time, period);
@@ -296,7 +296,7 @@ controller_interface::return_type JointTrajectoryController::update(
296296
}
297297
if (has_velocity_command_interface_)
298298
{
299-
if (use_closed_loop_pid_adapter_)
299+
if (use_closed_loop_control_law_)
300300
{
301301
assign_interface_from_point(joint_command_interface_[1], tmp_command_);
302302
}
@@ -704,13 +704,13 @@ controller_interface::CallbackReturn JointTrajectoryController::on_configure(
704704
contains_interface_type(params_.command_interfaces, hardware_interface::HW_IF_EFFORT);
705705

706706
// if there is only velocity or if there is effort command interface
707-
// then use also PID adapter
708-
use_closed_loop_pid_adapter_ =
707+
// then use also closed loop controller
708+
use_closed_loop_control_law_ =
709709
(has_velocity_command_interface_ && params_.command_interfaces.size() == 1 &&
710710
!params_.open_loop_control) ||
711711
has_effort_command_interface_;
712712

713-
if (use_closed_loop_pid_adapter_)
713+
if (use_closed_loop_control_law_)
714714
{
715715
try
716716
{
@@ -1011,14 +1011,6 @@ controller_interface::CallbackReturn JointTrajectoryController::on_activate(
10111011
cmd_timeout_ = 0.0;
10121012
}
10131013

1014-
// update gains of PID controller
1015-
if (use_closed_loop_pid_adapter_)
1016-
{
1017-
trajectory_msgs::msg::JointTrajectory traj;
1018-
traj.points.push_back(state_current_);
1019-
traj_contr_->computeGains(traj);
1020-
}
1021-
10221014
return CallbackReturn::SUCCESS;
10231015
}
10241016

@@ -1497,6 +1489,12 @@ void JointTrajectoryController::add_new_trajectory_msg(
14971489
const std::shared_ptr<trajectory_msgs::msg::JointTrajectory> & traj_msg)
14981490
{
14991491
traj_msg_external_point_ptr_.writeFromNonRT(traj_msg);
1492+
1493+
// update gains of controller
1494+
if (use_closed_loop_control_law_)
1495+
{
1496+
traj_contr_->computeGains(traj_msg);
1497+
}
15001498
}
15011499

15021500
void JointTrajectoryController::preempt_active_goal()

joint_trajectory_controller/test/test_trajectory_controller.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,8 @@ TEST_P(TrajectoryControllerTestParameterized, position_error_not_angle_wraparoun
567567
EXPECT_LT(0.0, state_msg->output.velocities[1]);
568568
EXPECT_LT(0.0, state_msg->output.velocities[2]);
569569

570-
// use_closed_loop_pid_adapter_
571-
if (traj_controller_->use_closed_loop_pid_adapter())
570+
// use_closed_loop_control_law_
571+
if (traj_controller_->use_closed_loop_control_law())
572572
{
573573
// we expect u = k_p * (s_d-s)
574574
EXPECT_NEAR(
@@ -806,8 +806,8 @@ TEST_P(TrajectoryControllerTestParameterized, position_error_angle_wraparound)
806806

807807
if (traj_controller_->has_velocity_command_interface())
808808
{
809-
// use_closed_loop_pid_adapter_
810-
if (traj_controller_->use_closed_loop_pid_adapter())
809+
// use_closed_loop_control_law_
810+
if (traj_controller_->use_closed_loop_control_law())
811811
{
812812
// we expect u = k_p * (s_d-s) for positions[0] and positions[1]
813813
EXPECT_NEAR(
@@ -834,8 +834,8 @@ TEST_P(TrajectoryControllerTestParameterized, position_error_angle_wraparound)
834834

835835
if (traj_controller_->has_effort_command_interface())
836836
{
837-
// use_closed_loop_pid_adapter_
838-
if (traj_controller_->use_closed_loop_pid_adapter())
837+
// use_closed_loop_control_law_
838+
if (traj_controller_->use_closed_loop_control_law())
839839
{
840840
// we expect u = k_p * (s_d-s) for positions[0] and positions[1]
841841
EXPECT_NEAR(
@@ -880,7 +880,7 @@ TEST_P(TrajectoryControllerTestParameterized, use_closed_loop_pid)
880880
!traj_controller_->is_open_loop()) ||
881881
traj_controller_->has_effort_command_interface())
882882
{
883-
EXPECT_TRUE(traj_controller_->use_closed_loop_pid_adapter());
883+
EXPECT_TRUE(traj_controller_->use_closed_loop_control_law());
884884
}
885885
}
886886

@@ -939,7 +939,7 @@ TEST_P(TrajectoryControllerTestParameterized, velocity_error)
939939
}
940940
// is the velocity error correct?
941941
if (
942-
traj_controller_->use_closed_loop_pid_adapter() // always needed for PID controller
942+
traj_controller_->use_closed_loop_control_law() // always needed for PID controller
943943
|| (traj_controller_->has_velocity_state_interface() &&
944944
traj_controller_->has_velocity_command_interface()))
945945
{
@@ -1005,8 +1005,8 @@ TEST_P(TrajectoryControllerTestParameterized, test_jumbled_joint_order)
10051005

10061006
if (traj_controller_->has_velocity_command_interface())
10071007
{
1008-
// if use_closed_loop_pid_adapter_==false: we expect desired velocities from direct sampling
1009-
// if use_closed_loop_pid_adapter_==true: we expect desired velocities, because we use PID with
1008+
// if use_closed_loop_control_law_==false: we expect desired velocities from direct sampling
1009+
// if use_closed_loop_control_law_==true: we expect desired velocities, because we use PID with
10101010
// feedforward term only
10111011
EXPECT_GT(0.0, joint_vel_[0]);
10121012
EXPECT_GT(0.0, joint_vel_[1]);

joint_trajectory_controller/test/test_trajectory_controller_utils.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class TestableJointTrajectoryController
126126

127127
bool has_effort_command_interface() const { return has_effort_command_interface_; }
128128

129-
bool use_closed_loop_pid_adapter() const { return use_closed_loop_pid_adapter_; }
129+
bool use_closed_loop_control_law() const { return use_closed_loop_control_law_; }
130130

131131
bool is_open_loop() const { return params_.open_loop_control; }
132132

@@ -255,7 +255,7 @@ class TrajectoryControllerTest : public ::testing::Test
255255

256256
// set pid parameters before activate. The PID plugin has to be loaded already, otherwise
257257
// parameters are not declared yet
258-
if (traj_controller_->use_closed_loop_pid_adapter())
258+
if (traj_controller_->use_closed_loop_control_law())
259259
{
260260
SetPidParameters(k_p, ff);
261261
}
@@ -481,7 +481,7 @@ class TrajectoryControllerTest : public ::testing::Test
481481
// i.e., active but trivial trajectory (one point only)
482482
EXPECT_TRUE(traj_controller_->has_trivial_traj());
483483

484-
if (traj_controller_->use_closed_loop_pid_adapter() == false)
484+
if (traj_controller_->use_closed_loop_control_law() == false)
485485
{
486486
if (traj_controller_->has_position_command_interface())
487487
{

joint_trajectory_controller_plugins/include/joint_trajectory_controller_plugins/pid_trajectory_plugin.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class PidTrajectoryPlugin : public TrajectoryControllerBase
3737
rclcpp_lifecycle::LifecycleNode::SharedPtr node,
3838
std::vector<std::string> command_joint_names) override;
3939

40-
bool computeGains(const trajectory_msgs::msg::JointTrajectory trajectory) override;
40+
bool computeGains(
41+
const std::shared_ptr<trajectory_msgs::msg::JointTrajectory> & trajectory) override;
4142

4243
void computeCommands(
4344
std::vector<double> & tmp_command, const trajectory_msgs::msg::JointTrajectoryPoint current,

joint_trajectory_controller_plugins/include/joint_trajectory_controller_plugins/trajectory_controller_base.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef JOINT_TRAJECTORY_CONTROLLER_PLUGINS__TRAJECTORY_CONTROLLER_BASE_HPP_
1616
#define JOINT_TRAJECTORY_CONTROLLER_PLUGINS__TRAJECTORY_CONTROLLER_BASE_HPP_
1717

18+
#include <memory>
1819
#include <string>
1920
#include <vector>
2021

@@ -47,7 +48,8 @@ class TrajectoryControllerBase
4748
/**
4849
*/
4950
JOINT_TRAJECTORY_CONTROLLER_PLUGINS_PUBLIC
50-
virtual bool computeGains(const trajectory_msgs::msg::JointTrajectory trajectory) = 0;
51+
virtual bool computeGains(
52+
const std::shared_ptr<trajectory_msgs::msg::JointTrajectory> & trajectory) = 0;
5153

5254
/**
5355
*/

joint_trajectory_controller_plugins/src/pid_trajectory_plugin.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ bool PidTrajectoryPlugin::initialize(
5050
return true;
5151
}
5252

53-
bool PidTrajectoryPlugin::computeGains(const trajectory_msgs::msg::JointTrajectory /*trajectory*/)
53+
bool PidTrajectoryPlugin::computeGains(
54+
const std::shared_ptr<trajectory_msgs::msg::JointTrajectory> & /*trajectory*/)
5455
{
5556
if (param_listener_->is_old(params_))
5657
{

joint_trajectory_controller_plugins/test/test_pid_trajectory.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ TEST_F(PidTrajectoryTest, TestSingleJoint)
3939
traj_contr->trigger_declare_parameters();
4040
node_->set_parameter(rclcpp::Parameter("gains.joint1.p", 1.0));
4141

42-
ASSERT_TRUE(traj_contr->computeGains(trajectory_msgs::msg::JointTrajectory()));
42+
ASSERT_TRUE(traj_contr->computeGains(std::make_shared<trajectory_msgs::msg::JointTrajectory>()));
4343

4444
trajectory_msgs::msg::JointTrajectoryPoint traj_msg;
4545
traj_msg.positions.push_back(0.0);
@@ -71,7 +71,7 @@ TEST_F(PidTrajectoryTest, TestMultipleJoints)
7171
node_->set_parameter(rclcpp::Parameter("gains.joint2.p", 1.0));
7272
node_->set_parameter(rclcpp::Parameter("gains.joint3.p", 1.0));
7373

74-
ASSERT_TRUE(traj_contr->computeGains(trajectory_msgs::msg::JointTrajectory()));
74+
ASSERT_TRUE(traj_contr->computeGains(std::make_shared<trajectory_msgs::msg::JointTrajectory>()));
7575

7676
trajectory_msgs::msg::JointTrajectoryPoint traj_msg;
7777
traj_msg.positions.push_back(0.0);

0 commit comments

Comments
 (0)