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@dav.ee>
36  Desc: Helps debug and visualize transforms via the TF infrastructure
37 */
38 
40 
41 // TF
43 
44 // C++
45 #include <string>
46 
47 namespace rviz_visual_tools
48 {
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::Affine3d& 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;
64  tf2_msg.header.stamp = ros::Time::now();
65  tf::transformEigenToMsg(transform, tf2_msg.transform);
66 
67  // Prevent TF_DENORMALIZED_QUATERNION errors in TF2 from happening.
68  double quatNorm;
69 
70  // Normalizing the Quaternion
71  quatNorm = 1 / sqrt(tf2_msg.transform.rotation.x * tf2_msg.transform.rotation.x +
72  tf2_msg.transform.rotation.y * tf2_msg.transform.rotation.y +
73  tf2_msg.transform.rotation.z * tf2_msg.transform.rotation.z +
74  tf2_msg.transform.rotation.w * tf2_msg.transform.rotation.w);
75  tf2_msg.transform.rotation.x *= quatNorm;
76  tf2_msg.transform.rotation.y *= quatNorm;
77  tf2_msg.transform.rotation.z *= quatNorm;
78  tf2_msg.transform.rotation.w *= quatNorm;
79 
80  tf2_msg.header.frame_id = from_frame;
81  tf2_msg.child_frame_id = to_frame;
82 
83  // Check if this transform has already been added
84  for (auto& transform : transforms_)
85  {
86  if (transform.child_frame_id == to_frame && transform.header.frame_id == from_frame)
87  {
88  // ROS_WARN_STREAM_NAMED("tf_visual_tools", "This transform has already been added, updating");
89  transform.transform = tf2_msg.transform;
90  return true;
91  }
92  }
93  // This transform is unique, add
94  transforms_.push_back(tf2_msg);
95 
96  return true;
97 }
98 
100 {
101  ROS_DEBUG_STREAM_NAMED("tf_visual_tools", "Publishing transforms");
102 
103  // Update timestamps
104  for (auto& transform : transforms_)
105  {
106  transform.header.stamp = ros::Time::now();
107  }
108  // Publish
109  tf_pub_.sendTransform(transforms_);
110 }
111 
112 } // namespace rviz_visual_tools
tf2_ros::TransformBroadcaster tf_pub_
#define ROS_DEBUG_STREAM_NAMED(name, args)
bool publishTransform(const Eigen::Affine3d &transform, const std::string &from_frame, const std::string &to_frame)
Visualize transforms in Rviz, etc.
void transformEigenToMsg(const Eigen::Affine3d &e, geometry_msgs::Transform &m)
#define ROS_INFO_STREAM_NAMED(name, args)
std::vector< geometry_msgs::TransformStamped > transforms_
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...
TFVisualTools(double loop_hz=2)
Constructor.
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
INLINE Rall1d< T, V, S > sqrt(const Rall1d< T, V, S > &arg)
void sendTransform(const geometry_msgs::TransformStamped &transform)
static Time now()


rviz_visual_tools
Author(s): Dave Coleman
autogenerated on Mon Feb 25 2019 03:54:12