Merge branch 'master' into abo_stats_pages_auto_swap
[inav.git] / src / main / sensors / boardalignment.c
blob205f7c8df305d89928a5b78905a8fd7c4706a2b9
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #include <stdbool.h>
19 #include <stdint.h>
20 #include <math.h>
21 #include <string.h>
23 #include "platform.h"
25 FILE_COMPILE_FOR_SPEED
27 #include "common/maths.h"
28 #include "common/vector.h"
29 #include "common/axis.h"
31 #include "config/parameter_group.h"
32 #include "config/parameter_group_ids.h"
34 #include "drivers/sensor.h"
36 #if defined(UNIT_TEST)
37 // Unit tests can't include settings. Provide some dummy limits.
38 #define SETTING_ALIGN_BOARD_ROLL_MIN -1800
39 #define SETTING_ALIGN_BOARD_ROLL_MAX 3600
40 #define SETTING_ALIGN_BOARD_PITCH_MIN -1800
41 #define SETTING_ALIGN_BOARD_PITCH_MAX 3600
42 #else
43 #include "fc/settings.h"
44 #endif
46 #include "boardalignment.h"
48 static bool standardBoardAlignment = true; // board orientation correction
49 static fpMat3_t boardRotMatrix;
51 // no template required since defaults are zero
52 PG_REGISTER(boardAlignment_t, boardAlignment, PG_BOARD_ALIGNMENT, 0);
54 static bool isBoardAlignmentStandard(const boardAlignment_t *boardAlignment)
56 return !boardAlignment->rollDeciDegrees && !boardAlignment->pitchDeciDegrees && !boardAlignment->yawDeciDegrees;
59 void initBoardAlignment(void)
61 if (isBoardAlignmentStandard(boardAlignment())) {
62 standardBoardAlignment = true;
63 } else {
64 fp_angles_t rotationAngles;
66 standardBoardAlignment = false;
68 rotationAngles.angles.roll = DECIDEGREES_TO_RADIANS(boardAlignment()->rollDeciDegrees );
69 rotationAngles.angles.pitch = DECIDEGREES_TO_RADIANS(boardAlignment()->pitchDeciDegrees);
70 rotationAngles.angles.yaw = DECIDEGREES_TO_RADIANS(boardAlignment()->yawDeciDegrees );
72 rotationMatrixFromAngles(&boardRotMatrix, &rotationAngles);
76 void updateBoardAlignment(int16_t roll, int16_t pitch)
78 const float sinAlignYaw = sin_approx(DECIDEGREES_TO_RADIANS(boardAlignment()->yawDeciDegrees));
79 const float cosAlignYaw = cos_approx(DECIDEGREES_TO_RADIANS(boardAlignment()->yawDeciDegrees));
81 int16_t rollDeciDegrees = boardAlignment()->rollDeciDegrees + -sinAlignYaw * pitch + cosAlignYaw * roll;
82 int16_t pitchDeciDegrees = boardAlignment()->pitchDeciDegrees + cosAlignYaw * pitch + sinAlignYaw * roll;
84 boardAlignmentMutable()->rollDeciDegrees = constrain(rollDeciDegrees, SETTING_ALIGN_BOARD_ROLL_MIN, SETTING_ALIGN_BOARD_ROLL_MAX);
85 boardAlignmentMutable()->pitchDeciDegrees = constrain(pitchDeciDegrees, SETTING_ALIGN_BOARD_PITCH_MIN, SETTING_ALIGN_BOARD_PITCH_MAX);
87 initBoardAlignment();
90 void applyBoardAlignment(int32_t *vec)
92 if (standardBoardAlignment) {
93 return;
96 fpVector3_t fpVec = { .v = { vec[X], vec[Y], vec[Z] } };
97 rotationMatrixRotateVector(&fpVec, &fpVec, &boardRotMatrix);
99 vec[X] = lrintf(fpVec.x);
100 vec[Y] = lrintf(fpVec.y);
101 vec[Z] = lrintf(fpVec.z);
104 void FAST_CODE applySensorAlignment(int32_t * dest, int32_t * src, uint8_t rotation)
106 // Create a copy so we could use the same buffer for src & dest
107 const int32_t x = src[X];
108 const int32_t y = src[Y];
109 const int32_t z = src[Z];
111 switch (rotation) {
112 default:
113 case CW0_DEG:
114 dest[X] = x;
115 dest[Y] = y;
116 dest[Z] = z;
117 break;
118 case CW90_DEG:
119 dest[X] = y;
120 dest[Y] = -x;
121 dest[Z] = z;
122 break;
123 case CW180_DEG:
124 dest[X] = -x;
125 dest[Y] = -y;
126 dest[Z] = z;
127 break;
128 case CW270_DEG:
129 dest[X] = -y;
130 dest[Y] = x;
131 dest[Z] = z;
132 break;
133 case CW0_DEG_FLIP:
134 dest[X] = -x;
135 dest[Y] = y;
136 dest[Z] = -z;
137 break;
138 case CW90_DEG_FLIP:
139 dest[X] = y;
140 dest[Y] = x;
141 dest[Z] = -z;
142 break;
143 case CW180_DEG_FLIP:
144 dest[X] = x;
145 dest[Y] = -y;
146 dest[Z] = -z;
147 break;
148 case CW270_DEG_FLIP:
149 dest[X] = -y;
150 dest[Y] = -x;
151 dest[Z] = -z;
152 break;