Skip to content

Commit 8bbccf2

Browse files
jaron-ldestogl
andauthored
fix: 🐛 make force_torque_sensor_broadcaster wait for realtime_publisher (ros-controls#327)
* fix: 🐛 make force_torque_sensor_broadcaster wait for realtime_publisher loop to be ready before finishing configure * test: ✅ check for published message in test_force_torque_sensor_broadcaster - create a loop that checks if force_torque_sensor_broadcaster published before checking subscribed message * refactor: ♻️ remove unnecessary includes * test: ✅ port realtime_publisher check to imu_sensor_broadcaster and joint_state_broadcaster * refactor: ♻️ remove unnecessary wait_for code from tests Co-authored-by: Denis Štogl <denis@stogl.de>
1 parent 7bd93cc commit 8bbccf2

File tree

3 files changed

+62
-48
lines changed

3 files changed

+62
-48
lines changed

force_torque_sensor_broadcaster/test/test_force_torque_sensor_broadcaster.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,6 @@ namespace
3737
{
3838
constexpr auto NODE_SUCCESS = controller_interface::CallbackReturn::SUCCESS;
3939
constexpr auto NODE_ERROR = controller_interface::CallbackReturn::ERROR;
40-
41-
rclcpp::WaitResultKind wait_for(rclcpp::SubscriptionBase::SharedPtr subscription)
42-
{
43-
rclcpp::WaitSet wait_set;
44-
wait_set.add_subscription(subscription);
45-
const auto timeout = std::chrono::seconds(20);
46-
return wait_set.wait(timeout).kind();
47-
}
48-
4940
} // namespace
5041

5142
void ForceTorqueSensorBroadcasterTest::SetUpTestCase() { rclcpp::init(0, nullptr); }
@@ -86,12 +77,21 @@ void ForceTorqueSensorBroadcasterTest::subscribe_and_get_message(
8677
"/test_force_torque_sensor_broadcaster/wrench", 10, subs_callback);
8778

8879
// call update to publish the test value
89-
ASSERT_EQ(
90-
fts_broadcaster_->update(rclcpp::Time(0), rclcpp::Duration::from_seconds(0.01)),
91-
controller_interface::return_type::OK);
92-
93-
// wait for message to be passed
94-
ASSERT_EQ(wait_for(subscription), rclcpp::WaitResultKind::Ready);
80+
// since update doesn't guarantee a published message, republish until received
81+
int max_sub_check_loop_count = 5; // max number of tries for pub/sub loop
82+
rclcpp::WaitSet wait_set; // block used to wait on message
83+
wait_set.add_subscription(subscription);
84+
while (max_sub_check_loop_count--)
85+
{
86+
fts_broadcaster_->update(rclcpp::Time(0), rclcpp::Duration::from_seconds(0.01));
87+
// check if message has been received
88+
if (wait_set.wait(std::chrono::milliseconds(2)).kind() == rclcpp::WaitResultKind::Ready)
89+
{
90+
break;
91+
}
92+
}
93+
ASSERT_GE(max_sub_check_loop_count, 0) << "Test was unable to publish a message through "
94+
"controller/broadcaster update loop";
9595

9696
// take message from subscription
9797
rclcpp::MessageInfo msg_info;

imu_sensor_broadcaster/test/test_imu_sensor_broadcaster.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,6 @@ namespace
3838
constexpr auto NODE_SUCCESS = controller_interface::CallbackReturn::SUCCESS;
3939
constexpr auto NODE_ERROR = controller_interface::CallbackReturn::ERROR;
4040

41-
rclcpp::WaitResultKind wait_for(rclcpp::SubscriptionBase::SharedPtr subscription)
42-
{
43-
rclcpp::WaitSet wait_set;
44-
wait_set.add_subscription(subscription);
45-
return wait_set.wait().kind();
46-
}
47-
4841
} // namespace
4942

5043
void IMUSensorBroadcasterTest::SetUpTestCase() { rclcpp::init(0, nullptr); }
@@ -88,12 +81,21 @@ void IMUSensorBroadcasterTest::subscribe_and_get_message(sensor_msgs::msg::Imu &
8881
"/test_imu_sensor_broadcaster/imu", 10, subs_callback);
8982

