Skip to content

icm20948 incorrect mangetometer sensitivity #596

@ydvo

Description

@ydvo

Answers checklist.

  • I have read the documentation ESP-IDF Programming Guide and the issue is not addressed there.
  • I have searched the issue tracker for a similar issue and not found a similar issue.

What is the expected behavior?

The icm20498 magnetometer should return values in a +/- 4900 uT range as indicated by the datasheet.

What is the actual behavior?

The magnetometer returns absurdly high values.
ex: -188374.5781 x 18856.1875 y 190086.9531 z

Steps to reproduce.

Call read_magnetometer(ec) from icm20948.cpp component

Build or installation Logs.


More Information.

It seems like the scaling for the magnetometer in the icm20948 component is applied incorrectly at line 699 in icm20948.cpp:

template <espp::icm20948::Interface I>
Icm20948<I>::Value Icm20948<I>::read_magnetometer(std::error_code &ec) {
  std::lock_guard<std::recursive_mutex> lock(base_mutex_);
  RawValue raw = get_magnetometer_raw(ec);
  if (ec) {
    return {0.0f, 0.0f, 0.0f};
  }
  float sensitivity = get_magnetometer_sensitivity();
  Value value = {
      static_cast<float>(raw.x) / sensitivity,
      static_cast<float>(raw.y) / sensitivity,
      static_cast<float>(raw.z) / sensitivity,
  };
  // update values
  mag_values_ = value;
  return value;
}

get_magnetometer_sensitivity() returns the scaling factor in uT/LSB so should be applied by multiplying not dividing.

Changing this locally returns values in the expected range.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingicm20948

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions