Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion tf2_ros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ find_package(rclcpp REQUIRED)
find_package(rclcpp_action REQUIRED)
find_package(tf2 REQUIRED)
find_package(tf2_msgs REQUIRED)
find_package(rclcpp_components REQUIRED)

include_directories(include)
ament_python_install_package(${PROJECT_NAME} PACKAGE_DIR src/${PROJECT_NAME})
Expand Down Expand Up @@ -65,13 +66,27 @@ add_executable(static_transform_publisher
)
target_link_libraries(static_transform_publisher
${PROJECT_NAME}
static_transform_broadcaster_node
)
ament_target_dependencies(static_transform_publisher
"geometry_msgs"
"rclcpp"
"tf2_msgs"
)

add_library(static_transform_broadcaster_node SHARED
src/static_transform_broadcaster_node.cpp
)
target_compile_definitions(static_transform_broadcaster_node PRIVATE "STATIC_TRANSFORM_BROADCASTER_BUILDING_DLL")
target_link_libraries(static_transform_broadcaster_node ${PROJECT_NAME})
ament_target_dependencies(static_transform_broadcaster_node
"geometry_msgs"
"rclcpp"
"tf2_msgs"
"rclcpp_components"
)
rclcpp_components_register_nodes(static_transform_broadcaster_node "tf2_ros::StaticTransformBroadcasterNode")

add_executable(tf2_echo
src/tf2_echo.cpp
)
Expand Down Expand Up @@ -101,6 +116,13 @@ install(TARGETS
RUNTIME DESTINATION bin
)

install(TARGETS
static_transform_broadcaster_node
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
)

