Skip to content

Commit 2d4bb65

Browse files
christophfroehlichmergify[bot]
authored andcommitted
Don't update MockComponent's state interfaces if command interfaces are not finite (#2786)
(cherry picked from commit bafd793)
1 parent 7bab457 commit 2d4bb65

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

hardware_interface/src/mock_components/generic_system.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ return_type GenericSystem::read(const rclcpp::Time & /*time*/, const rclcpp::Dur
485485
if (joint_command.get()->get_interface_name() == hardware_interface::HW_IF_POSITION)
486486
{
487487
const std::string & name = joint_command.get()->get_name();
488-
if (has_state(name))
488+
if (has_state(name) && std::isfinite(get_command(name)))
489489
{
490490
set_state(
491491
name, get_command(name) + (custom_interface_with_following_offset_.empty()
@@ -523,7 +523,10 @@ return_type GenericSystem::read(const rclcpp::Time & /*time*/, const rclcpp::Dur
523523
get_command(
524524
joint_state.get()->get_prefix_name() + "/" + hardware_interface::HW_IF_POSITION) +
525525
position_state_following_offset_;
526-
set_state(full_interface_name, cmd);
526+
if (std::isfinite(cmd))
527+
{
528+
set_state(full_interface_name, cmd);
529+
}
527530
}
528531
}
529532

hardware_interface/test/mock_components/test_generic_system.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,28 @@ void generic_system_functional_test(
10321032
EXPECT_TRUE(std::isnan(j2p_c.get_optional().value()));
10331033
EXPECT_TRUE(std::isnan(j2v_c.get_optional().value()));
10341034

1035+
// read() does not change values until commands are set (i.e, isfinite())
1036+
ASSERT_EQ(rm.read(TIME, PERIOD).result, hardware_interface::return_type::OK);
1037+
EXPECT_EQ(3.45 + offset, j1p_s.get_optional().value());
1038+
EXPECT_EQ(0.0, j1v_s.get_optional().value());
1039+
EXPECT_EQ(2.78 + offset, j2p_s.get_optional().value());
1040+
EXPECT_EQ(0.0, j2v_s.get_optional().value());
1041+
EXPECT_TRUE(std::isnan(j1p_c.get_optional().value()));
1042+
EXPECT_TRUE(std::isnan(j1v_c.get_optional().value()));
1043+
EXPECT_TRUE(std::isnan(j2p_c.get_optional().value()));
1044+
EXPECT_TRUE(std::isnan(j2v_c.get_optional().value()));
1045+
1046+
// write() does not change values until commands are set (i.e, isfinite())
1047+
ASSERT_EQ(rm.write(TIME, PERIOD).result, hardware_interface::return_type::OK);
1048+
EXPECT_EQ(3.45 + offset, j1p_s.get_optional().value());
1049+
EXPECT_EQ(0.0, j1v_s.get_optional().value());
1050+
EXPECT_EQ(2.78 + offset, j2p_s.get_optional().value());
1051+
EXPECT_EQ(0.0, j2v_s.get_optional().value());
1052+
EXPECT_TRUE(std::isnan(j1p_c.get_optional().value()));
1053+
EXPECT_TRUE(std::isnan(j1v_c.get_optional().value()));
1054+
EXPECT_TRUE(std::isnan(j2p_c.get_optional().value()));
1055+
EXPECT_TRUE(std::isnan(j2v_c.get_optional().value()));
1056+
10351057
// set some new values in commands
10361058
ASSERT_TRUE(j1p_c.set_value(0.11));
10371059
ASSERT_TRUE(j1v_c.set_value(0.22));

0 commit comments

Comments
 (0)