Skip to content

Commit 57318bf

Browse files
committed
doTransform() for KDL::Rotation
1 parent 036ca90 commit 57318bf

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

tf2_kdl/include/tf2_kdl/tf2_kdl.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,23 @@ struct defaultMessage<KDL::Rotation>
276276

277277
} // namespace impl
278278

279+
/**
280+
* \brief Transform a KDL::Rotation
281+
*
282+
* \param[in] data_in The data to be transformed.
283+
* \param[in,out] data_out A reference to the output data.
284+
* \param[in] transform The transform to apply to data_in to fill data_out.
285+
*/
286+
template<>
287+
void doTransform(
288+
const KDL::Rotation & in, KDL::Rotation & out,
289+
const geometry_msgs::msg::TransformStamped & transform)
290+
{
291+
KDL::Rotation t;
292+
tf2::fromMsg<>(transform.transform.rotation, t);
293+
out = t * in;
294+
}
295+
279296
} // namespace tf2
280297

281298
#endif // TF2_KDL_H

tf2_kdl/test/test_tf2_kdl.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,32 @@ TEST(TfKDL, Vector)
9696
EXPECT_NEAR(v_advanced[2], 27, EPS);
9797
}
9898

99+
TEST(TfKDL, Quaternion)
100+
{
101+
const tf2::Stamped<KDL::Rotation> q1{KDL::Rotation::Rot({0, 1, 0}, -0.5 * M_PI), tf2::timeFromSec(
102+
2.0), "A"};
103+
104+
// simple api
105+
const tf2::Stamped<KDL::Rotation> q_simple =
106+
tf_buffer->transform(q1, "B", tf2::durationFromSec(2.0));
107+
double x, y, z, w;
108+
q_simple.GetQuaternion(x, y, z, w);
109+
EXPECT_NEAR(x, 0.707107, EPS);
110+
EXPECT_NEAR(y, 0, EPS);
111+
EXPECT_NEAR(z, -0.707107, EPS);
112+
EXPECT_NEAR(w, 0, EPS);
113+
114+
// advanced api
115+
const tf2::Stamped<KDL::Rotation> q_advanced = tf_buffer->transform(
116+
q1, "B", tf2::timeFromSec(2.0),
117+
"A", tf2::durationFromSec(3.0));
118+
q_advanced.GetQuaternion(x, y, z, w);
119+
EXPECT_NEAR(x, 0.707107, EPS);
120+
EXPECT_NEAR(y, 0, EPS);
121+
EXPECT_NEAR(z, -0.707107, EPS);
122+
EXPECT_NEAR(w, 0, EPS);
123+
}
124+
99125
TEST(TfKDL, ConvertVector)
100126
{
101127
tf2::Stamped<KDL::Vector> v(
@@ -219,6 +245,7 @@ int main(int argc, char **argv){
219245

220246
rclcpp::Clock::SharedPtr clock = std::make_shared<rclcpp::Clock>(RCL_SYSTEM_TIME);
221247
tf_buffer = std::make_unique<tf2_ros::Buffer>(clock);
248+
tf_buffer->setUsingDedicatedThread(true);
222249

223250
// populate buffer
224251
geometry_msgs::msg::TransformStamped t;

0 commit comments

Comments
 (0)