Skip to content

Commit 213ec36

Browse files
committed
Smooth angular velocity to avoid jitter when prediction
1 parent d509563 commit 213ec36

File tree

2 files changed

+8
-58
lines changed

2 files changed

+8
-58
lines changed

src/psmoveservice/Filter/ExternalOrientationFilter.cpp

Lines changed: 7 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,6 @@ ExternalOrientationFilter::ExternalOrientationFilter() :
138138

139139
memset(&m_constants, 0, sizeof(OrientationFilterConstants));
140140
resetState();
141-
142-
last_orentation = Eigen::Quaternionf::Identity();
143141
}
144142

145143
ExternalOrientationFilter::~ExternalOrientationFilter()
@@ -200,8 +198,6 @@ bool ExternalOrientationFilter::init(const OrientationFilterConstants &constants
200198
m_state->orientation = initial_orientation;
201199
m_state->bIsValid = true;
202200

203-
last_orentation = initial_orientation;
204-
205201
return true;
206202
}
207203

@@ -415,63 +411,18 @@ void OrientationFilterExternal::update(
415411
}
416412
}
417413

418-
bool customVelocity = false;
419-
420-
// Get gyro angular velocity
421-
__size += 4;
422-
if (vector.size() >= __size)
423-
{
424-
customVelocity = true;
425-
426-
long timeStamp = (long)_atoi64(vector[__size - 4].c_str());
427-
new_angular_velocity.x() = (float)_atof_l(vector[__size - 3].c_str(), localeInvariant);
428-
new_angular_velocity.y() = (float)_atof_l(vector[__size - 2].c_str(), localeInvariant);
429-
new_angular_velocity.z() = (float)_atof_l(vector[__size - 1].c_str(), localeInvariant);
430-
}
431-
432414
if (isValid && eigen_quaternion_is_valid(new_orientation))
433415
{
434-
// Get angular velocity from quaternions
435-
if (!customVelocity)
436-
{
437-
// Apply basic prediction
438-
// Add the new blend to blend history
439-
//blendedPositionHistory.push_back(new_orientation);
440-
//while (blendedPositionHistory.size() > 50)
441-
//{
442-
// blendedPositionHistory.pop_front();
443-
//}
444-
445-
//Eigen::Vector3f quat_sum = Eigen::Vector3f::Zero();
446-
//float quat_w = 0.0f;
447-
448-
//int sampleCount = 0;
449-
//for (std::list<Eigen::Quaternionf>::iterator it = blendedPositionHistory.begin(); it != blendedPositionHistory.end(); it++)
450-
//{
451-
// Eigen::Quaternionf quat = *it;
452-
// quat_sum += Eigen::Vector3f(quat.x(), quat.y(), quat.z());
453-
// quat_w += quat.w();
454-
//
455-
// sampleCount++;
456-
//}
457-
458-
//quat_sum /= sampleCount;
459-
//quat_w /= sampleCount;
460-
461-
//Eigen::Quaternionf old_orientation = Eigen::Quaternionf(quat_w, quat_sum.x(), quat_sum.y(), quat_sum.z());
462-
//Eigen::Quaternionf deltaRotation = old_orientation.conjugate() * new_orientation;
463-
464-
//$TODO: This seems very glitchy
465-
Eigen::Quaternionf deltaRotation = last_orentation.conjugate() * new_orientation;
466-
Eigen::AngleAxisf axisAngle(deltaRotation);
416+
Eigen::Quaternionf deltaRotation = m_state->orientation.conjugate() * new_orientation;
417+
Eigen::AngleAxisf axisAngle(deltaRotation);
467418

468-
Eigen::Vector3f angularVelocity = axisAngle.axis() * (axisAngle.angle() / imu_delta_time);
419+
Eigen::Vector3f angularVelocity = axisAngle.axis() * (axisAngle.angle() / imu_delta_time);
469420

470-
new_angular_velocity = angularVelocity.transpose();
471-
}
421+
// Smooth angular velocity, otherwise its too jittery
422+
const float alpha = 0.2f;
423+
Eigen::Vector3f angVel = angularVelocity.transpose();
424+
new_angular_velocity = ((1.f - alpha) * m_state->angular_velocity + alpha * angVel);
472425

473-
last_orentation = new_orientation;
474-
475426
const Eigen::Vector3f new_angular_acceleration = (new_angular_velocity - m_state->angular_velocity) / imu_delta_time;
476427

477428
m_state->apply_imu_state(new_orientation, new_angular_velocity, new_angular_acceleration, timestamp, packet.isTemporary);

src/psmoveservice/Filter/ExternalOrientationFilter.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ExternalOrientationFilter : public IOrientationFilter
2929

3030
// -- IOrientationFilter --
3131
bool init(const OrientationFilterConstants &constant) override;
32-
bool init(const OrientationFilterConstants &constant, const Eigen::Quaternionf &initial_orientation) override;
32+
bool init(const OrientationFilterConstants &constant, const Eigen::Quaternionf &initial_orientation) override;
3333

3434
Eigen::Quaternionf getOrientation(float time = 0.f, float offset_x = 0.f, float offset_y = 0.f, float offset_z = 0.f, float offset_world_x = 0.f, float offset_world_y = 0.f, float offset_world_z = 0.f) const override;
3535
Eigen::Quaternionf getResetOrientation() const override;
@@ -45,7 +45,6 @@ class ExternalOrientationFilter : public IOrientationFilter
4545
bool showMessage;
4646

4747
std::list<Eigen::Quaternionf> blendedPositionHistory;
48-
Eigen::Quaternionf last_orentation;
4948

5049
#ifdef WIN32
5150
_locale_t localeInvariant;

0 commit comments

Comments
 (0)