tf_visual_tools.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2017, PickNik Consulting
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the Univ of CO, Boulder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Dave Coleman <dave@picknik.ai>
36  Desc: Helps debug and visualize transforms via the TF infrastructure
37 */
38 
40 
41 // TF2
42 #include <tf2_eigen/tf2_eigen.h>
43 
44 // C++
45 #include <string>
46 
47 namespace rviz_visual_tools
48 {
49 TFVisualTools::TFVisualTools(double loop_hz)
50 {
51  ros::Duration update_freq = ros::Duration(1.0 / loop_hz);
53 
54  ROS_INFO_STREAM_NAMED("tf_visual_tools", "TFVisualTools Ready.");
55 }
56 
57 bool TFVisualTools::publishTransform(const Eigen::Isometry3d& transform, const std::string& from_frame,
58  const std::string& to_frame)
59 {
60  ROS_DEBUG_STREAM_NAMED("tf_visual_tools", "Publishing transform from " << from_frame << " to " << to_frame);
61 
62  // Create transform msg
63  geometry_msgs::TransformStamped tf2_msg = tf2::eigenToTransform(transform);
64  tf2_msg.header.stamp = ros::Time::now();
65 
66  // Prevent TF_DENORMALIZED_QUATERNION errors in TF2 from happening.
67  double quat_norm;
68 
69  // Normalizing the Quaternion
70  quat_norm = 1 / sqrt(tf2_msg.transform.rotation.x * tf2_msg.transform.rotation.x +
71  tf2_msg.transform.rotation.y * tf2_msg.transform.rotation.y +
72  tf2_msg.transform.rotation.z * tf2_msg.transform.rotation.z +
73  tf2_msg.transform.rotation.w * tf2_msg.transform.rotation.w);
74  tf2_msg.transform.rotation.x *= quat_norm;
75  tf2_msg.transform.rotation.y *= quat_norm;
76  tf2_msg.transform.rotation.z *= quat_norm;
77  tf2_msg.transform.rotation.w *= quat_norm;
78 
79  tf2_msg.header.frame_id = from_frame;
80  tf2_msg.child_frame_id = to_frame;
81 
82  // Check if this transform has already been added
83  for (auto& transform : transforms_)
84  {
85  if (transform.child_frame_id == to_frame && transform.header.frame_id == from_frame)
86  {
87  // ROS_WARN_STREAM_NAMED("tf_visual_tools", "This transform has already been added, updating");
88  transform.transform = tf2_msg.transform;
89  return true;
90  }
91  }
92  // This transform is unique, add
93  transforms_.push_back(tf2_msg);
94 
95  return true;
96 }
97 
99 {
100  transforms_.clear();
101 }
102 
104 {
105  ROS_DEBUG_STREAM_NAMED("tf_visual_tools", "Publishing transforms");
106 
107  // Update timestamps
108  for (auto& transform : transforms_)
109  {
110  transform.header.stamp = ros::Time::now();
111  }
112  // Publish
114 }
115 
116 } // namespace rviz_visual_tools
rviz_visual_tools::TFVisualTools::nh_
ros::NodeHandle nh_
Definition: tf_visual_tools.h:156
rviz_visual_tools::TFVisualTools::publishTransform
bool publishTransform(const Eigen::Isometry3d &transform, const std::string &from_frame, const std::string &to_frame)
Visualize transforms in Rviz, etc.
Definition: tf_visual_tools.cpp:89
rviz_visual_tools::TFVisualTools::publishAllTransforms
void publishAllTransforms(const ros::TimerEvent &e)
At a certain frequency update the tf transforms that we are tracking This is called internally by a c...
Definition: tf_visual_tools.cpp:135
tf2_eigen.h
ROS_DEBUG_STREAM_NAMED
#define ROS_DEBUG_STREAM_NAMED(name, args)
rviz_visual_tools::TFVisualTools::non_realtime_loop_
ros::Timer non_realtime_loop_
Definition: tf_visual_tools.h:162
rviz_visual_tools
Definition: imarker_simple.h:55
rviz_visual_tools::TFVisualTools::transforms_
std::vector< geometry_msgs::TransformStamped > transforms_
Definition: tf_visual_tools.h:165
tf2::eigenToTransform
geometry_msgs::TransformStamped eigenToTransform(const Eigen::Affine3d &T)
rviz_visual_tools::TFVisualTools::tf_pub_
tf2_ros::TransformBroadcaster tf_pub_
Definition: tf_visual_tools.h:159
rviz_visual_tools::TFVisualTools::TFVisualTools
TFVisualTools(double loop_hz=2)
Constructor.
Definition: tf_visual_tools.cpp:81
tf2_ros::TransformBroadcaster::sendTransform
void sendTransform(const geometry_msgs::TransformStamped &transform)
ros::TimerEvent
tf_visual_tools.h
ROS_INFO_STREAM_NAMED
#define ROS_INFO_STREAM_NAMED(name, args)
rviz_visual_tools::TFVisualTools::clearAllTransforms
void clearAllTransforms()
Clear all transforms.
Definition: tf_visual_tools.cpp:130
ros::NodeHandle::createTimer
Timer createTimer(Duration period, const TimerCallback &callback, bool oneshot=false, bool autostart=true) const
ros::Duration
ros::Time::now
static Time now()


rviz_visual_tools
Author(s): Dave Coleman
autogenerated on Wed Mar 2 2022 01:03:26