Skip to content

Commit aac8306

Browse files
Rename pid_adapter variable and update gains with every new trajectory
1 parent 71b6efb commit aac8306

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
@@ -164,7 +164,7 @@ class JointTrajectoryController : public controller_interface::ControllerInterfa
164164
bool has_effort_command_interface_ = false;
165165

166166
/// If true, a velocity feedforward term plus corrective PID term is used
167-
bool use_closed_loop_pid_adapter_ = false;
167+
bool use_closed_loop_control_law_ = false;
168168
// class loader for actual trajectory controller
169169
std::shared_ptr<
170170
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
@@ -281,7 +281,7 @@ controller_interface::return_type JointTrajectoryController::update(
281281
// set values for next hardware write() if tolerance is met
282282
if (!tolerance_violated_while_moving && within_goal_time)
283283
{
284-
if (use_closed_loop_pid_adapter_)
284+
if (use_closed_loop_control_law_)
285285
{
286286
traj_contr_->computeCommands(
287287
tmp_command_, state_current_, state_error_, state_desired_, time, period);
@@ -294,7 +294,7 @@ controller_interface::return_type JointTrajectoryController::update(
294294
}
295295
if (has_velocity_command_interface_)
296296
{
297-
if (use_closed_loop_pid_adapter_)
297+
if (use_closed_loop_control_law_)
298298
{
299299
assign_interface_from_point(joint_command_interface_[1], tmp_command_);
300300
}
@@ -718,13 +718,13 @@ controller_interface::CallbackReturn JointTrajectoryController::on_configure(
718718
contains_interface_type(params_.command_interfaces, hardware_interface::HW_IF_EFFORT);
719719

720720
// if there is only velocity or if there is effort command interface
721-
// then use also PID adapter
722-
use_closed_loop_pid_adapter_ =
721+
// then use also closed loop controller
722+
use_closed_loop_control_law_ =
723723
(has_velocity_command_interface_ && params_.command_interfaces.size() == 1 &&
724724
!params_.open_loop_control) ||
725725
has_effort_command_interface_;
726726

727-
if (use_closed_loop_pid_adapter_)
727+
if (use_closed_loop_control_law_)
728728
{
729729
try
730730
{
@@ -1023,14 +1023,6 @@ controller_interface::CallbackReturn JointTrajectoryController::on_activate(
10231023
}
10241024
rt_is_holding_.writeFromNonRT(true);
10251025

1026-
// update gains of PID controller
1027-
if (use_closed_loop_pid_adapter_)
1028-
{
1029-
trajectory_msgs::msg::JointTrajectory traj;
1030-
traj.points.push_back(state_current_);
1031-
traj_contr_->computeGains(traj);
1032-
}
1033-
10341026
return CallbackReturn::SUCCESS;
10351027
}
10361028

@@ -1509,6 +1501,12 @@ void JointTrajectoryController::add_new_trajectory_msg(
15091501
const std::shared_ptr<trajectory_msgs::msg::JointTrajectory> & traj_msg)
15101502
{
15111503
traj_msg_external_point_ptr_.writeFromNonRT(traj_msg);
1504+
1505+
// update gains of controller
1506+
if (use_closed_loop_control_law_)
1507+
{
1508+
traj_contr_->computeGains(traj_msg);
1509+
}
15121510
}
15131511

15141512
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
@@ -533,8 +533,8 @@ TEST_P(TrajectoryControllerTestParameterized, position_error_not_normalized)
533533
EXPECT_LT(0.0, state_msg->output.velocities[1]);
534534
EXPECT_LT(0.0, state_msg->output.velocities[2]);
535535

536-
// use_closed_loop_pid_adapter_
537-
if (traj_controller_->use_closed_loop_pid_adapter())
536+
// use_closed_loop_control_law_
537+
if (traj_controller_->use_closed_loop_control_law())
538538
{
539539
// we expect u = k_p * (s_d-s)
540540
EXPECT_NEAR(
@@ -773,8 +773,8 @@ TEST_P(TrajectoryControllerTestParameterized, position_error_normalized)
773773

774774
if (traj_controller_->has_velocity_command_interface())
775775
{
776-
// use_closed_loop_pid_adapter_
777-
if (traj_controller_->use_closed_loop_pid_adapter())
776+
// use_closed_loop_control_law_
777+
if (traj_controller_->use_closed_loop_control_law())
778778
{
779779
// we expect u = k_p * (s_d-s) for positions[0] and positions[1]
780780
EXPECT_NEAR(
@@ -801,8 +801,8 @@ TEST_P(TrajectoryControllerTestParameterized, position_error_normalized)
801801

802802
if (traj_controller_->has_effort_command_interface())
803803
{
804-
// use_closed_loop_pid_adapter_
805-
if (traj_controller_->use_closed_loop_pid_adapter())
804+
// use_closed_loop_control_law_
805+
if (traj_controller_->use_closed_loop_control_law())
806806
{
807807
// we expect u = k_p * (s_d-s) for positions[0] and positions[1]
808808
EXPECT_NEAR(
@@ -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

@@ -906,7 +906,7 @@ TEST_P(TrajectoryControllerTestParameterized, velocity_error)
906906
}
907907
// is the velocity error correct?
908908
if (
909-
traj_controller_->use_closed_loop_pid_adapter() // always needed for PID controller
909+
traj_controller_->use_closed_loop_control_law() // always needed for PID controller
910910
|| (traj_controller_->has_velocity_state_interface() &&
911911
traj_controller_->has_velocity_command_interface()))
912912
{
@@ -972,8 +972,8 @@ TEST_P(TrajectoryControllerTestParameterized, test_jumbled_joint_order)
972972

973973
if (traj_controller_->has_velocity_command_interface())
974974
{
975-
// if use_closed_loop_pid_adapter_==false: we expect desired velocities from direct sampling
976-
// if use_closed_loop_pid_adapter_==true: we expect desired velocities, because we use PID with
975+
// if use_closed_loop_control_law_==false: we expect desired velocities from direct sampling
976+
// if use_closed_loop_control_law_==true: we expect desired velocities, because we use PID with
977977
// feedforward term only
978978
EXPECT_GT(0.0, joint_vel_[0]);
979979
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

@@ -248,7 +248,7 @@ class TrajectoryControllerTest : public ::testing::Test
248248

249249
// set pid parameters before activate. The PID plugin has to be loaded already, otherwise
250250
// parameters are not declared yet
251-
if (traj_controller_->use_closed_loop_pid_adapter())
251+
if (traj_controller_->use_closed_loop_control_law())
252252
{
253253
SetPidParameters(k_p, ff);
254254
}
@@ -466,7 +466,7 @@ class TrajectoryControllerTest : public ::testing::Test
466466
// i.e., active but trivial trajectory (one point only)
467467
EXPECT_TRUE(traj_controller_->has_trivial_traj());
468468

469-
if (traj_controller_->use_closed_loop_pid_adapter() == false)
469+
if (traj_controller_->use_closed_loop_control_law() == false)
470470
{
471471
if (traj_controller_->has_position_command_interface())
472472
{

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)