Skip to content

Commit e1798a7

Browse files
committed
Merge remote-tracking branch 'origin/master' into mmosca-gps-glonass
2 parents 7cd8076 + 97ffaa9 commit e1798a7

File tree

10 files changed

+97
-22
lines changed

10 files changed

+97
-22
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ jobs:
7979
- uses: actions/checkout@v3
8080
- name: Install dependencies
8181
run: |
82-
#brew update
8382
brew install cmake ninja ruby
8483
8584
- name: Setup environment

cmake/at32.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ set(CMSIS_DSP_DIR "${MAIN_LIB_DIR}/main/CMSIS/DSP")
1515
set(CMSIS_DSP_INCLUDE_DIR "${CMSIS_DSP_DIR}/Include")
1616

1717
set(CMSIS_DSP_SRC
18+
BasicMathFunctions/arm_scale_f32.c
19+
BasicMathFunctions/arm_sub_f32.c
1820
BasicMathFunctions/arm_mult_f32.c
1921
TransformFunctions/arm_rfft_fast_f32.c
2022
TransformFunctions/arm_cfft_f32.c

cmake/stm32.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ set(CMSIS_DSP_DIR "${MAIN_LIB_DIR}/main/CMSIS/DSP")
1616
set(CMSIS_DSP_INCLUDE_DIR "${CMSIS_DSP_DIR}/Include")
1717

1818
set(CMSIS_DSP_SRC
19+
BasicMathFunctions/arm_scale_f32.c
20+
BasicMathFunctions/arm_sub_f32.c
1921
BasicMathFunctions/arm_mult_f32.c
2022
TransformFunctions/arm_rfft_fast_f32.c
2123
TransformFunctions/arm_cfft_f32.c

src/main/common/maths.c

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,11 +538,57 @@ float fast_fsqrtf(const float value) {
538538
// function to calculate the normalization (pythagoras) of a 2-dimensional vector
539539
float NOINLINE calc_length_pythagorean_2D(const float firstElement, const float secondElement)
540540
{
541-
return fast_fsqrtf(sq(firstElement) + sq(secondElement));
541+
return fast_fsqrtf(sq(firstElement) + sq(secondElement));
542542
}
543543

544544
// function to calculate the normalization (pythagoras) of a 3-dimensional vector
545545
float NOINLINE calc_length_pythagorean_3D(const float firstElement, const float secondElement, const float thirdElement)
546546
{
547-
return fast_fsqrtf(sq(firstElement) + sq(secondElement) + sq(thirdElement));
548-
}
547+
return fast_fsqrtf(sq(firstElement) + sq(secondElement) + sq(thirdElement));
548+
}
549+
550+
#ifdef SITL_BUILD
551+
552+
/**
553+
* @brief Floating-point vector subtraction, equivalent of CMSIS arm_sub_f32.
554+
*/
555+
void arm_sub_f32(
556+
float * pSrcA,
557+
float * pSrcB,
558+
float * pDst,
559+
uint32_t blockSize)
560+
{
561+
for (uint32_t i = 0; i < blockSize; i++) {
562+
pDst[i] = pSrcA[i] - pSrcB[i];
563+
}
564+
}
565+
566+
/**
567+
* @brief Floating-point vector scaling, equivalent of CMSIS arm_scale_f32.
568+
*/
569+
void arm_scale_f32(
570+
float * pSrc,
571+
float scale,
572+
float * pDst,
573+
uint32_t blockSize)
574+
{
575+
for (uint32_t i = 0; i < blockSize; i++) {
576+
pDst[i] = pSrc[i] * scale;
577+
}
578+
}
579+
580+
/**
581+
* @brief Floating-point vector multiplication, equivalent of CMSIS arm_mult_f32.
582+
*/
583+
void arm_mult_f32(
584+
float * pSrcA,
585+
float * pSrcB,
586+
float * pDst,
587+
uint32_t blockSize)
588+
{
589+
for (uint32_t i = 0; i < blockSize; i++) {
590+
pDst[i] = pSrcA[i] * pSrcB[i];
591+
}
592+
}
593+
594+
#endif

src/main/common/maths.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,10 @@ float calc_length_pythagorean_3D(const float firstElement, const float secondEle
201201
* The most significat byte is placed at the highest address
202202
* in other words, the most significant byte is "last", on odd indexes
203203
*/
204-
#define int16_val_little_endian(v, idx) ((int16_t)(((uint8_t)v[2 * idx + 1] << 8) | v[2 * idx]))
204+
#define int16_val_little_endian(v, idx) ((int16_t)(((uint8_t)v[2 * idx + 1] << 8) | v[2 * idx]))
205+
206+
#ifdef SITL_BUILD
207+
void arm_sub_f32(float * pSrcA, float * pSrcB, float * pDst, uint32_t blockSize);
208+
void arm_scale_f32(float * pSrc, float scale, float * pDst, uint32_t blockSize);
209+
void arm_mult_f32(float * pSrcA, float * pSrcB, float * pDst, uint32_t blockSize);
210+
#endif

src/main/drivers/accgyro/accgyro_icm42605.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ static bool icm42605GyroRead(gyroDev_t *gyro)
232232
}
233233

234234
gyro->gyroADCRaw[X] = (float) int16_val_big_endian(data, 0);
235-
gyro->gyroADCRaw[Y] = (float) int16_val_big_endian(data, 0);
236-
gyro->gyroADCRaw[Z] = (float) int16_val_big_endian(data, 0);
235+
gyro->gyroADCRaw[Y] = (float) int16_val_big_endian(data, 1);
236+
gyro->gyroADCRaw[Z] = (float) int16_val_big_endian(data, 2);
237237

238238
return true;
239239
}

src/main/sensors/acceleration.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ static EXTENDED_FASTRAM pt1Filter_t accVibeFilter[XYZ_AXIS_COUNT];
8282
static EXTENDED_FASTRAM filterApplyFnPtr accNotchFilterApplyFn;
8383
static EXTENDED_FASTRAM void *accNotchFilter[XYZ_AXIS_COUNT];
8484

85+
static EXTENDED_FASTRAM float fAccZero[XYZ_AXIS_COUNT];
86+
static EXTENDED_FASTRAM float fAccGain[XYZ_AXIS_COUNT];
87+
8588
PG_REGISTER_WITH_RESET_FN(accelerometerConfig_t, accelerometerConfig, PG_ACCELEROMETER_CONFIG, 5);
8689

8790
void pgResetFn_accelerometerConfig(accelerometerConfig_t *instance)
@@ -105,6 +108,17 @@ void pgResetFn_accelerometerConfig(accelerometerConfig_t *instance)
105108
);
106109
}
107110

