Skip to content

utils: normalize AngleDiffDeg before folding about 180 - #6324

Merged
Nicolas Palpacuer (NickPPC) merged 2 commits into
mainfrom
fix-angle-diff-deg-normalization
Sep 1, 2026
Merged

utils: normalize AngleDiffDeg before folding about 180#6324
Nicolas Palpacuer (NickPPC) merged 2 commits into
mainfrom
fix-angle-diff-deg-normalization

Conversation

@NickPPC

Copy link
Copy Markdown
Member

Summary

AngleDiffDeg returns negative values for angles more than one revolution apart. An angular difference must lie in [0, 180].

AngleDiffDeg(  0, 370) = -10
AngleDiffDeg(720,  10) = -350
AngleDiffDeg(400,  30) = -10

From the audit that produced #6314#6323.

Why this is fixed rather than deleted

It has no callers inside this repository, so under the delete-vs-fix rule it would be a removal candidate. But a code search across viamrobotics, viam-modules and viam-labs found a real external consumer: viam-labs/stevebriskin-projects (rovie/go/rovie.go) calls it three times, on yaw and heading values:

diff_des := utils.AngleDiffDeg(d.Yaw_desired, stop_deg)
diff_real := utils.AngleDiffDeg(d.Yaw_real, stop_deg)
degDiff := utils.AngleDiffDeg(desired, heading)

Heading and yaw are exactly the quantities that drift outside [0, 360), so it gets repaired.

The bug

return float64(180) - math.Abs(math.Abs(a1-a2)-float64(180))

Folding about 180 is only valid once the separation is inside one revolution. |a1-a2| was used raw, so a separation of 370 folded to 180 - |370-180| = -10.

Now the separation is wrapped into [0, 360) first, then folded. The function stays commutative, and behaviour is unchanged for inputs already within one revolution of each other — which is why this went unnoticed.

Testing

go test ./... across the whole repo passes; golangci-lint clean.

Extended the existing table with out-of-range inputs: (0, 370), (720, 10), (400, 30), (-370, 0), (-180, 180), (1080, 0), (-1000, 1000), plus the commuted forms. Six of the new cases fail against the unpatched source, returning -10, -350 and similar.

Tickets

None

Claude Code Prompts Used

  • "Hi Claude, I would like you to take a hard look at this repo. I want you to search for bugs, mistakes, and strange choices..."
  • "Can you work on the remaining findings following the priority order you have outlined? ... Whenever you find code that seems to have 'zero production callers' try to search for usage under other repos in the viamrobotics, viam-modules, and viam-labs Github organizations. If no usage is found anywhere, and if the code has existed for over 6 months delete it, otherwise fix it"

🤖 Generated with Claude Code

AngleDiffDeg folded the raw separation, so any pair more than 360 apart produced
a negative "difference": AngleDiffDeg(0, 370) returned -10 and
AngleDiffDeg(720, 10) returned -350. An angular difference must lie in [0, 180].

Wrap the separation into [0, 360) before folding. Behaviour is unchanged for
inputs already within one revolution of each other, which is why this went
unnoticed.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@viambot viambot added the safe to test This pull request is marked safe to test from a trusted zone label Aug 3, 2026
@NickPPC
Nicolas Palpacuer (NickPPC) marked this pull request as ready for review August 3, 2026 19:09
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Aug 3, 2026
@NickPPC
Nicolas Palpacuer (NickPPC) merged commit 364eab6 into main Sep 1, 2026
24 checks passed
@NickPPC
Nicolas Palpacuer (NickPPC) deleted the fix-angle-diff-deg-normalization branch September 1, 2026 15:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

safe to test This pull request is marked safe to test from a trusted zone

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants