Skip to content

STM32MagneticSensorPWM::getSensorAngle() can underflow when getDutyCycleTicks() returns a value below min_ticks. #89

@thomasfla

Description

@thomasfla

return (ticks - min_ticks) * _2PI / (max_ticks - min_ticks);

Current behavior:

uint32_t ticks = getDutyCycleTicks();
return (ticks - min_ticks) * _2PI / (max_ticks - min_ticks);

Since ticks and min_ticks are unsigned, ticks < min_ticks wraps to a huge value. On an AS5048A PWM sensor near the zero boundary I see:

duty_ticks=152 angle=705512.000000 velocity=7055119.500000

Expected: values below min_ticks should clamp or be rejected.

Possible fix:

uint32_t ticks = getDutyCycleTicks();
ticks = constrain(ticks, min_ticks, max_ticks);
return (ticks - min_ticks) * _2PI / (max_ticks - min_ticks);

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions