urdf_visualizer.cc
Go to the documentation of this file.
1 /******************************************************************************
2 Copyright (c) 2017, Alexander W. Winkler. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6 
7 * Redistributions of source code must retain the above copyright notice, this
8  list of conditions and the following disclaimer.
9 
10 * Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation
12  and/or other materials provided with the distribution.
13 
14 * Neither the name of the copyright holder nor the names of its
15  contributors may be used to endorse or promote products derived from
16  this software without specific prior written permission.
17 
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 ******************************************************************************/
29 
31 
32 namespace xpp {
33 
34 UrdfVisualizer::UrdfVisualizer(const std::string& urdf_name,
35  const std::vector<URDFName>& joint_names_in_urdf,
36  const URDFName& base_joint_in_urdf,
37  const std::string& fixed_frame,
38  const std::string& state_topic,
39  const std::string& tf_prefix)
40 {
41  joint_names_in_urdf_ = joint_names_in_urdf;
42  base_joint_in_urdf_ = base_joint_in_urdf;
43  rviz_fixed_frame_ = fixed_frame;
44  tf_prefix_ = tf_prefix;
45 
47  state_sub_des_ = nh.subscribe(state_topic, 1, &UrdfVisualizer::StateCallback, this);
48  ROS_DEBUG("Subscribed to: %s", state_sub_des_.getTopic().c_str());
49 
50  // Load model from file
51  KDL::Tree my_kdl_tree;
52  urdf::Model my_urdf_model;
53  bool model_ok = my_urdf_model.initParam(urdf_name);
54  if(!model_ok)
55  {
56  ROS_ERROR("Invalid URDF File");
57  exit(EXIT_FAILURE);
58  }
59  ROS_DEBUG("URDF successfully parsed");
60  kdl_parser::treeFromUrdfModel(my_urdf_model, my_kdl_tree);
61  ROS_DEBUG("Robot tree is ready");
62 
63  robot_publisher = std::make_shared<robot_state_publisher::RobotStatePublisher>(my_kdl_tree);
64 }
65 
66 void
67 UrdfVisualizer::StateCallback(const xpp_msgs::RobotStateJoint& msg)
68 {
69  auto joint_positions = AssignAngleToURDFJointName(msg.joint_state);
70  auto W_X_B_message = GetBaseFromRos(::ros::Time::now(), msg.base.pose);
71 
72  tf_broadcaster_.sendTransform(W_X_B_message);
73  robot_publisher->publishTransforms(joint_positions, ::ros::Time::now(), tf_prefix_);
74  robot_publisher->publishFixedTransforms(tf_prefix_);
75 }
76 
78 UrdfVisualizer::AssignAngleToURDFJointName(const sensor_msgs::JointState &msg) const
79 {
81 
82  for (int i=0; i<msg.position.size(); ++i)
83  q[joint_names_in_urdf_.at(i)] = msg.position.at(i);
84 
85  return q;
86 }
87 
88 geometry_msgs::TransformStamped
89 UrdfVisualizer::GetBaseFromRos(const ::ros::Time& stamp,
90  const geometry_msgs::Pose &msg) const
91 {
92  // Converting from joint messages to robot state
93  geometry_msgs::TransformStamped W_X_B_message;
94  W_X_B_message.header.stamp = stamp;
95  W_X_B_message.header.frame_id = rviz_fixed_frame_;
96  W_X_B_message.child_frame_id = tf_prefix_ + "/" + base_joint_in_urdf_;
97 
98  W_X_B_message.transform.translation.x = msg.position.x;
99  W_X_B_message.transform.translation.y = msg.position.y;
100  W_X_B_message.transform.translation.z = msg.position.z;
101 
102  W_X_B_message.transform.rotation.w = msg.orientation.w;
103  W_X_B_message.transform.rotation.x = msg.orientation.x;
104  W_X_B_message.transform.rotation.y = msg.orientation.y;
105  W_X_B_message.transform.rotation.z = msg.orientation.z;
106 
107  return W_X_B_message;
108 }
109 
110 } // namespace xpp
tf::TransformBroadcaster::sendTransform
void sendTransform(const geometry_msgs::TransformStamped &transform)
xpp::UrdfVisualizer::joint_names_in_urdf_
std::vector< URDFName > joint_names_in_urdf_
Definition: urdf_visualizer.h:151
xpp::UrdfVisualizer::state_sub_des_
ros::Subscriber state_sub_des_
Definition: urdf_visualizer.h:141
xpp::UrdfVisualizer::rviz_fixed_frame_
std::string rviz_fixed_frame_
Definition: urdf_visualizer.h:155
ros::Subscriber::getTopic
std::string getTopic() const
ROS_DEBUG
#define ROS_DEBUG(...)
urdf::Model
kdl_parser::treeFromUrdfModel
KDL_PARSER_PUBLIC bool treeFromUrdfModel(const urdf::ModelInterface &robot_model, KDL::Tree &tree)
xpp::UrdfVisualizer::base_joint_in_urdf_
URDFName base_joint_in_urdf_
Definition: urdf_visualizer.h:152
xpp::UrdfVisualizer::UrdfnameToJointAngle
std::map< URDFName, double > UrdfnameToJointAngle
Definition: urdf_visualizer.h:117
xpp::UrdfVisualizer::robot_publisher
std::shared_ptr< robot_state_publisher::RobotStatePublisher > robot_publisher
Definition: urdf_visualizer.h:143
xpp::UrdfVisualizer::UrdfVisualizer
UrdfVisualizer(const std::string &urdf_name, const std::vector< URDFName > &joint_names_in_urdf, const URDFName &base_link_in_urdf, const std::string &rviz_fixed_frame, const std::string &state_topic, const std::string &tf_prefix="")
Constructs the visualizer for a specific URDF urdf_name.
Definition: urdf_visualizer.cc:61
ROS_ERROR
#define ROS_ERROR(...)
xpp::UrdfVisualizer::StateCallback
void StateCallback(const xpp_msgs::RobotStateJoint &msg)
Definition: urdf_visualizer.cc:94
ros::NodeHandle::subscribe
Subscriber subscribe(const std::string &topic, uint32_t queue_size, const boost::function< void(C)> &callback, const VoidConstPtr &tracked_object=VoidConstPtr(), const TransportHints &transport_hints=TransportHints())
ros::NodeHandle
xpp
xpp::UrdfVisualizer::GetBaseFromRos
geometry_msgs::TransformStamped GetBaseFromRos(const ::ros::Time &stamp, const geometry_msgs::Pose &msg) const
Definition: urdf_visualizer.cc:116
urdf::Model::initParam
URDF_EXPORT bool initParam(const std::string &param)
xpp::UrdfVisualizer::tf_prefix_
std::string tf_prefix_
Definition: urdf_visualizer.h:156
xpp::UrdfVisualizer::tf_broadcaster_
tf::TransformBroadcaster tf_broadcaster_
Definition: urdf_visualizer.h:142
urdf_visualizer.h
xpp::UrdfVisualizer::AssignAngleToURDFJointName
UrdfnameToJointAngle AssignAngleToURDFJointName(const sensor_msgs::JointState &msg) const
Definition: urdf_visualizer.cc:105
ros::Time::now
static Time now()


xpp_vis
Author(s): Alexander W. Winkler
autogenerated on Wed Mar 2 2022 01:14:16