SMACC2
Loading...
Searching...
No Matches
cl_rgb::CpCamera Class Reference

#include <cp_camera.hpp>

Inheritance diagram for cl_rgb::CpCamera:
Inheritance graph
Collaboration diagram for cl_rgb::CpCamera:
Collaboration graph

Public Member Functions

 CpCamera (const CameraParams &params)
 
 ~CpCamera () override
 
void onInitialize () override
 
const CameraParams & params () const
 
FramePtr latestFrame () const
 
std::optional< PinholeCamera > model (const Frame &frame) const
 
- Public Member Functions inherited from smacc2::ISmaccComponent
 ISmaccComponent ()
 
virtual ~ISmaccComponent ()
 
 ISmaccComponent (const ISmaccComponent &)=delete
 
ISmaccComponent & operator= (const ISmaccComponent &)=delete
 
 ISmaccComponent (ISmaccComponent &&)=delete
 
ISmaccComponent & operator= (ISmaccComponent &&)=delete
 
virtual std::string getName () const
 

Public Attributes

smacc2::SmaccSignal< void(const FramePtr &)> onFrame_
 

Private Member Functions

void onImage (const sensor_msgs::msg::Image::ConstSharedPtr msg)
 
void onInfo (const sensor_msgs::msg::CameraInfo::ConstSharedPtr msg)
 

Private Attributes

CameraParams params_
 
CpTransforms * transforms_ = nullptr
 
rclcpp::Subscription< sensor_msgs::msg::Image >::SharedPtr imageSubscriber_
 
rclcpp::Subscription< sensor_msgs::msg::CameraInfo >::SharedPtr infoSubscriber_
 
std::mutex mutex_
 
FramePtr latest_
 
uint64_t sequence_ = 0
 
bool calibrated_ = false
 
double fx_ = 1.0
 
double fy_ = 1.0
 
double cx_ = 0.0
 
double cy_ = 0.0
 

Additional Inherited Members

- Protected Member Functions inherited from smacc2::ISmaccComponent
template<typename TOrthogonal , typename TClient >
void onComponentInitialization ()
 
template<typename EventType >
void postEvent (const EventType &ev)
 
template<typename EventType >
void postEvent ()
 
template<typename TOrthogonal , typename TSourceObject >
void onStateOrthogonalAllocation ()
 
