robot_state_publisher.cpp
Go to the documentation of this file.
1 #include <ros/common.h>
2 
3 // HACK In melodic, we cannot substitute the RobotStatePublisher in JointStateListener, so we hack it like this
4 #if !ROS_VERSION_MINIMUM(1, 15, 0)
5 #define protected public
6 #endif
7 #include <robot_state_publisher/robot_state_publisher.h>
8 #if !ROS_VERSION_MINIMUM(1, 15, 0)
9 #undef protected
10 #endif
11 
13 
14 using namespace robot_state_publisher;
15 
16 std::string stripSlash(const std::string & in)
17 {
18  if (!in.empty() && in[0] == '/')
19  {
20  return in.substr(1);
21  }
22  return in;
23 }
24 
26 {
29 
30  auto old_segments_fixed = this->getFixedSegments();
31 
32  this->getSegments().clear();
33  this->getFixedSegments().clear();
34  this->addChildren(tree.getRootSegment());
35 
36  // find out which segments have disappeared and publish a special TF message
37  // that disconnects them from the main TF tree
38 
39  for (const auto& seg : this->getFixedSegments())
40  {
41  old_segments_fixed.erase(seg.first);
42  }
43  for (const auto& seg : this->getSegments())
44  {
45  old_segments_fixed.erase(seg.first);
46  }
47 
48  std::vector<geometry_msgs::TransformStamped> deleteTfs;
49  for (const auto& seg : old_segments_fixed)
50  {
51  geometry_msgs::TransformStamped tf;
52  tf.child_frame_id = stripSlash(seg.second.tip);
54  tf.header.stamp = ros::Time::now();
55  tf.transform.rotation.w = 1.0;
56  deleteTfs.push_back(tf);
57  }
58  this->getStaticTfBroadcaster().sendTransform(deleteTfs);
59 }
60 
61 #if ROS_VERSION_MINIMUM(1, 15, 0)
63  const KDL::Tree& tree, const urdf::Model& model) : robot_state_publisher::RobotStatePublisher(tree, model)
64 #else
66  robot_state_publisher::RobotStatePublisher *publisher) : publisher(publisher)
67 #endif
68 {
69 }
70 
72 {
73  return this->getSegments().size();
74 }
75 
77 {
78  return this->getFixedSegments().size();
79 }
80 
82 {
83 #if ROS_VERSION_MINIMUM(1, 15, 0)
84  return this->segments_;
85 #else
86  return publisher->segments_;
87 #endif
88 }
89 
91 {
92 #if ROS_VERSION_MINIMUM(1, 15, 0)
93  return this->segments_fixed_;
94 #else
95  return publisher->segments_fixed_;
96 #endif
97 }
98 
99 const std::map<std::string, SegmentPair>& robot_state_publisher::DynamicRobotStatePublisher::getSegments() const
100 {
101 #if ROS_VERSION_MINIMUM(1, 15, 0)
102  return this->segments_;
103 #else
104  return publisher->segments_;
105 #endif
106 }
107 
108 const std::map<std::string, SegmentPair>& robot_state_publisher::DynamicRobotStatePublisher::getFixedSegments() const
109 {
110 #if ROS_VERSION_MINIMUM(1, 15, 0)
111  return this->segments_fixed_;
112 #else
113  return publisher->segments_fixed_;
114 #endif
115 }
116 
118 {
119 #if ROS_VERSION_MINIMUM(1, 15, 0)
120  return this->static_tf_broadcaster_;
121 #else
122  return publisher->static_tf_broadcaster_;
123 #endif
124 }
125 
126 #if !ROS_VERSION_MINIMUM(1, 15, 0)
127 void robot_state_publisher::DynamicRobotStatePublisher::addChildren(const KDL::SegmentMap::const_iterator segment)
128 {
129  publisher->addChildren(segment);
130 }
131 #endif
robot_state_publisher::stripSlash
std::string stripSlash(const std::string &in)
robot_state_publisher::DynamicRobotStatePublisher::DynamicRobotStatePublisher
DynamicRobotStatePublisher(RobotStatePublisher *publisher)
Create the publisher.
Definition: robot_state_publisher.cpp:65
robot_state_publisher::DynamicRobotStatePublisher::getNumFixedJoints
virtual size_t getNumFixedJoints() const
Return the number of fixed joints in the currently represented model.
Definition: robot_state_publisher.cpp:76
robot_state_publisher::RobotStatePublisher
urdf::Model
robot_state_publisher::DynamicRobotStatePublisher::addChildren
virtual void addChildren(const KDL::SegmentMap::const_iterator segment)
Definition: robot_state_publisher.cpp:127
robot_state_publisher::DynamicRobotStatePublisher
An alternative RobotStatePublisher with update option.
Definition: robot_state_publisher.h:11
tf2_ros::StaticTransformBroadcaster
robot_state_publisher::DynamicRobotStatePublisher::getStaticTfBroadcaster
tf2_ros::StaticTransformBroadcaster & getStaticTfBroadcaster()
Definition: robot_state_publisher.cpp:117
robot_state_publisher
robot_state_publisher.h
robot_state_publisher::DynamicRobotStatePublisher::getSegments
std::map< std::string, SegmentPair > & getSegments()
Definition: robot_state_publisher.cpp:81
robot_state_publisher::DynamicRobotStatePublisher::DELETED_STATIC_TFS_FRAME
const std::string DELETED_STATIC_TFS_FRAME
The TF frame name of the virtual frame that's parent of all deleted static TF frames.
Definition: robot_state_publisher.h:21
tf
robot_state_publisher::DynamicRobotStatePublisher::updateTree
virtual void updateTree(const KDL::Tree &tree)
Sets the robot model.
Definition: robot_state_publisher.cpp:25
robot_state_publisher::DynamicRobotStatePublisher::getNumMovingJoints
virtual size_t getNumMovingJoints() const
Return the number of moving joints in the currently represented model.
Definition: robot_state_publisher.cpp:71
robot_state_publisher::DynamicRobotStatePublisher::getFixedSegments
std::map< std::string, SegmentPair > & getFixedSegments()
Definition: robot_state_publisher.cpp:90
ros::Time::now
static Time now()


dynamic_robot_state_publisher
Author(s): Martin Pecka
autogenerated on Fri May 6 2022 02:34:15