fix: implement sign-magnitude encoding for signed registers#2
Merged
Conversation
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
744812c to
de8d897
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Details
Feetech servos use sign-magnitude encoding (not two's complement) for signed values, with different sign bit positions:
position_offsetgoal_positionpresent_positionpresent_speedpresent_loadChanges
Protocol module:
encode_sign_magnitude/3anddecode_sign_magnitude/2functionsControlTable module:
:position_signedconversion type:speed_signedand:load_signedto use sign-magnitude{:sign_magnitude, bit}conversion for raw signed valuesSTS3215 control table:
goal_positionandpresent_positionto use:position_signedposition_offsetto use{:sign_magnitude, 11}Tests added
encode_sign_magnitude/3anddecode_sign_magnitude/2in protocol_test.exsTest plan