Merge pull request #8 from derdoktor667/dev

...DShot mode selection reworked and more
This commit is contained in:
Wastl Kraus 2025-07-30 14:49:25 +02:00 committed by GitHub
commit 2a34960e02
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 134 additions and 141 deletions

View File

@ -12,8 +12,18 @@
// This class provides an abstraction for sending and optionally receiving DShot frames.
// It uses ESP32's RMT peripheral for precise timing control, including BiDirectional RX.
DShotRMT::DShotRMT(gpio_num_t gpio, dshot_mode_t mode, bool isBidirectional, uint8_t pauseDuration)
: _gpio(gpio), _mode(mode), _isBidirectional(isBidirectional), _pauseDuration(pauseDuration) {}
DShotRMT::DShotRMT(gpio_num_t gpio, dshot_mode_t mode, bool isBidirectional)
: _gpio(gpio), _mode(mode), _isBidirectional(isBidirectional)
{
// Fixed Timings for compatibility
_frame_time = dshot_times.frameLength + DSHOT_SWITCH_TIME;
// DShot Frame answer padding
if (_isBidirectional)
{
_frame_time += _frame_time;
}
}
// Initializes RMT TX and RX channels and encoder configuration
void DShotRMT::begin()
@ -25,7 +35,7 @@ void DShotRMT::begin()
.gpio_num = _gpio,
.clk_src = DSHOT_CLOCK_SRC_DEFAULT,
.resolution_hz = DSHOT_RMT_RESOLUTION,
.mem_block_symbols = 64,
.mem_block_symbols = DSHOT_SYMBOLS_SIZE,
};
if (rmt_new_rx_channel(&_rmt_rx_channel_config, &_rmt_rx_channel) != 0)
@ -33,6 +43,7 @@ void DShotRMT::begin()
Serial.println("Failed to create RX channel");
return;
}
if (rmt_enable(_rmt_rx_channel) != 0)
{
Serial.println("Failed to enable RX channel");
@ -48,12 +59,11 @@ void DShotRMT::begin()
.gpio_num = _gpio,
.clk_src = DSHOT_CLOCK_SRC_DEFAULT,
.resolution_hz = DSHOT_RMT_RESOLUTION,
.mem_block_symbols = 64,
.trans_queue_depth = 10,
};
.mem_block_symbols = DSHOT_SYMBOLS_SIZE,
.trans_queue_depth = TX_BUFFER_SIZE};
// Transmission configuration
_transmit_config.loop_count = 0;
_transmit_config.loop_count = NULL;
_transmit_config.flags.eot_level = _isBidirectional;
if (rmt_new_tx_channel(&_rmt_tx_channel_config, &_rmt_tx_channel) != 0)
@ -61,6 +71,7 @@ void DShotRMT::begin()
Serial.println("Failed to create TX channel");
return;
}
if (rmt_enable(_rmt_tx_channel) != 0)
{
Serial.println("Failed to enable TX channel");
@ -82,27 +93,33 @@ void DShotRMT::begin()
// Encodes and transmits a valid DShot throttle value (48 - 2047)
void DShotRMT::setThrottle(uint16_t throttle)
{
// Clamp input range and mask to 11 bits
throttle = constrain(throttle, DSHOT_THROTTLE_MIN, DSHOT_THROTTLE_MAX) & 0x7FF;
// Simple timer
static unsigned long last_time = NULL;
_lastThrottle = throttle;
//
dshot_packet_t packet = {};
// Convert throttle value to DShot packet format
_tx_packet = assambleDShotPaket(_lastThrottle);
packet.throttle_value = (constrain(throttle, DSHOT_THROTTLE_MIN, DSHOT_THROTTLE_MAX) & 0b0000011111111111);
packet.telemetric_request = _isBidirectional;
packet.checksum = calculateCRC(packet);
// Encode RMT symbols
size_t count = 0;
encodeDShotTX(_tx_packet, _tx_symbols, count);
// DShot transcoding
rmt_symbol_word_t tx_symbols[DSHOT_BITS_PER_FRAME] = {};
encodeDShotTX(packet, tx_symbols);
// Transmit the packet
if (rmt_transmit(_rmt_tx_channel, _dshot_encoder, _tx_symbols, count * sizeof(rmt_symbol_word_t), &_transmit_config) != 0)
// Ensure frame lenght for compatibility
if (micros() - last_time >= _frame_time)
{
Serial.println("Failed to transmit DShot packet");
return;
}
// Transmit the packet
if (rmt_transmit(_rmt_tx_channel, _dshot_encoder, tx_symbols, DSHOT_SYMBOLS_SIZE, &_transmit_config) != 0)
{
Serial.println("Failed to transmit DShot packet");
return;
}
// Pause between frames
esp_rom_delay_us(_pauseDuration);
// Timestamp
last_time = micros();
}
}
// Receives and decodes a response frame from ESC containing eRPM info
@ -111,13 +128,17 @@ uint32_t DShotRMT::getERPM()
if (_isBidirectional)
{
if (_rmt_rx_channel == nullptr)
{
Serial.println("No bidirectional DShot support.");
return _last_erpm;
}
// Try to receive a new frame
if (!rmt_receive(_rmt_rx_channel, _rx_symbols, sizeof(_rx_symbols), &_receive_config))
if (!rmt_receive(_rmt_rx_channel, _rx_symbols, DSHOT_SYMBOLS_SIZE, &_receive_config))
{
Serial.println("No valid DShot frame received");
return _last_erpm;
}
_last_erpm = decodeDShotRX(_rx_symbols, DSHOT_BITS_PER_FRAME);
return _last_erpm;
@ -138,100 +159,53 @@ uint32_t DShotRMT::getMotorRPM(uint8_t magnet_count)
}
// Calculates CRC for DShot packet
uint16_t DShotRMT::calculateCRC(uint16_t dshot_packet)
uint16_t DShotRMT::calculateCRC(dshot_packet_t dshot_packet)
{
uint16_t packet = (dshot_packet << 1) | (_isBidirectional ? 1 : 0);
// Reset CRC container
_packet_crc = DSHOT_NULL_PACKET;
uint16_t crc = (dshot_packet.throttle_value << 1) | (dshot_packet.telemetric_request);
// CRC calculation for DShot (4 bits)
_packet_crc = ((packet ^ (packet >> 4) ^ (packet >> 8)) & 0xF);
dshot_packet.checksum = ((crc ^ (crc >> 4) ^ (crc >> 8)) & 0b0000000000001111);
// CRC is inverted for bidirectional DShot
if (_isBidirectional)
_packet_crc = (~_packet_crc) & 0xF;
if (dshot_packet.telemetric_request)
dshot_packet.checksum = (~dshot_packet.checksum) & 0b0000000000001111;
return _packet_crc;
return dshot_packet.checksum;
}
// Assembles DShot packet (11 bit throttle + 1 bit telemetry request + 4 bit CRC)
uint16_t DShotRMT::assambleDShotPaket(uint16_t value)
uint16_t DShotRMT::parseDShotPacket(const dshot_packet_t dshot_packet)
{
uint16_t throttle = value & 0x7FF;
// Reset packet container
_tx_packet = DSHOT_NULL_PACKET;
// Assemble raw DShot packet and add checksum
_packet_crc = calculateCRC(throttle);
_tx_packet = (throttle << 1) | (_isBidirectional ? 1 : 0);
_tx_packet = (_tx_packet << 4) | _packet_crc;
return _tx_packet;
uint16_t raw = (((dshot_packet.throttle_value << 1) | (dshot_packet.telemetric_request)) & 0b0000111111111111);
return (((raw << 4) | (dshot_packet.checksum)) & 0b1111111111111111);
}
// Converts a 16-bit packet into a valid DShot frame for RMT
void DShotRMT::encodeDShotTX(uint16_t dshot_packet, rmt_symbol_word_t *symbols, size_t &count)
void DShotRMT::encodeDShotTX(dshot_packet_t dshot_packet, rmt_symbol_word_t *symbols)
{
count = 0;
// Encoding to "raw" DShot Packet
uint16_t frame_bits = parseDShotPacket(dshot_packet);
uint32_t ticks_per_bit = 0;
uint32_t ticks_zero_high = 0;
uint32_t ticks_one_high = 0;
// Always start with the "first" bit
size_t count = NULL;
// Select timing based on DShot mode
switch (_mode)
// Convert the parsed dshot frame to rmt_tx data
for (int i = DSHOT_BITS_PER_FRAME - 1; i >= 0; i--)
{
case DSHOT150:
ticks_per_bit = 64;
ticks_zero_high = 24;
ticks_one_high = 48;
break;
case DSHOT300:
ticks_per_bit = 32;
ticks_zero_high = 12;
ticks_one_high = 24;
break;
case DSHOT600:
ticks_per_bit = 16;
ticks_zero_high = 6;
ticks_one_high = 12;
break;
case DSHOT1200:
ticks_per_bit = 8;
ticks_zero_high = 3;
ticks_one_high = 6;
break;
case DSHOT_OFF:
default:
ticks_per_bit = 0;
ticks_zero_high = 0;
ticks_one_high = 0;
break;
}
uint32_t ticks_zero_low = ticks_per_bit - ticks_zero_high;
uint32_t ticks_one_low = ticks_per_bit - ticks_one_high;
// Fill the 16 DShot bits array with selected timings
for (int i = 15; i >= 0; i--)
{
bool bit = (dshot_packet >> i) & 0x1;
bool bit = (frame_bits >> i) & 0b0000000000000001;
if (_isBidirectional)
{
symbols[count].level0 = 0;
symbols[count].duration0 = bit ? ticks_one_high : ticks_zero_high;
symbols[count].duration0 = bit ? dshot_times.ticks_one_high : dshot_times.ticks_zero_high;
symbols[count].level1 = 1;
symbols[count].duration1 = bit ? ticks_one_low : ticks_zero_low;
symbols[count].duration1 = bit ? dshot_times.ticks_one_low : dshot_times.ticks_zero_low;
}
else
{
symbols[count].level0 = 1;
symbols[count].duration0 = bit ? ticks_one_high : ticks_zero_high;
symbols[count].duration0 = bit ? dshot_times.ticks_one_high : dshot_times.ticks_zero_high;
symbols[count].level1 = 0;
symbols[count].duration1 = bit ? ticks_one_low : ticks_zero_low;
symbols[count].duration1 = bit ? dshot_times.ticks_one_low : dshot_times.ticks_zero_low;
}
count++;
}
@ -251,12 +225,12 @@ uint16_t DShotRMT::decodeDShotRX(const rmt_symbol_word_t *symbols, uint32_t coun
// Extract CRC and payload
uint16_t payload = received_frame >> 4;
uint8_t crc_received = received_frame & 0xF;
uint8_t crc_received = received_frame & 0b0000000000001111;
// Calculate CRC for received frame
uint8_t crc_calculated = (payload ^ (payload >> 4) ^ (payload >> 8)) & 0xF;
uint8_t crc_calculated = (payload ^ (payload >> 4) ^ (payload >> 8)) & 0b0000000000001111;
if (_isBidirectional)
crc_calculated = (~crc_calculated) & 0xF;
crc_calculated = (~crc_calculated) & 0b0000000000001111;
// Check CRC
if (crc_received != crc_calculated)
@ -265,8 +239,6 @@ uint16_t DShotRMT::decodeDShotRX(const rmt_symbol_word_t *symbols, uint32_t coun
return _last_erpm;
}
// Remove telemetry bit, keep raw value
uint16_t raw = payload >> 1;
return _last_erpm = raw;
// Remove telemetry bit
return _last_erpm = payload >> 1;
}

View File

@ -14,25 +14,16 @@
#include <driver/rmt_tx.h>
#include <driver/rmt_rx.h>
static constexpr bool DSHOT_OK = 0;
static constexpr bool DSHOT_FAILED = 1;
// --- DShot Protocol Constants ---
static constexpr uint16_t DSHOT_THROTTLE_FAILSAVE = 0;
static constexpr uint16_t DSHOT_THROTTLE_MIN = 48;
static constexpr uint16_t DSHOT_THROTTLE_MAX = 2047;
static constexpr uint8_t DSHOT_BITS_PER_FRAME = 16;
static constexpr uint16_t DSHOT_NULL_PACKET = 0x0000;
static constexpr uint16_t DSHOT_FULL_PACKET = 0xFFFF;
static constexpr uint16_t NO_ERPM_SIGNAL = 0;
// RMT configuration parameters
static constexpr rmt_clock_source_t DSHOT_CLOCK_SRC_DEFAULT = RMT_CLK_SRC_DEFAULT;
static constexpr uint32_t DSHOT_RMT_RESOLUTION = 10 * 1000 * 1000; // 10 MHz Clock
static constexpr size_t TX_BUFFER_SIZE = DSHOT_BITS_PER_FRAME;
static constexpr size_t RX_BUFFER_SIZE = 32; // Padding for RX decoding
// DShot Packet structure
typedef struct
typedef struct dshot_packet_s
{
uint16_t throttle_value : 11;
bool telemetric_request : 1;
@ -40,7 +31,7 @@ typedef struct
} dshot_packet_t;
// --- DShot Mode Selection ---
typedef enum
typedef enum dshot_mode_s
{
DSHOT_OFF,
DSHOT150,
@ -49,12 +40,33 @@ typedef enum
DSHOT1200
} dshot_mode_t;
// --- DShot Timings ---
typedef struct dshot_timing_s
{
uint16_t frameLength;
uint16_t ticks_per_bit;
uint16_t ticks_one_high;
uint16_t ticks_zero_high;
uint16_t ticks_zero_low;
uint16_t ticks_one_low;
} dshot_timing_t;
// DShot Pulse Length Settings
const dshot_timing_t dshot_timings[] = {
{0, 0, 0, 0, 0, 0}, // DSHOT_OFF
{128, 64, 48, 24, 40, 16}, // DSHOT150
{64, 32, 24, 12, 20, 8}, // DSHOT300
{32, 16, 12, 6, 10, 4}, // DSHOT600
{16, 8, 6, 3, 5, 2} // DSHOT1200
};
//
// --- DShotRMT Class ---
class DShotRMT
{
public:
// Constructor: initializes configuration state
DShotRMT(gpio_num_t gpio, dshot_mode_t mode = DSHOT300, bool isBidirectional = false, uint8_t pauseDuration = 120);
DShotRMT(gpio_num_t gpio, dshot_mode_t mode = DSHOT300, bool isBidirectional = false);
// Initializes the RMT TX and RX channels
void begin();
@ -69,33 +81,44 @@ public:
// Accessors for GPIO and DShot settings
gpio_num_t getGPIO() const { return _gpio; }
dshot_mode_t getDShotMode() const { return _mode; }
uint8_t getPauseDuration() const { return _pauseDuration; }
void setPauseDuration(uint8_t pauseDuration) { _pauseDuration = pauseDuration; }
// --- Configuration Parameters ---
gpio_num_t _gpio = GPIO_NUM_NC;
dshot_mode_t _mode = DSHOT_OFF;
bool _isBidirectional = false;
uint16_t _frame_time;
const dshot_timing_t &dshot_times = dshot_timings[_mode];
private:
// Calculates the checksum for a DShot packet
uint16_t calculateCRC(uint16_t dshot_packet);
static constexpr uint8_t DSHOT_BITS_PER_FRAME = 16;
static constexpr uint8_t DSHOT_SWITCH_TIME = 42;
// Assembles DShot packet (11 bit throttle + 1 bit telemetry request + 4 bit CRC)
uint16_t assambleDShotPaket(uint16_t value);
static constexpr uint16_t DSHOT_NULL_PACKET = 0b000000000000000;
static constexpr uint16_t DSHOT_FULL_PACKET = 0b111111111111111;
static constexpr uint16_t NO_ERPM_SIGNAL = 0;
// RMT configuration parameters
static constexpr rmt_clock_source_t DSHOT_CLOCK_SRC_DEFAULT = RMT_CLK_SRC_DEFAULT;
static constexpr uint32_t DSHOT_RMT_RESOLUTION = 10 * 1000 * 1000; // 10 MHz Clock
static constexpr size_t TX_BUFFER_SIZE = DSHOT_BITS_PER_FRAME;
static constexpr size_t RX_BUFFER_SIZE = 32;
static constexpr size_t DSHOT_SYMBOLS_SIZE = 64;
// Calculates the checksum for a DShot packet
uint16_t calculateCRC(dshot_packet_t dshot_packet);
// Parses the DShot packet (11 bit throttle + 1 bit telemetry request + 4 bit CRC)
uint16_t parseDShotPacket(const dshot_packet_t dshot_packet);
// Converts a 16-bit DShot packet into RMT symbols
void encodeDShotTX(uint16_t dshot_packet, rmt_symbol_word_t *symbols, size_t &count);
void encodeDShotTX(dshot_packet_t dshot_packet, rmt_symbol_word_t *symbols);
// Decodes the ESC answer
uint16_t decodeDShotRX(const rmt_symbol_word_t *symbols, uint32_t count);
// --- Configuration Parameters ---
gpio_num_t _gpio;
dshot_mode_t _mode;
bool _isBidirectional;
uint8_t _pauseDuration;
// --- DShot Packets Container ---
uint16_t _lastThrottle = DSHOT_NULL_PACKET;
uint16_t _rx_packet = DSHOT_NULL_PACKET;
uint16_t _tx_packet = DSHOT_NULL_PACKET;
uint8_t _packet_crc = 0;
// --- RMT Channel Handles ---
rmt_channel_handle_t _rmt_rx_channel = nullptr;
@ -112,8 +135,7 @@ private:
// --- RMT Symbol Buffers ---
rmt_symbol_word_t _rx_symbols[RX_BUFFER_SIZE] = {};
rmt_symbol_word_t _tx_symbols[TX_BUFFER_SIZE] = {};
// Stores the last valid eRPM received from the ESC
uint16_t _last_erpm = 0;
uint16_t _last_erpm = NULL;
};

View File

@ -114,7 +114,7 @@ Perfect for DShot:
## 📝 API Reference
- `DShotRMT(gpio_num_t gpio, dshot_mode_t mode, bool isBidirectional, uint8_t pauseDuration = 120)`
- `DShotRMT(gpio_num_t gpio, dshot_mode_t mode, bool isBidirectional)`
- `void begin()`
- `void setThrottle(uint16_t throttle)`
- `uint32_t getERPM()`

View File

@ -32,6 +32,7 @@ void printRPMPeriodically(uint16_t throttle);
// Reads throttle value from serial input
uint16_t readSerialThrottle();
//
void setup()
{
// Start the USB Serial Port
@ -45,9 +46,10 @@ void setup()
USB_SERIAL.println("**********************");
USB_SERIAL.println("DShotRMT Demo started.");
USB_SERIAL.println("Enter a throttle value (482047):");
USB_SERIAL.println("Enter a throttle value (48 2047):");
}
//
void loop()
{
// Read value input from Serial
@ -73,12 +75,10 @@ uint16_t readSerialThrottle()
String input = USB_SERIAL.readStringUntil('\n');
int throttle_input = input.toInt();
// Clamp the value to the DShot range
throttle_input = constrain(throttle_input, DSHOT_THROTTLE_MIN, DSHOT_THROTTLE_MAX);
if (throttle_input < DSHOT_THROTTLE_MIN || throttle_input > DSHOT_THROTTLE_MAX)
{
USB_SERIAL.println("Invalid input. Please enter a value between 48 and 2047.");
return last_throttle;
}
else
{
@ -88,7 +88,7 @@ uint16_t readSerialThrottle()
}
USB_SERIAL.println("*********************************");
USB_SERIAL.println("Enter a throttle value (482047):");
USB_SERIAL.println("Enter a throttle value (48 2047):");
}
return last_throttle;
@ -98,17 +98,16 @@ uint16_t readSerialThrottle()
void printRPMPeriodically(uint16_t throttle)
{
static unsigned long last_print_time = 0;
unsigned long now = millis();
if (now - last_print_time >= 2000)
if (millis() - last_print_time >= 2000)
{
last_print_time = now;
uint32_t rpm = motor01.getMotorRPM(MOTOR01_MAGNET_COUNT);
USB_SERIAL.print("Throttle: ");
USB_SERIAL.print(throttle);
USB_SERIAL.print(" | RPM: ");
USB_SERIAL.println(rpm);
last_print_time = millis();
}
}