|
33 | 33 | */ |
34 | 34 | #include <gtest/gtest.h> |
35 | 35 |
|
| 36 | +#include <memory> |
| 37 | +#include <mutex> |
| 38 | +#include <thread> |
| 39 | + |
36 | 40 | #include <fuse_msgs/srv/set_pose.hpp> |
37 | 41 | #include <geometry_msgs/msg/pose_with_covariance_stamped.hpp> |
38 | 42 | #include <nav_msgs/msg/odometry.hpp> |
@@ -67,12 +71,20 @@ class FixedLagIgnitionFixture : public ::testing::Test |
67 | 71 |
|
68 | 72 | void odom_callback(const nav_msgs::msg::Odometry::SharedPtr msg) |
69 | 73 | { |
| 74 | + std::lock_guard lock(received_odom_mutex_); |
70 | 75 | received_odom_msg_ = msg; |
71 | 76 | } |
72 | 77 |
|
| 78 | + nav_msgs::msg::Odometry::SharedPtr get_last_odom_msg() |
| 79 | + { |
| 80 | + std::lock_guard lock(received_odom_mutex_); |
| 81 | + return received_odom_msg_; |
| 82 | + } |
| 83 | + |
73 | 84 | std::thread spinner_; //!< Internal thread for spinning the executor |
74 | 85 | rclcpp::executors::SingleThreadedExecutor::SharedPtr executor_; |
75 | 86 | nav_msgs::msg::Odometry::SharedPtr received_odom_msg_; |
| 87 | + std::mutex received_odom_mutex_; |
76 | 88 | }; |
77 | 89 |
|
78 | 90 | TEST_F(FixedLagIgnitionFixture, SetInitialState) |
@@ -162,20 +174,23 @@ TEST_F(FixedLagIgnitionFixture, SetInitialState) |
162 | 174 |
|
163 | 175 | // Wait for the optimizer to process all queued transactions and publish the last odometry msg |
164 | 176 | rclcpp::Time result_timeout = node->now() + rclcpp::Duration::from_seconds(1.0); |
165 | | - while ((!received_odom_msg_ || received_odom_msg_->header.stamp != rclcpp::Time(3, 0, |
| 177 | + auto odom_msg = nav_msgs::msg::Odometry::SharedPtr(); |
| 178 | + while ((!odom_msg || odom_msg->header.stamp != rclcpp::Time(3, 0, |
166 | 179 | RCL_ROS_TIME)) && (node->now() < result_timeout)) |
167 | 180 | { |
168 | 181 | rclcpp::sleep_for(std::chrono::milliseconds(100)); |
| 182 | + odom_msg = this->get_last_odom_msg(); |
169 | 183 | } |
170 | | - ASSERT_EQ(rclcpp::Time(received_odom_msg_->header.stamp), rclcpp::Time(3, 0, RCL_ROS_TIME)); |
| 184 | + ASSERT_TRUE(static_cast<bool>(odom_msg)); |
| 185 | + ASSERT_EQ(rclcpp::Time(odom_msg->header.stamp), rclcpp::Time(3, 0, RCL_ROS_TIME)); |
171 | 186 |
|
172 | 187 | // The optimizer is configured for 0 iterations, so it should return the initial variable values |
173 | 188 | // If we did our job correctly, the initial variable values should be the same as the service call |
174 | 189 | // state, give or take the motion model forward prediction. |
175 | | - EXPECT_NEAR(100.1, received_odom_msg_->pose.pose.position.x, 0.10); |
176 | | - EXPECT_NEAR(100.2, received_odom_msg_->pose.pose.position.y, 0.10); |
177 | | - EXPECT_NEAR(0.8660, received_odom_msg_->pose.pose.orientation.z, 0.10); |
178 | | - EXPECT_NEAR(0.5000, received_odom_msg_->pose.pose.orientation.w, 0.10); |
| 190 | + EXPECT_NEAR(100.1, odom_msg->pose.pose.position.x, 0.10); |
| 191 | + EXPECT_NEAR(100.2, odom_msg->pose.pose.position.y, 0.10); |
| 192 | + EXPECT_NEAR(0.8660, odom_msg->pose.pose.orientation.z, 0.10); |
| 193 | + EXPECT_NEAR(0.5000, odom_msg->pose.pose.orientation.w, 0.10); |
179 | 194 | } |
180 | 195 |
|
181 | 196 | // NOTE(CH3): This main is required because the test is manually run by a launch test |
|
0 commit comments