2025-06-11 08:29:59 +01:00
|
|
|
/**
|
|
|
|
|
* @file DShotRMT.h
|
2025-06-12 23:45:48 +01:00
|
|
|
* @brief DShot signal generation using ESP32 RMT with continuous repeat and pause between frames, including BiDirectional support
|
2025-06-11 08:29:59 +01:00
|
|
|
* @author Wastl Kraus
|
|
|
|
|
* @date 2025-06-11
|
|
|
|
|
* @license MIT
|
|
|
|
|
*/
|
2021-06-29 19:05:20 +01:00
|
|
|
|
2025-06-11 08:29:59 +01:00
|
|
|
#pragma once
|
2021-06-29 19:05:20 +01:00
|
|
|
|
2023-03-10 16:45:18 +00:00
|
|
|
#include <Arduino.h>
|
2025-06-18 17:40:16 +01:00
|
|
|
#include <dshot_commands.h>
|
2025-06-13 20:50:00 +01:00
|
|
|
#include <driver/gpio.h>
|
2025-06-11 08:29:59 +01:00
|
|
|
#include <driver/rmt_tx.h>
|
2025-06-12 23:45:48 +01:00
|
|
|
#include <driver/rmt_rx.h>
|
2022-11-26 12:52:46 +00:00
|
|
|
|
2025-06-13 20:50:00 +01:00
|
|
|
// --- DShot Protocol Constants ---
|
2025-06-11 08:29:59 +01:00
|
|
|
static constexpr auto DSHOT_THROTTLE_FAILSAVE = 0;
|
|
|
|
|
static constexpr auto DSHOT_THROTTLE_MIN = 48;
|
|
|
|
|
static constexpr auto DSHOT_THROTTLE_MAX = 2047;
|
2025-07-17 22:24:28 +01:00
|
|
|
static constexpr auto DSHOT_BITS_PER_FRAME = 16;
|
2025-06-13 20:50:00 +01:00
|
|
|
|
2025-06-11 08:29:59 +01:00
|
|
|
static constexpr auto DSHOT_NULL_PACKET = 0b0000000000000000;
|
2025-06-13 12:50:10 +01:00
|
|
|
static constexpr auto DSHOT_FULL_PACKET = 0b1111111111111111;
|
|
|
|
|
static constexpr auto NO_ERPM_SIGNAL = 0;
|
2025-06-13 20:50:00 +01:00
|
|
|
|
|
|
|
|
// RMT configuration parameters
|
2025-06-13 12:50:10 +01:00
|
|
|
static constexpr auto DSHOT_CLOCK_SRC_DEFAULT = RMT_CLK_SRC_DEFAULT;
|
2025-07-17 22:24:28 +01:00
|
|
|
static constexpr auto DSHOT_RMT_RESOLUTION = 10 * 1000 * 1000; // 10 MHz Clock
|
2025-06-13 20:50:00 +01:00
|
|
|
|
|
|
|
|
static constexpr auto TX_BUFFER_SIZE = DSHOT_BITS_PER_FRAME;
|
2025-06-18 23:43:59 +01:00
|
|
|
static constexpr auto RX_BUFFER_SIZE = 32; // Padding for RX decoding
|
2021-06-29 19:05:20 +01:00
|
|
|
|
2025-07-19 12:26:54 +01:00
|
|
|
// DShot Packet
|
|
|
|
|
typedef struct dshot_packet_s
|
|
|
|
|
{
|
|
|
|
|
uint16_t throttle_value : 11;
|
|
|
|
|
bool telemetric_request : 1;
|
|
|
|
|
uint16_t checksum : 4;
|
|
|
|
|
} dshot_packet_t;
|
|
|
|
|
|
2025-06-13 20:50:00 +01:00
|
|
|
// --- DShot Mode Selection ---
|
2022-11-25 15:08:58 +00:00
|
|
|
typedef enum dshot_mode_e
|
|
|
|
|
{
|
2025-06-13 12:50:10 +01:00
|
|
|
DSHOT_OFF,
|
2023-03-24 12:44:49 +00:00
|
|
|
DSHOT150,
|
|
|
|
|
DSHOT300,
|
2025-06-13 12:50:10 +01:00
|
|
|
DSHOT600,
|
|
|
|
|
DSHOT1200
|
2021-06-29 19:05:20 +01:00
|
|
|
} dshot_mode_t;
|
|
|
|
|
|
2025-06-13 20:50:00 +01:00
|
|
|
// --- DShotRMT Class ---
|
2022-11-25 15:08:58 +00:00
|
|
|
class DShotRMT
|
|
|
|
|
{
|
2023-03-24 12:44:49 +00:00
|
|
|
public:
|
2025-06-13 20:50:00 +01:00
|
|
|
// Constructor: initializes configuration state
|
2025-07-19 12:26:54 +01:00
|
|
|
DShotRMT(gpio_num_t gpio, dshot_mode_t mode = DSHOT300, bool isBidirectional = false, uint8_t pauseDuration = 120);
|
2023-03-27 18:47:23 +01:00
|
|
|
|
2025-06-13 20:50:00 +01:00
|
|
|
// Initializes the RMT TX and RX channels
|
2025-06-11 08:29:59 +01:00
|
|
|
void begin();
|
2025-06-13 20:50:00 +01:00
|
|
|
|
2025-07-18 10:13:43 +01:00
|
|
|
// Sets a new throttle value (48-2047) and sends it
|
2025-06-12 23:45:48 +01:00
|
|
|
void setThrottle(uint16_t throttle);
|
2021-06-29 19:05:20 +01:00
|
|
|
|
2025-06-14 17:16:45 +01:00
|
|
|
// Receives and decodes the latest value from ESC, if available
|
2025-06-13 20:50:00 +01:00
|
|
|
uint32_t getERPM();
|
2025-06-14 17:16:45 +01:00
|
|
|
uint32_t getMotorRPM(uint8_t magnet_count);
|
2025-07-19 12:26:54 +01:00
|
|
|
|
|
|
|
|
// Accessors for GPIO and DShot Settings
|
2025-06-11 08:29:59 +01:00
|
|
|
gpio_num_t getGPIO() const { return _gpio; }
|
|
|
|
|
dshot_mode_t getDShotMode() const { return _mode; }
|
2025-07-19 12:26:54 +01:00
|
|
|
uint8_t getPauseDuration() const { return _pauseDuration; }
|
|
|
|
|
void setPauseDuration(uint8_t pauseDuration) { _pauseDuration = pauseDuration; }
|
2021-06-29 19:05:20 +01:00
|
|
|
|
2023-03-24 12:44:49 +00:00
|
|
|
private:
|
2025-07-19 12:26:54 +01:00
|
|
|
// Calculates the checksum for throttle value
|
2025-06-17 19:56:50 +01:00
|
|
|
uint16_t calculateCRC(uint16_t dshot_packet);
|
|
|
|
|
|
2025-07-19 12:26:54 +01:00
|
|
|
// Assembles DShot packet (11 bit throttle + 1 bit telemetry request + 4 bit crc)
|
2025-06-17 19:56:50 +01:00
|
|
|
uint16_t assambleDShotPaket(uint16_t value);
|
|
|
|
|
|
2025-06-13 20:50:00 +01:00
|
|
|
// Converts a 16-bit DShot packet into RMT symbols and appends pause
|
|
|
|
|
void encodeDShotTX(uint16_t dshot_packet, rmt_symbol_word_t *symbols, size_t &count);
|
|
|
|
|
|
2025-07-17 22:24:28 +01:00
|
|
|
// Decodes the ESC answer
|
|
|
|
|
uint16_t decodeDShotRX(const rmt_symbol_word_t *symbols, uint32_t count);
|
|
|
|
|
|
2025-07-19 12:26:54 +01:00
|
|
|
// --- Configuration Parameters ---
|
2025-06-11 08:29:59 +01:00
|
|
|
gpio_num_t _gpio;
|
|
|
|
|
dshot_mode_t _mode;
|
|
|
|
|
bool _isBidirectional;
|
2025-07-19 12:26:54 +01:00
|
|
|
uint8_t _pauseDuration;
|
2025-06-13 20:50:00 +01:00
|
|
|
|
|
|
|
|
// --- DShot Packets Container ---
|
2025-07-18 10:13:43 +01:00
|
|
|
uint16_t _lastThrottle = DSHOT_NULL_PACKET;
|
2025-07-19 12:26:54 +01:00
|
|
|
uint16_t _rx_packet = DSHOT_NULL_PACKET;
|
2025-06-13 20:50:00 +01:00
|
|
|
uint16_t _tx_packet = DSHOT_NULL_PACKET;
|
2025-07-18 10:13:43 +01:00
|
|
|
uint8_t _packet_crc = 0;
|
2025-07-19 12:26:54 +01:00
|
|
|
dshot_packet_t _dshot_packet = {};
|
2021-06-29 19:05:20 +01:00
|
|
|
|
2025-07-19 12:26:54 +01:00
|
|
|
// --- RMT Channel Handles ---
|
2025-06-12 23:45:48 +01:00
|
|
|
rmt_channel_handle_t _rmt_rx_channel = nullptr;
|
2025-06-13 20:50:00 +01:00
|
|
|
rmt_channel_handle_t _rmt_tx_channel = nullptr;
|
2025-06-18 23:43:59 +01:00
|
|
|
rmt_rx_channel_config_t _rmt_rx_channel_config = {};
|
|
|
|
|
rmt_tx_channel_config_t _rmt_tx_channel_config = {};
|
2025-06-13 20:50:00 +01:00
|
|
|
|
|
|
|
|
// --- DShot RMT Encoder ---
|
2025-06-12 23:45:48 +01:00
|
|
|
rmt_encoder_handle_t _dshot_encoder = nullptr;
|
2025-06-13 20:50:00 +01:00
|
|
|
|
|
|
|
|
// --- RMT Configuration ---
|
|
|
|
|
rmt_receive_config_t _receive_config = {};
|
2025-06-12 11:24:27 +01:00
|
|
|
rmt_transmit_config_t _transmit_config = {};
|
2023-03-27 18:47:23 +01:00
|
|
|
|
2025-07-19 12:26:54 +01:00
|
|
|
// --- RMT Symbol Buffers ---
|
2025-06-13 20:50:00 +01:00
|
|
|
rmt_symbol_word_t _rx_symbols[RX_BUFFER_SIZE] = {};
|
|
|
|
|
rmt_symbol_word_t _tx_symbols[TX_BUFFER_SIZE] = {};
|
2025-06-14 17:16:45 +01:00
|
|
|
|
|
|
|
|
// Stores the last valid eRPM received from the ESC
|
2025-07-17 22:24:28 +01:00
|
|
|
uint16_t _last_erpm = 0;
|
2025-06-11 08:29:59 +01:00
|
|
|
};
|