NinjaFlight
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
blackbox.h
Go to the documentation of this file.
1 /*
2  * This file is part of Ninjaflight.
3  *
4  * Ninjaflight is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Ninjaflight is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Ninjaflight. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #pragma once
19 
21 #include "config/blackbox.h"
22 
23 typedef enum BlackboxState {
37 
38 #define BLACKBOX_FIRST_HEADER_SENDING_STATE BLACKBOX_STATE_SEND_HEADER
39 #define BLACKBOX_LAST_HEADER_SENDING_STATE BLACKBOX_STATE_SEND_SYSINFO
40 
41 typedef struct blackboxMainState_s {
42  uint32_t time;
43 
45 
46  int16_t rcCommand[4];
49 
50  uint16_t vbatLatest;
51  uint16_t amperageLatest;
52 
53 #ifdef BARO
54  int32_t BaroAlt;
55 #endif
57 #ifdef SONAR
58  int32_t sonarRaw;
59 #endif
60  uint16_t rssi;
62 
63 typedef struct blackboxGpsState_s {
64  int32_t GPS_home[2], GPS_coord[2];
65  uint8_t GPS_numSat;
67 
68 // This data is updated really infrequently:
69 typedef struct blackboxSlowState_s {
70  uint16_t flightModeFlags;
71  uint8_t stateFlags;
72  uint8_t failsafePhase;
75 } __attribute__((__packed__)) blackboxSlowState_t; // We pack this struct so that padding doesn't interfere with memcmp()
76 
77 
79  uint32_t headerIndex;
80 
81  /* Since these fields are used during different blackbox states (never simultaneously) we can
82  * overlap them to save on RAM
83  */
84  union {
87  } u;
88 };
89 
90 struct blackbox {
91  struct ninja *ninja;
92 
98 
100  /*
101  * We store voltages in I-frames relative to this, which was the voltage when the blackbox was activated.
102  * This helps out since the voltage is only expected to fall from that point and we can reduce our diffs
103  * to encode:
104  */
105  uint16_t vbatReference;
106 
108  blackboxSlowState_t slowHistory;
109 
110  // Keep a history of length 2, plus a buffer for MW to store the new values into
112 
113  // These point into blackboxHistoryRing, use them to know where to store history of a given age (0, 1 or 2 generations old)
115 
117 
118  // Cache for FLIGHT_LOG_FIELD_CONDITION_* test results:
120 
121  const struct config const *config;
122 };
123 
124 void blackbox_init(struct blackbox *self, struct ninja *owner, const struct config * config);
125 
127 
128 void blackbox_update(struct blackbox *self);
129 void blackbox_start(struct blackbox *self);
130 void blackbox_stop(struct blackbox *self);
131 
132 bool blackbox_is_running(struct blackbox *self);
int32_t GPS_home[2]
Definition: blackbox.h:64
int32_t axisPID_I[XYZ_AXIS_COUNT]
Definition: blackbox.h:44
#define XYZ_AXIS_COUNT
Definition: axis.h:26
uint16_t amperageLatest
Definition: blackbox.h:51
void blackboxLogEvent(struct blackbox *self, FlightLogEvent event, flightLogEventData_t *data)
bool rxFlightChannelsValid
Definition: blackbox.h:74
Definition: config.h:81
bool blackbox_is_running(struct blackbox *self)
bool blackboxLoggedAnyFrames
Definition: blackbox.h:96
Definition: ninja.h:40
Definition: blackbox.h:69
struct blackboxGpsState_s blackboxGpsState_t
struct blackbox_xmit_state __attribute__
Definition: blackbox.h:41
const struct config const * config
Definition: blackbox.h:121
Definition: blackbox_fielddefs.h:136
int32_t axisPID_P[XYZ_AXIS_COUNT]
Definition: blackbox.h:44
struct blackbox_xmit_state xmitState
Definition: blackbox.h:97
uint16_t flightModeFlags
Definition: blackbox.h:70
uint8_t GPS_numSat
Definition: blackbox.h:65
FlightLogEvent
Definition: blackbox_fielddefs.h:104
uint32_t time
Definition: blackbox.h:42
void blackbox_start(struct blackbox *self)
sys_millis_t startTime
Definition: blackbox.h:86
int fieldIndex
Definition: blackbox.h:85
Definition: blackbox.h:32
Definition: blackbox.h:26
int32_t BaroAlt
Definition: barometer.c:41
blackboxMainState_t blackboxHistoryRing[3]
Definition: blackbox.h:111
Definition: blackbox.h:28
Definition: blackbox.h:63
void blackbox_update(struct blackbox *self)
struct blackboxMainState_s blackboxMainState_t
int32_t sys_millis_t
Definition: system_calls.h:6
Definition: blackbox.h:24
uint16_t blackboxSlowFrameIterationTimer
Definition: blackbox.h:95
void blackbox_init(struct blackbox *self, struct ninja *owner, const struct config *config)
uint8_t failsafePhase
Definition: blackbox.h:72
uint32_t blackboxConditionCache
Definition: blackbox.h:119
int16_t rcCommand[4]
Definition: blackbox.h:46
Definition: blackbox.h:35
uint32_t headerIndex
Definition: blackbox.h:79
blackboxMainState_t * blackboxHistory[3]
Definition: blackbox.h:114
Definition: blackbox.h:31
Definition: blackbox.h:78
BlackboxState blackboxState
Definition: blackbox.h:99
struct ninja * ninja
Definition: blackbox.h:91
int16_t gyroADC[XYZ_AXIS_COUNT]
Definition: blackbox.h:47
Definition: blackbox.h:25
Definition: blackbox.h:27
Definition: blackbox.h:30
bool blackboxModeActivationConditionPresent
Definition: blackbox.h:116
Definition: blackbox.h:29
Definition: blackbox.h:90
void blackbox_stop(struct blackbox *self)
bool rxSignalReceived
Definition: blackbox.h:73
Definition: blackbox.h:33
int32_t axisPID_D[XYZ_AXIS_COUNT]
Definition: blackbox.h:44
int16_t magADC[XYZ_AXIS_COUNT]
Definition: blackbox.h:56
blackboxSlowState_t slowHistory
Definition: blackbox.h:108
uint16_t blackboxIFrameIndex
Definition: blackbox.h:94
blackboxGpsState_t gpsHistory
Definition: blackbox.h:107
Definition: blackbox.h:34
uint16_t rssi
Definition: blackbox.h:60
uint16_t vbatLatest
Definition: blackbox.h:50
BlackboxState
Definition: blackbox.h:23
uint8_t stateFlags
Definition: blackbox.h:71
uint16_t vbatReference
Definition: blackbox.h:105
int16_t accSmooth[XYZ_AXIS_COUNT]
Definition: blackbox.h:48
uint16_t blackboxPFrameIndex
Definition: blackbox.h:94
uint32_t blackboxIteration
Definition: blackbox.h:93
int32_t GPS_coord[2]
Definition: blackbox.h:64
uint16_t data
Definition: config.c:91