SMACC2
Loading...
Searching...
No Matches
cb_wait_transform.cpp
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/*****************************************************************************************************************
16 *
17 * Authors: Pablo Inigo Blasco, Brett Aldrich
18 *
19 ******************************************************************************************************************/
21
22namespace cl_nav2z
23{
25 std::string targetFrame, std::string referenceFrame, rclcpp::Duration timeout)
26: targetFrame_(targetFrame), referenceFrame_(referenceFrame), timeout_(timeout)
27{
28}
29
31
33{
34 RCLCPP_INFO(
35 getLogger(), "[CbWaitTransform] ref %s -> target %s", referenceFrame_.c_str(),
36 targetFrame_.c_str());
37
38 tfBuffer_ = std::make_shared<tf2_ros::Buffer>(getNode()->get_clock());
39 tfListener_ = std::make_shared<tf2_ros::TransformListener>(*tfBuffer_);
40
41 tf2::Stamped<tf2::Transform> transform;
42 try
43 {
44 // the latest transform: this buffer is new, so it never holds data for the time of this call
45 auto transformstamped =
46 tfBuffer_->lookupTransform(targetFrame_, referenceFrame_, rclcpp::Time(0), timeout_);
47 tf2::fromMsg(transformstamped, transform);
48
49 result_ = transform;
50
51 RCLCPP_INFO(
52 getLogger(), "[CbWaitTransform] Success wait transform ref %s -> target %s",
53 referenceFrame_.c_str(), targetFrame_.c_str());
54 this->postSuccessEvent();
55 }
56 catch (tf2::TransformException & ex)
57 {
58 RCLCPP_ERROR_STREAM(
59 getLogger(), "[CbWaitTransform] Failure waiting transform ( ref "
60 << referenceFrame_ << "/ target " << targetFrame_ << " - " << ex.what());
61 this->postFailureEvent();
62 }
63}
64} // namespace cl_nav2z
std::shared_ptr< tf2_ros::TransformListener > tfListener_
CbWaitTransform(std::string targetFrame, std::string referenceFrame, rclcpp::Duration timeout)
std::shared_ptr< tf2_ros::Buffer > tfBuffer_
std::optional< tf2::Stamped< tf2::Transform > > result_
virtual rclcpp::Logger getLogger() const
virtual rclcpp::Node::SharedPtr getNode() const