Skip to content

Commit c568e63

Browse files
authored
[Controllers] Receive a valid period on the first update cycle (#2572) (#2602)
1 parent da82f3b commit c568e63

2 files changed

Lines changed: 15 additions & 17 deletions

File tree

controller_manager/src/controller_manager.cpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2976,21 +2976,13 @@ controller_interface::return_type ControllerManager::update(
29762976
run_controller_at_cm_rate ? period
29772977
: rclcpp::Duration::from_seconds((1.0 / controller_update_rate));
29782978

2979-
bool first_update_cycle = false;
2979+
const bool first_update_cycle =
2980+
(*loaded_controller.last_update_cycle_time ==
2981+
rclcpp::Time(0, 0, this->get_trigger_clock()->get_clock_type()));
29802982
const rclcpp::Time current_time = get_clock()->started() ? get_trigger_clock()->now() : time;
2981-
if (
2982-
*loaded_controller.last_update_cycle_time ==
2983-
rclcpp::Time(0, 0, this->get_trigger_clock()->get_clock_type()))
2984-
{
2985-
// last_update_cycle_time is zero after activation
2986-
first_update_cycle = true;
2987-
*loaded_controller.last_update_cycle_time = current_time;
2988-
RCLCPP_DEBUG(
2989-
get_logger(), "Setting last_update_cycle_time to %fs for the controller : %s",
2990-
loaded_controller.last_update_cycle_time->seconds(), loaded_controller.info.name.c_str());
2991-
}
29922983
const auto controller_actual_period =
2993-
(current_time - *loaded_controller.last_update_cycle_time);
2984+
first_update_cycle ? controller_period
2985+
: (current_time - *loaded_controller.last_update_cycle_time);
29942986

29952987
const double error_now =
29962988
std::abs((controller_actual_period.seconds() * controller_update_rate) - 1.0);

controller_manager/test/test_controller_manager.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,11 @@ TEST_P(TestControllerManagerWithStrictness, async_controller_lifecycle)
472472
std::this_thread::sleep_for(
473473
std::chrono::milliseconds(1000 / (test_controller->get_update_rate())));
474474
EXPECT_EQ(test_controller->internal_counter, 1u);
475-
EXPECT_EQ(test_controller->update_period_.seconds(), 0.0)
476-
<< "The first trigger cycle should have zero period";
475+
EXPECT_NEAR(
476+
test_controller->update_period_.seconds(),
477+
1.0 / (static_cast<double>(test_controller->get_update_rate())), 1.e-6)
478+
<< "The first trigger cycle should have non-zero period to allow for integration in the "
479+
"controllers";
477480

478481
const double exp_period = (cm_->get_trigger_clock()->now() - time_).seconds();
479482
time_ = cm_->get_trigger_clock()->now();
@@ -1064,8 +1067,11 @@ TEST_P(TestControllerUpdateRates, check_the_controller_update_rate)
10641067
}
10651068
else
10661069
{
1067-
// Check that the first cycle update period is zero
1068-
EXPECT_EQ(test_controller->update_period_.seconds(), 0.0);
1070+
EXPECT_NEAR(
1071+
test_controller->update_period_.seconds(),
1072+
1.0 / (static_cast<double>(test_controller->get_update_rate())), 1.e-6)
1073+
<< "The first trigger cycle should have non-zero period to allow for integration in the "
1074+
"controllers";
10691075
}
10701076

10711077
if (update_counter > 0 && update_counter % cm_update_rate == 0)

0 commit comments

Comments
 (0)