Skip to content

Commit 74ae3b9

Browse files
Rename pid_adapter variable and update gains with every new trajectory
1 parent a9a2cbf commit 74ae3b9

File tree

8 files changed

+28
-26
lines changed

8 files changed

+28
-26
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
@@ -284,7 +284,7 @@ controller_interface::return_type JointTrajectoryController::update(
284284
// set values for next hardware write() if tolerance is met
285285
if (!tolerance_violated_while_moving && within_goal_time)
286286
{
287-
if (use_closed_loop_pid_adapter_)
287+
if (use_closed_loop_control_law_)
288288
{
289289
traj_contr_->computeCommands(
290290
tmp_command_, state_current_, state_error_, state_desired_, time, period);
@@ -297,7 +297,7 @@ controller_interface::return_type JointTrajectoryController::update(
297297
}
298298
if (has_velocity_command_interface_)
299299
{
300-
if (use_closed_loop_pid_adapter_)
300+
if (use_closed_loop_control_law_)
301301
{
302302
assign_interface_from_point(joint_command_interface_[1], tmp_command_);
303303
}
@@ -705,13 +705,13 @@ controller_interface::CallbackReturn JointTrajectoryController::on_configure(
705705
contains_interface_type(params_.command_interfaces, hardware_interface::HW_IF_EFFORT);
706706

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

714-
if (use_closed_loop_pid_adapter_)
714+
if (use_closed_loop_control_law_)
715715
{
716716
try
717717
{
@@ -994,14 +994,6 @@ controller_interface::CallbackReturn JointTrajectoryController::on_activate(
994994
cmd_timeout_ = 0.0;
995995
}
996996

997-
// update gains of PID controller
998-
if (use_closed_loop_pid_adapter_)
999-
{
1000-
trajectory_msgs::msg::JointTrajectory traj;
1001-
traj.points.push_back(state_current_);
1002-
traj_contr_->computeGains(traj);
1003-
}
1004-
1005997
return CallbackReturn::SUCCESS;
1006998
}
1007999

@@ -1468,6 +1460,12 @@ void JointTrajectoryController::add_new_trajectory_msg(
14681460
const std::shared_ptr<trajectory_msgs::msg::JointTrajectory> & traj_msg)
14691461
{
14701462
traj_msg_external_point_ptr_.writeFromNonRT(traj_msg);
1463+
1464+
// update gains of controller
1465+
if (use_closed_loop_control_law_)
1466+
{
1467+
traj_contr_->computeGains(traj_msg);
1468+
}
14711469
}
14721470

14731471
void JointTrajectoryController::preempt_active_goal()

joint_trajectory_controller/test/test_trajectory_controller.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ TEST_P(TrajectoryControllerTestParameterized, use_closed_loop_pid)
847847
!traj_controller_->is_open_loop()) ||
848848
traj_controller_->has_effort_command_interface())
849849
{
850-
EXPECT_TRUE(traj_controller_->use_closed_loop_pid_adapter());
850+
EXPECT_TRUE(traj_controller_->use_closed_loop_control_law());
851851
}
852852
}
853853

@@ -904,7 +904,7 @@ TEST_P(TrajectoryControllerTestParameterized, velocity_error)
904904
}
905905
// is the velocity error correct?
906906
if (
907-
traj_controller_->use_closed_loop_pid_adapter() // always needed for PID controller
907+
traj_controller_->use_closed_loop_control_law() // always needed for PID controller
908908
|| (traj_controller_->has_velocity_state_interface() &&
909909
traj_controller_->has_velocity_command_interface()))
910910
{
@@ -969,8 +969,8 @@ TEST_P(TrajectoryControllerTestParameterized, test_jumbled_joint_order)
969969

970970
if (traj_controller_->has_velocity_command_interface())
971971
{
972-
// if use_closed_loop_pid_adapter_==false: we expect desired velocities from direct sampling
973-
// if use_closed_loop_pid_adapter_==true: we expect desired velocities, because we use PID with
972+
// if use_closed_loop_control_law_==false: we expect desired velocities from direct sampling
973+
// if use_closed_loop_control_law_==true: we expect desired velocities, because we use PID with
974974
// feedforward term only
975975
EXPECT_GT(0.0, joint_vel_[0]);
976976
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

@@ -275,7 +275,7 @@ class TrajectoryControllerTest : public ::testing::Test
275275

276276
// set pid parameters before activate. The PID plugin has to be loaded already, otherwise
277277
// parameters are not declared yet
278-
if (traj_controller_->use_closed_loop_pid_adapter())
278+
if (traj_controller_->use_closed_loop_control_law())
279279
{
280280
SetPidParameters(k_p, ff);
281281
}
@@ -545,7 +545,7 @@ class TrajectoryControllerTest : public ::testing::Test
545545
// i.e., active but trivial trajectory (one point only)
546546
EXPECT_TRUE(traj_controller_->has_trivial_traj());
547547

548-
if (traj_controller_->use_closed_loop_pid_adapter() == false)
548+
if (traj_controller_->use_closed_loop_control_law() == false)
549549
{
550550
if (traj_controller_->has_position_command_interface())
551551
{

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)