Skip to content

Commit 53193fe

Browse files
committed
feat: IMU delta class
1 parent a6b8a81 commit 53193fe

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

include/am_utils/imu_delta_class.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#ifndef AM_UTILS_IMU_DELTA_CLASS_H_
2+
#define AM_UTILS_IMU_DELTA_CLASS_H_
3+
4+
#include <sensor_msgs/msg/imu.hpp>
5+
#include <vb_util_lib/rotate.h>
6+
7+
namespace am
8+
{
9+
class ImuDelta
10+
{
11+
public:
12+
ImuDelta(bool use_original_roll, bool use_original_pitch);
13+
14+
~ImuDelta();
15+
16+
void updateImu(const sensor_msgs::msg::Imu::SharedPtr &src, sensor_msgs::msg::Imu &res);
17+
18+
void initialize(const geometry_msgs::msg::Quaternion &q);
19+
20+
void reset();
21+
22+
bool isInitialized() const;
23+
24+
private:
25+
26+
bool initialized_ = {false};
27+
28+
double init_roll_ = {0.0};
29+
30+
double init_pitch_ = {0.0};
31+
32+
double init_yaw_ = {0.0};
33+
34+
bool use_original_roll_ {false};
35+
36+
bool use_original_pitch_ {false};
37+
38+
39+
};
40+
}
41+
42+
#endif AM_UTILS_IMU_DELTA_CLASS_H_

src/am_utils/imu_delta_class.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <am_utils/imu_delta_class.h>
2+
3+
namespace am
4+
{
5+
ImuDelta::ImuDelta(bool use_original_roll, bool use_original_pitch) : use_original_roll_(use_original_roll), use_original_pitch_(use_original_pitch)
6+
{
7+
reset();
8+
}
9+
10+
ImuDelta::~ImuDelta()
11+
{
12+
13+
}
14+
15+
16+
void ImuDelta::updateImu(const sensor_msgs::msg::Imu::SharedPtr &src, sensor_msgs::msg::Imu &res)
17+
{
18+
19+
initialize(src->orientation);
20+
21+
res.header = src->header;
22+
double roll = 0.0, pitch = 0.0, yaw = 0.0;
23+
am::Rotate::getRPY(src->orientation, roll, pitch, yaw);
24+
if(use_original_roll_)
25+
{
26+
roll = am::Rotate::wrap_pi(roll - init_roll_);
27+
}
28+
if(use_original_pitch_)
29+
{
30+
pitch = am::Rotate::wrap_pi(pitch - init_pitch_);
31+
}
32+
33+
yaw = am::Rotate::wrap_pi(yaw - init_yaw_);
34+
35+
res = *src;
36+
res.orientation = am::Rotate::toQuaternionMsg(roll, pitch, yaw);
37+
}
38+
39+
void ImuDelta::initialize(const geometry_msgs::msg::Quaternion &q)
40+
{
41+
if(initialized_)
42+
{
43+
return;
44+
}
45+
46+
am::Rotate::getRPY(q, init_roll_, init_pitch_, init_yaw_);
47+
initialized_ = true;
48+
}
49+
50+
void ImuDelta::reset()
51+
{
52+
init_roll_ = 0.0;
53+
init_pitch_ = 0.0;
54+
init_yaw_ = 0.0;
55+
initialized_ = false;
56+
}
57+
58+
bool ImuDelta::isInitialized() const
59+
{
60+
return initialized_;
61+
}
62+
}

0 commit comments

Comments
 (0)