9083
// call update to publish the test value
91-
ASSERT_EQ(
92-
imu_broadcaster_->update(rclcpp::Time(0), rclcpp::Duration::from_seconds(0.01)),
93-
controller_interface::return_type::OK);
94-
95-
// wait for message to be passed
96-
ASSERT_EQ(wait_for(subscription), rclcpp::WaitResultKind::Ready);
84+
// since update doesn't guarantee a published message, republish until received
85+
int max_sub_check_loop_count = 5; // max number of tries for pub/sub loop
86+
rclcpp::WaitSet wait_set; // block used to wait on message
87+
wait_set.add_subscription(subscription);
88+
while (max_sub_check_loop_count--)
89+
{
90+
imu_broadcaster_->update(rclcpp::Time(0), rclcpp::Duration::from_seconds(0.01));
91+
// check if message has been received
92+
if (wait_set.wait(std::chrono::milliseconds(2)).kind() == rclcpp::WaitResultKind::Ready)
93+
{
94+
break;
95+
}
96+
}
97+
ASSERT_GE(max_sub_check_loop_count, 0) << "Test was unable to publish a message through "
98+
"controller/broadcaster update loop";
9799

98100
// take message from subscription
99101
rclcpp::MessageInfo msg_info;

joint_state_broadcaster/test/test_joint_state_broadcaster.cpp

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,6 @@ namespace
4545
{
4646
constexpr auto NODE_SUCCESS = controller_interface::CallbackReturn::SUCCESS;
4747
constexpr auto NODE_ERROR = controller_interface::CallbackReturn::ERROR;
48-
49-
rclcpp::WaitResultKind wait_for(rclcpp::SubscriptionBase::SharedPtr subscription)
50-
{
51-
rclcpp::WaitSet wait_set;
52-
wait_set.add_subscription(subscription);
53-
const auto timeout = std::chrono::seconds(10);
54-
return wait_set.wait(timeout).kind();
55-
}
5648
} // namespace
5749

5850
void JointStateBroadcasterTest::SetUpTestCase() { rclcpp::init(0, nullptr); }
@@ -606,12 +598,22 @@ void JointStateBroadcasterTest::test_published_joint_state_message(const std::st
606598
auto subscription =
607599
test_node.create_subscription<sensor_msgs::msg::JointState>(topic, 10, subs_callback);
608600

609-
ASSERT_EQ(
610-
state_broadcaster_->update(rclcpp::Time(0), rclcpp::Duration::from_seconds(0.01)),
611-
controller_interface::return_type::OK);
612-
613-
// wait for message to be passed
614-
ASSERT_EQ(wait_for(subscription), rclcpp::WaitResultKind::Ready);
601+
// call update to publish the test value
602+
// since update doesn't guarantee a published message, republish until received
603+
int max_sub_check_loop_count = 5; // max number of tries for pub/sub loop
604+
rclcpp::WaitSet wait_set; // block used to wait on message
605+
wait_set.add_subscription(subscription);
606+
while (max_sub_check_loop_count--)
607+
{
608+
state_broadcaster_->update(rclcpp::Time(0), rclcpp::Duration::from_seconds(0.01));
609+
// check if message has been received
610+
if (wait_set.wait(std::chrono::milliseconds(2)).kind() == rclcpp::WaitResultKind::Ready)
611+
{
612+
break;
613+
}
614+
}
615+
ASSERT_GE(max_sub_check_loop_count, 0) << "Test was unable to publish a message through "
616+
"controller/broadcaster update loop";
615617

616618
// take message from subscription
617619
sensor_msgs::msg::JointState joint_state_msg;
@@ -658,12 +660,22 @@ void JointStateBroadcasterTest::test_published_dynamic_joint_state_message(
658660
auto subscription =
659661
test_node.create_subscription<control_msgs::msg::DynamicJointState>(topic, 10, subs_callback);
660662

661-
ASSERT_EQ(
662-
state_broadcaster_->update(rclcpp::Time(0), rclcpp::Duration::from_seconds(0.01)),
663-
controller_interface::return_type::OK);
664-
665-
// wait for message to be passed
666-
ASSERT_EQ(wait_for(subscription), rclcpp::WaitResultKind::Ready);
663+
// call update to publish the test value
664+
// since update doesn't guarantee a published message, republish until received
665+
int max_sub_check_loop_count = 5; // max number of tries for pub/sub loop
666+
rclcpp::WaitSet wait_set; // block used to wait on message
667+
wait_set.add_subscription(subscription);
668+
while (max_sub_check_loop_count--)
669+
{
670+
state_broadcaster_->update(rclcpp::Time(0), rclcpp::Duration::from_seconds(0.01));
671+
// check if message has been received
672+
if (wait_set.wait(std::chrono::milliseconds(2)).kind() == rclcpp::WaitResultKind::Ready)
673+
{
674+
break;
675+
}
676+
}
677+
ASSERT_GE(max_sub_check_loop_count, 0) << "Test was unable to publish a message through "
678+
"controller/broadcaster update loop";
667679

668680
// take message from subscription
669681
control_msgs::msg::DynamicJointState dynamic_joint_state_msg;

0 commit comments

Comments
 (0)