Skip to content

Commit c365638

Browse files
author
Aaron Roller
authored
Merge pull request #35 from AutoModality/AM-232
feat: add more functionality
2 parents 8b02dc5 + 59f0454 commit c365638

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

include/vb_util_lib/imu_class.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,29 @@ namespace am
4545
class ImuClass
4646
{
4747
public:
48+
/*
49+
* @brief default constructor. the user needs to subscribe to imu topics
50+
*/
4851
ImuClass();
52+
53+
/*
54+
* @brief constructor
55+
*/
4956
ImuClass(int imu_pot_size);
57+
58+
/*
59+
* @brief constructor for PX4 and DJI imu
60+
*/
5061
ImuClass(ros::NodeHandle &nh, int imu_pot_size = 400);
62+
63+
/*
64+
* @brief constructor for customized imu source
65+
*/
66+
ImuClass(ros::NodeHandle &nh, const std::string &imu_topic, int imu_pot_size = 400);
67+
68+
/*
69+
* @brief destructor
70+
*/
5171
~ImuClass();
5272

5373
void addImu(const sensor_msgs::Imu &imu);

include/vb_util_lib/rotate.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <tf/tf.h>
77
#include <geometry_msgs/Quaternion.h>
88
#include <geometry_msgs/Point32.h>
9+
#include <tf2/LinearMath/Matrix3x3.h>
10+
#include <tf2_geometry_msgs/tf2_geometry_msgs.h>
911

1012
namespace am
1113
{
@@ -70,13 +72,21 @@ namespace am
7072

7173
static void rotate(double &x, double &y, double &z, geometry_msgs::Quaternion &q_rot);
7274

75+
static void rotate(double &x, double &y, double &z, const geometry_msgs::Quaternion &q_rot);
76+
7377
static void rotate(geometry_msgs::Point32 &p, const geometry_msgs::Quaternion &q_rot);
7478

79+
7580
/*
7681
* @brief transforms a point with the passed transformed stamped
7782
*/
7883
static void transform(geometry_msgs::Point32 &p_in, const geometry_msgs::TransformStamped &tranform);
7984

85+
/*
86+
* @brief transforms a pose with another pose
87+
*/
88+
static void transform(const geometry_msgs::Pose &src, geometry_msgs::Pose &des);
89+
8090
/*
8191
* @brief inverse transform
8292
*/
@@ -91,6 +101,9 @@ namespace am
91101
//Static method to convert quaternion to rpy
92102
static void getRPY(tf::Quaternion &q, double &roll, double &pitch, double &yaw);
93103

104+
//Static method to convert pose to rpy in geometry_msgs::Vector3
105+
static geometry_msgs::Vector3 getRPY(const geometry_msgs::Pose &pose);
106+
94107
//Static method to get Yaw from geometry_msgs Quaternion
95108
static double getYaw(const geometry_msgs::Quaternion &q);
96109

src/vb_util_lib/imu_class.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,18 @@
1111

1212
namespace am
1313
{
14+
15+
ImuClass::ImuClass(ros::NodeHandle &nh, const std::string &imu_topic, int imu_pot_size): imu_pot_size_(imu_pot_size), imu_cnt_(-1)
16+
{
17+
nh_ = nh;
18+
px4_imu_sub_= nh_.subscribe<sensor_msgs::Imu>(imu_topic, 100, &ImuClass::imuCB, this);
19+
imu_pot_ = std::vector<sensor_msgs::Imu>(imu_pot_size);
20+
}
21+
1422
ImuClass::ImuClass(ros::NodeHandle &nh, int imu_pot_size): imu_cnt_(-1)
1523
{
1624
nh_ = nh;
1725
dji_imu_sub_ = nh_.subscribe<sensor_msgs::Imu>(am_topics::DJI_SDK_IMU, 100, &ImuClass::imuCB, this);
18-
//TODO: use PX4_IMU_TOPIC from am_topics
1926
px4_imu_sub_ = nh_.subscribe<sensor_msgs::Imu>(am_topics::MAVROS_IMU_DATA, 100, &ImuClass::imuCB, this);
2027
imu_pot_size_ = imu_pot_size;
2128
imu_pot_ = std::vector<sensor_msgs::Imu>(imu_pot_size_);

src/vb_util_lib/rotate.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,16 @@ namespace am
339339
m.getRPY(roll, pitch, yaw);
340340
}
341341

342+
geometry_msgs::Vector3 Rotate::getRPY(const geometry_msgs::Pose &pose)
343+
{
344+
tf2::Quaternion tf2_q;
345+
tf2::fromMsg(pose.orientation, tf2_q);
346+
geometry_msgs::Vector3 rpy;
347+
348+
tf2::Matrix3x3(tf2_q).getRPY(rpy.x, rpy.y, rpy.z);
349+
return rpy;
350+
}
351+
342352
void Rotate::rotate(double &x, double &y, double &z, geometry_msgs::Quaternion &q_rot)
343353
{
344354
double old_x = x;
@@ -351,6 +361,18 @@ namespace am
351361

352362
}
353363

364+
void Rotate::rotate(double &x, double &y, double &z, const geometry_msgs::Quaternion &q_rot)
365+
{
366+
double old_x = x;
367+
double old_y = y;
368+
double old_z = z;
369+
370+
x = q_rot.w*q_rot.w*old_x + 2*q_rot.y*q_rot.w*old_z - 2*q_rot.z*q_rot.w*old_y + q_rot.x*q_rot.x*old_x + 2*q_rot.y*q_rot.x*old_y + 2*q_rot.z*q_rot.x*old_z - q_rot.z*q_rot.z*old_x - q_rot.y*q_rot.y*old_x;
371+
y = 2*q_rot.x*q_rot.y*old_x + q_rot.y*q_rot.y*old_y + 2*q_rot.z*q_rot.y*old_z + 2*q_rot.w*q_rot.z*old_x - q_rot.z*q_rot.z*old_y + q_rot.w*q_rot.w*old_y - 2*q_rot.x*q_rot.w*old_z - q_rot.x*q_rot.x*old_y;
372+
z = 2*q_rot.x*q_rot.z*old_x + 2*q_rot.y*q_rot.z*old_y + q_rot.z*q_rot.z*old_z - 2*q_rot.w*q_rot.y*old_x - q_rot.y*q_rot.y*old_z + 2*q_rot.w*q_rot.x*old_y - q_rot.x*q_rot.x*old_z + q_rot.w*q_rot.w*old_z;
373+
374+
}
375+
354376
void Rotate::rotate(geometry_msgs::Point32 &p, const geometry_msgs::Quaternion &q_rot)
355377
{
356378
geometry_msgs::Point32 old_p = p;
@@ -462,6 +484,21 @@ namespace am
462484
return angle;
463485
}
464486

487+
void Rotate::transform(const geometry_msgs::Pose &src, geometry_msgs::Pose &des)
488+
{
489+
des.position.x = des.position.x - src.position.x;
490+
des.position.y = des.position.y - src.position.y;
491+
des.position.z = des.position.z - src.position.z;
492+
493+
geometry_msgs::Vector3 rpy = Rotate::getRPY(src);
494+
rpy.x *= -1;
495+
rpy.y *= -1;
496+
rpy.z *= -1;
497+
//ROS_INFO("Q:[%f,%f,%f,%f]", src.orientation.x, src.orientation.y, src.orientation.z, src.orientation.w);
498+
Rotate::rotate(des.position.x, des.position.y, des.position.z, toQuaternionMsg(rpy.x, rpy.y, rpy.z));
499+
500+
}
501+
465502
}
466503

467504

0 commit comments

Comments
 (0)