Skip to content

Velocity Calculation rework #453

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/common/base_classes/Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ void Sensor::update() {
/** get current angular velocity (rad/s) */
float Sensor::getVelocity() {
// calculate sample time
// if timestamps were unsigned, we could get rid of this section, unsigned overflow handles it correctly
float Ts = (angle_prev_ts - vel_angle_prev_ts)*1e-6f;
if (Ts < 0.0f) { // handle micros() overflow - we need to reset vel_angle_prev_ts
vel_angle_prev = angle_prev;
Expand All @@ -28,10 +29,28 @@ float Sensor::getVelocity() {
}
if (Ts < min_elapsed_time) return velocity; // don't update velocity if deltaT is too small

velocity = ( (float)(full_rotations - vel_full_rotations)*_2PI + (angle_prev - vel_angle_prev) ) / Ts;
vel_angle_prev = angle_prev;
vel_full_rotations = full_rotations;
vel_angle_prev_ts = angle_prev_ts;
float current_angle = 0.0f;
float prev_angle = 0.0f;
// Avoid floating point precision loss for large full_rotations
// this is likely optional
if (full_rotations == vel_full_rotations) {
current_angle = angle_prev;
prev_angle = vel_angle_prev;
} else {
current_angle = (float) full_rotations * _2PI + angle_prev;
prev_angle = (float) vel_full_rotations * _2PI + vel_angle_prev;
}
const float delta_angle = current_angle - prev_angle;

// floating point equality checks are bad, so instead we check that the angle change is very small
if (fabsf(delta_angle) > 1e-8f) {
velocity = delta_angle / Ts;

vel_angle_prev = angle_prev;
vel_full_rotations = full_rotations;
vel_angle_prev_ts = angle_prev_ts;
}

return velocity;
}

Expand Down