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/>.
29 #include "build/debug.h"
31 #include "common/filter.h"
32 #include "common/utils.h"
34 #include "config/feature.h"
36 #include "sensors/acceleration_init.h"
37 #include "sensors/boardalignment.h"
38 #include "sensors/gyro.h"
40 #include "acceleration.h"
42 FAST_DATA_ZERO_INIT acc_t acc
; // acc access functions
44 static inline void alignAccelerometer(void)
46 switch (acc
.dev
.accAlign
) {
48 alignSensorViaMatrix(&acc
.accADC
, &acc
.dev
.rotationMatrix
);
51 alignSensorViaRotation(&acc
.accADC
, acc
.dev
.accAlign
);
56 static inline void calibrateAccelerometer(void)
58 if (!accIsCalibrationComplete()) {
59 // acc.accADC is held at 0 until calibration is completed
60 performAcclerationCalibration(&accelerometerConfigMutable()->accelerometerTrims
);
63 if (featureIsEnabled(FEATURE_INFLIGHT_ACC_CAL
)) {
64 performInflightAccelerationCalibration(&accelerometerConfigMutable()->accelerometerTrims
);
68 static inline void applyAccelerationTrims(const flightDynamicsTrims_t
*accelerationTrims
)
70 acc
.accADC
.x
-= accelerationTrims
->raw
[X
];
71 acc
.accADC
.y
-= accelerationTrims
->raw
[Y
];
72 acc
.accADC
.z
-= accelerationTrims
->raw
[Z
];
75 static inline void postProcessAccelerometer(void)
77 static vector3_t accAdcPrev
;
79 for (unsigned axis
= 0; axis
< XYZ_AXIS_COUNT
; axis
++) {
81 // Apply anti-alias filter for attitude task (if enabled)
82 if (axis
== gyro
.gyroDebugAxis
) {
83 DEBUG_SET(DEBUG_ACCELEROMETER
, 0, lrintf(acc
.accADC
.v
[axis
]));
86 if (accelerationRuntime
.accLpfCutHz
) {
87 acc
.accADC
.v
[axis
] = pt2FilterApply(&accelerationRuntime
.accFilter
[axis
], acc
.accADC
.v
[axis
]);
90 // Calculate derivative of acc (jerk)
91 acc
.jerk
.v
[axis
] = (acc
.accADC
.v
[axis
] - accAdcPrev
.v
[axis
]) * acc
.sampleRateHz
;
92 accAdcPrev
.v
[axis
] = acc
.accADC
.v
[axis
];
94 if (axis
== gyro
.gyroDebugAxis
) {
95 DEBUG_SET(DEBUG_ACCELEROMETER
, 1, lrintf(acc
.accADC
.v
[axis
]));
96 DEBUG_SET(DEBUG_ACCELEROMETER
, 3, lrintf(acc
.jerk
.v
[axis
] * 1e-2f
));
100 acc
.accMagnitude
= vector3Norm(&acc
.accADC
) * acc
.dev
.acc_1G_rec
;
101 acc
.jerkMagnitude
= vector3Norm(&acc
.jerk
) * acc
.dev
.acc_1G_rec
;
103 DEBUG_SET(DEBUG_ACCELEROMETER
, 2, lrintf(acc
.accMagnitude
* 1e3f
));
104 DEBUG_SET(DEBUG_ACCELEROMETER
, 4, lrintf(acc
.jerkMagnitude
* 1e3f
));
107 void accUpdate(timeUs_t currentTimeUs
)
109 UNUSED(currentTimeUs
);
111 if (!acc
.dev
.readFn(&acc
.dev
)) {
115 for (int axis
= 0; axis
< XYZ_AXIS_COUNT
; axis
++) {
116 acc
.accADC
.v
[axis
] = acc
.dev
.ADCRaw
[axis
];
119 alignAccelerometer();
120 calibrateAccelerometer();
121 applyAccelerationTrims(accelerationRuntime
.accelerationTrims
);
122 postProcessAccelerometer();
124 acc
.isAccelUpdatedAtLeastOnce
= true;