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/>.
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
43 #include "fc/settings.h"
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;
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
);
90 void applyBoardAlignment(int32_t *vec
)
92 if (standardBoardAlignment
) {
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
];