-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tf2 conversion library and replace cv::Quat
- Loading branch information
1 parent
c2f2a98
commit 858cbfb
Showing
3 changed files
with
97 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include <Eigen/Geometry> | ||
#include <apriltag/common/matd.h> | ||
#include <apriltag_pose.h> | ||
#include <geometry_msgs/msg/quaternion.hpp> | ||
#include <geometry_msgs/msg/vector3.hpp> | ||
#include <opencv2/core/mat.hpp> | ||
#include <opencv2/core/quaternion.hpp> | ||
#include <tf2/convert.h> | ||
|
||
template<> | ||
void tf2::convert(const Eigen::Quaterniond& eq, geometry_msgs::msg::Quaternion& gq) | ||
{ | ||
gq.w = eq.w(); | ||
gq.x = eq.x(); | ||
gq.y = eq.y(); | ||
gq.z = eq.z(); | ||
} | ||
|
||
template<> | ||
void tf2::convert(const matd_t& mat, geometry_msgs::msg::Vector3& t) | ||
{ | ||
assert(mat.nrows == 3 || mat.ncols == 3); | ||
|
||
t.x = mat.data[0]; | ||
t.y = mat.data[1]; | ||
t.z = mat.data[2]; | ||
} | ||
|
||
template<> | ||
void tf2::convert(const cv::Mat_<double>& vec, geometry_msgs::msg::Vector3& t) | ||
{ | ||
assert(vec.rows == 3 || vec.cols == 3); | ||
|
||
t.x = vec.at<double>(0); | ||
t.y = vec.at<double>(1); | ||
t.z = vec.at<double>(2); | ||
} | ||
|
||
template<> | ||
geometry_msgs::msg::Transform | ||
tf2::toMsg(const apriltag_pose_t& pose) | ||
{ | ||
const Eigen::Quaterniond q(Eigen::Map<const Eigen::Matrix<double, 3, 3, Eigen::RowMajor>>(pose.R->data)); | ||
|
||
geometry_msgs::msg::Transform t; | ||
tf2::convert(*pose.t, t.translation); | ||
tf2::convert(q, t.rotation); | ||
return t; | ||
} | ||
|
||
template<> | ||
geometry_msgs::msg::Transform | ||
tf2::toMsg(const std::pair<cv::Mat_<double>, cv::Mat_<double>>& pose) | ||
{ | ||
const cv::Quat<double> cvq = cv::Quat<double>::createFromRvec(pose.second); | ||
|
||
std::cout << "cv: " << cvq << std::endl; | ||
|
||
assert(pose.first.rows == 3 || pose.first.cols == 3); | ||
assert(pose.second.rows == 3 || pose.second.cols == 3); | ||
|
||
// convert compact rotation vector to angle-axis to quaternion | ||
const Eigen::Map<const Eigen::Vector3d> rvec(reinterpret_cast<double*>(pose.second.data)); | ||
const Eigen::Quaterniond q({rvec.norm(), rvec.normalized()}); | ||
|
||
std::cout << "eigen (x,y,z,w): " << q.coeffs().transpose() << std::endl; | ||
|
||
geometry_msgs::msg::Transform t; | ||
tf2::convert(pose.first, t.translation); | ||
tf2::convert(q, t.rotation); | ||
return t; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters