1515 * \file robot_publisher.hpp
1616 * \date January 2016
1717 * \author Jan Issac (jan.issac@gmail.com)
18+ * \author C. Garcia Cifuentes (c.garciacifuentes@gmail.com)
1819 */
1920
2021#pragma once
@@ -40,101 +41,114 @@ class ObjectRenderTools
4041template <typename State>
4142RobotPublisher<State>::RobotPublisher(
4243 const std::shared_ptr<KinematicsFromURDF>& urdf_kinematics,
43- const std::string& prefix ,
44- const std::string& target_frame_id )
44+ const std::string& estimated_prefix ,
45+ const std::string& connecting_frame_name )
4546 : node_handle_(" ~" ),
46- prefix_ (prefix),
47- connecting_frame_id(target_frame_id),
47+ prefix_ (estimated_prefix),
4848 robot_state_publisher_(
4949 std::make_shared<robot_state_pub::RobotStatePublisher>(
5050 urdf_kinematics->get_tree ())),
51+ joint_names_(urdf_kinematics->get_joint_map ()),
52+ root_frame_name_(urdf_kinematics->get_root_frame_id ()),
53+ connecting_frame_name_(connecting_frame_name),
5154 transformer_(robot_state_publisher_)
5255{
53- // get the name of the root frame
54- root_frame_id_ = urdf_kinematics->get_root_frame_id ();
55-
56- // get joint map
57- joint_names_ = urdf_kinematics->get_joint_map ();
58-
59- // setup basic joint state message
60- joint_state_.position .resize (joint_names_.size ());
61- joint_state_.effort .resize (joint_names_.size ());
62- joint_state_.velocity .resize (joint_names_.size ());
63- for (int i = 0 ; i < joint_names_.size (); ++i)
64- {
65- joint_state_.name .push_back (joint_names_[i]);
56+ // setup basic joint angle message
57+ auto sz = joint_names_.size ();
58+ joint_state_msg_.position .resize (sz);
59+ joint_state_msg_.effort .resize (sz);
60+ joint_state_msg_.velocity .resize (sz);
61+ joint_state_msg_.name .clear ();
62+ for (int i = 0 ; i < sz; ++i) {
63+ joint_state_msg_.name .push_back (joint_names_[i]);
6664 }
6765
6866 // joint angle publisher
69- pub_joint_state_ = node_handle_.advertise <sensor_msgs::JointState>(
67+ joint_state_publisher_ = node_handle_.advertise <sensor_msgs::JointState>(
7068 prefix_ + " /joint_states" , 0 );
7169}
7270
7371template <typename State>
7472void RobotPublisher<State>::publish_joint_state(const State& state,
75- const ros::Time time)
73+ const ros::Time time)
7674{
77- ROS_FATAL_COND (joint_state_ .position .size () != state.size (),
75+ ROS_FATAL_COND (joint_state_msg_ .position .size () != state.size (),
7876 " Joint state message and robot state sizes do not match" );
7977
80- joint_state_ .header .stamp = time;
78+ joint_state_msg_ .header .stamp = time;
8179 for (int i = 0 ; i < state.size (); ++i)
8280 {
83- joint_state_ .position [i] = state (i, 0 );
81+ joint_state_msg_ .position [i] = state (i, 0 );
8482 }
8583
86- pub_joint_state_ .publish (joint_state_ );
84+ joint_state_publisher_ .publish (joint_state_msg_ );
8785}
8886
8987template <typename State>
9088void RobotPublisher<State>::publish_tf(const State& state,
9189 const ros::Time& time)
9290{
93- publish_id_transform (
94- time, " /" + root_frame_id_, tf::resolve (prefix_, root_frame_id_));
91+ /* *
92+ * [Cris] I'm removing this id transform. Either we have and use an explicit
93+ * connecting_frame_id, or we don't connect at all.
94+ *
95+ * This may break some rviz visualization, but prevents the user from
96+ * inadvertedly looking up transforms that don't make sense.
97+ *
98+ * To obtain this id behavior, the user needs to set the
99+ * connecting_frame_id to be the root (in the corresponding config file).
100+ */
101+ // Publish id transform between roots
102+ // publish_id_transform(
103+ // time,
104+ // tf::resolve("", root_frame_name_),
105+ // tf::resolve(prefix_, root_frame_name_));
106+
107+ // Publish estimated tree
95108 publish_tf_tree (state, time);
96109}
97110
98111template <typename State>
99- void RobotPublisher<State>::publish_tf(
100- const State& state,
101- const JointsObsrv& obsrv,
102- const ros::Time& time)
112+ void RobotPublisher<State>::publish_tf(const State& state,
113+ const JointsObsrv& obsrv,
114+ const ros::Time& time)
103115{
104- std::map<std::string, double > state_joint_map;
105- std::map<std::string, double > obsrv_joint_map;
106- state.GetJointState (state_joint_map);
107- to_joint_map (obsrv, obsrv_joint_map);
108-
109- // get the transform between the estimated root and measured root both
110- // linked at the specified target frame
111- tf::StampedTransform root_transform = get_root_transform (state_joint_map,
112- obsrv_joint_map,
113- time);
114-
116+ std::map<std::string, double > estimated_joint_positions;
117+ std::map<std::string, double > observed_joint_positions;
118+ state.GetJointState (estimated_joint_positions);
119+ to_joint_map (obsrv, observed_joint_positions);
120+
121+ // Get the transform between the estimated root and measured root,
122+ // such that the estimated tree and measured tree are aligned
123+ // at the connecting frame
124+ tf::StampedTransform root_transform = get_root_transform (
125+ estimated_joint_positions, observed_joint_positions, time);
126+
127+ // Publish transform between roots
115128 static tf::TransformBroadcaster br;
116129 br.sendTransform (root_transform);
117130
131+ // Publish estimated tree
118132 publish_tf_tree (state, time);
119133}
120134
121135template <typename State>
122136void RobotPublisher<State>::publish_tf_tree(const State& state,
123- const ros::Time& time)
137+ const ros::Time& time)
124138{
125- // publish movable joints
139+ // Publish movable joints
126140 std::map<std::string, double > joint_positions;
127141 state.GetJointState (joint_positions);
128142 robot_state_publisher_->publishTransforms (joint_positions, time, prefix_);
129143
130- // publish fixed transforms
144+ // Publish fixed transforms
131145 robot_state_publisher_->publishFixedTransforms (prefix_, time);
132146}
133147
134148template <typename State>
135149void RobotPublisher<State>::publish_id_transform(const ros::Time& time,
136- const std::string& from,
137- const std::string& to)
150+ const std::string& from,
151+ const std::string& to)
138152{
139153 if (from == to) return ;
140154
@@ -146,30 +160,30 @@ void RobotPublisher<State>::publish_id_transform(const ros::Time& time,
146160
147161template <typename State>
148162tf::StampedTransform RobotPublisher<State>::get_root_transform(
149- const std::map<std::string, double >& state_joint_map ,
150- const std::map<std::string, double >& obsrv_joint_map ,
163+ const std::map<std::string, double >& estimated_joint_positions ,
164+ const std::map<std::string, double >& observed_joint_positions ,
151165 const ros::Time& time)
152166{
153- // Lookup transform from target to root of joint map 1
154- transformer_.set_joints (state_joint_map );
155- tf::StampedTransform transform_t_r ;
167+ // Lookup transform target<- root in estimated tree
168+ transformer_.set_joints (estimated_joint_positions );
169+ tf::StampedTransform estimated_target_root ;
156170 transformer_.lookup_transform (
157- connecting_frame_id, root_frame_id_, transform_t_r );
171+ connecting_frame_name_, root_frame_name_, estimated_target_root );
158172
159- // Lookup transform from root to target of joint map 2
160- transformer_.set_joints (obsrv_joint_map );
161- tf::StampedTransform transform_r_t ;
173+ // Lookup transform root<- target in measured tree
174+ transformer_.set_joints (observed_joint_positions );
175+ tf::StampedTransform observed_root_target ;
162176 transformer_.lookup_transform (
163- root_frame_id_, connecting_frame_id, transform_r_t );
177+ root_frame_name_, connecting_frame_name_, observed_root_target );
164178
165- // Compose the two tf_transforms
166- tf::StampedTransform transform_r_r (
167- transform_r_t * transform_t_r ,
179+ // Compose the two transforms
180+ tf::StampedTransform transform_root_root (
181+ observed_root_target * estimated_target_root ,
168182 time,
169- tf::resolve (" " , root_frame_id_ ),
170- tf::resolve (prefix_, root_frame_id_ ));
183+ tf::resolve (" " , root_frame_name_ ),
184+ tf::resolve (prefix_, root_frame_name_ ));
171185
172- return transform_r_r ;
186+ return transform_root_root ;
173187}
174188
175189template <typename State>
0 commit comments