rigid_body_publisher.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, Houston Mechatronics Inc., JD Yamokoski
3  * Copyright (c) 2012, Clearpath Robotics, Inc., Alex Bencz
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. 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
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
31 
32 #include <geometry_msgs/PoseStamped.h>
33 #include <geometry_msgs/Pose2D.h>
34 #include <nav_msgs/Odometry.h>
35 #include <vector>
36 
37 namespace mocap_optitrack
38 {
39 
40 namespace utilities
41 {
42 geometry_msgs::PoseStamped getRosPose(RigidBody const& body, const Version& coordinatesVersion)
43 {
44  geometry_msgs::PoseStamped poseStampedMsg;
45  if (coordinatesVersion < Version("2.0") && coordinatesVersion >= Version("1.7"))
46  {
47  // Motive 1.7+ and < Motive 2.0 coordinate system
48  poseStampedMsg.pose.position.x = -body.pose.position.x;
49  poseStampedMsg.pose.position.y = body.pose.position.z;
50  poseStampedMsg.pose.position.z = body.pose.position.y;
51 
52  poseStampedMsg.pose.orientation.x = -body.pose.orientation.x;
53  poseStampedMsg.pose.orientation.y = body.pose.orientation.z;
54  poseStampedMsg.pose.orientation.z = body.pose.orientation.y;
55  poseStampedMsg.pose.orientation.w = body.pose.orientation.w;
56  }
57  else
58  {
59  // y & z axes are swapped in the Optitrack coordinate system
60  // Also compatible with versions > Motive 2.0
61  poseStampedMsg.pose.position.x = body.pose.position.x;
62  poseStampedMsg.pose.position.y = -body.pose.position.z;
63  poseStampedMsg.pose.position.z = body.pose.position.y;
64 
65  poseStampedMsg.pose.orientation.x = body.pose.orientation.x;
66  poseStampedMsg.pose.orientation.y = -body.pose.orientation.z;
67  poseStampedMsg.pose.orientation.z = body.pose.orientation.y;
68  poseStampedMsg.pose.orientation.w = body.pose.orientation.w;
69  }
70  return poseStampedMsg;
71 }
72 nav_msgs::Odometry getRosOdom(RigidBody const& body, const Version& coordinatesVersion)
73 {
74  nav_msgs::Odometry OdometryMsg;
75  if (coordinatesVersion < Version("2.0") && coordinatesVersion >= Version("1.7"))
76  {
77  // Motive 1.7+ and < Motive 2.0 coordinate system
78  OdometryMsg.pose.pose.position.x = -body.pose.position.x;
79  OdometryMsg.pose.pose.position.y = body.pose.position.z;
80  OdometryMsg.pose.pose.position.z = body.pose.position.y;
81 
82  OdometryMsg.pose.pose.orientation.x = -body.pose.orientation.x;
83  OdometryMsg.pose.pose.orientation.y = body.pose.orientation.z;
84  OdometryMsg.pose.pose.orientation.z = body.pose.orientation.y;
85  OdometryMsg.pose.pose.orientation.w = body.pose.orientation.w;
86  }
87  else
88  {
89  // y & z axes are swapped in the Optitrack coordinate system
90  // Also compatible with versions > Motive 2.0
91  OdometryMsg.pose.pose.position.x = body.pose.position.x;
92  OdometryMsg.pose.pose.position.y = -body.pose.position.z;
93  OdometryMsg.pose.pose.position.z = body.pose.position.y;
94 
95  OdometryMsg.pose.pose.orientation.x = body.pose.orientation.x;
96  OdometryMsg.pose.pose.orientation.y = -body.pose.orientation.z;
97  OdometryMsg.pose.pose.orientation.z = body.pose.orientation.y;
98  OdometryMsg.pose.pose.orientation.w = body.pose.orientation.w;
99  }
100  return OdometryMsg;
101 }
102 } // namespace utilities
103 
105  Version const& natNetVersion,
106  PublisherConfiguration const& config) :
107  config(config)
108 {
109  if (config.publishPose)
110  posePublisher = nh.advertise<geometry_msgs::PoseStamped>(config.poseTopicName, 1000);
111 
112  if (config.publishPose2d)
113  pose2dPublisher = nh.advertise<geometry_msgs::Pose2D>(config.pose2dTopicName, 1000);
114 
115  if (config.publishOdom)
116  odomPublisher = nh.advertise<nav_msgs::Odometry>(config.odomTopicName, 1000);
117 
118  // Motive 1.7+ uses a new coordinate system
119  // natNetVersion = (natNetVersion >= Version("1.7"));
120  coordinatesVersion = natNetVersion;
121 }
122 
124 {
125 }
126 
127 void RigidBodyPublisher::publish(ros::Time const& time, RigidBody const& body)
128 {
129  // don't do anything if no new data was provided
130  if (!body.hasValidData())
131  {
132  return;
133  }
134 
135  // NaN?
136  if (body.pose.position.x != body.pose.position.x)
137  {
138  return;
139  }
140 
141  geometry_msgs::PoseStamped pose = utilities::getRosPose(body, coordinatesVersion);
142  nav_msgs::Odometry odom = utilities::getRosOdom(body, coordinatesVersion);
143 
144  pose.header.stamp = time;
145  odom.header.stamp = time;
146 
147  if (config.publishPose)
148  {
149  pose.header.frame_id = config.parentFrameId;
150  posePublisher.publish(pose);
151  }
152 
153  tf::Quaternion q(pose.pose.orientation.x,
154  pose.pose.orientation.y,
155  pose.pose.orientation.z,
156  pose.pose.orientation.w);
157 
158  if (config.publishOdom)
159  {
160  odom.header.frame_id = config.parentFrameId;
161  odom.child_frame_id = config.childFrameId;
162  odomPublisher.publish(odom);
163  }
164  if (config.publishOdom)
165  {
166  odom.header.frame_id = config.parentFrameId;
167  odom.child_frame_id = config.childFrameId;
168  odomPublisher.publish(odom);
169  }
170  // publish 2D pose
171  if (config.publishPose2d)
172  {
173  geometry_msgs::Pose2D pose2d;
174  pose2d.x = pose.pose.position.x;
175  pose2d.y = pose.pose.position.y;
176  pose2d.theta = tf::getYaw(q);
177  pose2dPublisher.publish(pose2d);
178  }
179 
180  if (config.publishTf)
181  {
182  // publish transform
183  tf::Transform transform;
184  transform.setOrigin(tf::Vector3(pose.pose.position.x,
185  pose.pose.position.y,
186  pose.pose.position.z));
187 
188  // Handle different coordinate systems (Arena vs. rviz)
189  transform.setRotation(q);
191  time,
194  }
195 }
196 
197 
199  ros::NodeHandle &nh,
200  Version const& natNetVersion,
201  PublisherConfigurations const& configs)
202 {
203  for (auto const& config : configs)
204  {
205  rigidBodyPublisherMap[config.rigidBodyId] =
206  RigidBodyPublisherPtr(new RigidBodyPublisher(nh, natNetVersion, config));
207  }
208 }
209 
211  ros::Time const& time,
212  std::vector<RigidBody> const& rigidBodies)
213 {
214  for (auto const& rigidBody : rigidBodies)
215  {
216  auto const& iter = rigidBodyPublisherMap.find(rigidBody.bodyId);
217 
218  if (iter != rigidBodyPublisherMap.end())
219  {
220  (*iter->second).publish(time, rigidBody);
221  }
222  }
223 }
224 
225 } // namespace mocap_optitrack
TFSIMD_FORCE_INLINE void setRotation(const Quaternion &q)
nav_msgs::Odometry getRosOdom(RigidBody const &body, const Version &coordinatesVersion)
void publish(ros::Time const &time, std::vector< RigidBody > const &)
RigidBodyPublishDispatcher(ros::NodeHandle &nh, Version const &natNetVersion, PublisherConfigurations const &configs)
void publish(const boost::shared_ptr< M > &message) const
geometry_msgs::PoseStamped getRosPose(RigidBody const &body, const Version &coordinatesVersion)
static double getYaw(const Quaternion &bt_q)
Data object holding information about a single rigid body within a mocap skeleton.
Definition: data_model.h:68
std::shared_ptr< RigidBodyPublisher > RigidBodyPublisherPtr
Version class containing the version information and helpers for comparison.
Definition: version.h:38
void publish(ros::Time const &time, RigidBody const &)
ROS publisher configuration.
Definition: mocap_config.h:61
void sendTransform(const StampedTransform &transform)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
RigidBodyPublisher(ros::NodeHandle &nh, Version const &natNetVersion, PublisherConfiguration const &config)
std::vector< PublisherConfiguration > PublisherConfigurations
Definition: mocap_config.h:77
bool hasValidData() const
Definition: data_model.cpp:40
TFSIMD_FORCE_INLINE void setOrigin(const Vector3 &origin)


mocap_optitrack
Author(s): Kathrin Gräve , Alex Bencz/ , Tony Baltovski , JD Yamokoski
autogenerated on Fri Mar 26 2021 02:05:51