Fix CRC mismatch on HG LowCmd/LowState caused by struct padding#31
Open
yaodeng-anitron wants to merge 1 commit into
Open
Fix CRC mismatch on HG LowCmd/LowState caused by struct padding#31yaodeng-anitron wants to merge 1 commit into
yaodeng-anitron wants to merge 1 commit into
Conversation
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
The current CRC computation for
unitree_hg::msg::dds_::LowCmd_/LowState_reinterprets the in-memory struct as auint32_t[]:MotorCmd_/MotorState_lay out asuint8_t modefollowed byfloat, so the C++ compiler inserts 3 bytes of alignment padding. Those padding bytes are not guaranteed zero (the IDL setters only touch the named fields), so the CRC walks bytes the firmware never sees and disagrees with the firmware's CRC computed from the wire-serialized frame.Symptom: firmware silently drops the frame; motors never go under user control —
motor_cmd[i].mode = 1is sent butLowState.motor_state[i].modestays0.The Python SDK (
unitree_sdk2py.utils.crc.CRC) already does the righ thing: pack into a deterministic wire-format buffer first, then CRC. This PR brings the C++ SDK into alignment with the Python SDK and the firmware.Change
crc32_coreoverloads forunitree_hg::msg::dds_::LowCmd_andLowState_ininclude/unitree/dds_wrapper/common/crc.h. They build a packed buffer with explicit zero padding matching the Python format strings:LowCmd_:'<2B2x' + 'B3x5fI' * 35 + '5I'→ 1004 B, CRC over 1000 B (250 u32 words)LowState_:'<2I2B2xI' + '13fh2x' + 'B3x4f2hf7I' * 35 + '40B5I'→ 2092 B, CRC over 2088 B (522 u32 words)g1_pub.hswitches from the raw-pointer call tocrc32_core(msg_)(typed dispatch).crc32_core(uint32_t*, uint32_t)and all Go2 / example callers are untouched.Out of scope
unitree_go)LowCmd_/LowState_likely have the same issue (sameuint8_t mode+ float layout in theirMotorCmd_), but that fix is unverified on hardware and is left for a follow-up PR.crc32_corecopies underexample/are unchanged.Test plan
unitree_sdk2py.utils.crc.CRCfor: emptyLowCmd, emptyLowState, partial fields, fully-populated 35-motorLowCmd, fully-populatedLowState(IMU + 35 motors + wireless_remote + reserve).LowCmd.