Velocity Calculation rework#453
Merged
runger1101001 merged 2 commits intosimplefoc:devfrom Jul 28, 2025
Merged
Conversation
src/common/base_classes/Sensor.cpp
Outdated
| 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) { |
Member
There was a problem hiding this comment.
Don't we want to check for angles larger than this, rather than smaller?
Contributor
Author
There was a problem hiding this comment.
Good catch, whoops! Should be fixed now.
Member
|
Hey, thanks a lot for contributing this. I think it is a good idea, as you have mentioned the unstable velocity signal is a real pain for many people. This seems like a very reasonable approach, but I'd like to merge it to the dev branch and try it a bit before releasing. So if its ok with you I would merge it immediately after doing the next release? |
Contributor
Author
|
Merging after this release is totally fine by me! |
Member
|
Merged, as we have now released version 2.3.5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Seeing the relatively frequent questions and problems associated with noisy velocity measurements on the discord server, what do you think about a change like this to the velocity calculation?
The idea being that currently when we make a call to
getVelocity(), if the angle change is zero then we get a zero velocity, if we have a non-zero angle change it appears as a very high velocity because all of that change is accounted for in one cycle. Because our loop can often run many cycles between angle changes. This shows up (unfiltered) as a series of zero velocity measurements with large single sample spikes, this is obviously nonphysical. The current fix to this is to heavily filter the velocity signal, which introduces a large phase loss in the control system.This change only updates the velocity if an angle change is observed and otherwise reports the previous velocity. This results in a closer approximation to a continuous signal.
One degenerate case that is not handled is if the motor comes to a complete and total stop, the velocity will not be reported as zero. Practically, this doesn't seem like a problem because the reported velocity will be very small, very few motors will come to an absolute stop for any long period of time, and a floating point zero value (vs very small non-zero) isn't actually very useful.
This change is currently untested on hardware.