111+
static void updateAccCoefficients(void) {
112+
113+
for (uint8_t i = 0; i < XYZ_AXIS_COUNT; i++) {
114+
//Float zero
115+
fAccZero[i] = (float)accelerometerConfig()->accZero.raw[i];
116+
//Float gain
117+
fAccGain[i] = (float)accelerometerConfig()->accGain.raw[i] / 4096.0f;
118+
}
119+
120+
}
121+
108122
static bool accDetect(accDev_t *dev, accelerationSensor_e accHardwareToUse)
109123
{
110124
accelerationSensor_e accHardware = ACC_NONE;
@@ -280,6 +294,7 @@ bool accInit(uint32_t targetLooptime)
280294
acc.accTargetLooptime = targetLooptime;
281295
acc.accClipCount = 0;
282296
accInitFilters();
297+
updateAccCoefficients();
283298

284299
for (int axis = 0; axis < XYZ_AXIS_COUNT; axis++) {
285300
acc.extremes[axis].min = 100;
@@ -477,25 +492,28 @@ static void performAcclerationCalibration(void)
477492
// saveConfigAndNotify will trigger eepromREAD and in turn call back the accelerometer gain validation
478493
// that will set ENABLE_STATE(ACCELEROMETER_CALIBRATED) if all is good
479494
saveConfigAndNotify();
495+
//Recompute all coeffs
496+
updateAccCoefficients();
480497
}
481498
}
482499
}
483500

484-
static void applyAccelerationZero(const flightDynamicsTrims_t * accZero, const flightDynamicsTrims_t * accGain)
501+
static void applyAccelerationZero(void)
485502
{
486-
accADC[X] = (accADC[X] - accZero->raw[X]) * accGain->raw[X] / 4096;
487-
accADC[Y] = (accADC[Y] - accZero->raw[Y]) * accGain->raw[Y] / 4096;
488-
accADC[Z] = (accADC[Z] - accZero->raw[Z]) * accGain->raw[Z] / 4096;
503+
float tmp[XYZ_AXIS_COUNT];
504+
505+
//Apply zero
506+
arm_sub_f32(accADC, fAccZero, tmp, XYZ_AXIS_COUNT);
507+
//Apply gain
508+
arm_mult_f32(tmp, fAccGain, accADC, XYZ_AXIS_COUNT);
489509
}
490510

491511
/*
492512
* Calculate measured acceleration in body frame in m/s^2
493513
*/
494514
void accGetMeasuredAcceleration(fpVector3_t *measuredAcc)
495515
{
496-
for (int axis = 0; axis < XYZ_AXIS_COUNT; axis++) {
497-
measuredAcc->v[axis] = acc.accADCf[axis] * GRAVITY_CMSS;
498-
}
516+
arm_scale_f32(acc.accADCf, GRAVITY_CMSS, measuredAcc->v, XYZ_AXIS_COUNT);
499517
}
500518

501519
/*
@@ -530,7 +548,7 @@ void accUpdate(void)
530548

531549
if (!ARMING_FLAG(SIMULATOR_MODE_SITL)) {
532550
performAcclerationCalibration();
533-
applyAccelerationZero(&accelerometerConfig()->accZero, &accelerometerConfig()->accGain);
551+
applyAccelerationZero();
534552
}
535553

536554
applySensorAlignment(accADC, accADC, acc.dev.accAlign);

src/main/sensors/gyro.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -411,19 +411,15 @@ static bool FAST_CODE NOINLINE gyroUpdateAndCalibrate(gyroDev_t * gyroDev, zeroC
411411
if (zeroCalibrationIsCompleteV(gyroCal)) {
412412
float gyroADCtmp[XYZ_AXIS_COUNT];
413413

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

419417
// Apply sensor alignment
420418
applySensorAlignment(gyroADCtmp, gyroADCtmp, gyroDev->gyroAlign);
421419
applyBoardAlignment(gyroADCtmp);
422420

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

428424
return true;
429425
} else {

src/main/sensors/gyro.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
#include "drivers/sensor.h"
2626
#include "flight/dynamic_gyro_notch.h"
2727
#include "flight/secondary_dynamic_gyro_notch.h"
28+
#if !defined(SITL_BUILD)
29+
#include "arm_math.h"
30+
#else
31+
#include <math.h>
32+
#endif
2833

2934
typedef enum {
3035
GYRO_NONE = 0,

src/test/unit/target.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#pragma once
1919

20+
#define SITL_BUILD
2021
#define USE_MAG
2122
#define USE_BARO
2223
#define USE_GPS

0 commit comments

Comments
 (0)