Skip to content

Commit

Permalink
feat: add diagnostics (#19)
Browse files Browse the repository at this point in the history
* feat: add diagnostics
* set update_rate
  • Loading branch information
0x126 authored May 17, 2024
1 parent 1029edd commit 046d49d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ ament_target_dependencies(v4l2_camera
"rclcpp"
"rclcpp_components"
"image_transport"
"camera_info_manager")
"camera_info_manager"
"diagnostic_updater")

target_compile_options(v4l2_camera PRIVATE -Werror)

Expand Down
7 changes: 6 additions & 1 deletion include/v4l2_camera/v4l2_camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <camera_info_manager/camera_info_manager.hpp>
#include <image_transport/image_transport.hpp>
#include <diagnostic_updater/diagnostic_updater.hpp>

#include <ostream>
#include <rclcpp/rclcpp.hpp>
Expand Down Expand Up @@ -120,6 +121,10 @@ class V4L2Camera : public rclcpp::Node
bool publish_next_frame_;
bool use_image_transport_;

diagnostic_updater::Updater diagnostic_updater_{this};
rclcpp::Time last_capture_stamp_{rclcpp::Time(0)};
double capture_rate_{0.0};

#ifdef ENABLE_CUDA
// Memory region to communicate with GPU
std::allocator<GPUMemoryManager> allocator_;
Expand All @@ -143,8 +148,8 @@ class V4L2Camera : public rclcpp::Node
bool checkCameraInfo(
sensor_msgs::msg::Image const & img,
sensor_msgs::msg::CameraInfo const & ci);
void updateDiagnostics(diagnostic_updater::DiagnosticStatusWrapper & stat);
};

} // namespace v4l2_camera

#endif // V4L2_CAMERA__V4L2_CAMERA_HPP_
3 changes: 2 additions & 1 deletion package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
<depend>sensor_msgs</depend>
<depend>image_transport</depend>
<depend>camera_info_manager</depend>

<depend>diagnostic_updater</depend>

<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>

Expand Down
29 changes: 28 additions & 1 deletion src/v4l2_camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ V4L2Camera::V4L2Camera(rclcpp::NodeOptions const & options)
// Read parameters and set up callback
createParameters();

diagnostic_updater_.setHardwareID(get_name());
diagnostic_updater_.add("capture_status", this, &V4L2Camera::updateDiagnostics);
diagnostic_updater_.setPeriod(0.1);

// Start the camera
if (!camera_->start()) {
return;
Expand All @@ -119,6 +123,7 @@ V4L2Camera::V4L2Camera(rclcpp::NodeOptions const & options)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
continue;
}

if(publish_next_frame_ == false){
continue;
}
Expand All @@ -144,7 +149,10 @@ V4L2Camera::V4L2Camera(rclcpp::NodeOptions const & options)
ci->header.stamp = stamp;
ci->header.frame_id = camera_frame_id_;
publish_next_frame_ = publish_rate_ < 0;

if(last_capture_stamp_.seconds() > 0){
capture_rate_ = 1.0 / (rclcpp::Time(stamp) - last_capture_stamp_).seconds();
}
last_capture_stamp_ = rclcpp::Time(stamp);
if (use_image_transport_) {
camera_transport_pub_.publish(*img, *ci);
} else {
Expand Down Expand Up @@ -590,6 +598,25 @@ bool V4L2Camera::checkCameraInfo(
return ci.width == img.width && ci.height == img.height;
}

void V4L2Camera::updateDiagnostics(diagnostic_updater::DiagnosticStatusWrapper & stat)
{
using diagnostic_msgs::msg::DiagnosticStatus;

auto current_stamp = get_clock()->now();
if(current_stamp.seconds() - last_capture_stamp_.seconds() > 5.0){
stat.summary(DiagnosticStatus::STALE, "timeout");
}else if (capture_rate_ < 1.0) {
stat.summary(DiagnosticStatus::ERROR, "error");
} else if (capture_rate_ < 5.0) {
stat.summary(DiagnosticStatus::WARN, "warn");
} else {
stat.summary(DiagnosticStatus::OK, "ok");
}
stat.addf("last_capture_stamp", "%.2f", last_capture_stamp_.seconds());
stat.addf("now", "%.2f", current_stamp.seconds());
stat.addf("Capture rate", "%.2f Hz", capture_rate_);
}

#ifdef ENABLE_CUDA
sensor_msgs::msg::Image::UniquePtr V4L2Camera::convertOnGpu(sensor_msgs::msg::Image const & img)
{
Expand Down

0 comments on commit 046d49d

Please sign in to comment.