SMACC2
Loading...
Searching...
No Matches
cb_survey_table.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 <chrono>
18#include <thread>
19
20namespace cl_rgb
21{
22
24: CbRgbClientBehaviorBase(params.camera), params_(params)
25{
26}
27
29{
30 if (camera_ == nullptr || table_ == nullptr || segmenter_ == nullptr || survey_ == nullptr)
31 {
32 RCLCPP_ERROR(
33 getLogger(),
34 "CbSurveyTable: a component is missing (camera '%s' %d, table %d, segmenter %d, survey %d)",
35 params_.camera.c_str(), camera_ != nullptr, table_ != nullptr, segmenter_ != nullptr,
36 survey_ != nullptr);
37 this->postRgbFailure();
38 return;
39 }
40
41 // The latest image, with the camera as it stood when the image was taken.
42 FramePtr frame;
43 std::optional<PinholeCamera> model;
44 const auto deadline =
45 std::chrono::steady_clock::now() + std::chrono::duration<double>(params_.waitS);
46 while (!this->isShutdownRequested())
47 {
48 frame = camera_->latestFrame();
49 if (frame)
50 {
51 model = camera_->model(*frame);
52 if (model)
53 {
54 break;
55 }
56 }
57 if (std::chrono::steady_clock::now() > deadline)
58 {
59 RCLCPP_ERROR(
60 getLogger(),
61 "CbSurveyTable: no usable image of '%s' after %.1f s (image %d, calibration and pose %d)",
62 params_.camera.c_str(), params_.waitS, frame != nullptr, model.has_value());
63 this->postRgbFailure();
64 return;
65 }
66 std::this_thread::sleep_for(std::chrono::milliseconds(20));
67 }
68 if (!frame || !model)
69 {
70 this->postRgbFailure();
71 return;
72 }
73
74 const auto start = std::chrono::steady_clock::now();
75 const cv::Mat region = table_->regionMask(*model, frame->image.size());
76 const cv::Mat foreground = segmenter_->foreground(frame->image, region);
77 auto survey = std::make_shared<Survey>();
78 survey->stamp = frame->stamp;
79 survey->camera = params_.camera;
80 survey->model = *model;
81 survey->sequence = frame->sequence;
82 survey->blobs = findBlobs(frame->image, foreground, *model, table_->params(), params_.blobs);
83 const double ms =
84 std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - start).count();
85
86 RCLCPP_INFO(
87 getLogger(), "CbSurveyTable: %zu blob(s) in image %lu of '%s' (%.1f ms)", survey->blobs.size(),
88 static_cast<unsigned long>(frame->sequence), params_.camera.c_str(), ms);
89 for (const auto & blob : survey->blobs)
90 {
91 RCLCPP_INFO(
92 getLogger(),
93 "CbSurveyTable: blob %d at x %+.3f y %+.3f m, %.0f x %.0f mm, axis %+.0f deg, %d px, "
94 "colour L %.0f a %+.0f b %+.0f",
95 blob.id, blob.tablePoint[0], blob.tablePoint[1], blob.lengthM * 1000.0, blob.widthM * 1000.0,
96 blob.axisRad * 180.0 / CV_PI, blob.areaPx, blob.meanLab[0], blob.meanLab[1], blob.meanLab[2]);
97 }
98
99 const bool enough = static_cast<int>(survey->blobs.size()) >= params_.minBlobs;
100 survey_->store(survey);
101 if (enough)
102 {
103 this->postRgbSuccess();
104 }
105 else
106 {
107 RCLCPP_WARN(
108 getLogger(), "CbSurveyTable: %zu blob(s), %d needed - posting failure", survey->blobs.size(),
110 this->postRgbFailure();
111 }
112}
113
114} // namespace cl_rgb
SurveyTableParams params_
CbSurveyTable(const SurveyTableParams &params=SurveyTableParams())
FramePtr latestFrame() const
Definition cp_camera.cpp:87
std::optional< PinholeCamera > model(const Frame &frame) const
Definition cp_camera.cpp:93
void store(const SurveyPtr &survey)
cv::Mat regionMask(const PinholeCamera &camera, const cv::Size &size) const
const TableParams & params() const
cv::Mat foreground(const cv::Mat &bgr, const cv::Mat &tableMask) const
virtual rclcpp::Logger getLogger() const
bool isShutdownRequested()
onEntry is executed in a new thread. However the current state cannot be left until the onEntry threa...
std::shared_ptr< const Frame > FramePtr
Definition types.hpp:39
std::vector< Blob > findBlobs(const cv::Mat &bgr, const cv::Mat &foreground, const PinholeCamera &camera, const TableParams &table, const BlobParams &params)