9 : _frameName(frameName),
11 _linear_accel_cov(linear_accel_cov),
12 _angular_velocity_cov(angular_velocity_cov),
14 _steadyBaseTime(
std::chrono::steady_clock::now()) {
24 static std::deque<dai::IMUReportAccelerometer> accelHist;
25 static std::deque<dai::IMUReportGyroscope> gyroHist;
28 for(
int i = 0; i < imuPackets.size(); ++i) {
29 if(accelHist.size() == 0) {
30 accelHist.push_back(imuPackets[i].acceleroMeter);
31 }
else if(accelHist.back().sequence != imuPackets[i].acceleroMeter.sequence) {
32 accelHist.push_back(imuPackets[i].acceleroMeter);
35 if(gyroHist.size() == 0) {
36 gyroHist.push_back(imuPackets[i].gyroscope);
37 }
else if(gyroHist.back().sequence != imuPackets[i].gyroscope.sequence) {
38 gyroHist.push_back(imuPackets[i].gyroscope);
42 if(accelHist.size() < 3) {
45 dai::IMUReportAccelerometer accel0, accel1;
46 dai::IMUReportGyroscope currGyro;
49 while(accelHist.size()) {
50 if(accel0.sequence == -1) {
51 accel0 = accelHist.front();
52 accelHist.pop_front();
54 accel1 = accelHist.front();
55 accelHist.pop_front();
61 std::chrono::duration<double, std::milli> duration_ms = accel1.timestamp.get() - accel0.timestamp.get();
62 double dt = duration_ms.count();
64 if(!gyroHist.size()) {
67 while(gyroHist.size()) {
68 currGyro = gyroHist.front();
71 "IMU INTERPOLATION: ",
72 "Accel 0: Seq => " << accel0.sequence <<
" timeStamp => " << (accel0.timestamp.get() -
_steadyBaseTime).count() << std::endl);
74 "currGyro 0: Seq => " << currGyro.sequence <<
"timeStamp => " 77 "IMU INTERPOLATION: ",
78 "Accel 1: Seq => " << accel1.sequence <<
" timeStamp => " << (accel1.timestamp.get() -
_steadyBaseTime).count() << std::endl);
79 if(currGyro.timestamp.get() > accel0.timestamp.get() && currGyro.timestamp.get() <= accel1.timestamp.get()) {
83 std::chrono::duration<double, std::milli>
diff = currGyro.timestamp.get() - accel0.timestamp.get();
84 const double alpha = diff.count() / dt;
85 dai::IMUReportAccelerometer interpAccel =
lerpImu(accel0, accel1, alpha);
88 }
else if(currGyro.timestamp.get() > accel1.timestamp.get()) {
90 if(accelHist.size()) {
91 accel1 = accelHist.front();
92 accelHist.pop_front();
93 duration_ms = accel1.timestamp.get() - accel0.timestamp.get();
94 dt = duration_ms.count();
107 DEPTHAI_ROS_DEBUG_STREAM(
"IMU INTERPOLATION: ",
"Count ->" << i <<
" Placing Accel 0 Seq Number :" << accel0.sequence << std::endl);
109 accelHist.push_back(accel0);
112 if(gyroHist.size() < 3) {
115 dai::IMUReportGyroscope gyro0, gyro1;
116 dai::IMUReportAccelerometer currAccel;
119 while(gyroHist.size()) {
120 if(gyro0.sequence == -1) {
121 gyro0 = gyroHist.front();
122 gyroHist.pop_front();
124 gyro1 = gyroHist.front();
125 gyroHist.pop_front();
127 std::chrono::duration<double, std::milli> duration_ms = gyro1.timestamp.get() - gyro0.timestamp.get();
128 double dt = duration_ms.count();
130 if(!accelHist.size()) {
133 while(accelHist.size()) {
134 currAccel = accelHist.front();
136 "gyro 0: Seq => " << gyro0.sequence << std::endl
137 <<
" timeStamp => " << (gyro0.timestamp.get() -
_steadyBaseTime).count()
140 "currAccel 0: Seq => " << currAccel.sequence << std::endl
141 <<
" timeStamp => " << (currAccel.timestamp.get() -
_steadyBaseTime).count()
144 "gyro 1: Seq => " << gyro1.sequence << std::endl
145 <<
" timeStamp => " << (gyro1.timestamp.get() -
_steadyBaseTime).count()
147 if(currAccel.timestamp.get() > gyro0.timestamp.get() && currAccel.timestamp.get() <= gyro1.timestamp.get()) {
149 std::chrono::duration<double, std::milli>
diff = currAccel.timestamp.get() - gyro0.timestamp.get();
150 const double alpha = diff.count() / dt;
151 dai::IMUReportGyroscope interpGyro =
lerpImu(gyro0, gyro1, alpha);
153 accelHist.pop_front();
154 }
else if(currAccel.timestamp.get() > gyro1.timestamp.get()) {
156 if(gyroHist.size()) {
157 gyro1 = gyroHist.front();
158 gyroHist.pop_front();
159 duration_ms = gyro1.timestamp.get() - gyro0.timestamp.get();
160 dt = duration_ms.count();
165 accelHist.pop_front();
172 gyroHist.push_back(gyro0);
179 ImuMsgs::Imu interpMsg;
180 interpMsg.linear_acceleration.x = accel.x;
181 interpMsg.linear_acceleration.y = accel.y;
182 interpMsg.linear_acceleration.z = accel.z;
184 interpMsg.angular_velocity.x = gyro.x;
185 interpMsg.angular_velocity.y = gyro.y;
186 interpMsg.angular_velocity.z = gyro.z;
188 interpMsg.orientation.x = 0.0;
189 interpMsg.orientation.y = 0.0;
190 interpMsg.orientation.z = 0.0;
191 interpMsg.orientation.w = 0.0;
193 interpMsg.orientation_covariance = {-1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
218 for(
int i = 0; i < inData->packets.size(); ++i) {
219 auto accel = inData->packets[i].acceleroMeter;
220 auto gyro = inData->packets[i].gyroscope;
void FillImuData_LinearInterpolation(std::vector< IMUPacket > &imuPackets, std::deque< ImuMsgs::Imu > &imuMsgs)
const std::string _frameName
void toRosMsg(std::shared_ptr< dai::IMUData > inData, std::deque< ImuMsgs::Imu > &outImuMsg)
T lerpImu(const T &a, const T &b, const double t)
ImuConverter(const std::string &frameName, ImuSyncMethod syncMode=ImuSyncMethod::LINEAR_INTERPOLATE_ACCEL, double linear_accel_cov=0.0, double angular_velocity_cov=0.0)
#define DEPTHAI_ROS_DEBUG_STREAM(loggerName, args)
IMETHOD Vector diff(const Vector &p_w_a, const Vector &p_w_b, double dt=1)
ImuMsgs::Imu CreateUnitMessage(dai::IMUReportAccelerometer accel, dai::IMUReportGyroscope gyro)
#define DEPTHAI_ROS_WARN_STREAM(loggerName, args)
double _angular_velocity_cov
std::chrono::time_point< std::chrono::steady_clock > _steadyBaseTime
::ros::Time getFrameTime(::ros::Time rosBaseTime, std::chrono::time_point< std::chrono::steady_clock > steadyBaseTime, std::chrono::time_point< std::chrono::steady_clock, std::chrono::steady_clock::duration > currTimePoint)