Galaxis Codebase  1.0.0
Team Galaxis software stack for the Carolo Cup 2023 - ROS2 based software
CameraFrameObserver Class Reference

#include <AsyncCamera.hpp>

Inheritance diagram for CameraFrameObserver:
Collaboration diagram for CameraFrameObserver:

Public Member Functions

 CameraFrameObserver (AVT::VmbAPI::CameraPtr camera)
 
 ~CameraFrameObserver ()
 
virtual void FrameReceived (const AVT::VmbAPI::FramePtr pFrame)
 
std::shared_ptr< sensor_msgs::msg::Image > PopLastImage (bool blocking)
 

Private Attributes

cv_bridge::CvImage m_bridgeImg
 
std::shared_ptr< sensor_msgs::msg::Image > m_currentImageSPtr
 
std::mutex m_currentImageSPtrMutex
 
std::condition_variable m_newImageConditionVar
 
rclcpp::Clock m_rosClock
 
AVT::VmbAPI::CameraPtr m_cameraPtr
 

Detailed Description

Definition at line 24 of file AsyncCamera.hpp.

Constructor & Destructor Documentation

◆ CameraFrameObserver()

CameraFrameObserver::CameraFrameObserver ( AVT::VmbAPI::CameraPtr  camera)

Definition at line 22 of file AsyncCamera.cpp.

22  : AVT::VmbAPI::IFrameObserver( camera ) {
23  m_currentImageSPtr = nullptr;
24 
25  m_cameraPtr = camera;
26  m_cameraPtr->FlushQueue();
27 }

References m_cameraPtr, and m_currentImageSPtr.

◆ ~CameraFrameObserver()

CameraFrameObserver::~CameraFrameObserver ( )

Definition at line 29 of file AsyncCamera.cpp.

29  {
30  m_cameraPtr->FlushQueue();
31 }

References m_cameraPtr.

Member Function Documentation

◆ FrameReceived()

void CameraFrameObserver::FrameReceived ( const AVT::VmbAPI::FramePtr  pFrame)
virtual

Definition at line 34 of file AsyncCamera.cpp.

34  {
35  auto timestampCameraImage = m_rosClock.now();
36  log("[CAMERA] [ALVIUM] Frame received!");
37 
38  VmbUint32_t nImageSize = 0;
39  if(VmbErrorSuccess != imgFrame->GetImageSize( nImageSize )) {
40  log_warning("[CAMERA] [ALVIUM] GetImageSize");
41  m_cameraPtr->QueueFrame(imgFrame);
42  return;
43  }
44  VmbUint32_t nWidth = 0;
45  if(VmbErrorSuccess != imgFrame->GetWidth( nWidth )) {
46  log_warning("[CAMERA] [ALVIUM] GetImageSize");
47  m_cameraPtr->QueueFrame(imgFrame);
48  return;
49  }
50  VmbUint32_t nHeight = 0;
51  if(VmbErrorSuccess != imgFrame->GetHeight( nHeight )) {
52  log_warning("[CAMERA] [ALVIUM] GetImageSize");
53  m_cameraPtr->QueueFrame(imgFrame);
54  return;
55  }
56 
57  // Get Image
58  unsigned char* imagePtr;
59  if(VmbErrorSuccess != imgFrame->GetImage( imagePtr )) {
60  log_warning("[CAMERA] [ALVIUM] GetImage");
61  m_cameraPtr->QueueFrame(imgFrame);
62  return;
63  }
64 
65  // COPY TO OPENCV (for resizing)
66  // Resize Parameters cv::Size(Width, Height)
67  auto outImgSize = cv::Size(512, 256);
68 
69  if(static_cast<float>(nHeight)/static_cast<float>(nWidth) != static_cast<float>(outImgSize.height)/static_cast<float>(outImgSize.width)) {
70  log_warning("[CAMERA] [ALVIUM] Camera Image Ratio NOT equal to desired Image Ration");
71  }
72 
73  cv::Mat img(nHeight, nWidth, CV_8UC3, imagePtr);
74  cv::Mat imgResize(outImgSize, CV_8UC3);
75  cv::resize(img, imgResize, outImgSize);
76 
77 
78  // release camera img frame => not used afterwards
79  m_cameraPtr->QueueFrame(imgFrame);
80 
81 
82  // create image msg and bridge to sensor image msg
83  std::shared_ptr<sensor_msgs::msg::Image> msgImgSPtr = std::make_shared<sensor_msgs::msg::Image>();
84  std_msgs::msg::Header header;
85  header.frame_id = "cam";
86  header.stamp = timestampCameraImage;
87 
88  m_bridgeImg = cv_bridge::CvImage(header, sensor_msgs::image_encodings::RGB8, imgResize);
89  m_bridgeImg.toImageMsg(*msgImgSPtr); // cv_bridge to sensor_msgs::Image
90 
91 
93  m_currentImageSPtr = msgImgSPtr;
94  m_newImageConditionVar.notify_one();
95  m_currentImageSPtrMutex.unlock();
96 
97 }

