Skip to content

Commit bf253f1

Browse files
Change the tests to work without deprecated PID settings (#1824)
1 parent 76c07f9 commit bf253f1

File tree

4 files changed

+67
-35
lines changed

4 files changed

+67
-35
lines changed

pid_controller/test/pid_controller_params.yaml

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**:
2+
ros__parameters:
3+
# TODO(christohfroehlich): Remove this global parameters once the deprecated antiwindup parameters are removed.
4+
gains:
5+
joint1: {antiwindup_strategy: "none", i_clamp_max: .inf, i_clamp_min: -.inf}
6+
joint2: {antiwindup_strategy: "none", i_clamp_max: .inf, i_clamp_min: -.inf}
7+
18
test_pid_controller:
29
ros__parameters:
310
dof_names:
@@ -8,7 +15,19 @@ test_pid_controller:
815
reference_and_state_interfaces: ["position"]
916

1017
gains:
11-
joint1: {p: 1.0, i: 2.0, d: 3.0, i_clamp_max: 5.0, i_clamp_min: -5.0}
18+
joint1: {p: 1.0, i: 2.0, d: 3.0, u_clamp_max: 5.0, u_clamp_min: -5.0}
19+
20+
test_pid_controller_unlimited:
21+
ros__parameters:
22+
dof_names:
23+
- joint1
24+
25+
command_interface: position
26+
27+
reference_and_state_interfaces: ["position"]
28+
29+
gains:
30+
joint1: {p: 1.0, i: 2.0, d: 3.0}
1231

1332
test_pid_controller_angle_wraparound_on:
1433
ros__parameters:
@@ -20,7 +39,7 @@ test_pid_controller_angle_wraparound_on:
2039
reference_and_state_interfaces: ["position"]
2140

2241
gains:
23-
joint1: {p: 1.0, i: 2.0, d: 3.0, i_clamp_max: 5.0, i_clamp_min: -5.0, angle_wraparound: true}
42+
joint1: {p: 1.0, i: 2.0, d: 3.0, angle_wraparound: true}
2443

2544
test_pid_controller_with_feedforward_gain:
2645
ros__parameters:
@@ -32,7 +51,7 @@ test_pid_controller_with_feedforward_gain:
3251
reference_and_state_interfaces: ["position"]
3352

3453
gains:
35-
joint1: {p: 0.5, i: 0.0, d: 0.0, i_clamp_max: 5.0, i_clamp_min: -5.0, feedforward_gain: 1.0}
54+
joint1: {p: 0.5, i: 0.0, d: 0.0, feedforward_gain: 1.0}
3655

3756
test_pid_controller_with_feedforward_gain_dual_interface:
3857
ros__parameters:
@@ -45,8 +64,8 @@ test_pid_controller_with_feedforward_gain_dual_interface:
4564
reference_and_state_interfaces: ["position", "velocity"]
4665

4766
gains:
48-
joint1: {p: 0.5, i: 0.3, d: 0.4, i_clamp_max: 5.0, i_clamp_min: -5.0, feedforward_gain: 1.0}
49-
joint2: {p: 0.5, i: 0.3, d: 0.4, i_clamp_max: 5.0, i_clamp_min: -5.0, feedforward_gain: 1.0}
67+
joint1: {p: 0.5, i: 0.3, d: 0.4, feedforward_gain: 1.0}
68+
joint2: {p: 0.5, i: 0.3, d: 0.4, feedforward_gain: 1.0}
5069

5170
test_save_i_term_off:
5271
ros__parameters:
@@ -58,7 +77,7 @@ test_save_i_term_off:
5877
reference_and_state_interfaces: ["position"]
5978

6079
gains:
61-
joint1: {p: 1.0, i: 2.0, d: 3.0, i_clamp_max: 5.0, i_clamp_min: -5.0, save_i_term: false}
80+
joint1: {p: 1.0, i: 2.0, d: 3.0, save_i_term: false}
6281

6382
test_save_i_term_on:
6483
ros__parameters:
@@ -70,4 +89,4 @@ test_save_i_term_on:
7089
reference_and_state_interfaces: ["position"]
7190

7291
gains:
73-
joint1: {p: 1.0, i: 2.0, d: 3.0, i_clamp_max: 5.0, i_clamp_min: -5.0, save_i_term: true}
92+
joint1: {p: 1.0, i: 2.0, d: 3.0, save_i_term: true}

pid_controller/test/test_pid_controller.cpp

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ TEST_F(PidControllerTest, all_parameters_set_configure_success)
4646
ASSERT_EQ(controller_->params_.gains.dof_names_map[dof_name].i, 2.0);
4747
ASSERT_EQ(controller_->params_.gains.dof_names_map[dof_name].d, 3.0);
4848
ASSERT_FALSE(controller_->params_.gains.dof_names_map[dof_name].antiwindup);
49-
ASSERT_EQ(controller_->params_.gains.dof_names_map[dof_name].i_clamp_max, 5.0);
50-
ASSERT_EQ(controller_->params_.gains.dof_names_map[dof_name].i_clamp_min, -5.0);
49+
ASSERT_EQ(controller_->params_.gains.dof_names_map[dof_name].u_clamp_max, 5.0);
50+
ASSERT_EQ(controller_->params_.gains.dof_names_map[dof_name].u_clamp_min, -5.0);
5151
ASSERT_EQ(controller_->params_.gains.dof_names_map[dof_name].feedforward_gain, 0.0);
5252
}
5353
ASSERT_EQ(controller_->params_.command_interface, command_interface_);
@@ -201,7 +201,7 @@ TEST_F(PidControllerTest, reactivate_success)
201201

