Skip to content

Comments

fix(icm20948): Fix endianness and scaling of magnetometer data#597

Merged
finger563 merged 1 commit intomainfrom
fix/596-icm20948-magnetometer-endianness
Feb 7, 2026
Merged

fix(icm20948): Fix endianness and scaling of magnetometer data#597
finger563 merged 1 commit intomainfrom
fix/596-icm20948-magnetometer-endianness

Conversation

@finger563
Copy link
Contributor

Description

  • Fix scaling (multiply instead of divide) for magnetometer data
  • Fix reading of raw magnetometer data to use correct endianness

Motivation and Context

Closes #596

How has this been tested?

Build and run icm20948/example and verify that magnetometer data is being read correctly and is scaled appropriately.

Screenshots (if appropriate, e.g. schematic, board, console logs, lab pictures):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update
  • Hardware (schematic, board, system design) change
  • Software change

Checklist:

  • My change requires a change to the documentation.
  • I have added / updated the documentation related to this change via either README or WIKI

Software

  • I have added tests to cover my changes.
  • I have updated the .github/workflows/build.yml file to add my new test to the automated cloud build github action.
  • All new and existing tests passed.
  • My code follows the code style of this project.

Copilot AI review requested due to automatic review settings February 7, 2026 23:11
@finger563 finger563 self-assigned this Feb 7, 2026
@finger563 finger563 added bug Something isn't working icm20948 labels Feb 7, 2026
@github-actions
Copy link

github-actions bot commented Feb 7, 2026

✅Static analysis result - no issues found! ✅

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@finger563 finger563 requested a review from Copilot February 7, 2026 23:41
@finger563 finger563 merged commit 1e11250 into main Feb 7, 2026
110 of 111 checks passed
@finger563 finger563 deleted the fix/596-icm20948-magnetometer-endianness branch February 7, 2026 23:48
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Icm20948<I>::RawValue Icm20948<I>::get_magnetometer_raw(std::error_code &ec) {
return get_raw(RegisterBank0::MAG_DATA, ec);
// the magnetometer data is read through the I2C master, but it has a
// different endianess than the accelerometer and gyroscope data.
Copy link

Copilot AI Feb 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in comment: “endianess” should be “endianness”.

Suggested change
// different endianess than the accelerometer and gyroscope data.
// different endianness than the accelerometer and gyroscope data.

Copilot uses AI. Check for mistakes.
Comment on lines +1030 to +1034
return {
static_cast<int16_t>(((raw_mag.x & 0xFF00) >> 8) | ((raw_mag.x & 0xFF) << 8)),
static_cast<int16_t>(((raw_mag.y & 0xFF00) >> 8) | ((raw_mag.y & 0xFF) << 8)),
static_cast<int16_t>(((raw_mag.z & 0xFF00) >> 8) | ((raw_mag.z & 0xFF) << 8)),
};
Copy link

Copilot AI Feb 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The manual byte-swap logic is duplicated for x/y/z and is a bit hard to read/maintain. Consider extracting a small helper (e.g., a local lambda) that swaps a 16-bit value (ideally operating on uint16_t) and reuse it for each axis to reduce repetition and avoid subtle signed/bitshift pitfalls.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working icm20948

Projects

None yet

Development

Successfully merging this pull request may close these issues.

icm20948 incorrect mangetometer sensitivity

1 participant