2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
28 #include "common/utils.h"
29 #include "common/maths.h"
30 #include "common/axis.h"
31 #include "common/sensor_alignment.h"
34 #include "pg/pg_ids.h"
36 #include "drivers/sensor.h"
38 #include "boardalignment.h"
40 static bool standardBoardAlignment
= true; // board orientation correction
41 static fp_rotationMatrix_t boardRotation
;
43 // no template required since defaults are zero
44 PG_REGISTER(boardAlignment_t
, boardAlignment
, PG_BOARD_ALIGNMENT
, 0);
46 static bool isBoardAlignmentStandard(const boardAlignment_t
*boardAlignment
)
48 return !boardAlignment
->rollDegrees
&& !boardAlignment
->pitchDegrees
&& !boardAlignment
->yawDegrees
;
51 void initBoardAlignment(const boardAlignment_t
*boardAlignment
)
53 if (isBoardAlignmentStandard(boardAlignment
)) {
57 standardBoardAlignment
= false;
59 fp_angles_t rotationAngles
;
60 rotationAngles
.angles
.roll
= degreesToRadians(boardAlignment
->rollDegrees
);
61 rotationAngles
.angles
.pitch
= degreesToRadians(boardAlignment
->pitchDegrees
);
62 rotationAngles
.angles
.yaw
= degreesToRadians(boardAlignment
->yawDegrees
);
64 buildRotationMatrix(&rotationAngles
, &boardRotation
);
67 static void alignBoard(float *vec
)
69 applyMatrixRotation(vec
, &boardRotation
);
72 FAST_CODE_NOINLINE
void alignSensorViaMatrix(float *dest
, fp_rotationMatrix_t
* sensorRotationMatrix
)
74 applyMatrixRotation(dest
, sensorRotationMatrix
);
76 if (!standardBoardAlignment
) {
81 void alignSensorViaRotation(float *dest
, uint8_t rotation
)
83 const float x
= dest
[X
];
84 const float y
= dest
[Y
];
85 const float z
= dest
[Z
];
131 if (!standardBoardAlignment
) {