mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-23 08:15:30 +03:00
Merge branch 'master' into serial-cleanup
Conflicts: src/main/blackbox/blackbox_io.c src/main/config/config.c
This commit is contained in:
commit
b6509dd1eb
60 changed files with 2471 additions and 386 deletions
|
@ -52,7 +52,9 @@ TESTS = \
|
|||
telemetry_hott_unittest \
|
||||
rc_controls_unittest \
|
||||
ledstrip_unittest \
|
||||
ws2811_unittest
|
||||
ws2811_unittest \
|
||||
encoding_unittest \
|
||||
lowpass_unittest
|
||||
|
||||
# All Google Test headers. Usually you shouldn't change this
|
||||
# definition.
|
||||
|
@ -129,6 +131,25 @@ battery_unittest : \
|
|||
$(OBJECT_DIR)/gtest_main.a
|
||||
|
||||
$(CXX) $(CXX_FLAGS) -lpthread $^ -o $(OBJECT_DIR)/$@
|
||||
|
||||
$(OBJECT_DIR)/common/encoding.o : $(USER_DIR)/common/encoding.c $(USER_DIR)/common/encoding.h $(GTEST_HEADERS)
|
||||
@mkdir -p $(dir $@)
|
||||
$(CC) $(C_FLAGS) $(TEST_CFLAGS) -c $(USER_DIR)/common/encoding.c -o $@
|
||||
|
||||
$(OBJECT_DIR)/encoding_unittest.o : \
|
||||
$(TEST_DIR)/encoding_unittest.cc \
|
||||
$(USER_DIR)/common/encoding.h \
|
||||
$(GTEST_HEADERS)
|
||||
|
||||
@mkdir -p $(dir $@)
|
||||
$(CXX) $(CXX_FLAGS) $(TEST_CFLAGS) -c $(TEST_DIR)/encoding_unittest.cc -o $@
|
||||
|
||||
encoding_unittest : \
|
||||
$(OBJECT_DIR)/common/encoding.o \
|
||||
$(OBJECT_DIR)/encoding_unittest.o \
|
||||
$(OBJECT_DIR)/gtest_main.a
|
||||
|
||||
$(CXX) $(CXX_FLAGS) -lpthread $^ -o $(OBJECT_DIR)/$@
|
||||
|
||||
$(OBJECT_DIR)/flight/imu.o : \
|
||||
$(USER_DIR)/flight/imu.c \
|
||||
|
@ -319,6 +340,31 @@ ws2811_unittest : \
|
|||
|
||||
$(CXX) $(CXX_FLAGS) -lpthread $^ -o $(OBJECT_DIR)/$@
|
||||
|
||||
|
||||
$(OBJECT_DIR)/flight/lowpass.o : \
|
||||
$(USER_DIR)/flight/lowpass.c \
|
||||
$(USER_DIR)/flight/lowpass.h \
|
||||
$(GTEST_HEADERS)
|
||||
|
||||
@mkdir -p $(dir $@)
|
||||
$(CC) $(C_FLAGS) $(TEST_CFLAGS) -c $(USER_DIR)/flight/lowpass.c -o $@
|
||||
|
||||
$(OBJECT_DIR)/lowpass_unittest.o : \
|
||||
$(TEST_DIR)/lowpass_unittest.cc \
|
||||
$(USER_DIR)/flight/lowpass.h \
|
||||
$(GTEST_HEADERS)
|
||||
|
||||
@mkdir -p $(dir $@)
|
||||
$(CXX) $(CXX_FLAGS) $(TEST_CFLAGS) -c $(TEST_DIR)/lowpass_unittest.cc -o $@
|
||||
|
||||
lowpass_unittest : \
|
||||
$(OBJECT_DIR)/flight/lowpass.o \
|
||||
$(OBJECT_DIR)/lowpass_unittest.o \
|
||||
$(OBJECT_DIR)/gtest_main.a
|
||||
|
||||
$(CXX) $(CXX_FLAGS) -lpthread $^ -o $(OBJECT_DIR)/$@
|
||||
|
||||
|
||||
test: $(TESTS)
|
||||
set -e && for test in $(TESTS) ; do \
|
||||
$(OBJECT_DIR)/$$test; \
|
||||
|
|
84
src/test/unit/encoding_unittest.cc
Normal file
84
src/test/unit/encoding_unittest.cc
Normal file
|
@ -0,0 +1,84 @@
|
|||
/*
|
||||
* This file is part of Cleanflight.
|
||||
*
|
||||
* Cleanflight is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Cleanflight is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stdint.h>
|
||||
|
||||
extern "C" {
|
||||
#include "common/encoding.h"
|
||||
}
|
||||
|
||||
#include "unittest_macros.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
typedef struct zigzagEncodingExpectation_t {
|
||||
int32_t input;
|
||||
uint32_t expected;
|
||||
} zigzagEncodingExpectation_t;
|
||||
|
||||
typedef struct floatToIntEncodingExpectation_t {
|
||||
float input;
|
||||
uint32_t expected;
|
||||
} floatToIntEncodingExpectation_t;
|
||||
|
||||
TEST(EncodingTest, ZigzagEncodingTest)
|
||||
{
|
||||
// given
|
||||
zigzagEncodingExpectation_t expectations[] = {
|
||||
{ 0, 0},
|
||||
{-1, 1},
|
||||
{ 1, 2},
|
||||
{-2, 3},
|
||||
{ 2, 4},
|
||||
|
||||
{ 2147483646, 4294967292},
|
||||
{-2147483647, 4294967293},
|
||||
{ 2147483647, 4294967294},
|
||||
{-2147483648, 4294967295},
|
||||
};
|
||||
int expectationCount = sizeof(expectations) / sizeof(expectations[0]);
|
||||
|
||||
// expect
|
||||
|
||||
for (int i = 0; i < expectationCount; i++) {
|
||||
zigzagEncodingExpectation_t *expectation = &expectations[i];
|
||||
|
||||
EXPECT_EQ(expectation->expected, zigzagEncode(expectation->input));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(EncodingTest, FloatToIntEncodingTest)
|
||||
{
|
||||
// given
|
||||
floatToIntEncodingExpectation_t expectations[] = {
|
||||
{0.0, 0x00000000},
|
||||
{2.0, 0x40000000}, // Exponent should be in the top bits
|
||||
{4.5, 0x40900000}
|
||||
};
|
||||
int expectationCount = sizeof(expectations) / sizeof(expectations[0]);
|
||||
|
||||
// expect
|
||||
|
||||
for (int i = 0; i < expectationCount; i++) {
|
||||
floatToIntEncodingExpectation_t *expectation = &expectations[i];
|
||||
|
||||
EXPECT_EQ(expectation->expected, castFloatBytesToInt(expectation->input));
|
||||
}
|
||||
}
|
||||
|
||||
// STUBS
|
||||
|
||||
extern "C" {
|
||||
}
|
128
src/test/unit/lowpass_unittest.cc
Normal file
128
src/test/unit/lowpass_unittest.cc
Normal file
|
@ -0,0 +1,128 @@
|
|||
/*
|
||||
* This file is part of Cleanflight.
|
||||
*
|
||||
* Cleanflight is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Cleanflight is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stdint.h>
|
||||
#include <limits.h>
|
||||
|
||||
extern "C" {
|
||||
#include "flight/lowpass.h"
|
||||
}
|
||||
|
||||
static lowpass_t lowpassFilterReference;
|
||||
static lowpass_t lowpassFilter;
|
||||
|
||||
#include "unittest_macros.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
static float lowpassRef(lowpass_t *filter, float in, int16_t freq)
|
||||
{
|
||||
int16_t coefIdx;
|
||||
float out;
|
||||
|
||||
// Check to see if cutoff frequency changed
|
||||
if (freq != filter->freq) {
|
||||
filter->init = false;
|
||||
}
|
||||
|
||||
// Initialize if needed
|
||||
if (!filter->init) {
|
||||
generateLowpassCoeffs2(freq, filter);
|
||||
for (coefIdx = 0; coefIdx < LOWPASS_NUM_COEF; coefIdx++) {
|
||||
filter->xf[coefIdx] = in;
|
||||
filter->yf[coefIdx] = in;
|
||||
}
|
||||
filter->init = true;
|
||||
}
|
||||
|
||||
// Delays
|
||||
for (coefIdx = LOWPASS_NUM_COEF-1; coefIdx > 0; coefIdx--) {
|
||||
filter->xf[coefIdx] = filter->xf[coefIdx-1];
|
||||
filter->yf[coefIdx] = filter->yf[coefIdx-1];
|
||||
}
|
||||
filter->xf[0] = in;
|
||||
|
||||
// Accumulate result
|
||||
out = filter->xf[0] * filter->bf[0];
|
||||
for (coefIdx = 1; coefIdx < LOWPASS_NUM_COEF; coefIdx++) {
|
||||
out += filter->xf[coefIdx] * filter->bf[coefIdx];
|
||||
out -= filter->yf[coefIdx] * filter->af[coefIdx];
|
||||
}
|
||||
filter->yf[0] = out;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
TEST(LowpassTest, Lowpass)
|
||||
{
|
||||
int16_t servoCmds[3000];
|
||||
int16_t expectedOut[3000];
|
||||
int16_t referenceOut;
|
||||
int16_t filterOut;
|
||||
uint16_t sampleIdx;
|
||||
int16_t freq;
|
||||
|
||||
uint16_t sampleCount = sizeof(servoCmds) / sizeof(int16_t);
|
||||
|
||||
// generate inputs and expecteds
|
||||
for (sampleIdx = 0; sampleIdx < sampleCount; sampleIdx++) {
|
||||
if (sampleIdx < 1000) {
|
||||
servoCmds[sampleIdx] = 500;
|
||||
} else if (sampleIdx >= 1000 && sampleIdx < 2000) {
|
||||
servoCmds[sampleIdx] = 2500;
|
||||
} else {
|
||||
servoCmds[sampleIdx] = 1500;
|
||||
}
|
||||
|
||||
if ((sampleIdx >= 900 && sampleIdx < 1000) ||
|
||||
(sampleIdx >= 1900 && sampleIdx < 2000)||
|
||||
(sampleIdx >= 2900 && sampleIdx < 3000)) {
|
||||
expectedOut[sampleIdx] = servoCmds[sampleIdx];
|
||||
} else {
|
||||
expectedOut[sampleIdx] = -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Test all frequencies
|
||||
for (freq = 10; freq <= 400; freq++) {
|
||||
printf("*** Testing freq: %d (%f)\n", freq, ((float)freq * 0.001f));
|
||||
|
||||
// Run tests
|
||||
for (sampleIdx = 0; sampleIdx < sampleCount; sampleIdx++)
|
||||
{
|
||||
// Filter under test
|
||||
filterOut = (int16_t)lowpassFixed(&lowpassFilter, servoCmds[sampleIdx], freq);
|
||||
|
||||
// Floating-point reference
|
||||
referenceOut = (int16_t)(lowpassRef(&lowpassFilterReference, (float)servoCmds[sampleIdx], freq) + 0.5f);
|
||||
|
||||
if (expectedOut[sampleIdx] >= 0) {
|
||||
EXPECT_EQ(filterOut, expectedOut[sampleIdx]);
|
||||
}
|
||||
// Some tolerance
|
||||
// TODO adjust precision to remove the need for this?
|
||||
EXPECT_LE(filterOut, referenceOut + 1);
|
||||
EXPECT_GE(filterOut, referenceOut - 1);
|
||||
} // for each sample
|
||||
} // for each freq
|
||||
}
|
||||
|
||||
// STUBS
|
||||
|
||||
extern "C" {
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -22,6 +22,7 @@
|
|||
#define GPS
|
||||
#define TELEMETRY
|
||||
#define LED_STRIP
|
||||
#define USE_SERVOS
|
||||
|
||||
#define SERIAL_PORT_COUNT 4
|
||||
|
||||
|
|
|
@ -705,6 +705,9 @@ void mwArm(void) {}
|
|||
void feature(uint32_t) {}
|
||||
void sensors(uint32_t) {}
|
||||
void mwDisarm(void) {}
|
||||
void displayDisablePageCycling() {}
|
||||
void displayEnablePageCycling() {}
|
||||
|
||||
uint8_t getCurrentControlRateProfile(void) {
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue