xref: /openbmc/linux/Documentation/devicetree/bindings/iio/mount-matrix.txt (revision 976e3645923bdd2fe7893aae33fd7a21098bfb28)
1*06ae56b9SLinus WalleijFor discussion. Unclear are:
2*06ae56b9SLinus Walleij* is the definition of +/- values practical or counterintuitive?
3*06ae56b9SLinus Walleij* are the definitions unambiguous and easy to follow?
4*06ae56b9SLinus Walleij* are the examples correct?
5*06ae56b9SLinus Walleij* should we have HOWTO engineer a correct matrix for a new device (without comparing to a different one)?
6*06ae56b9SLinus Walleij
7*06ae56b9SLinus Walleij====
8*06ae56b9SLinus Walleij
9*06ae56b9SLinus Walleij
10*06ae56b9SLinus WalleijMounting matrix
11*06ae56b9SLinus Walleij
12*06ae56b9SLinus WalleijThe mounting matrix is a device tree property used to orient any device
13*06ae56b9SLinus Walleijthat produce three-dimensional data in relation to the world where it is
14*06ae56b9SLinus Walleijdeployed.
15*06ae56b9SLinus Walleij
16*06ae56b9SLinus WalleijThe purpose of the mounting matrix is to translate the sensor frame of
17*06ae56b9SLinus Walleijreference into the device frame of reference using a translation matrix as
18*06ae56b9SLinus Walleijdefined in linear algebra.
19*06ae56b9SLinus Walleij
20*06ae56b9SLinus WalleijThe typical usecase is that where a component has an internal representation
21*06ae56b9SLinus Walleijof the (x,y,z) triplets, such as different registers to read these coordinates,
22*06ae56b9SLinus Walleijand thus implying that the component should be mounted in a certain orientation
23*06ae56b9SLinus Walleijrelative to some specific device frame of reference.
24*06ae56b9SLinus Walleij
25*06ae56b9SLinus WalleijFor example a device with some kind of screen, where the user is supposed to
26*06ae56b9SLinus Walleijinteract with the environment using an accelerometer, gyroscope or magnetometer
27*06ae56b9SLinus Walleijmounted on the same chassis as this screen, will likely take the screen as
28*06ae56b9SLinus Walleijreference to (x,y,z) orientation, with (x,y) corresponding to these axes on the
29*06ae56b9SLinus Walleijscreen and (z) being depth, the axis perpendicular to the screen.
30*06ae56b9SLinus Walleij
31*06ae56b9SLinus WalleijFor a screen you probably want (x) coordinates to go from negative on the left
32*06ae56b9SLinus Walleijto positive on the right, (y) from negative on the bottom to positive on top
33*06ae56b9SLinus Walleijand (z) depth to be negative under the screen and positive in front of it,
34*06ae56b9SLinus Walleijtoward the face of the user.
35*06ae56b9SLinus Walleij
36*06ae56b9SLinus WalleijA sensor can be mounted in any angle along the axes relative to the frame of
37*06ae56b9SLinus Walleijreference. This means that the sensor may be flipped upside-down, left-right,
38*06ae56b9SLinus Walleijor tilted at any angle relative to the frame of reference.
39*06ae56b9SLinus Walleij
40*06ae56b9SLinus WalleijAnother frame of reference is how the device with its sensor relates to the
41*06ae56b9SLinus Walleijexternal world, the environment where the device is deployed. Usually the data
42*06ae56b9SLinus Walleijfrom the sensor is used to figure out how the device is oriented with respect
43*06ae56b9SLinus Walleijto this world. When using the mounting matrix, the sensor and device orientation
44*06ae56b9SLinus Walleijbecomes identical and we can focus on the data as it relates to the surrounding
45*06ae56b9SLinus Walleijworld.
46*06ae56b9SLinus Walleij
47*06ae56b9SLinus WalleijDevice-to-world examples for some three-dimensional sensor types:
48*06ae56b9SLinus Walleij
49*06ae56b9SLinus Walleij- Accelerometers have their world frame of reference toward the center of
50*06ae56b9SLinus Walleij  gravity, usually to the core of the planet. A reading of the (x,y,z) values
51*06ae56b9SLinus Walleij  from the sensor will give a projection of the gravity vector through the
52*06ae56b9SLinus Walleij  device relative to the center of the planet, i.e. relative to its surface at
53*06ae56b9SLinus Walleij  this point. Up and down in the world relative to the device frame of
54*06ae56b9SLinus Walleij  reference can thus be determined. and users would likely expect a value of
55*06ae56b9SLinus Walleij  9.81 m/s^2 upwards along the (z) axis, i.e. out of the screen when the device
56*06ae56b9SLinus Walleij  is held with its screen flat on the planets surface and 0 on the other axes,
57*06ae56b9SLinus Walleij  as the gravity vector is projected 1:1 onto the sensors (z)-axis.
58*06ae56b9SLinus Walleij
59*06ae56b9SLinus Walleij  If you tilt the device, the g vector virtually coming out of the display
60*06ae56b9SLinus Walleij  is projected onto the (x,y) plane of the display panel.
61*06ae56b9SLinus Walleij
62*06ae56b9SLinus Walleij  Example:
63*06ae56b9SLinus Walleij
64*06ae56b9SLinus Walleij         ^ z: +g                   ^ z: > 0
65*06ae56b9SLinus Walleij         !                        /!
66*06ae56b9SLinus Walleij         ! x=y=0                 / ! x: > 0
67*06ae56b9SLinus Walleij     +--------+             +--------+
68*06ae56b9SLinus Walleij     !        !             !        !
69*06ae56b9SLinus Walleij     +--------+             +--------+
70*06ae56b9SLinus Walleij         !                    /
71*06ae56b9SLinus Walleij         !                   /
72*06ae56b9SLinus Walleij         v                  v
73*06ae56b9SLinus Walleij      center of         center of
74*06ae56b9SLinus Walleij       gravity           gravity
75*06ae56b9SLinus Walleij
76*06ae56b9SLinus Walleij
77*06ae56b9SLinus Walleij  If the device is tilted to the left, you get a positive x value. If you point
78*06ae56b9SLinus Walleij  its top towards surface, you get a negative y axis.
79*06ae56b9SLinus Walleij
80*06ae56b9SLinus Walleij     (---------)
81*06ae56b9SLinus Walleij     !         !           y: -g
82*06ae56b9SLinus Walleij     !         !             ^
83*06ae56b9SLinus Walleij     !         !             !
84*06ae56b9SLinus Walleij     !         !
85*06ae56b9SLinus Walleij     !         !  x: +g <- z: +g  -> x: -g
86*06ae56b9SLinus Walleij     ! 1  2  3 !
87*06ae56b9SLinus Walleij     ! 4  5  6 !             !
88*06ae56b9SLinus Walleij     ! 7  8  9 !             v
89*06ae56b9SLinus Walleij     ! *  0  # !           y: +g
90*06ae56b9SLinus Walleij     (---------)
91*06ae56b9SLinus Walleij
92*06ae56b9SLinus Walleij
93*06ae56b9SLinus Walleij- Magnetometers (compasses) have their world frame of reference relative to the
94*06ae56b9SLinus Walleij  geomagnetic field. The system orientation vis-a-vis the world is defined with
95*06ae56b9SLinus Walleij  respect to the local earth geomagnetic reference frame where (y) is in the
96*06ae56b9SLinus Walleij  ground plane and positive towards magnetic North, (x) is in the ground plane,
97*06ae56b9SLinus Walleij  perpendicular to the North axis and positive towards the East and (z) is
98*06ae56b9SLinus Walleij  perpendicular to the ground plane and positive upwards.
99*06ae56b9SLinus Walleij
100*06ae56b9SLinus Walleij
101*06ae56b9SLinus Walleij     ^^^ North: y > 0
102*06ae56b9SLinus Walleij
103*06ae56b9SLinus Walleij     (---------)
104*06ae56b9SLinus Walleij     !         !
105*06ae56b9SLinus Walleij     !         !
106*06ae56b9SLinus Walleij     !         !
107*06ae56b9SLinus Walleij     !         !  >
108*06ae56b9SLinus Walleij     !         !  > North: x > 0
109*06ae56b9SLinus Walleij     ! 1  2  3 !  >
110*06ae56b9SLinus Walleij     ! 4  5  6 !
111*06ae56b9SLinus Walleij     ! 7  8  9 !
112*06ae56b9SLinus Walleij     ! *  0  # !
113*06ae56b9SLinus Walleij     (---------)
114*06ae56b9SLinus Walleij
115*06ae56b9SLinus Walleij  Since the geomagnetic field is not uniform this definition fails if we come
116*06ae56b9SLinus Walleij  closer to the poles.
117*06ae56b9SLinus Walleij
118*06ae56b9SLinus Walleij  Sensors and driver can not and should not take care of this because there
119*06ae56b9SLinus Walleij  are complex calculations and empirical data to be taken care of. We leave
120*06ae56b9SLinus Walleij  this up to user space.
121*06ae56b9SLinus Walleij
122*06ae56b9SLinus Walleij  The definition we take:
123*06ae56b9SLinus Walleij
124*06ae56b9SLinus Walleij  If the device is placed at the equator and the top is pointing north, the
125*06ae56b9SLinus Walleij  display is readable by a person standing upright on the earth surface, this
126*06ae56b9SLinus Walleij  defines a positive y value.
127*06ae56b9SLinus Walleij
128*06ae56b9SLinus Walleij
129*06ae56b9SLinus Walleij- Gyroscopes detects the movement relative the device itself. The angular
130*06ae56b9SLinus Walleij  velocity is defined as orthogonal to the plane of rotation, so if you put the
131*06ae56b9SLinus Walleij  device on a flat surface and spin it around the z axis (such as rotating a
132*06ae56b9SLinus Walleij  device with a screen lying flat on a table), you should get a negative value
133*06ae56b9SLinus Walleij  along the (z) axis if rotated clockwise, and a positive value if rotated
134*06ae56b9SLinus Walleij  counter-clockwise according to the right-hand rule.
135*06ae56b9SLinus Walleij
136*06ae56b9SLinus Walleij
137*06ae56b9SLinus Walleij     (---------)     y > 0
138*06ae56b9SLinus Walleij     !         !     v---\
139*06ae56b9SLinus Walleij     !         !
140*06ae56b9SLinus Walleij     !         !
141*06ae56b9SLinus Walleij     !         !      <--\
142*06ae56b9SLinus Walleij     !         !         ! z > 0
143*06ae56b9SLinus Walleij     ! 1  2  3 !       --/
144*06ae56b9SLinus Walleij     ! 4  5  6 !
145*06ae56b9SLinus Walleij     ! 7  8  9 !
146*06ae56b9SLinus Walleij     ! *  0  # !
147*06ae56b9SLinus Walleij     (---------)
148*06ae56b9SLinus Walleij
149*06ae56b9SLinus Walleij
150*06ae56b9SLinus WalleijSo unless the sensor is ideally mounted, we need a means to indicate the
151*06ae56b9SLinus Walleijrelative orientation of any given sensor of this type with respect to the
152*06ae56b9SLinus Walleijframe of reference.
153*06ae56b9SLinus Walleij
154*06ae56b9SLinus WalleijTo achieve this, use the device tree property "mount-matrix" for the sensor.
155*06ae56b9SLinus Walleij
156*06ae56b9SLinus WalleijThis supplies a 3x3 rotation matrix in the strict linear algebraic sense,
157*06ae56b9SLinus Walleijto orient the senor axes relative to a desired point of reference. This means
158*06ae56b9SLinus Walleijthe resulting values from the sensor, after scaling to proper units, should be
159*06ae56b9SLinus Walleijmultiplied by this matrix to give the proper vectors values in three-dimensional
160*06ae56b9SLinus Walleijspace, relative to the device or world point of reference.
161*06ae56b9SLinus Walleij
162*06ae56b9SLinus WalleijFor more information, consult:
163*06ae56b9SLinus Walleijhttps://en.wikipedia.org/wiki/Rotation_matrix
164*06ae56b9SLinus Walleij
165*06ae56b9SLinus WalleijThe mounting matrix has the layout:
166*06ae56b9SLinus Walleij
167*06ae56b9SLinus Walleij (mxx, myx, mzx)
168*06ae56b9SLinus Walleij (mxy, myy, mzy)
169*06ae56b9SLinus Walleij (mxz, myz, mzz)
170*06ae56b9SLinus Walleij
171*06ae56b9SLinus WalleijValues are intended to be multiplied as:
172*06ae56b9SLinus Walleij
173*06ae56b9SLinus Walleij  x' = mxx * x + myx * y + mzx * z
174*06ae56b9SLinus Walleij  y' = mxy * x + myy * y + mzy * z
175*06ae56b9SLinus Walleij  z' = mxz * x + myz * y + mzz * z
176*06ae56b9SLinus Walleij
177*06ae56b9SLinus WalleijIt is represented as an array of strings containing the real values for
178*06ae56b9SLinus Walleijproducing the transformation matrix.
179*06ae56b9SLinus Walleij
180*06ae56b9SLinus WalleijExamples:
181*06ae56b9SLinus Walleij
182*06ae56b9SLinus WalleijIdentity matrix (nothing happens to the coordinates, which means the device was
183*06ae56b9SLinus Walleijmechanically mounted in an ideal way and we need no transformation):
184*06ae56b9SLinus Walleij
185*06ae56b9SLinus Walleijmount-matrix = "1", "0", "0",
186*06ae56b9SLinus Walleij               "0", "1", "0",
187*06ae56b9SLinus Walleij               "0", "0", "1";
188*06ae56b9SLinus Walleij
189*06ae56b9SLinus WalleijThe sensor is mounted 30 degrees (Pi/6 radians) tilted along the X axis, so we
190*06ae56b9SLinus Walleijcompensate by performing a -30 degrees rotation around the X axis:
191*06ae56b9SLinus Walleij
192*06ae56b9SLinus Walleijmount-matrix = "1", "0", "0",
193*06ae56b9SLinus Walleij               "0", "0.866", "0.5",
194*06ae56b9SLinus Walleij               "0", "-0.5", "0.866";
195*06ae56b9SLinus Walleij
196*06ae56b9SLinus WalleijThe sensor is flipped 180 degrees (Pi radians) around the Z axis, i.e. mounted
197*06ae56b9SLinus Walleijupside-down:
198*06ae56b9SLinus Walleij
199*06ae56b9SLinus Walleijmount-matrix = "0.998", "0.054", "0",
200*06ae56b9SLinus Walleij               "-0.054", "0.998", "0",
201*06ae56b9SLinus Walleij               "0", "0", "1";
202*06ae56b9SLinus Walleij
203*06ae56b9SLinus Walleij???: this does not match "180 degrees" - factors indicate ca. 3 degrees compensation
204