Skip to content

Commit 8ce81fb

Browse files
author
Cristina Garcia Cifuentes
committed
add call to publish_joint_state in fusion_tracker_node.cpp
also: add comments and rename variables in robot_publisher and robot_transformer so that they are more understandable.
1 parent 25bc930 commit 8ce81fb

File tree

5 files changed

+154
-111
lines changed

5 files changed

+154
-111
lines changed

source/dbrt/robot_publisher.h

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* \file robot_publisher.h
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
@@ -41,58 +42,85 @@ namespace dbrt
4142
typedef Eigen::Matrix<fl::Real, Eigen::Dynamic, 1> JointsObsrv;
4243

4344
/**
44-
* \brief Represents the object tracker publisher. This publishes the object
45-
* estimated state and its marker.
45+
* \brief Represents the robot tracker publisher.
4646
*/
4747
template <typename State>
4848
class RobotPublisher
4949
{
5050
public:
5151
RobotPublisher(
5252
const std::shared_ptr<KinematicsFromURDF>& urdf_kinematics,
53-
const std::string& prefix,
54-
const std::string& target_frame_id);
53+
const std::string& estimated_prefix,
54+
const std::string& connecting_frame_name);
5555

56+
/**
57+
* \brief publish estimated tree on /tf topic
58+
*/
5659
void publish_tf(const State& state, const ros::Time& time);
60+
61+
/**
62+
* \brief publish estimated tree on /tf, connected to measured tree at the
63+
* root such that the connecting_frame_id in both trees align.
64+
*/
5765
void publish_tf(const State& state,
5866
const JointsObsrv& obsrv,
5967
const ros::Time& time);
6068

69+
/**
70+
* \brief publish estimated joint states to topic named
71+
* prefix_ + '/joint_states'
72+
*/
6173
void publish_joint_state(const State& state, const ros::Time time);
6274

75+
6376
protected:
77+
78+
void publish_tf_tree(const State& state, const ros::Time& time);
79+
6480
/**
65-
* This mapping function is required since the received JointsObsrv order
66-
* differs from the state type order provided through state.get_joint_map by
67-
* the URDF model.
81+
* \todo obsolete? -- better to not connect the trees than to connect them
82+
* with an arbitrary transform.
6883
*/
69-
void to_joint_map(const JointsObsrv& joint_values,
70-
std::map<std::string, double>& named_joint_values) const;
71-
void publish_tf_tree(const State& state, const ros::Time& time);
7284
void publish_id_transform(const ros::Time& time,
7385
const std::string& from,
7486
const std::string& to);
87+
7588
tf::StampedTransform get_root_transform(
76-
const std::map<std::string, double>& state_joint_map,
77-
const std::map<std::string, double>& obsrv_joint_map,
89+
const std::map<std::string, double>& estimated_joint_positions,
90+
const std::map<std::string, double>& observed_joint_positions,
7891
const ros::Time& time);
7992

93+
/**
94+
* This mapping function is required since the received JointsObsrv order
95+
* differs from the state type order provided through state.get_joint_map by
96+
* the URDF model.
97+
*/
98+
void to_joint_map(const JointsObsrv& joint_values,
99+
std::map<std::string, double>& named_joint_values) const;
100+
101+
80102

81103
protected:
82-
sensor_msgs::JointState joint_state_;
83104
ros::NodeHandle node_handle_;
105+
106+
// use this prefix to publish anything related to the estimated state
84107
std::string prefix_;
108+
109+
// for publishing estimated robot state of /tf topic
85110
std::shared_ptr<robot_state_pub::RobotStatePublisher>
86111
robot_state_publisher_;
87-
ros::Publisher pub_joint_state_;
112+
113+
// for publishing estimated joint states
114+
std::vector<std::string> joint_names_;
115+
sensor_msgs::JointState joint_state_msg_;
116+
ros::Publisher joint_state_publisher_;
88117

89118
// These are needed for publishing a transform between the two roots
90-
// such that a target frame_id is aligned in both.
119+
// such that a connecting_frame_id is aligned in both.
91120
// \todo There is probably redundancy in all this publisher mess.
121+
std::string root_frame_name_;
122+
std::string connecting_frame_name_;
92123
RobotTransformer transformer_;
93-
std::string connecting_frame_id;
94-
std::string root_frame_id_;
95-
std::vector<std::string> joint_names_;
96124

97125
};
98126
}

source/dbrt/robot_publisher.hpp

Lines changed: 75 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
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
4041
template <typename State>
4142
RobotPublisher<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

7371
template <typename State>
7472
void 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

8987
template <typename State>
9088
void 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

98111
template <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

121135
template <typename State>
122136
void 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

134148
template <typename State>
135149
void 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

147161
template <typename State>
148162
tf::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

175189
template <typename State>

0 commit comments

Comments
 (0)