SMACC2
Loading...
Searching...
No Matches
cp_camera.cpp
Go to the documentation of this file.
1// Copyright 2026 RobosoftAI Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16
17#include <cv_bridge/cv_bridge.hpp>
18#include <sensor_msgs/image_encodings.hpp>
19
20namespace cl_rgb
21{
22
23CpCamera::CpCamera(const CameraParams & params) : params_(params) {}
24
26
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}
41
42void CpCamera::onImage(const sensor_msgs::msg::Image::ConstSharedPtr msg)
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}
70
71void CpCamera::onInfo(const sensor_msgs::msg::CameraInfo::ConstSharedPtr msg)
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}
86
88{
89 std::lock_guard<std::mutex> lock(mutex_);
90 return latest_;
91}
92
93std::optional<PinholeCamera> CpCamera::model(const Frame & frame) const
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}
124
125} // namespace cl_rgb
void onImage(const sensor_msgs::msg::Image::ConstSharedPtr msg)
Definition cp_camera.cpp:42
FramePtr latestFrame() const
Definition cp_camera.cpp:87
CameraParams params_
Definition cp_camera.hpp:68
CpCamera(const CameraParams &params)
Definition cp_camera.cpp:23
rclcpp::Subscription< sensor_msgs::msg::CameraInfo >::SharedPtr infoSubscriber_
Definition cp_camera.hpp:71
void onInitialize() override
Definition cp_camera.cpp:27
uint64_t sequence_
Definition cp_camera.hpp:75
std::mutex mutex_
Definition cp_camera.hpp:73
~CpCamera() override
Definition cp_camera.cpp:25
smacc2::SmaccSignal< void(const FramePtr &)> onFrame_
Definition cp_camera.hpp:62
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
CpTransforms * transforms_
Definition cp_camera.hpp:69
std::optional< PinholeCamera > model(const Frame &frame) const
Definition cp_camera.cpp:93
std::optional< tf2::Transform > lookup(const std::string &targetFrame, const std::string &sourceFrame, const rclcpp::Time &stamp) const
rclcpp::Logger getLogger() const
void requiresComponent(TComponent *&requiredComponentStorage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
rclcpp::Node::SharedPtr getNode()
std::shared_ptr< const Frame > FramePtr
Definition types.hpp:39
std::string infoTopic
Definition cp_camera.hpp:37
std::string imageTopic
Definition cp_camera.hpp:36
std::string worldFrame
Definition cp_camera.hpp:38
std::string frameId
Definition types.hpp:36
rclcpp::Time stamp
Definition types.hpp:35
cv::Matx33d rotation
Definition geometry.hpp:32