Skip to content

fix: implement sign-magnitude encoding for signed registers#2

Merged
jimsynz merged 1 commit intomainfrom
fix/sign-magnitude-encoding
Jan 17, 2026
Merged

fix: implement sign-magnitude encoding for signed registers#2
jimsynz merged 1 commit intomainfrom
fix/sign-magnitude-encoding

Conversation

@jimsynz
Copy link
Contributor

@jimsynz jimsynz commented Jan 17, 2026

Summary

  • Implements sign-magnitude encoding for Feetech servo signed registers (previously using two's complement which was incorrect)
  • Adds comprehensive test coverage for encoding/decoding signed values

Details

Feetech servos use sign-magnitude encoding (not two's complement) for signed values, with different sign bit positions:

Register Type Sign Bit Valid Range
position_offset Bit 11 -2047 to +2047
goal_position Bit 15 -32767 to +32767
present_position Bit 15 -32767 to +32767
present_speed Bit 15 -32767 to +32767
present_load Bit 10 -1023 to +1023

Changes

Protocol module:

  • Added encode_sign_magnitude/3 and decode_sign_magnitude/2 functions

ControlTable module:

  • Added :position_signed conversion type
  • Fixed :speed_signed and :load_signed to use sign-magnitude
  • Added {:sign_magnitude, bit} conversion for raw signed values

STS3215 control table:

  • Updated goal_position and present_position to use :position_signed
  • Updated position_offset to use {:sign_magnitude, 11}

Tests added

  • 12 new tests for encode_sign_magnitude/3 and decode_sign_magnitude/2 in protocol_test.exs
  • 12 new tests for signed value conversions in sts3215_test.exs (negative positions, speeds, loads, position offsets)

Test plan

  • All 90 tests pass
  • Formatter check passes
  • Credo check passes
  • Dialyzer check passes

Feetech servos use sign-magnitude encoding (not two's complement) for
signed values, with different sign bit positions for different register
types:

- position_offset: bit 11 (range -2047 to +2047)
- goal_position, present_position: bit 15 (range -32767 to +32767)
- present_speed: bit 15 (range -32767 to +32767)
- present_load: bit 10 (range -1023 to +1023)

This commit:
- Adds encode_sign_magnitude/3 and decode_sign_magnitude/2 to Protocol
- Adds :position_signed conversion type for goal/present position
- Fixes :speed_signed and :load_signed to use sign-magnitude encoding
- Adds {:sign_magnitude, bit} conversion for position_offset
- Updates STS3215 register definitions to use correct conversion types
- Adds comprehensive tests for all sign-magnitude encoding/decoding
@jimsynz jimsynz force-pushed the fix/sign-magnitude-encoding branch from 744812c to de8d897 Compare January 17, 2026 22:14
@jimsynz jimsynz merged commit 8f3ee97 into main Jan 17, 2026
15 checks passed
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.

1 participant