Blackbox code tidy

This commit is contained in:
Martin Budden 2017-04-29 08:58:05 +01:00
parent ecc4b3c8a1
commit 342148524f
2 changed files with 55 additions and 65 deletions

View File

@ -39,10 +39,9 @@
#include "config/parameter_group.h" #include "config/parameter_group.h"
#include "config/parameter_group_ids.h" #include "config/parameter_group_ids.h"
#include "drivers/sensor.h"
#include "drivers/compass/compass.h" #include "drivers/compass/compass.h"
#include "drivers/sensor.h"
#include "drivers/system.h" #include "drivers/system.h"
#include "drivers/pwm_output.h"
#include "fc/config.h" #include "fc/config.h"
#include "fc/controlrate_profile.h" #include "fc/controlrate_profile.h"
@ -65,7 +64,6 @@
#include "sensors/barometer.h" #include "sensors/barometer.h"
#include "sensors/battery.h" #include "sensors/battery.h"
#include "sensors/compass.h" #include "sensors/compass.h"
#include "sensors/compass.h"
#include "sensors/gyro.h" #include "sensors/gyro.h"
#include "sensors/sonar.h" #include "sensors/sonar.h"
@ -90,8 +88,6 @@ PG_RESET_TEMPLATE(blackboxConfig_t, blackboxConfig,
#define BLACKBOX_SHUTDOWN_TIMEOUT_MILLIS 200 #define BLACKBOX_SHUTDOWN_TIMEOUT_MILLIS 200
#define SLOW_FRAME_INTERVAL 4096 #define SLOW_FRAME_INTERVAL 4096
#define ARRAY_LENGTH(x) (sizeof((x))/sizeof((x)[0]))
#define STATIC_ASSERT(condition, name ) \ #define STATIC_ASSERT(condition, name ) \
typedef char assert_failed_ ## name [(condition) ? 1 : -1 ] typedef char assert_failed_ ## name [(condition) ? 1 : -1 ]
@ -127,7 +123,7 @@ typedef struct blackboxFieldDefinition_s {
uint8_t arr[1]; uint8_t arr[1];
} blackboxFieldDefinition_t; } blackboxFieldDefinition_t;
#define BLACKBOX_DELTA_FIELD_HEADER_COUNT ARRAY_LENGTH(blackboxFieldHeaderNames) #define BLACKBOX_DELTA_FIELD_HEADER_COUNT ARRAYLEN(blackboxFieldHeaderNames)
#define BLACKBOX_SIMPLE_FIELD_HEADER_COUNT (BLACKBOX_DELTA_FIELD_HEADER_COUNT - 2) #define BLACKBOX_SIMPLE_FIELD_HEADER_COUNT (BLACKBOX_DELTA_FIELD_HEADER_COUNT - 2)
#define BLACKBOX_CONDITIONAL_FIELD_HEADER_COUNT (BLACKBOX_DELTA_FIELD_HEADER_COUNT - 2) #define BLACKBOX_CONDITIONAL_FIELD_HEADER_COUNT (BLACKBOX_DELTA_FIELD_HEADER_COUNT - 2)
@ -288,7 +284,7 @@ typedef struct blackboxMainState_s {
int16_t rcCommand[4]; int16_t rcCommand[4];
int16_t gyroADC[XYZ_AXIS_COUNT]; int16_t gyroADC[XYZ_AXIS_COUNT];
int16_t accSmooth[XYZ_AXIS_COUNT]; int16_t accSmooth[XYZ_AXIS_COUNT];
int16_t debug[4]; int16_t debug[DEBUG16_VALUE_COUNT];
int16_t motor[MAX_SUPPORTED_MOTORS]; int16_t motor[MAX_SUPPORTED_MOTORS];
int16_t servo[MAX_SUPPORTED_SERVOS]; int16_t servo[MAX_SUPPORTED_SERVOS];
@ -376,12 +372,13 @@ static bool blackboxModeActivationConditionPresent = false;
/** /**
* Return true if it is safe to edit the Blackbox configuration. * Return true if it is safe to edit the Blackbox configuration.
*/ */
bool blackboxMayEditConfig() bool blackboxMayEditConfig(void)
{ {
return blackboxState <= BLACKBOX_STATE_STOPPED; return blackboxState <= BLACKBOX_STATE_STOPPED;
} }
static bool blackboxIsOnlyLoggingIntraframes() { static bool blackboxIsOnlyLoggingIntraframes(void)
{
return blackboxConfig()->rate_num == 1 && blackboxConfig()->rate_denom == 32; return blackboxConfig()->rate_num == 1 && blackboxConfig()->rate_denom == 32;
} }
@ -449,7 +446,7 @@ static bool testBlackboxConditionUncached(FlightLogFieldCondition condition)
} }
} }
static void blackboxBuildConditionCache() static void blackboxBuildConditionCache(void)
{ {
FlightLogFieldCondition cond; FlightLogFieldCondition cond;
@ -612,7 +609,6 @@ static void blackboxWriteMainStateArrayUsingAveragePredictor(int arrOffsetInHist
static void writeInterframe(void) static void writeInterframe(void)
{ {
int x;
int32_t deltas[8]; int32_t deltas[8];
blackboxMainState_t *blackboxCurrent = blackboxHistory[0]; blackboxMainState_t *blackboxCurrent = blackboxHistory[0];
@ -642,7 +638,7 @@ static void writeInterframe(void)
* The PID D term is frequently set to zero for yaw, which makes the result from the calculation * The PID D term is frequently set to zero for yaw, which makes the result from the calculation
* always zero. So don't bother recording D results when PID D terms are zero. * always zero. So don't bother recording D results when PID D terms are zero.
*/ */
for (x = 0; x < XYZ_AXIS_COUNT; x++) { for (int x = 0; x < XYZ_AXIS_COUNT; x++) {
if (testBlackboxCondition(FLIGHT_LOG_FIELD_CONDITION_NONZERO_PID_D_0 + x)) { if (testBlackboxCondition(FLIGHT_LOG_FIELD_CONDITION_NONZERO_PID_D_0 + x)) {
blackboxWriteSignedVB(blackboxCurrent->axisPID_D[x] - blackboxLast->axisPID_D[x]); blackboxWriteSignedVB(blackboxCurrent->axisPID_D[x] - blackboxLast->axisPID_D[x]);
} }
@ -652,7 +648,7 @@ static void writeInterframe(void)
* RC tends to stay the same or fairly small for many frames at a time, so use an encoding that * RC tends to stay the same or fairly small for many frames at a time, so use an encoding that
* can pack multiple values per byte: * can pack multiple values per byte:
*/ */
for (x = 0; x < 4; x++) { for (int x = 0; x < 4; x++) {
deltas[x] = blackboxCurrent->rcCommand[x] - blackboxLast->rcCommand[x]; deltas[x] = blackboxCurrent->rcCommand[x] - blackboxLast->rcCommand[x];
} }
@ -671,7 +667,7 @@ static void writeInterframe(void)
#ifdef MAG #ifdef MAG
if (testBlackboxCondition(FLIGHT_LOG_FIELD_CONDITION_MAG)) { if (testBlackboxCondition(FLIGHT_LOG_FIELD_CONDITION_MAG)) {
for (x = 0; x < XYZ_AXIS_COUNT; x++) { for (int x = 0; x < XYZ_AXIS_COUNT; x++) {
deltas[optionalFieldCount++] = blackboxCurrent->magADC[x] - blackboxLast->magADC[x]; deltas[optionalFieldCount++] = blackboxCurrent->magADC[x] - blackboxLast->magADC[x];
} }
} }
@ -787,10 +783,8 @@ static int gcd(int num, int denom)
return gcd(denom, num % denom); return gcd(denom, num % denom);
} }
void blackboxValidateConfig() void blackboxValidateConfig(void)
{ {
int div;
if (blackboxConfig()->rate_num == 0 || blackboxConfig()->rate_denom == 0 if (blackboxConfig()->rate_num == 0 || blackboxConfig()->rate_denom == 0
|| blackboxConfig()->rate_num >= blackboxConfig()->rate_denom) { || blackboxConfig()->rate_num >= blackboxConfig()->rate_denom) {
blackboxConfigMutable()->rate_num = 1; blackboxConfigMutable()->rate_num = 1;
@ -799,7 +793,7 @@ void blackboxValidateConfig()
/* Reduce the fraction the user entered as much as possible (makes the recorded/skipped frame pattern repeat /* Reduce the fraction the user entered as much as possible (makes the recorded/skipped frame pattern repeat
* itself more frequently) * itself more frequently)
*/ */
div = gcd(blackboxConfig()->rate_num, blackboxConfig()->rate_denom); const int div = gcd(blackboxConfig()->rate_num, blackboxConfig()->rate_denom);
blackboxConfigMutable()->rate_num /= div; blackboxConfigMutable()->rate_num /= div;
blackboxConfigMutable()->rate_denom /= div; blackboxConfigMutable()->rate_denom /= div;
@ -955,7 +949,7 @@ bool inMotorTestMode(void) {
} }
#ifdef GPS #ifdef GPS
static void writeGPSHomeFrame() static void writeGPSHomeFrame(void)
{ {
blackboxWrite('H'); blackboxWrite('H');
@ -1191,7 +1185,7 @@ static bool sendFieldDefinition(char mainFrameChar, char deltaFrameChar, const v
* Transmit a portion of the system information headers. Call the first time with xmitState.headerIndex == 0. Returns * Transmit a portion of the system information headers. Call the first time with xmitState.headerIndex == 0. Returns
* true iff transmission is complete, otherwise call again later to continue transmission. * true iff transmission is complete, otherwise call again later to continue transmission.
*/ */
static bool blackboxWriteSysinfo() static bool blackboxWriteSysinfo(void)
{ {
// Make sure we have enough room in the buffer for our longest line (as of this writing, the "Firmware date" line) // Make sure we have enough room in the buffer for our longest line (as of this writing, the "Firmware date" line)
if (blackboxDeviceReserveBufferSpace(64) != BLACKBOX_RESERVE_SUCCESS) { if (blackboxDeviceReserveBufferSpace(64) != BLACKBOX_RESERVE_SUCCESS) {
@ -1369,7 +1363,7 @@ void blackboxLogEvent(FlightLogEvent event, flightLogEventData_t *data)
} }
/* If an arming beep has played since it was last logged, write the time of the arming beep to the log as a synchronization point */ /* If an arming beep has played since it was last logged, write the time of the arming beep to the log as a synchronization point */
static void blackboxCheckAndLogArmingBeep() static void blackboxCheckAndLogArmingBeep(void)
{ {
flightLogEvent_syncBeep_t eventData; flightLogEvent_syncBeep_t eventData;
@ -1384,7 +1378,7 @@ static void blackboxCheckAndLogArmingBeep()
} }
/* monitor the flight mode event status and trigger an event record if the state changes */ /* monitor the flight mode event status and trigger an event record if the state changes */
static void blackboxCheckAndLogFlightMode() static void blackboxCheckAndLogFlightMode(void)
{ {
flightLogEvent_flightMode_t eventData; // Add new data for current flight mode flags flightLogEvent_flightMode_t eventData; // Add new data for current flight mode flags
@ -1410,12 +1404,13 @@ static bool blackboxShouldLogPFrame(uint32_t pFrameIndex)
return (pFrameIndex + blackboxConfig()->rate_num - 1) % blackboxConfig()->rate_denom < blackboxConfig()->rate_num; return (pFrameIndex + blackboxConfig()->rate_num - 1) % blackboxConfig()->rate_denom < blackboxConfig()->rate_num;
} }
static bool blackboxShouldLogIFrame() { static bool blackboxShouldLogIFrame(void)
{
return blackboxPFrameIndex == 0; return blackboxPFrameIndex == 0;
} }
// Called once every FC loop in order to keep track of how many FC loop iterations have passed // Called once every FC loop in order to keep track of how many FC loop iterations have passed
static void blackboxAdvanceIterationTimers() static void blackboxAdvanceIterationTimers(void)
{ {
blackboxSlowFrameIterationTimer++; blackboxSlowFrameIterationTimer++;
blackboxIteration++; blackboxIteration++;
@ -1529,7 +1524,7 @@ void blackboxUpdate(timeUs_t currentTimeUs)
case BLACKBOX_STATE_SEND_MAIN_FIELD_HEADER: case BLACKBOX_STATE_SEND_MAIN_FIELD_HEADER:
blackboxReplenishHeaderBudget(); blackboxReplenishHeaderBudget();
//On entry of this state, xmitState.headerIndex is 0 and xmitState.u.fieldIndex is -1 //On entry of this state, xmitState.headerIndex is 0 and xmitState.u.fieldIndex is -1
if (!sendFieldDefinition('I', 'P', blackboxMainFields, blackboxMainFields + 1, ARRAY_LENGTH(blackboxMainFields), if (!sendFieldDefinition('I', 'P', blackboxMainFields, blackboxMainFields + 1, ARRAYLEN(blackboxMainFields),
&blackboxMainFields[0].condition, &blackboxMainFields[1].condition)) { &blackboxMainFields[0].condition, &blackboxMainFields[1].condition)) {
#ifdef GPS #ifdef GPS
if (feature(FEATURE_GPS)) { if (feature(FEATURE_GPS)) {
@ -1543,7 +1538,7 @@ void blackboxUpdate(timeUs_t currentTimeUs)
case BLACKBOX_STATE_SEND_GPS_H_HEADER: case BLACKBOX_STATE_SEND_GPS_H_HEADER:
blackboxReplenishHeaderBudget(); blackboxReplenishHeaderBudget();
//On entry of this state, xmitState.headerIndex is 0 and xmitState.u.fieldIndex is -1 //On entry of this state, xmitState.headerIndex is 0 and xmitState.u.fieldIndex is -1
if (!sendFieldDefinition('H', 0, blackboxGpsHFields, blackboxGpsHFields + 1, ARRAY_LENGTH(blackboxGpsHFields), if (!sendFieldDefinition('H', 0, blackboxGpsHFields, blackboxGpsHFields + 1, ARRAYLEN(blackboxGpsHFields),
NULL, NULL)) { NULL, NULL)) {
blackboxSetState(BLACKBOX_STATE_SEND_GPS_G_HEADER); blackboxSetState(BLACKBOX_STATE_SEND_GPS_G_HEADER);
} }
@ -1551,7 +1546,7 @@ void blackboxUpdate(timeUs_t currentTimeUs)
case BLACKBOX_STATE_SEND_GPS_G_HEADER: case BLACKBOX_STATE_SEND_GPS_G_HEADER:
blackboxReplenishHeaderBudget(); blackboxReplenishHeaderBudget();
//On entry of this state, xmitState.headerIndex is 0 and xmitState.u.fieldIndex is -1 //On entry of this state, xmitState.headerIndex is 0 and xmitState.u.fieldIndex is -1
if (!sendFieldDefinition('G', 0, blackboxGpsGFields, blackboxGpsGFields + 1, ARRAY_LENGTH(blackboxGpsGFields), if (!sendFieldDefinition('G', 0, blackboxGpsGFields, blackboxGpsGFields + 1, ARRAYLEN(blackboxGpsGFields),
&blackboxGpsGFields[0].condition, &blackboxGpsGFields[1].condition)) { &blackboxGpsGFields[0].condition, &blackboxGpsGFields[1].condition)) {
blackboxSetState(BLACKBOX_STATE_SEND_SLOW_HEADER); blackboxSetState(BLACKBOX_STATE_SEND_SLOW_HEADER);
} }
@ -1560,7 +1555,7 @@ void blackboxUpdate(timeUs_t currentTimeUs)
case BLACKBOX_STATE_SEND_SLOW_HEADER: case BLACKBOX_STATE_SEND_SLOW_HEADER:
blackboxReplenishHeaderBudget(); blackboxReplenishHeaderBudget();
//On entry of this state, xmitState.headerIndex is 0 and xmitState.u.fieldIndex is -1 //On entry of this state, xmitState.headerIndex is 0 and xmitState.u.fieldIndex is -1
if (!sendFieldDefinition('S', 0, blackboxSlowFields, blackboxSlowFields + 1, ARRAY_LENGTH(blackboxSlowFields), if (!sendFieldDefinition('S', 0, blackboxSlowFields, blackboxSlowFields + 1, ARRAYLEN(blackboxSlowFields),
NULL, NULL)) { NULL, NULL)) {
blackboxSetState(BLACKBOX_STATE_SEND_SYSINFO); blackboxSetState(BLACKBOX_STATE_SEND_SYSINFO);
} }
@ -1571,7 +1566,6 @@ void blackboxUpdate(timeUs_t currentTimeUs)
//Keep writing chunks of the system info headers until it returns true to signal completion //Keep writing chunks of the system info headers until it returns true to signal completion
if (blackboxWriteSysinfo()) { if (blackboxWriteSysinfo()) {
/* /*
* Wait for header buffers to drain completely before data logging begins to ensure reliable header delivery * Wait for header buffers to drain completely before data logging begins to ensure reliable header delivery
* (overflowing circular buffers causes all data to be discarded, so the first few logged iterations * (overflowing circular buffers causes all data to be discarded, so the first few logged iterations
@ -1611,7 +1605,6 @@ void blackboxUpdate(timeUs_t currentTimeUs)
break; break;
case BLACKBOX_STATE_SHUTTING_DOWN: case BLACKBOX_STATE_SHUTTING_DOWN:
//On entry of this state, startTime is set //On entry of this state, startTime is set
/* /*
* Wait for the log we've transmitted to make its way to the logger before we release the serial port, * Wait for the log we've transmitted to make its way to the logger before we release the serial port,
* since releasing the port clears the Tx buffer. * since releasing the port clears the Tx buffer.

View File

@ -18,9 +18,7 @@
#pragma once #pragma once
#include "blackbox/blackbox_fielddefs.h" #include "blackbox/blackbox_fielddefs.h"
#include "common/time.h" #include "common/time.h"
#include "config/parameter_group.h" #include "config/parameter_group.h"
typedef enum BlackboxDevice { typedef enum BlackboxDevice {
@ -49,5 +47,4 @@ void blackboxInit(void);
void blackboxUpdate(timeUs_t currentTimeUs); void blackboxUpdate(timeUs_t currentTimeUs);
void blackboxValidateConfig(); void blackboxValidateConfig();
void blackboxFinish(void); void blackboxFinish(void);
bool blackboxMayEditConfig(void); bool blackboxMayEditConfig(void);