Velocity precision

When I run the magnetic_sensor_spi_example on my Nucleo-64, I get the following result (angle in blue, velocity in red):

As you can see, the velocity is very noisy, probably because of float imprecision and very short Δt.

If I add a _delay(100) in my main loop, thus increasing Δt, the noise disappears.


On this forum, there is a discussion about float precision:

Also, maybe a basic filter could be added to magnetic_sensor_spi_example to remove the noise.

Hey Quentin,

This is due to the fact that we are measuring time in microseconds and when you are reading the sensor only ( without foc loop) this can be very crude.

So therefore for the motion control we have a low pass filter to deal with this in the BLDCMotor class and a bit more similar to what you’ve done ( and more recommended) is to downsample the motion control and only calculate the velocity each N calls of sensor angle call.

This gives a bit more time in between velocity calls. And you can set it with:

motor.motion_downsample = 5; // 0 - disabled

If you’re using only the sensor itself without the rest of the library, the best choice would be to add the delay function or not to call the getVelocity in the each loop.

1 Like

Thanks @Antun_Skuric.

Are you sure motor.motion_downsample gives more time in between velocity calls? I’m asking because changing this parameter doesn’t seem to get me the expected results. Moreover, looking at the code, I’m not sure I understand how it could work (see lines 304 and 309):

Hey @quentin,

You are absolutely right. The shaftVelocity call should be after the downsampling snippet. Or better said the downsampling snippet should be the first thing in the move function.
This has been corrected by @runger in the dev branch. (as well as many other improvements such as floating point error).

1 Like