References galaxis::utils::log(), galaxis::utils::log_warning(), m_bridgeImg, m_cameraPtr, m_currentImageSPtr, m_currentImageSPtrMutex, m_newImageConditionVar, and m_rosClock.

Here is the call graph for this function:

◆ PopLastImage()

std::shared_ptr< sensor_msgs::msg::Image > CameraFrameObserver::PopLastImage ( bool  blocking)

Definition at line 99 of file AsyncCamera.cpp.

99  {
100  std::unique_lock<std::mutex> lock(m_currentImageSPtrMutex);
101  std::shared_ptr<sensor_msgs::msg::Image> ret = m_currentImageSPtr;
102  m_currentImageSPtr = nullptr;
103 
104  // if return image is nullptr and blocking is enabled then wait for new image
105  if(ret == nullptr && blocking) {
106  m_newImageConditionVar.wait(lock); // release lock and wait until notify_one called
107  ret = m_currentImageSPtr;
108  m_currentImageSPtr = nullptr;
109  }
110 
111  return ret;
112 }

References m_currentImageSPtr, m_currentImageSPtrMutex, and m_newImageConditionVar.

Referenced by AlviumAsyncCamera::pop_image().

Here is the caller graph for this function:

Member Data Documentation

◆ m_bridgeImg

cv_bridge::CvImage CameraFrameObserver::m_bridgeImg
private

Definition at line 26 of file AsyncCamera.hpp.

Referenced by FrameReceived().

◆ m_cameraPtr

AVT::VmbAPI::CameraPtr CameraFrameObserver::m_cameraPtr
private

Definition at line 33 of file AsyncCamera.hpp.

Referenced by CameraFrameObserver(), FrameReceived(), and ~CameraFrameObserver().

◆ m_currentImageSPtr

std::shared_ptr<sensor_msgs::msg::Image> CameraFrameObserver::m_currentImageSPtr
private

Definition at line 27 of file AsyncCamera.hpp.

Referenced by CameraFrameObserver(), FrameReceived(), and PopLastImage().

◆ m_currentImageSPtrMutex

std::mutex CameraFrameObserver::m_currentImageSPtrMutex
private

Definition at line 28 of file AsyncCamera.hpp.

Referenced by FrameReceived(), and PopLastImage().

◆ m_newImageConditionVar

std::condition_variable CameraFrameObserver::m_newImageConditionVar
private

Definition at line 29 of file AsyncCamera.hpp.

Referenced by FrameReceived(), and PopLastImage().

◆ m_rosClock

rclcpp::Clock CameraFrameObserver::m_rosClock
private

Definition at line 31 of file AsyncCamera.hpp.

Referenced by FrameReceived().


The documentation for this class was generated from the following files:
CameraFrameObserver::m_currentImageSPtrMutex
std::mutex m_currentImageSPtrMutex
Definition: AsyncCamera.hpp:28
galaxis::utils::log_warning
void log_warning(const Args &... args)
Definition: logger.hpp:41
CameraFrameObserver::m_rosClock
rclcpp::Clock m_rosClock
Definition: AsyncCamera.hpp:31
CameraFrameObserver::m_newImageConditionVar
std::condition_variable m_newImageConditionVar
Definition: AsyncCamera.hpp:29
CameraFrameObserver::m_bridgeImg
cv_bridge::CvImage m_bridgeImg
Definition: AsyncCamera.hpp:26
CameraFrameObserver::m_currentImageSPtr
std::shared_ptr< sensor_msgs::msg::Image > m_currentImageSPtr
Definition: AsyncCamera.hpp:27
galaxis::utils::log
void log(const Args &... args)
Definition: logger.hpp:33
CameraFrameObserver::m_cameraPtr
AVT::VmbAPI::CameraPtr m_cameraPtr
Definition: AsyncCamera.hpp:33