Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Magnetometer: correct field calculation #2460

Merged
merged 4 commits into from
Jul 8, 2024

Conversation

srmainwaring
Copy link
Contributor

@srmainwaring srmainwaring commented Jul 1, 2024

🦟 Bug fix

Summary

Correct the magnitude and direction of the magnetic field calculation in the Magnetometer sensor.

Details

  • The magnetic intensity table has units centi gauss not centi tesla.
  • Add a conversion to publish the magnetic field in tesla.
  • The formula to calculate the field from the declination, inclination and intensity is in the NED frame.
  • Convert the field orientation to the ENU frame to conform to the Gazebo world frame convention.

Context

The issue was found during the development of external sensor support for ArduPilot using DDS to subscribe to ROS based sensor topics (in this case magnetic field strength published from Gazebo via the ros_gz bridge).

ArduPilot SITL and Gazebo use similar calculations to simulate the earth's magnetic field at a given location (lat, lon). There was an inconsistency between the two calculations which can be attributed to a combination of incorrect units and frame convention.

The attached notebook reproduces both calculations in Python for comparison.

earth_mag_field.ipynb.zip

Checklist

  • Signed all commits for DCO
  • Added tests
  • Updated documentation (as needed)
  • Updated migration guide (as needed)
  • Consider updating Python bindings (if the library has them)
  • codecheck passed (See contributing)
  • All tests passed (See test coverage)
  • While waiting for a review on your PR, please help review another open pull request to support the maintainers

Note to maintainers: Remember to use Squash-Merge and edit the commit message to match the pull request summary while retaining Signed-off-by messages.

- The magnetic intensity table has units centi gauss not centi tesla
- Add conversion to publish field in telsa.
- Field is calculated in NED frame.
- Convert to ENU frame for Gazebo world frame convention.

Signed-off-by: Rhys Mainwaring <[email protected]>
@srmainwaring srmainwaring requested a review from mjcarroll as a code owner July 1, 2024 13:48
@github-actions github-actions bot added the 🎵 harmonic Gazebo Harmonic label Jul 1, 2024
@azeey
Copy link
Contributor

azeey commented Jul 1, 2024

There's a stalled PR #2336 that was fixing the units. There we decided to add a flag to enable this so as to not break behavior. Can we do the same here?

@srmainwaring
Copy link
Contributor Author

srmainwaring commented Jul 1, 2024

There's a stalled PR #2336 that was fixing the units. There we decided to add a flag to enable this so as to not break behavior. Can we do the same here?

Thanks for the link to the PR, missed that.

The problem with the approach of continuing with the wrong units and orientation by default is that it creates issues downstream - particularly once the data is pushed into the ROS data space where the assumption is that it will be standards conforming.

Perhaps we could do the following:

Add params, which for garden and harmonic default to current behaviour:

<use_units_gauss>true</use_units_gauss>
<use_earth_frame_ned>true</use_earth_frame_ned>

and mark this behaviour as deprecated. Then switch to standard conventions as default if omitted (i.e. both false) in a future release.

Updated in: e8f1628

Copy link
Contributor

@azeey azeey left a comment

Choose a reason for hiding this comment

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

Adding explicit parameters for this sounds good to me. As far as changing the default, I see that https://www.ros.org/reps/rep-0103.html NED for outdoor systems. Shouldn't NED be the default then?

I just have one minor comment. Thanks!

src/systems/magnetometer/Magnetometer.cc Outdated Show resolved Hide resolved
@srmainwaring
Copy link
Contributor Author

srmainwaring commented Jul 2, 2024

Shouldn't NED be the default then?

The reasoning is that for Gazebo the world frame is ENU, so to be consistent with the orientation given by other sensors (say NavSat when spherical coordinates are provided) I think we need to use ENU when setting the mag field in the world frame.

I'll need to review REP 103 again for the ROS msgs, if the convention is NED rather than ENU that should probably be handled in the ros_gz bridge.

Update

REP 103 suggests

For short-range Cartesian representations of geographic locations, use the east north up [5] (ENU) convention:

X east
Y north
Z up

If magnetic field data is regarded as geographic data the default frame (no suffix) is ENU. To use the NED frame (which is what aerial vehicles use in flight controllers) the ROS frame_id would add the _ned suffix.

@scpeters scpeters merged commit 771d4fa into gazebosim:gz-sim8 Jul 8, 2024
9 checks passed
@azeey
Copy link
Contributor

azeey commented Jul 8, 2024

Okay, thanks for the explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🎵 harmonic Gazebo Harmonic
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants