mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-16 21:05:35 +03:00
Update RC Controls so that functions with different modes can be
applied. The existing modes are 'step'. A new mode is 'select'. The other unit tests need fixing up so that appropriate code is compiled as C and not C++ code.
This commit is contained in:
parent
e22b37026c
commit
cac814923c
4 changed files with 105 additions and 55 deletions
|
@ -273,78 +273,85 @@ uint8_t adjustmentStateMask = 0;
|
|||
static const adjustmentConfig_t defaultAdjustmentConfigs[ADJUSTMENT_FUNCTION_COUNT - 1] = {
|
||||
{
|
||||
.adjustmentFunction = ADJUSTMENT_RC_RATE,
|
||||
.step = 1
|
||||
.mode = ADJUSTMENT_MODE_STEP,
|
||||
.data.stepConfig.step = 1
|
||||
},
|
||||
{
|
||||
.adjustmentFunction = ADJUSTMENT_RC_EXPO,
|
||||
.step = 1
|
||||
.mode = ADJUSTMENT_MODE_STEP,
|
||||
.data.stepConfig.step = 1
|
||||
},
|
||||
{
|
||||
.adjustmentFunction = ADJUSTMENT_THROTTLE_EXPO,
|
||||
.step = 1
|
||||
.mode = ADJUSTMENT_MODE_STEP,
|
||||
.data.stepConfig.step = 1
|
||||
},
|
||||
{
|
||||
.adjustmentFunction = ADJUSTMENT_PITCH_ROLL_RATE,
|
||||
.step = 1
|
||||
.mode = ADJUSTMENT_MODE_STEP,
|
||||
.data.stepConfig.step = 1
|
||||
},
|
||||
{
|
||||
.adjustmentFunction = ADJUSTMENT_YAW_RATE,
|
||||
.step = 1
|
||||
.mode = ADJUSTMENT_MODE_STEP,
|
||||
.data.stepConfig.step = 1
|
||||
},
|
||||
{
|
||||
.adjustmentFunction = ADJUSTMENT_PITCH_ROLL_P,
|
||||
.step = 1
|
||||
.mode = ADJUSTMENT_MODE_STEP,
|
||||
.data.stepConfig.step = 1
|
||||
},
|
||||
{
|
||||
.adjustmentFunction = ADJUSTMENT_PITCH_ROLL_I,
|
||||
.step = 1
|
||||
.mode = ADJUSTMENT_MODE_STEP,
|
||||
.data.stepConfig.step = 1
|
||||
},
|
||||
{
|
||||
.adjustmentFunction = ADJUSTMENT_PITCH_ROLL_D,
|
||||
.step = 1
|
||||
.mode = ADJUSTMENT_MODE_STEP,
|
||||
.data.stepConfig.step = 1
|
||||
},
|
||||
{
|
||||
.adjustmentFunction = ADJUSTMENT_YAW_P,
|
||||
.step = 1
|
||||
.mode = ADJUSTMENT_MODE_STEP,
|
||||
.data.stepConfig.step = 1
|
||||
},
|
||||
{
|
||||
.adjustmentFunction = ADJUSTMENT_YAW_I,
|
||||
.step = 1
|
||||
.mode = ADJUSTMENT_MODE_STEP,
|
||||
.data.stepConfig.step = 1
|
||||
},
|
||||
{
|
||||
.adjustmentFunction = ADJUSTMENT_YAW_D,
|
||||
.step = 1
|
||||
.mode = ADJUSTMENT_MODE_STEP,
|
||||
.data.stepConfig.step = 1
|
||||
},
|
||||
{
|
||||
.adjustmentFunction = ADJUSTMENT_RATE_PROFILE,
|
||||
.mode = ADJUSTMENT_MODE_SELECT,
|
||||
.data.selectConfig.switchPositions = 3
|
||||
}
|
||||
};
|
||||
|
||||
#define ADJUSTMENT_FUNCTION_CONFIG_INDEX_OFFSET 1
|
||||
|
||||
|
||||
typedef struct adjustmentState_s {
|
||||
uint8_t auxChannelIndex;
|
||||
uint8_t adjustmentFunction;
|
||||
uint8_t step;
|
||||
uint32_t timeoutAt;
|
||||
} adjustmentState_t;
|
||||
|
||||
static adjustmentState_t adjustmentStates[MAX_SIMULTANEOUS_ADJUSTMENT_COUNT];
|
||||
adjustmentState_t adjustmentStates[MAX_SIMULTANEOUS_ADJUSTMENT_COUNT];
|
||||
|
||||
void configureAdjustment(uint8_t index, uint8_t auxSwitchChannelIndex, const adjustmentConfig_t *adjustmentConfig) {
|
||||
adjustmentState_t *adjustmentState = &adjustmentStates[index];
|
||||
|
||||
if (adjustmentState->adjustmentFunction == adjustmentConfig->adjustmentFunction) {
|
||||
if (adjustmentState->config == adjustmentConfig) {
|
||||
// already configured
|
||||
return;
|
||||
}
|
||||
adjustmentState->auxChannelIndex = auxSwitchChannelIndex;
|
||||
adjustmentState->adjustmentFunction = adjustmentConfig->adjustmentFunction;
|
||||
adjustmentState->step = adjustmentConfig->step;
|
||||
adjustmentState->config = adjustmentConfig;
|
||||
adjustmentState->timeoutAt = 0;
|
||||
|
||||
MARK_ADJUSTMENT_FUNCTION_AS_READY(index);
|
||||
}
|
||||
|
||||
void applyAdjustment(controlRateConfig_t *controlRateConfig, uint8_t adjustmentFunction, int delta) {
|
||||
void applyStepAdjustment(controlRateConfig_t *controlRateConfig, uint8_t adjustmentFunction, int delta) {
|
||||
int newValue;
|
||||
|
||||
if (delta > 0) {
|
||||
|
@ -421,7 +428,10 @@ void processRcAdjustments(controlRateConfig_t *controlRateConfig, rxConfig_t *rx
|
|||
for (adjustmentIndex = 0; adjustmentIndex < MAX_SIMULTANEOUS_ADJUSTMENT_COUNT; adjustmentIndex++) {
|
||||
adjustmentState_t *adjustmentState = &adjustmentStates[adjustmentIndex];
|
||||
|
||||
uint8_t adjustmentFunction = adjustmentState->adjustmentFunction;
|
||||
if (!adjustmentState->config) {
|
||||
continue;
|
||||
}
|
||||
uint8_t adjustmentFunction = adjustmentState->config->adjustmentFunction;
|
||||
if (adjustmentFunction == ADJUSTMENT_NONE) {
|
||||
continue;
|
||||
}
|
||||
|
@ -437,24 +447,25 @@ void processRcAdjustments(controlRateConfig_t *controlRateConfig, rxConfig_t *rx
|
|||
|
||||
uint8_t channelIndex = NON_AUX_CHANNEL_COUNT + adjustmentState->auxChannelIndex;
|
||||
|
||||
if (adjustmentState->config->mode == ADJUSTMENT_MODE_STEP) {
|
||||
int delta;
|
||||
if (rcData[channelIndex] > rxConfig->midrc + 200) {
|
||||
delta = adjustmentState->step;
|
||||
delta = adjustmentState->config->data.stepConfig.step;
|
||||
} else if (rcData[channelIndex] < rxConfig->midrc - 200) {
|
||||
delta = 0 - adjustmentState->step;
|
||||
delta = 0 - adjustmentState->config->data.stepConfig.step;
|
||||
} else {
|
||||
// returning the switch to the middle immediately resets the ready state
|
||||
MARK_ADJUSTMENT_FUNCTION_AS_READY(adjustmentIndex);
|
||||
adjustmentState->timeoutAt = now + RESET_FREQUENCY_2HZ;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (IS_ADJUSTMENT_FUNCTION_BUSY(adjustmentIndex)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
MARK_ADJUSTMENT_FUNCTION_AS_BUSY(adjustmentIndex);
|
||||
applyAdjustment(controlRateConfig, adjustmentFunction, delta);
|
||||
applyStepAdjustment(controlRateConfig, adjustmentFunction, delta);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -146,13 +146,33 @@ typedef enum {
|
|||
ADJUSTMENT_YAW_P,
|
||||
ADJUSTMENT_YAW_I,
|
||||
ADJUSTMENT_YAW_D,
|
||||
ADJUSTMENT_RATE_PROFILE
|
||||
} adjustmentFunction_e;
|
||||
|
||||
#define ADJUSTMENT_FUNCTION_COUNT 12
|
||||
#define ADJUSTMENT_FUNCTION_COUNT 13
|
||||
|
||||
typedef enum {
|
||||
ADJUSTMENT_MODE_STEP,
|
||||
ADJUSTMENT_MODE_SELECT
|
||||
} adjustmentMode_e;
|
||||
|
||||
typedef struct adjustmentStepConfig_s {
|
||||
uint8_t step;
|
||||
} adjustmentStepConfig_t;
|
||||
|
||||
typedef struct adjustmentSelectConfig_s {
|
||||
uint8_t switchPositions;
|
||||
} adjustmentSelectConfig_t;
|
||||
|
||||
typedef union adjustmentConfig_u {
|
||||
adjustmentStepConfig_t stepConfig;
|
||||
adjustmentSelectConfig_t selectConfig;
|
||||
} adjustmentData_t;
|
||||
|
||||
typedef struct adjustmentConfig_s {
|
||||
uint8_t adjustmentFunction;
|
||||
uint8_t step;
|
||||
uint8_t mode;
|
||||
adjustmentData_t data;
|
||||
} adjustmentConfig_t;
|
||||
|
||||
typedef struct adjustmentRange_s {
|
||||
|
@ -170,7 +190,14 @@ typedef struct adjustmentRange_s {
|
|||
|
||||
#define ADJUSTMENT_INDEX_OFFSET 1
|
||||
|
||||
typedef struct adjustmentState_s {
|
||||
uint8_t auxChannelIndex;
|
||||
const adjustmentConfig_t *config;
|
||||
uint32_t timeoutAt;
|
||||
} adjustmentState_t;
|
||||
|
||||
#define MAX_SIMULTANEOUS_ADJUSTMENT_COUNT 4 // enough for 4 x 3position switches / 4 aux channel
|
||||
|
||||
#define MAX_ADJUSTMENT_RANGE_COUNT 12 // enough for 2 * 6pos switches.
|
||||
|
||||
void configureAdjustment(uint8_t index, uint8_t auxChannelIndex, const adjustmentConfig_t *adjustmentConfig);
|
||||
|
|
|
@ -140,7 +140,7 @@ $(OBJECT_DIR)/telemetry/hott.o : $(USER_DIR)/telemetry/hott.c $(USER_DIR)/teleme
|
|||
$(OBJECT_DIR)/telemetry_hott_unittest.o : $(TEST_DIR)/telemetry_hott_unittest.cc \
|
||||
$(USER_DIR)/telemetry/hott.h $(GTEST_HEADERS)
|
||||
@mkdir -p $(dir $@)
|
||||
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(TEST_CFLAGS) -c $(TEST_DIR)/telemetry_hott_unittest.cc -o $@
|
||||
$(CC) $(CPPFLAGS) $(CXXFLAGS) $(TEST_CFLAGS) -c $(TEST_DIR)/telemetry_hott_unittest.cc -o $@
|
||||
|
||||
telemetry_hott_unittest :$(OBJECT_DIR)/telemetry/hott.o $(OBJECT_DIR)/telemetry_hott_unittest.o $(OBJECT_DIR)/flight/gps_conversion.o $(OBJECT_DIR)/gtest_main.a
|
||||
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $(OBJECT_DIR)/$@
|
||||
|
@ -149,7 +149,7 @@ telemetry_hott_unittest :$(OBJECT_DIR)/telemetry/hott.o $(OBJECT_DIR)/telemetry_
|
|||
|
||||
$(OBJECT_DIR)/io/rc_controls.o : $(USER_DIR)/io/rc_controls.c $(USER_DIR)/io/rc_controls.h $(GTEST_HEADERS)
|
||||
@mkdir -p $(dir $@)
|
||||
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(TEST_CFLAGS) -c $(USER_DIR)/io/rc_controls.c -o $@
|
||||
$(CC) $(CPPFLAGS) $(CXXFLAGS) $(TEST_CFLAGS) -c $(USER_DIR)/io/rc_controls.c -o $@
|
||||
|
||||
$(OBJECT_DIR)/rc_controls_unittest.o : $(TEST_DIR)/rc_controls_unittest.cc \
|
||||
$(USER_DIR)/io/rc_controls.h $(GTEST_HEADERS)
|
||||
|
@ -167,7 +167,7 @@ $(OBJECT_DIR)/io/ledstrip.o : $(USER_DIR)/io/ledstrip.c $(USER_DIR)/io/ledstrip.
|
|||
$(OBJECT_DIR)/ledstrip_unittest.o : $(TEST_DIR)/ledstrip_unittest.cc \
|
||||
$(USER_DIR)/io/ledstrip.h $(GTEST_HEADERS)
|
||||
@mkdir -p $(dir $@)
|
||||
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(TEST_CFLAGS) -c $(TEST_DIR)/ledstrip_unittest.cc -o $@
|
||||
$(CC) $(CPPFLAGS) $(CXXFLAGS) $(TEST_CFLAGS) -c $(TEST_DIR)/ledstrip_unittest.cc -o $@
|
||||
|
||||
ledstrip_unittest :$(OBJECT_DIR)/io/ledstrip.o $(OBJECT_DIR)/ledstrip_unittest.o $(OBJECT_DIR)/gtest_main.a
|
||||
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $(OBJECT_DIR)/$@
|
||||
|
@ -176,7 +176,7 @@ ledstrip_unittest :$(OBJECT_DIR)/io/ledstrip.o $(OBJECT_DIR)/ledstrip_unittest.o
|
|||
|
||||
$(OBJECT_DIR)/drivers/light_ws2811strip.o : $(USER_DIR)/drivers/light_ws2811strip.c $(USER_DIR)/drivers/light_ws2811strip.h $(GTEST_HEADERS)
|
||||
@mkdir -p $(dir $@)
|
||||
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(TEST_CFLAGS) -c $(USER_DIR)/drivers/light_ws2811strip.c -o $@
|
||||
$(CC) $(CPPFLAGS) $(CXXFLAGS) $(TEST_CFLAGS) -c $(USER_DIR)/drivers/light_ws2811strip.c -o $@
|
||||
|
||||
$(OBJECT_DIR)/ws2811_unittest.o : $(TEST_DIR)/ws2811_unittest.cc \
|
||||
$(USER_DIR)/drivers/light_ws2811strip.h $(GTEST_HEADERS)
|
||||
|
|
|
@ -17,17 +17,18 @@
|
|||
#include <stdint.h>
|
||||
|
||||
#include <limits.h>
|
||||
extern "C" {
|
||||
#include "common/axis.h"
|
||||
#include "flight/flight.h"
|
||||
|
||||
#include "common/axis.h"
|
||||
#include "flight/flight.h"
|
||||
|
||||
#include "rx/rx.h"
|
||||
#include "io/escservo.h"
|
||||
#include "io/rc_controls.h"
|
||||
|
||||
#include "rx/rx.h"
|
||||
#include "io/escservo.h"
|
||||
#include "io/rc_controls.h"
|
||||
}
|
||||
#include "unittest_macros.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
extern "C" {
|
||||
int constrain(int amt, int low, int high)
|
||||
{
|
||||
if (amt < low)
|
||||
|
@ -37,6 +38,7 @@ int constrain(int amt, int low, int high)
|
|||
else
|
||||
return amt;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(RcControlsTest, updateActivatedModesWithAllInputsAtMidde)
|
||||
{
|
||||
|
@ -168,6 +170,7 @@ static int callCounts[CALL_COUNT_ITEM_COUNT];
|
|||
|
||||
#define CALL_COUNTER(item) (callCounts[item])
|
||||
|
||||
extern "C" {
|
||||
void generatePitchRollCurve(controlRateConfig_t *) {
|
||||
callCounts[COUNTER_GENERATE_PITCH_ROLL_CURVE]++;
|
||||
}
|
||||
|
@ -175,15 +178,19 @@ void generatePitchRollCurve(controlRateConfig_t *) {
|
|||
void queueConfirmationBeep(uint8_t) {
|
||||
callCounts[COUNTER_QUEUE_CONFIRMATION_BEEP]++;
|
||||
}
|
||||
}
|
||||
|
||||
void resetCallCounters(void) {
|
||||
memset(&callCounts, 0, sizeof(callCounts));
|
||||
}
|
||||
|
||||
uint32_t fixedMillis = 0;
|
||||
|
||||
extern "C" {
|
||||
uint32_t millis(void) {
|
||||
return fixedMillis;
|
||||
}
|
||||
}
|
||||
|
||||
#define DEFAULT_MIN_CHECK 1100
|
||||
#define DEFAULT_MAX_CHECK 1900
|
||||
|
@ -191,10 +198,12 @@ uint32_t millis(void) {
|
|||
rxConfig_t rxConfig;
|
||||
|
||||
extern uint8_t adjustmentStateMask;
|
||||
extern adjustmentState_t adjustmentStates[MAX_SIMULTANEOUS_ADJUSTMENT_COUNT];
|
||||
|
||||
static const adjustmentConfig_t rateAdjustmentConfig = {
|
||||
.adjustmentFunction = ADJUSTMENT_RC_RATE,
|
||||
.step = 1
|
||||
.mode = ADJUSTMENT_MODE_STEP,
|
||||
.data = { 1 }
|
||||
};
|
||||
|
||||
TEST(RcControlsTest, processRcAdjustmentsSticksInMiddle)
|
||||
|
@ -215,6 +224,7 @@ TEST(RcControlsTest, processRcAdjustmentsSticksInMiddle)
|
|||
rxConfig.maxcheck = DEFAULT_MAX_CHECK;
|
||||
rxConfig.midrc = 1500;
|
||||
|
||||
memset(&adjustmentStates, 0, sizeof(adjustmentStates));
|
||||
configureAdjustment(0, AUX3 - NON_AUX_CHANNEL_COUNT, &rateAdjustmentConfig);
|
||||
|
||||
// and
|
||||
|
@ -393,6 +403,7 @@ TEST(RcControlsTest, processRcAdjustmentsWithRcRateFunctionSwitchUp)
|
|||
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void saveConfigAndNotify(void) {}
|
||||
void generateThrottleCurve(controlRateConfig_t *, escAndServoConfig_t *) {}
|
||||
void changeProfile(uint8_t) {}
|
||||
|
@ -410,3 +421,4 @@ int16_t heading;
|
|||
uint8_t stateFlags = 0;
|
||||
int16_t rcData[MAX_SUPPORTED_RC_CHANNEL_COUNT];
|
||||
rxRuntimeConfig_t rxRuntimeConfig;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue