SMACC2
Loading...
Searching...
No Matches
cp_transforms.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 <tf2_geometry_msgs/tf2_geometry_msgs.hpp>
18
19namespace cl_rgb
20{
21
23
25
27{
28 auto node = this->getNode();
29 buffer_ = std::make_shared<tf2_ros::Buffer>(node->get_clock());
30 // no thread of its own: the transforms arrive on the state machine's executor
31 listener_ = std::make_shared<tf2_ros::TransformListener>(*buffer_, node, false);
32}
33
34std::optional<tf2::Transform> CpTransforms::lookup(
35 const std::string & targetFrame, const std::string & sourceFrame,
36 const rclcpp::Time & stamp) const
37{
38 try
39 {
40 const auto msg = buffer_->lookupTransform(
41 targetFrame, sourceFrame, tf2::TimePoint(std::chrono::nanoseconds(stamp.nanoseconds())));
42 tf2::Transform transform;
43 tf2::fromMsg(msg.transform, transform);
44 return transform;
45 }
46 catch (const tf2::TransformException &)
47 {
48 return std::nullopt;
49 }
50}
51
52} // namespace cl_rgb
std::optional< tf2::Transform > lookup(const std::string &targetFrame, const std::string &sourceFrame, const rclcpp::Time &stamp) const
std::shared_ptr< tf2_ros::Buffer > buffer_
void onInitialize() override
std::shared_ptr< tf2_ros::TransformListener > listener_
rclcpp::Node::SharedPtr getNode()