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