Fixed getDshotAverageRpm function (#12178)
[betaflight.git] / src / main / drivers / dshot.h
blob1a91c9fcd672915ea6b2b625e9a4edfaf3d11386
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #pragma once
23 #include "common/time.h"
25 #include "pg/motor.h"
27 #define DSHOT_MIN_THROTTLE (48)
28 #define DSHOT_MAX_THROTTLE (2047)
29 #define DSHOT_3D_FORWARD_MIN_THROTTLE (1048)
30 #define DSHOT_RANGE (DSHOT_MAX_THROTTLE - DSHOT_MIN_THROTTLE)
32 #define DSHOT_TELEMETRY_NOEDGE (0xfffe)
33 #define DSHOT_TELEMETRY_INVALID (0xffff)
35 #define MIN_GCR_EDGES (7)
36 #define MAX_GCR_EDGES (22)
38 // comment out to see frame dump of corrupted frames in dshot_telemetry_info
39 //#define DEBUG_BBDECODE
41 #ifdef USE_DSHOT_TELEMETRY_STATS
42 #define DSHOT_TELEMETRY_QUALITY_WINDOW 1 // capture a rolling 1 second of packet stats
43 #define DSHOT_TELEMETRY_QUALITY_BUCKET_MS 100 // determines the granularity of the stats and the overall number of rolling buckets
44 #define DSHOT_TELEMETRY_QUALITY_BUCKET_COUNT (DSHOT_TELEMETRY_QUALITY_WINDOW * 1000 / DSHOT_TELEMETRY_QUALITY_BUCKET_MS)
46 typedef struct dshotTelemetryQuality_s {
47 uint32_t packetCountSum;
48 uint32_t invalidCountSum;
49 uint32_t packetCountArray[DSHOT_TELEMETRY_QUALITY_BUCKET_COUNT];
50 uint32_t invalidCountArray[DSHOT_TELEMETRY_QUALITY_BUCKET_COUNT];
51 uint8_t lastBucketIndex;
52 } dshotTelemetryQuality_t;
54 extern dshotTelemetryQuality_t dshotTelemetryQuality[MAX_SUPPORTED_MOTORS];
55 #endif // USE_DSHOT_TELEMETRY_STATS
57 #define DSHOT_NORMAL_TELEMETRY_MASK (1 << DSHOT_TELEMETRY_TYPE_eRPM)
58 #define DSHOT_EXTENDED_TELEMETRY_MASK (~DSHOT_NORMAL_TELEMETRY_MASK)
60 typedef enum dshotTelemetryType_e {
61 DSHOT_TELEMETRY_TYPE_eRPM = 0,
62 DSHOT_TELEMETRY_TYPE_TEMPERATURE = 1,
63 DSHOT_TELEMETRY_TYPE_VOLTAGE = 2,
64 DSHOT_TELEMETRY_TYPE_CURRENT = 3,
65 DSHOT_TELEMETRY_TYPE_DEBUG1 = 4,
66 DSHOT_TELEMETRY_TYPE_DEBUG2 = 5,
67 DSHOT_TELEMETRY_TYPE_DEBUG3 = 6,
68 DSHOT_TELEMETRY_TYPE_STATE_EVENTS = 7,
69 DSHOT_TELEMETRY_TYPE_COUNT = 8
70 } dshotTelemetryType_t;
72 typedef enum dshotRawValueState_e {
73 DSHOT_RAW_VALUE_STATE_INVALID = 0,
74 DSHOT_RAW_VALUE_STATE_NOT_PROCESSED = 1,
75 DSHOT_RAW_VALUE_STATE_PROCESSED = 2
76 } dshotRawValueState_t;
78 typedef struct dshotProtocolControl_s {
79 uint16_t value;
80 bool requestTelemetry;
81 } dshotProtocolControl_t;
83 void dshotInitEndpoints(const motorConfig_t *motorConfig, float outputLimit, float *outputLow, float *outputHigh, float *disarm, float *deadbandMotor3dHigh, float *deadbandMotor3dLow);
84 float dshotConvertFromExternal(uint16_t externalValue);
85 uint16_t dshotConvertToExternal(float motorValue);
87 uint16_t prepareDshotPacket(dshotProtocolControl_t *pcb);
89 #ifdef USE_DSHOT_TELEMETRY
90 extern bool useDshotTelemetry;
92 typedef struct dshotTelemetryMotorState_s {
93 uint16_t rawValue;
94 uint16_t telemetryData[DSHOT_TELEMETRY_TYPE_COUNT];
95 uint8_t telemetryTypes;
96 uint8_t maxTemp;
97 } dshotTelemetryMotorState_t;
100 typedef struct dshotTelemetryState_s {
101 bool useDshotTelemetry;
102 uint32_t invalidPacketCount;
103 uint32_t readCount;
104 dshotTelemetryMotorState_t motorState[MAX_SUPPORTED_MOTORS];
105 uint32_t inputBuffer[MAX_GCR_EDGES];
106 uint16_t averageErpm;
107 dshotRawValueState_t rawValueState;
108 } dshotTelemetryState_t;
110 extern dshotTelemetryState_t dshotTelemetryState;
112 #ifdef USE_DSHOT_TELEMETRY_STATS
113 void updateDshotTelemetryQuality(dshotTelemetryQuality_t *qualityStats, bool packetValid, timeMs_t currentTimeMs);
114 #endif
115 #endif
117 uint16_t getDshotTelemetry(uint8_t index);
118 uint32_t erpmToRpm(uint16_t erpm);
119 uint32_t getDshotAverageRpm(void);
120 bool isDshotMotorTelemetryActive(uint8_t motorIndex);
121 bool isDshotTelemetryActive(void);
123 int16_t getDshotTelemetryMotorInvalidPercent(uint8_t motorIndex);
125 void validateAndfixMotorOutputReordering(uint8_t *array, const unsigned size);
126 void dshotCleanTelemetryData(void);