202202
TEST_F(PidControllerTest, test_update_logic_zero_feedforward_gain)
203203
{
204-
SetUpController();
204+
SetUpController("test_pid_controller_unlimited");
205205
rclcpp::executors::MultiThreadedExecutor executor;
206206
executor.add_node(controller_->get_node()->get_node_base_interface());
207207

@@ -238,10 +238,10 @@ TEST_F(PidControllerTest, test_update_logic_zero_feedforward_gain)
238238
// check the command value:
239239
// ref = 101.101, state = 1.1, ds = 0.01
240240
// error = ref - state = 100.001, error_dot = error/ds = 10000.1,
241-
// p_term = 100.001 * 1, i_term = 1.00001 * 2 = 2.00002, d_term = error/ds = 10000.1 * 3
241+
// p_term = 100.001 * 1, i_term = 0.0 at first update call, d_term = error/ds = 10000.1 * 3
242242
// feedforward ON, feedforward_gain = 0
243-
// -> cmd = p_term + i_term + d_term + feedforward_gain * ref = 30102.3 + 0 * 101.101 = 30102.3
244-
const double expected_command_value = 30102.301020;
243+
// -> cmd = p_term + i_term + d_term + feedforward_gain * ref = 30100.3 + 0 * 101.101 = 30102.3
244+
const double expected_command_value = 30100.301000;
245245

246246
double actual_value =
247247
std::round(controller_->command_interfaces_[0].get_optional().value() * 1e5) / 1e5;
@@ -257,7 +257,7 @@ TEST_F(PidControllerTest, test_update_logic_zero_feedforward_gain)
257257

258258
TEST_F(PidControllerTest, test_update_logic_chainable_not_use_subscriber_update)
259259
{
260-
SetUpController();
260+
SetUpController("test_pid_controller_unlimited");
261261
rclcpp::executors::MultiThreadedExecutor executor;
262262
executor.add_node(controller_->get_node()->get_node_base_interface());
263263

@@ -294,28 +294,29 @@ TEST_F(PidControllerTest, test_update_logic_chainable_not_use_subscriber_update)
294294
// ref = 5.0, state = 1.1, ds = 0.01, p_gain = 1.0, i_gain = 2.0, d_gain = 3.0
295295
// error = ref - state = 5.0 - 1.1 = 3.9, error_dot = error/ds = 3.9/0.01 = 390.0,
296296
// p_term = error * p_gain = 3.9 * 1.0 = 3.9,
297-
// i_term = error * ds * i_gain = 3.9 * 0.01 * 2.0 = 0.078,
297+
// i_term = zero at first update
298298
// d_term = error_dot * d_gain = 390.0 * 3.0 = 1170.0
299299
// feedforward OFF -> cmd = p_term + i_term + d_term = 3.9 + 0.078 + 1170.0 = 1173.978
300-
const double expected_command_value = 1173.978;
301-
302-
EXPECT_EQ(controller_->command_interfaces_[0].get_optional().value(), expected_command_value);
300+
{
301+
const double expected_command_value = 1173.9;
302+
EXPECT_EQ(controller_->command_interfaces_[0].get_optional().value(), expected_command_value);
303+
}
303304
}
304305

305306
/**
306307
* @brief check default calculation with angle_wraparound turned off
307308
*/
308309
TEST_F(PidControllerTest, test_update_logic_angle_wraparound_off)
309310
{
310-
SetUpController();
311+
SetUpController("test_pid_controller_unlimited");
311312
rclcpp::executors::MultiThreadedExecutor executor;
312313
executor.add_node(controller_->get_node()->get_node_base_interface());
313314

314315
ASSERT_EQ(controller_->on_configure(rclcpp_lifecycle::State()), NODE_SUCCESS);
315316
ASSERT_EQ(controller_->on_activate(rclcpp_lifecycle::State()), NODE_SUCCESS);
316317
ASSERT_FALSE(controller_->params_.gains.dof_names_map[dof_names_[0]].angle_wraparound);
317318

318-
// write reference interface so that the values are would be wrapped
319+
// write reference interface so that the values would be wrapped
319320
controller_->reference_interfaces_[0] = 10.0;
320321

321322
// run update
@@ -324,7 +325,13 @@ TEST_F(PidControllerTest, test_update_logic_angle_wraparound_off)
324325
controller_interface::return_type::OK);
325326

326327
// check the result of the commands - the values are not wrapped
327-
const double expected_command_value = 2679.078;
328+
// ref = 10.0, state = 1.1, ds = 0.01, p_gain = 1.0, i_gain = 2.0, d_gain = 3.0
329+
// error = ref - state = 10.0 - 1.1 = 8.9, error_dot = error/ds = 8.9/0.01 = 890.0,
330+
// p_term = error * p_gain = 8.9 * 1.0 = 8.9,
331+
// i_term = zero at first update
332+
// d_term = error_dot * d_gain = 890.0 * 3.0 = 2670.0
333+
// feedforward OFF -> cmd = p_term + i_term + d_term = 8.9 + 0.0 + 2670.0 = 2678.9
334+
const double expected_command_value = 2678.9;
328335
EXPECT_NEAR(
329336
controller_->command_interfaces_[0].get_optional().value(), expected_command_value, 1e-5);
330337
}
@@ -354,8 +361,13 @@ TEST_F(PidControllerTest, test_update_logic_angle_wraparound_on)
354361
controller_->update(rclcpp::Time(0), rclcpp::Duration::from_seconds(0.01)),
355362
controller_interface::return_type::OK);
356363

357-
// Check the command value
358-
const double expected_command_value = 787.713559;
364+
// Check the command value with wrapped error
365+
// ref = 10.0, state = 1.1, ds = 0.01, p_gain = 1.0, i_gain = 2.0, d_gain = 3.0
366+
// error = ref - state = wrap(10.0 - 1.1) = 8.9-2*pi = 2.616814, error_dot = error/ds
367+
// = 2.6168/0.01 = 261.6814, p_term = error * p_gain = 2.6168 * 1.0 = 2.6168, i_term = zero at
368+
// first update d_term = error_dot * d_gain = 261.6814 * 3.0 = 785.0444079 feedforward OFF -> cmd
369+
// = p_term + i_term + d_term = 2.616814, + 0.0 + 785.0444079 = 787.6612219
370+
const double expected_command_value = 787.6612219;
359371
EXPECT_NEAR(
360372
controller_->command_interfaces_[0].get_optional().value(), expected_command_value, 1e-5);
361373
}
@@ -607,9 +619,9 @@ TEST_F(PidControllerTest, test_save_i_term_off)
607619

608620
// check the command value
609621
// error = ref - state = 100.001, error_dot = error/ds = 10000.1,
610-
// p_term = 100.001 * 1, i_term = 1.00001 * 2 = 2.00002, d_term = error/ds = 10000.1 * 3
611-
// feedforward OFF -> cmd = p_term + i_term + d_term = 30102.3
612-
const double expected_command_value = 30102.30102;
622+
// p_term = 100.001 * 1, i_term = zero at first update, d_term = error/ds = 10000.1 * 3
623+
// feedforward OFF -> cmd = p_term + i_term + d_term = 30100.301
624+
const double expected_command_value = 30100.3010;
613625

614626
double actual_value =
615627
std::round(controller_->command_interfaces_[0].get_optional().value() * 1e5) / 1e5;
@@ -657,9 +669,9 @@ TEST_F(PidControllerTest, test_save_i_term_on)
657669

658670
// check the command value
659671
// error = ref - state = 100.001, error_dot = error/ds = 10000.1,
660-
// p_term = 100.001 * 1, i_term = 1.00001 * 2 = 2.00002, d_term = error/ds = 10000.1 * 3
661-
// feedforward OFF -> cmd = p_term + i_term + d_term = 30102.3
662-
const double expected_command_value = 30102.30102;
672+
// p_term = 100.001 * 1, i_term = zero at first update, d_term = error/ds = 10000.1 * 3
673+
// feedforward OFF -> cmd = p_term + i_term + d_term = 30102.301
674+
const double expected_command_value = 30100.3010;
663675

664676
double actual_value =
665677
std::round(controller_->command_interfaces_[0].get_optional().value() * 1e5) / 1e5;

pid_controller/test/test_pid_controller.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,8 @@ class PidControllerFixture : public ::testing::Test
137137
// initialize controller
138138
controller_ = std::make_unique<CtrlType>();
139139

140-
auto test = std::make_shared<rclcpp::Node>("command_publisher");
141-
command_publisher_node_ = test;
142-
command_publisher_ = command_publisher_node_->create_publisher<ControllerCommandMsg>(
143-
"/test_pid_controller/reference", rclcpp::SystemDefaultsQoS());
140+
// create a publisher node, publisher will be created in SetUpController
141+
command_publisher_node_ = std::make_shared<rclcpp::Node>("command_publisher");
144142
}
145143

146144
static void TearDownTestCase() { rclcpp::shutdown(); }
@@ -150,6 +148,9 @@ class PidControllerFixture : public ::testing::Test
150148
protected:
151149
void SetUpController(const std::string controller_name = "test_pid_controller")
152150
{
151+
command_publisher_ = command_publisher_node_->create_publisher<ControllerCommandMsg>(
152+
"/" + controller_name + "/reference", rclcpp::SystemDefaultsQoS());
153+
153154
ASSERT_EQ(
154155
controller_->init(controller_name, "", 0, "", controller_->define_custom_node_options()),
155156
controller_interface::return_type::OK);

pid_controller/test/test_pid_controller_dual_interface.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ TEST_F(PidControllerDualInterfaceTest, test_chained_feedforward_with_gain_dual_i
8888
controller_interface::return_type::OK);
8989

9090
// check the commands
91-
const double joint1_expected_cmd = 8.915;
92-
const double joint2_expected_cmd = 9.915;
91+
const double joint1_expected_cmd = 8.90;
92+
const double joint2_expected_cmd = 9.90;
9393
ASSERT_EQ(controller_->command_interfaces_[0].get_optional().value(), joint1_expected_cmd);
9494
ASSERT_EQ(controller_->command_interfaces_[1].get_optional().value(), joint2_expected_cmd);
9595
}

0 commit comments

Comments
 (0)