# install executables
install(TARGETS
buffer_server
Expand Down Expand Up @@ -199,7 +221,7 @@ endif()
endif()

ament_export_include_directories(include)
ament_export_libraries(${PROJECT_NAME})
ament_export_libraries(${PROJECT_NAME} static_transform_broadcaster_node)
ament_export_dependencies(message_filters)
ament_export_dependencies(rclcpp)
ament_export_dependencies(tf2_msgs)
Expand Down
54 changes: 54 additions & 0 deletions tf2_ros/include/tf2_ros/static_transform_broadcaster_node.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2019, Open Source Robotics Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef TF2_ROS__STATIC_TRANSFORM_BROADCASTER_NODE_HPP_
#define TF2_ROS__STATIC_TRANSFORM_BROADCASTER_NODE_HPP_

#include <memory>

#include "rclcpp/rclcpp.hpp"
#include "tf2_ros/static_transform_broadcaster.h"
#include "tf2_ros/static_transform_broadcaster_visibility_control.h"

namespace tf2_ros
{
class StaticTransformBroadcasterNode final : public rclcpp::Node
{
public:
STATIC_TRANSFORM_BROADCASTER_PUBLIC
explicit StaticTransformBroadcasterNode(const rclcpp::NodeOptions & options);

STATIC_TRANSFORM_BROADCASTER_PUBLIC
~StaticTransformBroadcasterNode() override = default;

private:
std::unique_ptr<tf2_ros::StaticTransformBroadcaster> broadcaster_;
};
}
#endif // TF2_ROS__STATIC_TRANSFORM_BROADCASTER_NODE_HPP_
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2019 Open Source Robotics Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef STATIC_TRANSFORM_BROADCASTER__STATIC_TRANSFORM_BROADCASTER_VISIBILITY_CONTROL_H_
#define STATIC_TRANSFORM_BROADCASTER__STATIC_TRANSFORM_BROADCASTER_VISIBILITY_CONTROL_H_

// This logic was borrowed (then namespaced) from the examples on the gcc wiki:
// https://gcc.gnu.org/wiki/Visibility

#if defined _WIN32 || defined __CYGWIN__
#ifdef __GNUC__
#define STATIC_TRANSFORM_BROADCASTER_EXPORT __attribute__ ((dllexport))
#define STATIC_TRANSFORM_BROADCASTER_IMPORT __attribute__ ((dllimport))
#else
#define STATIC_TRANSFORM_BROADCASTER_EXPORT __declspec(dllexport)
#define STATIC_TRANSFORM_BROADCASTER_IMPORT __declspec(dllimport)
#endif
#ifdef STATIC_TRANSFORM_BROADCASTER_BUILDING_DLL
#define STATIC_TRANSFORM_BROADCASTER_PUBLIC STATIC_TRANSFORM_BROADCASTER_EXPORT
#else
#define STATIC_TRANSFORM_BROADCASTER_PUBLIC STATIC_TRANSFORM_BROADCASTER_IMPORT
#endif
#define STATIC_TRANSFORM_BROADCASTER_PUBLIC_TYPE STATIC_TRANSFORM_BROADCASTER_PUBLIC
#define STATIC_TRANSFORM_BROADCASTER_LOCAL
#else
#define STATIC_TRANSFORM_BROADCASTER_EXPORT __attribute__ ((visibility("default")))
#define STATIC_TRANSFORM_BROADCASTER_IMPORT
#if __GNUC__ >= 4
#define STATIC_TRANSFORM_BROADCASTER_PUBLIC __attribute__ ((visibility("default")))
#define STATIC_TRANSFORM_BROADCASTER_LOCAL __attribute__ ((visibility("hidden")))
#else
#define STATIC_TRANSFORM_BROADCASTER_PUBLIC
#define STATIC_TRANSFORM_BROADCASTER_LOCAL
#endif
#define STATIC_TRANSFORM_BROADCASTER_PUBLIC_TYPE
#endif

#endif // STATIC_TRANSFORM_BROADCASTER__VISIBILITY_CONTROL_H_
1 change: 1 addition & 0 deletions tf2_ros/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<depend>message_filters</depend>
<depend>rclcpp</depend>
<depend>rclcpp_action</depend>
<depend>rclcpp_components</depend>
<depend>rclpy</depend>
<depend>tf2</depend>
<depend>tf2_msgs</depend>
Expand Down
104 changes: 104 additions & 0 deletions tf2_ros/src/static_transform_broadcaster_node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright (c) 2019, Open Source Robotics Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include <utility>
#include <memory>
#include <stdexcept>
#include <string>
#include <random>

#include "tf2_ros/static_transform_broadcaster_node.hpp"
#include "rclcpp/rclcpp.hpp"

namespace
{
std::string get_unique_node_name()
{
const static std::string chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
static std::random_device rd;
static std::minstd_rand g{rd()};

// The uniform_int_distribution takes a closed interval of [a, b]; since that
// would include the \0, we remove one from our string length.
static std::uniform_int_distribution<std::string::size_type> pick(0, chars.length() - 1);

std::string s{"static_transform_publisher_"};

size_t orig_length = s.length();
s.resize(orig_length + 16);

for (size_t i = orig_length; i < s.length(); ++i) {
s[i] = chars[pick(g)];
}

return s;
}
}

namespace tf2_ros
{
StaticTransformBroadcasterNode::StaticTransformBroadcasterNode(const rclcpp::NodeOptions & options)
: rclcpp::Node(get_unique_node_name().c_str(), options)
// TODO(clalancette): Anonymize the node name like it is in ROS1.
{
geometry_msgs::msg::TransformStamped tf_msg;
rcl_interfaces::msg::ParameterDescriptor descriptor;
descriptor.read_only = true;

tf_msg.header.stamp = this->now();
tf_msg.transform.translation.x = this->declare_parameter("/translation/x", 0.0, descriptor);
tf_msg.transform.translation.y = this->declare_parameter("/translation/y", 0.0, descriptor);
tf_msg.transform.translation.z = this->declare_parameter("/translation/z", 0.0, descriptor);
tf_msg.transform.rotation.x = this->declare_parameter("/rotation/x", 0.0, descriptor);
tf_msg.transform.rotation.y = this->declare_parameter("/rotation/y", 0.0, descriptor);
tf_msg.transform.rotation.z = this->declare_parameter("/rotation/z", 0.0, descriptor);
tf_msg.transform.rotation.w = this->declare_parameter("/rotation/w", 1.0, descriptor);
tf_msg.header.frame_id =
this->declare_parameter("/frame_id", std::string("/frame"), descriptor);
tf_msg.child_frame_id =
this->declare_parameter("/child_frame_id", std::string("/child"), descriptor);

// check frame_id != child_frame_id
if (tf_msg.header.frame_id == tf_msg.child_frame_id) {
RCLCPP_ERROR(this->get_logger(),
"cannot publish static transform from '%s' to '%s', exiting",
tf_msg.header.frame_id.c_str(), tf_msg.child_frame_id.c_str());
throw std::runtime_error("child_frame_id cannot equal frame_id");
}

broadcaster_ = std::make_unique<tf2_ros::StaticTransformBroadcaster>(this);

// send transform
broadcaster_->sendTransform(tf_msg);
}
} // namespace tf2_ros

#include "rclcpp_components/register_node_macro.hpp"

RCLCPP_COMPONENTS_REGISTER_NODE(tf2_ros::StaticTransformBroadcasterNode)
Loading