Skip to content

Commit

Permalink
blackbox config refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rtlopez committed Oct 26, 2024
1 parent bb9d641 commit 36358c4
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 60 deletions.
18 changes: 9 additions & 9 deletions lib/Espfc/src/Blackbox/Blackbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int Blackbox::begin()

if(!_model.blackboxEnabled()) return 0;

if(_model.config.blackboxDev == 3)
if(_model.config.blackbox.dev == BLACKBOX_DEV_SERIAL)
{
_serial = _model.getSerialStream(SERIAL_FUNCTION_BLACKBOX);
if(!_serial) return 0;
Expand Down Expand Up @@ -156,17 +156,17 @@ int Blackbox::begin()
targetPidLooptime = _model.state.loopTimer.interval;
activePidLoopDenom = _model.config.loopSync;

if(_model.config.blackboxPdenom >= 0 && _model.config.blackboxPdenom <= 4)
if(_model.config.blackbox.pDenom >= 0 && _model.config.blackbox.pDenom <= 4)
{
blackboxConfigMutable()->sample_rate = _model.config.blackboxPdenom;
blackboxConfigMutable()->sample_rate = _model.config.blackbox.pDenom;
}
else
{
blackboxConfigMutable()->sample_rate = blackboxCalculateSampleRate(_model.config.blackboxPdenom);
blackboxConfigMutable()->sample_rate = blackboxCalculateSampleRate(_model.config.blackbox.pDenom);
}
blackboxConfigMutable()->device = _model.config.blackboxDev;
blackboxConfigMutable()->fields_disabled_mask = ~_model.config.blackboxFieldsMask;
blackboxConfigMutable()->mode = _model.config.blackboxMode;
blackboxConfigMutable()->device = _model.config.blackbox.dev;
blackboxConfigMutable()->fields_disabled_mask = ~_model.config.blackbox.fieldsMask;
blackboxConfigMutable()->mode = _model.config.blackbox.mode;

featureConfigMutable()->enabledFeatures = _model.config.featureMask;

Expand Down Expand Up @@ -206,7 +206,7 @@ int Blackbox::begin()
int FAST_CODE_ATTR Blackbox::update()
{
if(!_model.blackboxEnabled()) return 0;
if(_model.config.blackboxDev == 3 && !_serial) return 0;
if(_model.config.blackbox.dev == BLACKBOX_DEV_SERIAL && !_serial) return 0;

Stats::Measure measure(_model.state.stats, COUNTER_BLACKBOX);

Expand All @@ -219,7 +219,7 @@ int FAST_CODE_ATTR Blackbox::update()
}
//PIN_DEBUG(HIGH);
blackboxUpdate(_model.state.loopTimer.last);
if(_model.config.blackboxDev == 3)
if(_model.config.blackbox.dev == BLACKBOX_DEV_SERIAL)
{
_buffer.flush();
}
Expand Down
35 changes: 18 additions & 17 deletions lib/Espfc/src/Cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ class Cli

static const char* voltageSourceChoices[] = { PSTR("NONE"), PSTR("ADC"), NULL };
static const char* currentSourceChoices[] = { PSTR("NONE"), PSTR("ADC"), NULL };
static const char* blackboxDevChoices[] = { PSTR("NONE"), PSTR("FLASH"), PSTR("SD_CARD"), PSTR("SERIAL"), NULL };
static const char* blackboxModeChoices[] = { PSTR("NORMAL"), PSTR("TEST"), PSTR("ALWAYS"), NULL };

size_t i = 0;
Expand Down Expand Up @@ -732,23 +733,23 @@ class Cli
//Param(PSTR("telemetry"), &c.telemetry),
Param(PSTR("telemetry_interval"), &c.telemetryInterval),

Param(PSTR("blackbox_dev"), &c.blackboxDev),
Param(PSTR("blackbox_mode"), &c.blackboxMode, blackboxModeChoices),
Param(PSTR("blackbox_rate"), &c.blackboxPdenom),
Param(PSTR("blackbox_log_acc"), &c.blackboxFieldsMask, BLACKBOX_FIELD_ACC),
Param(PSTR("blackbox_log_alt"), &c.blackboxFieldsMask, BLACKBOX_FIELD_ALTITUDE),
Param(PSTR("blackbox_log_bat"), &c.blackboxFieldsMask, BLACKBOX_FIELD_BATTERY),
Param(PSTR("blackbox_log_debug"), &c.blackboxFieldsMask, BLACKBOX_FIELD_DEBUG_LOG),
Param(PSTR("blackbox_log_gps"), &c.blackboxFieldsMask, BLACKBOX_FIELD_GPS),
Param(PSTR("blackbox_log_gyro"), &c.blackboxFieldsMask, BLACKBOX_FIELD_GYRO),
Param(PSTR("blackbox_log_gyro_raw"), &c.blackboxFieldsMask, BLACKBOX_FIELD_GYROUNFILT),
Param(PSTR("blackbox_log_mag"), &c.blackboxFieldsMask, BLACKBOX_FIELD_MAG),
Param(PSTR("blackbox_log_motor"), &c.blackboxFieldsMask, BLACKBOX_FIELD_MOTOR),
Param(PSTR("blackbox_log_pid"), &c.blackboxFieldsMask, BLACKBOX_FIELD_PID),
Param(PSTR("blackbox_log_rc"), &c.blackboxFieldsMask, BLACKBOX_FIELD_RC_COMMANDS),
Param(PSTR("blackbox_log_rpm"), &c.blackboxFieldsMask, BLACKBOX_FIELD_RPM),
Param(PSTR("blackbox_log_rssi"), &c.blackboxFieldsMask, BLACKBOX_FIELD_RSSI),
Param(PSTR("blackbox_log_sp"), &c.blackboxFieldsMask, BLACKBOX_FIELD_SETPOINT),
Param(PSTR("blackbox_dev"), &c.blackbox.dev, blackboxDevChoices),
Param(PSTR("blackbox_mode"), &c.blackbox.mode, blackboxModeChoices),
Param(PSTR("blackbox_rate"), &c.blackbox.pDenom),
Param(PSTR("blackbox_log_acc"), &c.blackbox.fieldsMask, BLACKBOX_FIELD_ACC),
Param(PSTR("blackbox_log_alt"), &c.blackbox.fieldsMask, BLACKBOX_FIELD_ALTITUDE),
Param(PSTR("blackbox_log_bat"), &c.blackbox.fieldsMask, BLACKBOX_FIELD_BATTERY),
Param(PSTR("blackbox_log_debug"), &c.blackbox.fieldsMask, BLACKBOX_FIELD_DEBUG_LOG),
Param(PSTR("blackbox_log_gps"), &c.blackbox.fieldsMask, BLACKBOX_FIELD_GPS),
Param(PSTR("blackbox_log_gyro"), &c.blackbox.fieldsMask, BLACKBOX_FIELD_GYRO),
Param(PSTR("blackbox_log_gyro_raw"), &c.blackbox.fieldsMask, BLACKBOX_FIELD_GYROUNFILT),
Param(PSTR("blackbox_log_mag"), &c.blackbox.fieldsMask, BLACKBOX_FIELD_MAG),
Param(PSTR("blackbox_log_motor"), &c.blackbox.fieldsMask, BLACKBOX_FIELD_MOTOR),
Param(PSTR("blackbox_log_pid"), &c.blackbox.fieldsMask, BLACKBOX_FIELD_PID),
Param(PSTR("blackbox_log_rc"), &c.blackbox.fieldsMask, BLACKBOX_FIELD_RC_COMMANDS),
Param(PSTR("blackbox_log_rpm"), &c.blackbox.fieldsMask, BLACKBOX_FIELD_RPM),
Param(PSTR("blackbox_log_rssi"), &c.blackbox.fieldsMask, BLACKBOX_FIELD_RSSI),
Param(PSTR("blackbox_log_sp"), &c.blackbox.fieldsMask, BLACKBOX_FIELD_SETPOINT),

#ifdef ESPFC_SERIAL_SOFT_0_WIFI
Param(PSTR("wifi_ssid"), PARAM_STRING, &c.wireless.ssid[0], NULL, 32),
Expand Down
2 changes: 1 addition & 1 deletion lib/Espfc/src/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class Model
bool blackboxEnabled() const
{
// serial or flash
return (config.blackboxDev == 3 || config.blackboxDev == 1) && config.blackboxPdenom > 0;
return (config.blackbox.dev == BLACKBOX_DEV_SERIAL || config.blackbox.dev == BLACKBOX_DEV_FLASH) && config.blackbox.pDenom > 0;
}

bool gyroActive() const /* IRAM_ATTR */
Expand Down
58 changes: 35 additions & 23 deletions lib/Espfc/src/ModelConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ enum FlightMode {
MODE_FAILSAFE,
MODE_BLACKBOX,
MODE_BLACKBOX_ERASE,
MODE_COUNT
MODE_COUNT,
};

enum ScalerDimension {
Expand All @@ -103,10 +103,10 @@ enum ScalerDimension {
ACT_AXIS_PITCH = 1 << 9, // 512
ACT_AXIS_YAW = 1 << 10, // 1024
ACT_AXIS_THRUST = 1 << 11, // 2048
ACT_GYRO_THRUST = 1 << 12 // 4096
ACT_GYRO_THRUST = 1 << 12, // 4096
};

const size_t SCALER_COUNT = 3;
constexpr size_t SCALER_COUNT = 3;

struct ScalerConfig {
uint32_t dimension = 0;
Expand Down Expand Up @@ -179,7 +179,7 @@ enum DebugMode {
DEBUG_BLACKBOX_OUTPUT,
DEBUG_GYRO_SAMPLE,
DEBUG_RX_TIMING,
DEBUG_COUNT
DEBUG_COUNT,
};

enum Axis {
Expand All @@ -199,7 +199,7 @@ enum Axis {
AXIS_AUX_10,
AXIS_AUX_11,
AXIS_AUX_12,
AXIS_COUNT
AXIS_COUNT,
};

enum Feature {
Expand All @@ -222,18 +222,18 @@ enum InputInterpolation {

enum InputFilterType : uint8_t {
INPUT_INTERPOLATION,
INPUT_FILTER
INPUT_FILTER,
};

const size_t MODEL_NAME_LEN = 16;
const size_t AXES = 4;
const size_t AXES_RPY = 3;
const size_t INPUT_CHANNELS = AXIS_COUNT;
const size_t OUTPUT_CHANNELS = ESC_CHANNEL_COUNT;
constexpr size_t MODEL_NAME_LEN = 16;
constexpr size_t AXES = 4;
constexpr size_t AXES_RPY = 3;
constexpr size_t INPUT_CHANNELS = AXIS_COUNT;
constexpr size_t OUTPUT_CHANNELS = ESC_CHANNEL_COUNT;
static_assert(ESC_CHANNEL_COUNT == ESPFC_OUTPUT_COUNT, "ESC_CHANNEL_COUNT and ESPFC_OUTPUT_COUNT must be equal");

const size_t RPM_FILTER_MOTOR_MAX = 4;
const size_t RPM_FILTER_HARMONICS_MAX = 3;
constexpr size_t RPM_FILTER_MOTOR_MAX = 4;
constexpr size_t RPM_FILTER_HARMONICS_MAX = 3;

enum PinFunction {
#ifdef ESPFC_INPUT
Expand Down Expand Up @@ -288,10 +288,10 @@ enum PinFunction {
PIN_SPI_CS1,
PIN_SPI_CS2,
#endif
PIN_COUNT
PIN_COUNT,
};

#define ACTUATOR_CONDITIONS 8
constexpr size_t ACTUATOR_CONDITIONS = 8;

struct ActuatorCondition
{
Expand All @@ -311,7 +311,7 @@ struct SerialPortConfig
int32_t blackboxBaud;
};

#define BUZZER_MAX_EVENTS 8
constexpr size_t BUZZER_MAX_EVENTS = 8;

enum BuzzerEvent {
BUZZER_SILENCE = 0, // Silence, see beeperSilence()
Expand Down Expand Up @@ -359,7 +359,7 @@ enum PidIndex {
FC_PID_LEVEL,
FC_PID_MAG,
FC_PID_VEL,
FC_PID_ITEM_COUNT
FC_PID_ITEM_COUNT,
};

enum BlacboxLogField { // no more than 32, sync with FlightLogFieldSelect_e
Expand All @@ -377,7 +377,14 @@ enum BlacboxLogField { // no more than 32, sync with FlightLogFieldSelect_e
BLACKBOX_FIELD_GPS,
BLACKBOX_FIELD_RPM,
BLACKBOX_FIELD_GYROUNFILT,
BLACKBOX_FIELD_COUNT
BLACKBOX_FIELD_COUNT,
};

enum BlackboxLogDevice {
BLACKBOX_DEV_NONE = 0,
BLACKBOX_DEV_FLASH = 1,
BLACKBOX_DEV_SDCARD = 2,
BLACKBOX_DEV_SERIAL = 3,
};

struct PidConfig
Expand Down Expand Up @@ -517,6 +524,14 @@ struct FailsafeConfig
uint8_t killSwitch = 0;
};

struct BlackboxConfig
{
int8_t dev = 0;
int16_t pDenom = 32; // 1k
int32_t fieldsMask = 0xffff;
int8_t mode = 0;
};

// persistent data
class ModelConfig
{
Expand Down Expand Up @@ -599,10 +614,7 @@ class ModelConfig
int32_t telemetryInterval = 1000;
int8_t telemetryPort; // unused

int8_t blackboxDev = 0;
int16_t blackboxPdenom = 32; // 1k
int32_t blackboxFieldsMask = 0xffff;
int8_t blackboxMode = 0;
BlackboxConfig blackbox;

SerialPortConfig serial[SERIAL_UART_COUNT];

Expand Down Expand Up @@ -769,7 +781,7 @@ class ModelConfig
void devPreset()
{
#ifdef ESPFC_DEV_PRESET_BLACKBOX
blackboxDev = 3; // serial
blackbox.dev = BLACKBOX_DEV_SERIAL; // serial
debugMode = DEBUG_GYRO_SCALED;
serial[ESPFC_DEV_PRESET_BLACKBOX].functionMask |= SERIAL_FUNCTION_BLACKBOX;
serial[ESPFC_DEV_PRESET_BLACKBOX].blackboxBaud = SERIAL_SPEED_250000;
Expand Down
20 changes: 10 additions & 10 deletions lib/Espfc/src/Msp/MspProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -642,18 +642,18 @@ class MspProcessor

case MSP_BLACKBOX_CONFIG:
r.writeU8(1); // Blackbox supported
r.writeU8(_model.config.blackboxDev); // device serial or none
r.writeU8(_model.config.blackbox.dev); // device serial or none
r.writeU8(1); // blackboxGetRateNum()); // unused
r.writeU8(1); // blackboxGetRateDenom());
r.writeU16(_model.config.blackboxPdenom);//blackboxGetPRatio()); // p_denom
//r.writeU8(_model.config.blackboxPdenom); // sample_rate
//r.writeU32(~_model.config.blackboxFieldsMask);
r.writeU16(_model.config.blackbox.pDenom);//blackboxGetPRatio()); // p_denom
//r.writeU8(_model.config.blackbox.pDenom); // sample_rate
//r.writeU32(~_model.config.blackbox.fieldsMask);
break;

case MSP_SET_BLACKBOX_CONFIG:
// Don't allow config to be updated while Blackbox is logging
if (true) {
_model.config.blackboxDev = m.readU8();
_model.config.blackbox.dev = m.readU8();
const int rateNum = m.readU8(); // was rate_num
const int rateDenom = m.readU8(); // was rate_denom
uint16_t pRatio = 0;
Expand All @@ -664,16 +664,16 @@ class MspProcessor
//pRatio = blackboxCalculatePDenom(rateNum, rateDenom);
(void)(rateNum + rateDenom);
}
_model.config.blackboxPdenom = pRatio;
_model.config.blackbox.pDenom = pRatio;

/*if (m.remain() >= 1) {
_model.config.blackboxPdenom = m.readU8();
_model.config.blackbox.pDenom = m.readU8();
} else if(pRatio > 0) {
_model.config.blackboxPdenom = blackboxCalculateSampleRate(pRatio);
//_model.config.blackboxPdenom = pRatio;
_model.config.blackbox.pDenom = blackboxCalculateSampleRate(pRatio);
//_model.config.blackbox.pDenom = pRatio;
}
if (m.remain() >= 4) {
_model.config.blackboxFieldsMask = ~m.readU32();
_model.config.blackbox.fieldsMask = ~m.readU32();
}*/
}
break;
Expand Down

0 comments on commit 36358c4

Please sign in to comment.