Angle sensor#

End of shaft angle sensing is a classical example for a magnetic position system. The goal is to determine the angular position of a rotating shaft. A magnet, typically a diametrically magnetized cylinder, is mounted at the end of the shaft. A 2D sensor is mounted below. When the shaft rotates the two sensor outputs will be \(s_1=B_0 sin(\varphi)\) and \(s_2=B_0 cos(\varphi)\), so that the angle is uniquely given by \(\varphi = arctan(s_1/s_2)\).

In the example below we show such a typical end-of-shaft system with a 2-pixel sensor, that is commonly used to eliminate external stray fields. In addition, we assume that the magnet is not perfectly mounted at the end of the shaft, but slightly displaced to the side, which results in a wobble motion. Such tolerances are easily implemented with Magpylib, they can be visualized and their influence on the sensor output signal can be tested quickly.

import numpy as np
import as px
import magpylib as magpy

# create magnet
magnet = magpy.magnet.Cylinder(
    magnetization=(1000, 0, 0),
    dimension=(6, 2),
    position=(0, 0, 1.5),

# create shaft dummy with 3D model
shaft = magpy.misc.CustomSource(
    position=(0, 0, 7),
shaft_trace =

# shaft rotation / magnet wobble motion
displacement = 1
angles = np.linspace(0, 360, 72)
coll = magnet + shaft
magnet.move((displacement, 0, 0))
coll.rotate_from_angax(angles, "z", anchor=0, start=0)

# create sensor
gap = 3
sens = magpy.Sensor(
    position=(0, 0, -gap),
    pixel=[(1, 0, 0), (-1, 0, 0)],

# show 3D animation of wobble motion
fig1 = go.Figure(), sens, shaft, animation=True, backend="plotly", canvas=fig1)

# show sensor output in plotly
fig2 = go.Figure()
df = sens.getB(magnet, output="dataframe")
df["angle [deg]"] = angles[df["path"]]

fig2 = px.line(
    x="angle [deg]",
    y=["Bx", "By"],
    labels={"value": "Field [mT]"},
NameError                                 Traceback (most recent call last)
Cell In[1], line 46
     38 sens = magpy.Sensor(
     39     position=(0, 0, -gap),
     40     pixel=[(1, 0, 0), (-1, 0, 0)],
     41     style_pixel_size=0.5,
     42     style_size=1.5,
     43 )
     45 # show 3D animation of wobble motion
---> 46 fig1 = go.Figure()
     47, sens, shaft, animation=True, backend="plotly", canvas=fig1)
     48 fig1.update_layout(scene_camera_eye_z=-1.1)

NameError: name 'go' is not defined