Skip to content

Commit 80c96b0

Browse files
Juliajmergify[bot]
authored andcommitted
[pid_controller] Update tests (#1538)
(cherry picked from commit b24b310) # Conflicts: # pid_controller/src/pid_controller.cpp # pid_controller/test/pid_controller_params.yaml # pid_controller/test/test_pid_controller.cpp # pid_controller/test/test_pid_controller.hpp
1 parent ae649a9 commit 80c96b0

File tree

6 files changed

+413
-121
lines changed

6 files changed

+413
-121
lines changed

pid_controller/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,19 @@ if(BUILD_TESTING)
9090
hardware_interface
9191
)
9292

93+
add_rostest_with_parameters_gmock(
94+
test_pid_controller_dual_interface
95+
test/test_pid_controller_dual_interface.cpp
96+
${CMAKE_CURRENT_SOURCE_DIR}/test/pid_controller_params.yaml
97+
)
98+
target_include_directories(test_pid_controller_dual_interface PRIVATE include)
99+
target_link_libraries(test_pid_controller_dual_interface pid_controller)
100+
ament_target_dependencies(
101+
test_pid_controller_dual_interface
102+
controller_interface
103+
hardware_interface
104+
)
105+
93106
ament_add_gmock(test_load_pid_controller test/test_load_pid_controller.cpp)
94107
target_include_directories(test_load_pid_controller PRIVATE include)
95108
ament_target_dependencies(

pid_controller/src/pid_controller.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ controller_interface::return_type PidController::update_and_write_commands(
411411
// check for any parameter updates
412412
update_parameters();
413413

414+
// Update feedback either from external measured state or from state interfaces
414415
if (params_.use_external_measured_states)
415416
{
416417
const auto measured_state = *(measured_state_.readFromRT());
@@ -431,17 +432,27 @@ controller_interface::return_type PidController::update_and_write_commands(
431432
}
432433
}
433434

435+
<<<<<<< HEAD
436+
=======
437+
// Fill the information of the exported state interfaces
438+
for (size_t i = 0; i < measured_state_values_.size(); ++i)
439+
{
440+
state_interfaces_values_[i] = measured_state_values_[i];
441+
}
442+
443+
// Iterate through all the dofs to calculate the output command
444+
>>>>>>> b24b310 ([pid_controller] Update tests (#1538))
434445
for (size_t i = 0; i < dof_; ++i)
435446
{
436447
double tmp_command = 0.0;
437448

438-
if (!std::isnan(reference_interfaces_[i]) && !std::isnan(measured_state_values_[i]))
449+
if (std::isfinite(reference_interfaces_[i]) && std::isfinite(measured_state_values_[i]))
439450
{
440451
// calculate feed-forward
441452
if (*(control_mode_.readFromRT()) == feedforward_mode_type::ON)
442453
{
443454
// two interfaces
444-
if (reference_interfaces_.size() == 2 * dof_ && measured_state_values_.size() == 2 * dof_)
455+
if (reference_interfaces_.size() == 2 * dof_)
445456
{
446457
if (std::isfinite(reference_interfaces_[dof_ + i]))
447458
{
@@ -468,8 +479,8 @@ controller_interface::return_type PidController::update_and_write_commands(
468479
if (reference_interfaces_.size() == 2 * dof_ && measured_state_values_.size() == 2 * dof_)
469480
{
470481
if (
471-
!std::isnan(reference_interfaces_[dof_ + i]) &&
472-
!std::isnan(measured_state_values_[dof_ + i]))
482+
std::isfinite(reference_interfaces_[dof_ + i]) &&
483+
std::isfinite(measured_state_values_[dof_ + i]))
473484
{
474485
// use calculation with 'error' and 'error_dot'
475486
tmp_command += pids_[i]->computeCommand(
@@ -488,7 +499,13 @@ controller_interface::return_type PidController::update_and_write_commands(
488499
}
489500

490501
// write calculated values
491-
command_interfaces_[i].set_value(tmp_command);
502+
auto success = command_interfaces_[i].set_value(tmp_command);
503+
if (!success)
504+
{
505+
RCLCPP_ERROR(
506+
get_node()->get_logger(), "Failed to set command value for %s",
507+
command_interfaces_[i].get_name().c_str());
508+
}
492509
}
493510
}
494511

pid_controller/test/pid_controller_params.yaml

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,52 @@ test_pid_controller:
88
reference_and_state_interfaces: ["position"]
99

1010
gains:
11-
joint1: {p: 1.0, i: 2.0, d: 10.0, i_clamp_max: 5.0, i_clamp_min: -5.0}
11+
joint1: {p: 1.0, i: 2.0, d: 3.0, i_clamp_max: 5.0, i_clamp_min: -5.0}
1212

13+
test_pid_controller_angle_wraparound_on:
14+
ros__parameters:
15+
dof_names:
16+
- joint1
17+
18+
command_interface: position
19+
20+
reference_and_state_interfaces: ["position"]
21+
22+
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}
24+
25+
test_pid_controller_with_feedforward_gain:
26+
ros__parameters:
27+
dof_names:
28+
- joint1
29+
30+
command_interface: position
31+
32+
reference_and_state_interfaces: ["position"]
33+
34+
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}
36+
37+
test_pid_controller_with_feedforward_gain_dual_interface:
38+
ros__parameters:
39+
dof_names:
40+
- joint1
41+
- joint2
42+
43+
command_interface: velocity
44+
45+
reference_and_state_interfaces: ["position", "velocity"]
46+
47+
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}
50+
51+
<<<<<<< HEAD
1352

1453
test_pid_controller_with_feedforward_gain:
54+
=======
55+
test_save_i_term_off:
56+
>>>>>>> b24b310 ([pid_controller] Update tests (#1538))
1557
ros__parameters:
1658
dof_names:
1759
- joint1
@@ -21,4 +63,20 @@ test_pid_controller_with_feedforward_gain:
2163
reference_and_state_interfaces: ["position"]
2264

2365
gains:
66+
<<<<<<< HEAD
2467
joint1: {p: 0.5, i: 0.0, d: 0.0, i_clamp_max: 5.0, i_clamp_min: -5.0, feedforward_gain: 1.0}
68+
=======
69+
joint1: {p: 1.0, i: 2.0, d: 3.0, i_clamp_max: 5.0, i_clamp_min: -5.0, save_i_term: false}
70+
71+
test_save_i_term_on:
72+
ros__parameters:
73+
dof_names:
74+
- joint1
75+
76+
command_interface: position
77+
78+
reference_and_state_interfaces: ["position"]
79+
80+
gains:
81+
joint1: {p: 1.0, i: 2.0, d: 3.0, i_clamp_max: 5.0, i_clamp_min: -5.0, save_i_term: true}
82+
>>>>>>> b24b310 ([pid_controller] Update tests (#1538))

0 commit comments

Comments
 (0)