SMACC2
Loading...
Searching...
No Matches
cp_object_tracker_1.hpp
Go to the documentation of this file.
1// Copyright 2025 Robosoft 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
15#pragma once
18#include <smacc2/component.hpp>
19#include <vision_msgs/msg/detection3_d_array.hpp>
20
21namespace cl_foundation_pose
22{
23
24using namespace smacc2::client_core_components;
25
27{
28public:
29 // Declare the Component's default constructor.
31 {
32 // Gain access to the foundationpose subscriber component.
34
35 // Then hook each received message to store it into our little map/database.
37 }
38
39 void onDetection3DArrayReceived(const vision_msgs::msg::Detection3DArray & msg)
40 {
41 RCLCPP_INFO(getLogger(), "Received %ld detections", msg.detections.size());
42
43 for (auto & detection : msg.detections)
44 {
45 auto previouslyExistingObjectEntry = detectedObjects.find(detection.id);
46
47 // if we have seen this object before...
48 if (previouslyExistingObjectEntry != detectedObjects.end())
49 {
50 auto & previouslyExistingObject = previouslyExistingObjectEntry->second;
51 previouslyExistingObject.msg = detection;
52 }
53 else
54 {
55 DetectedObject detectedObject;
56 detectedObject.msg = detection;
57 detectedObjects[detection.id] = detectedObject;
58 }
59
61 }
62 }
63
64 std::optional<geometry_msgs::msg::PoseStamped> getPose(const std::string & object_id)
65 {
66 auto object = detectedObjects.find(object_id);
67 if (object != detectedObjects.end())
68 {
69 geometry_msgs::msg::PoseStamped pose;
70 pose.header = object->second.msg.header;
71 pose.pose = object->second.msg.results[0].pose.pose;
72 return pose;
73 }
74 return std::nullopt;
75 }
76
77private:
78 // Declare the subscriber component.
80
81 // Declare a data structure to store the detected objects.
82 std::map<std::string, DetectedObject> detectedObjects;
83};
84
85} // namespace cl_foundation_pose
std::optional< geometry_msgs::msg::PoseStamped > getPose(const std::string &object_id)
CpTopicSubscriber< vision_msgs::msg::Detection3DArray > * fondationPoseTopic_
void onDetection3DArrayReceived(const vision_msgs::msg::Detection3DArray &msg)
std::map< std::string, DetectedObject > detectedObjects
rclcpp::Logger getLogger() const
void requiresComponent(TComponent *&requiredComponentStorage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
smacc2::SmaccSignalConnection onMessageReceived(void(T::*callback)(const MessageType &), T *object)
vision_msgs::msg::Detection3D msg