Skip to content

Commit 925bfef

Browse files
committed
Add check in controller interface to update 'update_rate' parameter only when doing 'configure' transition.
1 parent b0ff68a commit 925bfef

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

controller_interface/src/controller_interface.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include <utility>
2020
#include <vector>
2121

22-
#include "hardware_interface/types/lifecycle_state_names.hpp"
22+
#include "lifecycle_msgs/msg/state.hpp"
2323

2424
namespace controller_interface
2525
{
@@ -73,9 +73,20 @@ return_type ControllerInterface::init(const std::string & controller_name)
7373

7474
const rclcpp_lifecycle::State & ControllerInterface::configure()
7575
{
76-
update_rate_ = node_->get_parameter("update_rate").as_int();
76+
// TODO(destogl): this should actually happen in "on_configure" but I am not sure how to get
77+
// overrides correctly in combination with std::bind. The goal is to have the following calls:
78+
// 1. CM: controller.get_node()->configure()
79+
// 2. LifecycleNode: ControllerInterface::on_configure()
80+
// 3. ControllerInterface: <controller>::on_configure()
81+
// Then we don't need to do state-machine related checks.
82+
//
83+
// Other solution is to add check into the LifecycleNode if a transition is valid to trigger
84+
if (get_state().id() == lifecycle_msgs::msg::State::PRIMARY_STATE_UNCONFIGURED)
85+
{
86+
update_rate_ = get_node()->get_parameter("update_rate").as_int();
87+
}
7788

78-
return node_->configure();
89+
return get_node()->configure();
7990
}
8091

8192
void ControllerInterface::assign_interfaces(

controller_interface/test/test_controller_interface.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,26 @@ TEST(TestableControllerInterface, setting_update_rate_in_configure)
8383

8484
// Test updating of update_rate parameter
8585
EXPECT_EQ(std::system("ros2 param set /testable_controller_interface update_rate 623"), 0);
86+
// Keep the same update rate until transition from 'UNCONFIGURED' TO 'INACTIVE' does not happen
87+
controller.configure(); // No transition so the update rate should stay intact
88+
ASSERT_NE(controller.get_update_rate(), 623u);
8689
ASSERT_EQ(controller.get_update_rate(), 2812u);
90+
8791
controller.get_node()->activate();
92+
controller.configure(); // No transition so the update rate should stay intact
93+
ASSERT_NE(controller.get_update_rate(), 623u);
94+
ASSERT_EQ(controller.get_update_rate(), 2812u);
95+
8896
controller.update(controller.get_node()->now(), rclcpp::Duration::from_seconds(0.1));
97+
controller.configure(); // No transition so the update rate should stay intact
98+
ASSERT_NE(controller.get_update_rate(), 623u);
99+
ASSERT_EQ(controller.get_update_rate(), 2812u);
100+
89101
controller.get_node()->deactivate();
102+
controller.configure(); // No transition so the update rate should stay intact
103+
ASSERT_NE(controller.get_update_rate(), 623u);
104+
ASSERT_EQ(controller.get_update_rate(), 2812u);
105+
90106
controller.get_node()->cleanup();
91107
ASSERT_EQ(controller.get_update_rate(), 2812u);
92108
// It is first changed after controller is configured again.

0 commit comments

Comments
 (0)