Skip to content

Commit

Permalink
fix race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Docker authored and scpeters committed Apr 19, 2018
1 parent e9d72ba commit 9cd1ae5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace gazebo

protected: void PreRender();

protected: bool triggered = false;
protected: int triggered = 0;

protected: std::mutex mutex;

Expand Down
9 changes: 6 additions & 3 deletions gazebo_plugins/src/gazebo_ros_triggered_camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,17 @@ void GazeboRosTriggeredCamera::OnNewFrame(const unsigned char *_image,
this->PublishCameraInfo();
}
this->SetCameraEnabled(false);

std::lock_guard<std::mutex> lock(this->mutex);
this->triggered = false;
this->triggered = std::max(this->triggered-1, 0);
}

void GazeboRosTriggeredCamera::TriggerCamera()
{
std::lock_guard<std::mutex> lock(this->mutex);
if (!this->parentSensor_)
return;
this->triggered = true;
this->triggered++;
}

bool GazeboRosTriggeredCamera::CanTriggerCamera()
Expand All @@ -117,8 +118,10 @@ bool GazeboRosTriggeredCamera::CanTriggerCamera()
void GazeboRosTriggeredCamera::PreRender()
{
std::lock_guard<std::mutex> lock(this->mutex);
if (this->triggered)
if (this->triggered > 0)
{
this->SetCameraEnabled(true);
}
}

void GazeboRosTriggeredCamera::SetCameraEnabled(const bool _enabled)
Expand Down
11 changes: 6 additions & 5 deletions gazebo_plugins/test/camera/triggered_camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CameraTest : public testing::Test
TEST_F(CameraTest, cameraSubscribeTest)
{
image_transport::ImageTransport it(nh_);
cam_sub_ = it.subscribe("camera1/image_raw", 1,
cam_sub_ = it.subscribe("camera1/image_raw", 5,
&CameraTest::imageCallback,
dynamic_cast<CameraTest*>(this));

Expand Down Expand Up @@ -80,27 +80,28 @@ TEST_F(CameraTest, cameraSubscribeTest)
}
EXPECT_EQ(images_received_, 1);

// then send two trigger messages very close together, but only expect one image
// then send two trigger messages very close together, and expect two more
// images
trigger_pub.publish(msg);
ros::spinOnce();
ros::Duration(0.01).sleep();
trigger_pub.publish(msg);
ros::spinOnce();
ros::Duration(0.01).sleep();
for (unsigned int i = 0; i < 10 && 1 == images_received_; ++i)
for (unsigned int i = 0; i < 10 && images_received_ < 2; ++i)
{
ros::spinOnce();
ros::Duration(0.1).sleep();
}
EXPECT_EQ(images_received_, 2);
EXPECT_EQ(images_received_, 3);

// then wait for 3 seconds to confirm that we don't receive any more images
for (unsigned int i = 0; i < 30; ++i)
{
ros::spinOnce();
ros::Duration(0.1).sleep();
}
EXPECT_EQ(images_received_, 2);
EXPECT_EQ(images_received_, 3);

cam_sub_.shutdown();
}
Expand Down

0 comments on commit 9cd1ae5

Please sign in to comment.