template<typename TComponent >
void requiresComponent (TComponent *&requiredComponentStorage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
 
template<typename TComponent >
void requiresComponent (const std::string &name, TComponent *&requiredComponentStorage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
 
template<typename TClient >
void requiresClient (TClient *&requiredClientStorage)
 
template<typename SmaccComponentType , typename TOrthogonal , typename TClient , typename... TArgs>
SmaccComponentType * createSiblingComponent (const TArgs &... targs)
 
template<typename SmaccComponentType , typename TOrthogonal , typename TClient , typename... TArgs>
SmaccComponentType * createSiblingNamedComponent (const std::string &name, const TArgs &... targs)
 
rclcpp::Node::SharedPtr getNode ()
 
rclcpp::Logger getLogger () const
 
ISmaccStateMachine * getStateMachine ()
 
- Protected Attributes inherited from smacc2::ISmaccComponent
ISmaccStateMachine * stateMachine_ = nullptr
 
ISmaccClient * owner_ = nullptr
 

Detailed Description

Definition at line 44 of file cp_camera.hpp.

Constructor & Destructor Documentation

◆ CpCamera()

cl_rgb::CpCamera::CpCamera ( const CameraParams & params)
explicit

Definition at line 23 of file cp_camera.cpp.

23: params_(params) {}
CameraParams params_
Definition cp_camera.hpp:68
const CameraParams & params() const
Definition cp_camera.hpp:52

◆ ~CpCamera()

cl_rgb::CpCamera::~CpCamera ( )
override

Definition at line 25 of file cp_camera.cpp.

25{}

Member Function Documentation

◆ latestFrame()

FramePtr cl_rgb::CpCamera::latestFrame ( ) const

Definition at line 87 of file cp_camera.cpp.

88{
89 std::lock_guard<std::mutex> lock(mutex_);
90 return latest_;
91}
std::mutex mutex_
Definition cp_camera.hpp:73

References latest_, and mutex_.

Referenced by cl_rgb::CbSurveyTable::onEntry(), and cl_rgb::CbWaitForFrame::wireCompletionSignals().

Here is the caller graph for this function:

◆ model()

std::optional< PinholeCamera > cl_rgb::CpCamera::model ( const Frame & frame) const

Definition at line 93 of file cp_camera.cpp.

94{
95 PinholeCamera camera;
96 {
97 std::lock_guard<std::mutex> lock(mutex_);
98 if (!calibrated_)
99 {
100 return std::nullopt;
101 }
102 camera.fx = fx_;
103 camera.fy = fy_;
104 camera.cx = cx_;
105 camera.cy = cy_;
106 }
107 const auto pose = transforms_->lookup(params_.worldFrame, frame.frameId, frame.stamp);
108 if (!pose)
109 {
110 return std::nullopt;
111 }
112 const tf2::Matrix3x3 & basis = pose->getBasis();
113 for (int r = 0; r < 3; ++r)
114 {
115 for (int c = 0; c < 3; ++c)
116 {
117 camera.rotation(r, c) = basis[r][c];
118 }
119 }
120 const tf2::Vector3 & origin = pose->getOrigin();
121 camera.position = cv::Vec3d(origin.x(), origin.y(), origin.z());
122 return camera;
123}
CpTransforms * transforms_
Definition cp_camera.hpp:69
std::optional< tf2::Transform > lookup(const std::string &targetFrame, const std::string &sourceFrame, const rclcpp::Time &stamp) const
std::string worldFrame
Definition cp_camera.hpp:38

References calibrated_, cl_rgb::PinholeCamera::cx, cx_, cl_rgb::PinholeCamera::cy, cy_, cl_rgb::Frame::frameId, cl_rgb::PinholeCamera::fx, fx_, cl_rgb::PinholeCamera::fy, fy_, cl_rgb::CpTransforms::lookup(), mutex_, params_, cl_rgb::PinholeCamera::position, cl_rgb::PinholeCamera::rotation, cl_rgb::Frame::stamp, transforms_, and cl_rgb::CameraParams::worldFrame.

Referenced by cl_rgb::CbSurveyTable::onEntry().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ onImage()

void cl_rgb::CpCamera::onImage ( const sensor_msgs::msg::Image::ConstSharedPtr msg)
private

Definition at line 42 of file cp_camera.cpp.

43{
44 cv_bridge::CvImageConstPtr converted;
45 try
46 {
47 converted = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
48 }
49 catch (const cv_bridge::Exception & e)
50 {
51 RCLCPP_ERROR_THROTTLE(
52 getLogger(), *this->getNode()->get_clock(), 5000, "CpCamera '%s': cannot convert image: %s",
53 params_.name.c_str(), e.what());
54 return;
55 }
56
57 auto frame = std::make_shared<Frame>();
58 frame->image = converted->image;
59 frame->stamp = rclcpp::Time(msg->header.stamp, this->getNode()->get_clock()->get_clock_type());
60 frame->frameId = msg->header.frame_id;
61 FramePtr snapshot;
62 {
63 std::lock_guard<std::mutex> lock(mutex_);
64 frame->sequence = ++sequence_;
65 latest_ = frame;
66 snapshot = latest_;
67 }
68 onFrame_(snapshot);
69}
uint64_t sequence_
Definition cp_camera.hpp:75
smacc2::SmaccSignal< void(const FramePtr &)> onFrame_
Definition cp_camera.hpp:62
rclcpp::Logger getLogger() const
rclcpp::Node::SharedPtr getNode()
std::shared_ptr< const Frame > FramePtr
Definition types.hpp:39

References smacc2::ISmaccComponent::getLogger(), smacc2::ISmaccComponent::getNode(), latest_, mutex_, cl_rgb::CameraParams::name, onFrame_, params_, and sequence_.

Referenced by onInitialize().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ onInfo()

void cl_rgb::CpCamera::onInfo ( const sensor_msgs::msg::CameraInfo::ConstSharedPtr msg)
private

Definition at line 71 of file cp_camera.cpp.

72{
73 std::lock_guard<std::mutex> lock(mutex_);
74 if (!calibrated_)
75 {
76 RCLCPP_INFO(
77 getLogger(), "CpCamera '%s': calibrated, focal length %.1f px, %ux%u", params_.name.c_str(),
78 msg->k[0], msg->width, msg->height);
79 }
80 fx_ = msg->k[0];
81 fy_ = msg->k[4];
82 cx_ = msg->k[2];
83 cy_ = msg->k[5];
84 calibrated_ = fx_ > 0.0 && fy_ > 0.0;
85}

References calibrated_, cx_, cy_, fx_, fy_, smacc2::ISmaccComponent::getLogger(), mutex_, cl_rgb::CameraParams::name, and params_.

Referenced by onInitialize().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ onInitialize()

void cl_rgb::CpCamera::onInitialize ( )
overridevirtual

Reimplemented from smacc2::ISmaccComponent.

Definition at line 27 of file cp_camera.cpp.

28{
30 auto node = this->getNode();
31 // A machine that acts on single frames must not lose one: reliable, only the newest kept.
32 const rclcpp::QoS qos = rclcpp::QoS(rclcpp::KeepLast(2)).reliable();
33 imageSubscriber_ = node->create_subscription<sensor_msgs::msg::Image>(
34 params_.imageTopic, qos, std::bind(&CpCamera::onImage, this, std::placeholders::_1));
35 infoSubscriber_ = node->create_subscription<sensor_msgs::msg::CameraInfo>(
36 params_.infoTopic, qos, std::bind(&CpCamera::onInfo, this, std::placeholders::_1));
37 RCLCPP_INFO(
38 getLogger(), "CpCamera '%s': images on %s, calibration on %s", params_.name.c_str(),
39 params_.imageTopic.c_str(), params_.infoTopic.c_str());
40}
void onImage(const sensor_msgs::msg::Image::ConstSharedPtr msg)
Definition cp_camera.cpp:42
rclcpp::Subscription< sensor_msgs::msg::CameraInfo >::SharedPtr infoSubscriber_
Definition cp_camera.hpp:71
rclcpp::Subscription< sensor_msgs::msg::Image >::SharedPtr imageSubscriber_
Definition cp_camera.hpp:70
void onInfo(const sensor_msgs::msg::CameraInfo::ConstSharedPtr msg)
Definition cp_camera.cpp:71
void requiresComponent(TComponent *&requiredComponentStorage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
std::string infoTopic
Definition cp_camera.hpp:37
std::string imageTopic
Definition cp_camera.hpp:36

References smacc2::ISmaccComponent::getLogger(), smacc2::ISmaccComponent::getNode(), smacc2::HARD, imageSubscriber_, cl_rgb::CameraParams::imageTopic, infoSubscriber_, cl_rgb::CameraParams::infoTopic, cl_rgb::CameraParams::name, onImage(), onInfo(), params_, smacc2::ISmaccComponent::requiresComponent(), and transforms_.

Here is the call graph for this function:

◆ params()

const CameraParams & cl_rgb::CpCamera::params ( ) const
inline

Definition at line 52 of file cp_camera.hpp.

52{ return params_; }

References params_.

Member Data Documentation

◆ calibrated_

bool cl_rgb::CpCamera::calibrated_ = false
private

Definition at line 76 of file cp_camera.hpp.

Referenced by model(), and onInfo().

◆ cx_

double cl_rgb::CpCamera::cx_ = 0.0
private

Definition at line 77 of file cp_camera.hpp.

Referenced by model(), and onInfo().

◆ cy_

double cl_rgb::CpCamera::cy_ = 0.0
private

Definition at line 77 of file cp_camera.hpp.

Referenced by model(), and onInfo().

◆ fx_

double cl_rgb::CpCamera::fx_ = 1.0
private

Definition at line 77 of file cp_camera.hpp.

Referenced by model(), and onInfo().

◆ fy_

double cl_rgb::CpCamera::fy_ = 1.0
private

Definition at line 77 of file cp_camera.hpp.

Referenced by model(), and onInfo().

◆ imageSubscriber_

rclcpp::Subscription<sensor_msgs::msg::Image>::SharedPtr cl_rgb::CpCamera::imageSubscriber_
private

Definition at line 70 of file cp_camera.hpp.

Referenced by onInitialize().

◆ infoSubscriber_

rclcpp::Subscription<sensor_msgs::msg::CameraInfo>::SharedPtr cl_rgb::CpCamera::infoSubscriber_
private

Definition at line 71 of file cp_camera.hpp.

Referenced by onInitialize().

◆ latest_

FramePtr cl_rgb::CpCamera::latest_
private

Definition at line 74 of file cp_camera.hpp.

Referenced by latestFrame(), and onImage().

◆ mutex_

std::mutex cl_rgb::CpCamera::mutex_
mutableprivate

Definition at line 73 of file cp_camera.hpp.

Referenced by latestFrame(), model(), onImage(), and onInfo().

◆ onFrame_

smacc2::SmaccSignal<void(const FramePtr &)> cl_rgb::CpCamera::onFrame_

Definition at line 62 of file cp_camera.hpp.

Referenced by onImage(), and cl_rgb::CbWaitForFrame::wireCompletionSignals().

◆ params_

CameraParams cl_rgb::CpCamera::params_
private

Definition at line 68 of file cp_camera.hpp.

Referenced by model(), onImage(), onInfo(), onInitialize(), and params().

◆ sequence_

uint64_t cl_rgb::CpCamera::sequence_ = 0
private

Definition at line 75 of file cp_camera.hpp.

Referenced by onImage().

◆ transforms_

CpTransforms* cl_rgb::CpCamera::transforms_ = nullptr
private

Definition at line 69 of file cp_camera.hpp.

Referenced by model(), and onInitialize().


The documentation for this class was generated from the following files: