Skip to content

Commit

Permalink
Merge pull request #9078 from iNavFlight/dzikuvx-dsp-gyro-operations
Browse files Browse the repository at this point in the history
DSP based gyro operations
  • Loading branch information
DzikuVx authored Jun 11, 2023
2 parents a5e700f + 1c38017 commit 12e13da
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 11 deletions.
2 changes: 2 additions & 0 deletions cmake/at32.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ set(CMSIS_DSP_DIR "${MAIN_LIB_DIR}/main/CMSIS/DSP")
set(CMSIS_DSP_INCLUDE_DIR "${CMSIS_DSP_DIR}/Include")

set(CMSIS_DSP_SRC
BasicMathFunctions/arm_scale_f32.c
BasicMathFunctions/arm_sub_f32.c
BasicMathFunctions/arm_mult_f32.c
TransformFunctions/arm_rfft_fast_f32.c
TransformFunctions/arm_cfft_f32.c
Expand Down
2 changes: 2 additions & 0 deletions cmake/stm32.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ set(CMSIS_DSP_DIR "${MAIN_LIB_DIR}/main/CMSIS/DSP")
set(CMSIS_DSP_INCLUDE_DIR "${CMSIS_DSP_DIR}/Include")

set(CMSIS_DSP_SRC
BasicMathFunctions/arm_scale_f32.c
BasicMathFunctions/arm_sub_f32.c
BasicMathFunctions/arm_mult_f32.c
TransformFunctions/arm_rfft_fast_f32.c
TransformFunctions/arm_cfft_f32.c
Expand Down
38 changes: 35 additions & 3 deletions src/main/common/maths.c
Original file line number Diff line number Diff line change
Expand Up @@ -538,11 +538,43 @@ float fast_fsqrtf(const float value) {
// function to calculate the normalization (pythagoras) of a 2-dimensional vector
float NOINLINE calc_length_pythagorean_2D(const float firstElement, const float secondElement)
{
return fast_fsqrtf(sq(firstElement) + sq(secondElement));
return fast_fsqrtf(sq(firstElement) + sq(secondElement));
}

// function to calculate the normalization (pythagoras) of a 3-dimensional vector
float NOINLINE calc_length_pythagorean_3D(const float firstElement, const float secondElement, const float thirdElement)
{
return fast_fsqrtf(sq(firstElement) + sq(secondElement) + sq(thirdElement));
}
return fast_fsqrtf(sq(firstElement) + sq(secondElement) + sq(thirdElement));
}

#ifdef SITL_BUILD

/**
* @brief Floating-point vector subtraction, equivalent of CMSIS arm_sub_f32.
*/
void arm_sub_f32(
float * pSrcA,
float * pSrcB,
float * pDst,
uint32_t blockSize)
{
for (uint32_t i = 0; i < blockSize; i++) {
pDst[i] = pSrcA[i] - pSrcB[i];
}
}

/**
* @brief Floating-point vector scaling, equivalent of CMSIS arm_scale_f32.
*/
void arm_scale_f32(
float * pSrc,
float scale,
float * pDst,
uint32_t blockSize)
{
for (uint32_t i = 0; i < blockSize; i++) {
pDst[i] = pSrc[i] * scale;
}
}

#endif
7 changes: 6 additions & 1 deletion src/main/common/maths.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,9 @@ float calc_length_pythagorean_3D(const float firstElement, const float secondEle
* The most significat byte is placed at the highest address
* in other words, the most significant byte is "last", on odd indexes
*/
#define int16_val_little_endian(v, idx) ((int16_t)(((uint8_t)v[2 * idx + 1] << 8) | v[2 * idx]))
#define int16_val_little_endian(v, idx) ((int16_t)(((uint8_t)v[2 * idx + 1] << 8) | v[2 * idx]))

#ifdef SITL_BUILD
void arm_sub_f32(float * pSrcA, float * pSrcB, float * pDst, uint32_t blockSize);
void arm_scale_f32(float * pSrc, float scale, float * pDst, uint32_t blockSize);
#endif
10 changes: 3 additions & 7 deletions src/main/sensors/gyro.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,19 +411,15 @@ static bool FAST_CODE NOINLINE gyroUpdateAndCalibrate(gyroDev_t * gyroDev, zeroC
if (zeroCalibrationIsCompleteV(gyroCal)) {
float gyroADCtmp[XYZ_AXIS_COUNT];

// Copy gyro value into int32_t (to prevent overflow) and then apply calibration and alignment
gyroADCtmp[X] = gyroDev->gyroADCRaw[X] - (int32_t)gyroDev->gyroZero[X];
gyroADCtmp[Y] = gyroDev->gyroADCRaw[Y] - (int32_t)gyroDev->gyroZero[Y];
gyroADCtmp[Z] = gyroDev->gyroADCRaw[Z] - (int32_t)gyroDev->gyroZero[Z];
//Apply zero calibration with CMSIS DSP
arm_sub_f32(gyroDev->gyroADCRaw, gyroDev->gyroZero, gyroADCtmp, 3);

// Apply sensor alignment
applySensorAlignment(gyroADCtmp, gyroADCtmp, gyroDev->gyroAlign);
applyBoardAlignment(gyroADCtmp);

// Convert to deg/s and store in unified data
gyroADCf[X] = (float)gyroADCtmp[X] * gyroDev->scale;
gyroADCf[Y] = (float)gyroADCtmp[Y] * gyroDev->scale;
gyroADCf[Z] = (float)gyroADCtmp[Z] * gyroDev->scale;
arm_scale_f32(gyroADCtmp, gyroDev->scale, gyroADCf, 3);

return true;
} else {
Expand Down
5 changes: 5 additions & 0 deletions src/main/sensors/gyro.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
#include "drivers/sensor.h"
#include "flight/dynamic_gyro_notch.h"
#include "flight/secondary_dynamic_gyro_notch.h"
#if !defined(SITL_BUILD)
#include "arm_math.h"
#else
#include <math.h>
#endif

typedef enum {
GYRO_NONE = 0,
Expand Down
1 change: 1 addition & 0 deletions src/test/unit/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#pragma once

#define SITL_BUILD
#define USE_MAG
#define USE_BARO
#define USE_GPS
Expand Down

0 comments on commit 12e13da

Please sign in to comment.