Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change to image_transport to allow compressed images + fix deadlock #202

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/astra_camera/ob_camera_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ class OBCameraNode {
std::map<stream_index_pair, std::vector<openni::VideoMode>> supported_video_modes_;
std::map<stream_index_pair, FrameCallbackFunction> stream_frame_callback_;
std::map<stream_index_pair, int> unit_step_size_;
std::map<stream_index_pair, ros::Publisher> image_publishers_;
std::map<stream_index_pair, image_transport::Publisher> image_publishers_;
std::map<stream_index_pair, ros::Publisher> camera_info_publishers_;
std::map<stream_index_pair, bool> flip_image_;

Expand Down
2 changes: 1 addition & 1 deletion include/astra_camera/uvc_camera_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class UVCCameraDriver {
ros::ServiceServer set_uvc_mirror_srv_;
ros::ServiceServer toggle_uvc_camera_srv_;
ros::ServiceServer save_image_srv_;
ros::Publisher image_publisher_;
image_transport::Publisher image_publisher_;
ros::Publisher camera_info_publisher_;
sensor_msgs::CameraInfo camera_info_;
std::recursive_mutex device_lock_;
Expand Down
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
<depend>tf2</depend>
<depend>message_filters</depend>
<depend>camera_info_manager</depend>
<depend>libuvc-dev</depend>
</package>
9 changes: 5 additions & 4 deletions src/ob_camera_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,17 +598,18 @@ void OBCameraNode::imageUnsubscribedCallback(const stream_index_pair &stream_ind
}

void OBCameraNode::setupPublishers() {
image_transport::ImageTransport it(nh_);
for (const auto &stream_index : IMAGE_STREAMS) {
std::string name = stream_name_[stream_index];
camera_info_publishers_[stream_index] =
nh_.advertise<sensor_msgs::CameraInfo>(name + "/camera_info", 1, true);
if (enable_[stream_index] && device_->hasSensor(stream_index.first)) {
ros::SubscriberStatusCallback image_subscribed_cb =
image_transport::SubscriberStatusCallback image_subscribed_cb =
boost::bind(&OBCameraNode::imageSubscribedCallback, this, stream_index);
ros::SubscriberStatusCallback image_unsubscribed_cb =
image_transport::SubscriberStatusCallback image_unsubscribed_cb =
boost::bind(&OBCameraNode::imageUnsubscribedCallback, this, stream_index);
image_publishers_[stream_index] = nh_.advertise<sensor_msgs::Image>(
name + "/image_raw", 1, image_subscribed_cb, image_unsubscribed_cb);
image_publishers_[stream_index] =
it.advertise(name + "/image_raw", 1, image_subscribed_cb, image_unsubscribed_cb);
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/uvc_camera_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ UVCCameraDriver::UVCCameraDriver(ros::NodeHandle& nh, ros::NodeHandle& nh_privat
ROS_ERROR_STREAM("init uvc context failed, exit");
throw std::runtime_error("init uvc context failed");
}
image_transport::ImageTransport it(nh_);
config_.serial_number = serial_number;
device_num_ = nh_private_.param<int>("device_num", 1);
uvc_flip_ = nh_private_.param<bool>("uvc_flip", false);
Expand All @@ -163,7 +164,7 @@ UVCCameraDriver::UVCCameraDriver(ros::NodeHandle& nh, ros::NodeHandle& nh_privat
color_info_manager_ =
std::make_shared<camera_info_manager::CameraInfoManager>(nh_, "rgb_camera", color_info_uri_);
std::lock_guard<decltype(device_lock_)> lock(device_lock_);
image_publisher_ = nh_.advertise<sensor_msgs::Image>(
image_publisher_ =it.advertise(
"color/image_raw", 10, boost::bind(&UVCCameraDriver::imageSubscribedCallback, this),
boost::bind(&UVCCameraDriver::imageUnsubscribedCallback, this));
setupCameraControlService();
Expand Down Expand Up @@ -657,7 +658,11 @@ void UVCCameraDriver::frameCallbackWrapper(uvc_frame_t* frame, void* ptr) {
void UVCCameraDriver::frameCallback(uvc_frame_t* frame) {
CHECK_NOTNULL(frame);
CHECK_NOTNULL(frame_buffer_);
std::lock_guard<decltype(device_lock_)> lock(device_lock_);
std::unique_lock<decltype(device_lock_)> lock(device_lock_, std::defer_lock);
if(!lock.try_lock()) {
// if we don't get the lock, just return. Otherwise it might result in a uvc deadlock.
return;
}
static constexpr int unit_step = 3;
sensor_msgs::Image image;
image.width = frame->width;
Expand Down