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