Skip to content

Instantly share code, notes, and snippets.

@SharpCoder
Last active March 6, 2023 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SharpCoder/ffb87925fdb979135d8aa8d0e1cc6668 to your computer and use it in GitHub Desktop.
Save SharpCoder/ffb87925fdb979135d8aa8d0e1cc6668 to your computer and use it in GitHub Desktop.
Absolute Orientation Problem

Absolute Orientation Problem

This project is all about calculating the position of a telescope. I derive absolute orientation using a magnetometer sensor and accelerometer sensor. By absolute orientation, I mean deriving the euler angles of the telescope. The root problem I'm facing is that the yaw reading experiences a mostly linear error after pitching the device beyond a specific threshold. (About 50 degrees or so).

Reproducing the Problem

To consistently observe the error, a simple experiment can be performed which reliably yields the problem.

Experiment

  • Fix the horizontal axis of the telescope (x-axis) so that it does not move
  • Slowly tilt the telescope upwards (along the y-axis)
  • Review the roll (y) and yaw (x) readings

Observed behavior:

After some threshold of roll, the yaw reading begins to deviate in a linear fashion.

Expected behavior:

The yaw reading should remain constant for the duration of the experiment, since there is no movement along the x-axis of the telescope.

Visual Representation of Error

In this video, you can see me fix the x-axis and then tilt the assembly upwards. The visual representation on the computer shows the yaw and roll values as calculated by the sensor. After a certain threshold, the yellow box shifts significantly to the right.

https://joshcole.dev/telescope-error.mp4

Formulas

The absolute orientation is described via euler angles derived from a 9DoF sensor cluster. Only 6 axis are integrated, however. The accelerometer and the magnetometer.

Variable Formula
Pitch -atan2(-accX, sqrt(accY^2 * accZ^2))
Roll atan2(accY, accZ)

Calculating Yaw

This formula integrates the accelerometer sensor (gravity) and the magnetometer sensor (compass heading) to produce a tilt-compensated heading. And this calculation is where the error is introduced. Pitch and Roll seem to be correct (as far as I can tell).

xHor = magX * cos(pitch) + magY * sin(roll) * sin(pitch) - magZ * cos(roll) * sin(pitch)
yHor = magY * cos(roll) + magZ * sin(roll)
yaw = atan2(yHor, xHor)

Complexities

Mounting Angle

The sensor device is affixed to the telescope at a 45 degree angle (relative to earth horizon). On my particular telescope, that is non-negotiable due to the location of the mounting bracket. Since the readings are relative to gravity, I don't expect this to matter. But perhaps it has unforseen implications.

Circuit position

Calibration

The magnetometer is uncalibrated. In the past, I have attempted to perform a Soft and Hard Iron calibration, with even worse results.

Root Cause Hypothesis

The problem could be one of a few possibilities:

  • The mounting angle changes the dynamics of the math.
  • The magnetometer is not properly calibrated which affects the calculated values
  • The sensors are simply not capable of producing good results at the poles
  • Something something gimbal lock
  • At some point, the accelerometer or magnetometer (or both) have one or more axis flipped

Open Questions

Question 1

Can the mounting angle affect the mathematical formulas I'm using? To my knowledge, these angles are gravity-compensated, so I don't understand why the mounting angle would affect the readings.

Question 2

Can the Hard and Soft iron calibration yield this exact kind of error? What I find interesting is that this error has persisted across multiple devices. I've ran this experiment using a phone as the sensor source, a dedicated circuit, and an off-the-shelf circuit. In all cases, I experienced the same exact problem. Which leads me to question whether calibration is really responsible for the entirety of my error.

Glossary

Term Meaning
Accelerometer A 3d vector (x, y, z) representing the direction of gravity. The unit is generally normalized to m^2
Magnetometer A 3d vector (x, y, z) representing magnetic north. The unit is generally in gauss
Yaw X-axis of the telescope
Roll Y-axis of the telescope
Hard Iron Calibration A procedure to correct the magnetometer based on sensor domain and range
Soft Iron Calibration A procedure to correct the magnetometer based on magnetic interference
9DoF Nine dimensions of freedom. This represents a sensor comprised of a accelerometer, a magnetometer, and a gyroscope
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment