Arduino-FOC-drivers icon indicating copy to clipboard operation
Arduino-FOC-drivers copied to clipboard

Bug calibrated sensor + uint16 lut

Open askuric opened this issue 1 month ago • 3 comments

This PR fixes the bug #73 + It fixes the bug of the hard-coded number of samples per motor (5 * motor.pole_pairs) - see more info in the community.

Automatic number of samples given a LUT size

Now the code will automatically choose the number of samples to run in one motor rotation based on the LUT size.

It will always do the closest posible number of samples to the LUT size that is multiple of the motor's pole_pairs number. That way we guarantee that the sampling is equal for each electrical angle.

Then at the final stage the the samples will be remapped to the user's requested LUT size.

For example if we have a BLDC motor with 7 pole pairs and he wants a LUT size of 200 The calibration will be done with ceil(200/7) * 7 = 203 positins And then at the end of the calibration process the 203 values will be mapped to the 200.

This mapping at the moment does not implement any interpolation, it only extrapolates. It should work for most use cases.

uint16 LUT rather than float

The main reason is the memory footprint. The LUT now requires half the memory.

The float to uint16 scaling is done with the encodeOffsetU16 and decodeOffsetU16 functions that basically just scale float angle of [-pi, pi] to [0, 65535].

For the moment the resolution is ~0.000096 rad (~0.0055 deg) , which is probably fine for most of applications.

For example if we take a stepper motor with 50pp, in one electrical rotation is discretized in 360/50/0.0055=1309 elements which amounts the resolution of the electrical angle of around ~0.275 deg.

The resolution can be greatly improved if we consider that the LUT will have a smaller range than [-pi,pi] (which is probably always the case). In that case we can increase the scaling factor LUT_SCALE in the CalibratedSensor.h and increase the overall resolution of the LUT.

askuric avatar Dec 05 '25 10:12 askuric