Skip to content

Improve sensor delta_angle calculation#527

Merged
runger1101001 merged 3 commits intosimplefoc:devfrom
austin-bowen:improve-sensor-delta-angle
Apr 23, 2026
Merged

Improve sensor delta_angle calculation#527
runger1101001 merged 3 commits intosimplefoc:devfrom
austin-bowen:improve-sensor-delta-angle

Conversation

@austin-bowen
Copy link
Copy Markdown

@austin-bowen austin-bowen commented Apr 17, 2026

Description

Improve delta_angle calculation in Sensor::getVelocity() by:

  1. Handling full_rotations integer wrap-arounds.
  2. Preventing float precision loss issues from unbounded float growth.
  3. Fewer float ops --> faster.

Math explanation:

// Current implementation
delta_angle = (full_rotations * 2pi + angle_prev) - (vel_full_rotations * 2pi + vel_angle_prev);
                           // ^ this op grows without bound; eventually not enough precision for angle_prev

// Proposed implementation
delta_angle = 2pi * (full_rotations - vel_full_rotations) + (angle_prev - vel_angle_prev);
               // ^ this op should always be small; the full_rotations delta will never be >1 under normal circumstances

Comparing Floating Point Ops (assuming other ops are basically free)

Code Path Current Code Proposed Code
Hot (rotations equal) 1 fsub 1 fsub
Cold (rotations differ) 7 total: 2 i2f, 2 fmul, 2 fadd, 1 fsub 4 total: 1 i2f, 1 fmul, 1 fadd, 1 fsub

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Basically just put a motor on velocity control mode at 5RPS for a long time.

Test Configuration/Setup:

  • Hardware:
    • SimpleFOC Mini
    • 2804 gimbal motor
    • AS5600 magnetic encoder (using analog OUT)
    • All from this Amazon listing
  • IDE: Arduino
  • MCU package version: XIAO RP2040

@austin-bowen austin-bowen marked this pull request as ready for review April 17, 2026 04:04
@runger1101001 runger1101001 changed the base branch from master to dev April 18, 2026 12:55
@runger1101001
Copy link
Copy Markdown
Member

This is a very good idea - will be included in the next release.

I’ve changed the branch from master to dev, as we only accept PRs to the dev branch.

@austin-bowen
Copy link
Copy Markdown
Author

Thanks @runger1101001 , is there anything further I need to do to get the change merged into dev, or just sit tight?

@runger1101001
Copy link
Copy Markdown
Member

:-) no, I’ll merge it now, thanks for the reminder. The tests take a while to run so that’s why I didn’t merge right away, and by the time they had finished I had moved on and forgotten…

@runger1101001 runger1101001 merged commit 0fb1329 into simplefoc:dev Apr 23, 2026
30 checks passed
@runger1101001 runger1101001 added this to the 2.4.1_Release milestone Apr 24, 2026
@austin-bowen austin-bowen deleted the improve-sensor-delta-angle branch April 25, 2026 15:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants