DShotRMT/DShotRMT.cpp

218 lines
6.3 KiB
C++
Raw Normal View History

/**
* @file DShotRMT.cpp
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
* @author Wastl Kraus
* @date 2025-06-11
* @license MIT
*/
2021-07-04 02:01:26 +01:00
#include <DShotRMT.h>
2021-06-29 19:05:20 +01:00
2025-06-12 23:45:48 +01:00
//
DShotRMT::DShotRMT(gpio_num_t gpio, dshot_mode_t mode, bool isBidirectional)
: _gpio(gpio), _mode(mode), _isBidirectional(isBidirectional) {}
2025-06-13 20:50:00 +01:00
// Sets up RMT TX and RX channels as well as encoder configuration
void DShotRMT::begin()
2022-11-25 15:08:58 +00:00
{
2025-06-13 20:50:00 +01:00
// RX RMT Channel Configuration (for BiDirectional DShot)
2025-06-12 23:45:48 +01:00
if (_isBidirectional)
{
rmt_rx_channel_config_t rmt_rx_channel_config = {
.gpio_num = _gpio,
.clk_src = DSHOT_CLOCK_SRC_DEFAULT,
.resolution_hz = DSHOT_RMT_RESOLUTION,
2025-06-12 23:45:48 +01:00
.mem_block_symbols = 64,
.flags = {
.invert_in = false,
.with_dma = false}};
rmt_new_rx_channel(&rmt_rx_channel_config, &_rmt_rx_channel);
rmt_enable(_rmt_rx_channel);
2025-06-13 20:50:00 +01:00
_receive_config.signal_range_min_ns = 100;
_receive_config.signal_range_max_ns = 10000;
2025-06-12 23:45:48 +01:00
}
2025-06-13 20:50:00 +01:00
// TX RMT Channel Configuration
rmt_tx_channel_config_t rmt_tx_channel_config = {
.gpio_num = _gpio,
.clk_src = DSHOT_CLOCK_SRC_DEFAULT,
.resolution_hz = DSHOT_RMT_RESOLUTION,
.mem_block_symbols = 64,
.trans_queue_depth = 1,
.flags = {
2025-06-12 23:45:48 +01:00
// invert Signal if BiDirectional DShot Mode
.invert_out = _isBidirectional,
.with_dma = false}};
2025-06-12 23:45:48 +01:00
rmt_new_tx_channel(&rmt_tx_channel_config, &_rmt_tx_channel);
rmt_enable(_rmt_tx_channel);
2025-06-13 20:50:00 +01:00
// Use a copy encoder to send raw symbols
2025-06-12 23:45:48 +01:00
if (!_dshot_encoder)
{
rmt_copy_encoder_config_t enc_cfg = {};
2025-06-12 23:45:48 +01:00
rmt_new_copy_encoder(&enc_cfg, &_dshot_encoder);
}
2021-06-29 19:05:20 +01:00
2025-06-13 20:50:00 +01:00
// Configure transmission looping
2025-06-12 23:45:48 +01:00
_transmit_config.loop_count = -1;
_transmit_config.flags.eot_level = _isBidirectional;
2021-06-29 19:05:20 +01:00
}
2025-06-13 20:50:00 +01:00
// Encodes and transmits a valid DShot Throttle value (48 - 2047)
2025-06-12 23:45:48 +01:00
void DShotRMT::setThrottle(uint16_t throttle)
2022-11-25 15:08:58 +00:00
{
2025-06-13 20:50:00 +01:00
// Safety first - double check input range and 10 bit "translation"
throttle = (constrain(throttle, DSHOT_THROTTLE_MIN, DSHOT_THROTTLE_MAX)) & 0b0000011111111111;
2025-06-12 23:45:48 +01:00
// Has Throttle really changed?
if (throttle == _lastThrottle)
return;
2021-06-29 19:05:20 +01:00
_lastThrottle = throttle;
2021-06-29 19:05:20 +01:00
2025-06-13 20:50:00 +01:00
// Assemble raw DShot packet and compute checksum
_tx_packet = (throttle << 1) | (_isBidirectional ? 1 : 0);
2025-06-12 23:45:48 +01:00
2025-06-13 20:50:00 +01:00
uint16_t crc = _isBidirectional
? (~(_tx_packet ^ (_tx_packet >> 4) ^ (_tx_packet >> 8))) & 0x0F
: (_tx_packet ^ (_tx_packet >> 4) ^ (_tx_packet >> 8)) & 0x0F;
2025-06-12 23:45:48 +01:00
2025-06-13 20:50:00 +01:00
_tx_packet = (_tx_packet << 4) | crc;
2024-02-11 09:44:56 +00:00
2025-06-13 20:50:00 +01:00
// Encode RMT symbols
size_t count = 0;
2025-06-13 20:50:00 +01:00
encodeDShotTX(_tx_packet, _tx_symbols, count);
2024-02-11 09:44:56 +00:00
2025-06-13 20:50:00 +01:00
// Restart transmission with new data
2025-06-12 23:45:48 +01:00
rmt_disable(_rmt_tx_channel);
rmt_enable(_rmt_tx_channel);
2025-06-13 20:50:00 +01:00
rmt_transmit(_rmt_tx_channel, _dshot_encoder, _tx_symbols, count * sizeof(rmt_symbol_word_t), &_transmit_config);
2021-06-29 19:05:20 +01:00
}
2025-06-13 20:50:00 +01:00
// --- Get eRPM from ESC ---
// Receives and decodes a response frame from ESC containing eRPM info
uint32_t DShotRMT::getERPM()
{
2025-06-13 20:50:00 +01:00
static size_t rx_size = sizeof(_rx_symbols);
if (_rmt_rx_channel == nullptr)
return _last_erpm;
// Attempt to receive a new frame
if (!rmt_receive(_rmt_rx_channel, _rx_symbols, rx_size, &_receive_config))
return _last_erpm;
uint16_t received_bits = 0;
_received_packet = 0;
// Decode raw RMT encoded bits
for (int i = 0; i < DSHOT_BITS_PER_FRAME; ++i)
{
rmt_symbol_word_t symbols = _rx_symbols[i];
// Validate signal polarity
if (symbols.level0 != 1 || symbols.level1 != 0)
break;
uint32_t total_ticks = symbols.duration0 + symbols.duration1;
bool bit = (symbols.duration0 > (total_ticks / 2));
_received_packet <<= 1;
_received_packet |= bit ? 1 : 0;
received_bits++;
}
if (received_bits < 16)
return _last_erpm;
// Extract data & checksum from packet
uint16_t packet_data = _received_packet >> 4;
uint8_t recalc_packet_crc = (packet_data ^ (packet_data >> 4) ^ (packet_data >> 8)) & 0x0F;
uint8_t packet_crc = _received_packet & 0x0F;
if (recalc_packet_crc != packet_crc)
return _last_erpm;
// Assume received value is DShot eRPM
uint16_t throttle = packet_data >> 1;
// Filter noise values
if (throttle < DSHOT_THROTTLE_MIN || throttle > DSHOT_THROTTLE_MAX)
return _last_erpm;
// Approximate eRPM (ESC dependent, scale factor can be tuned)
_last_erpm = throttle * 100;
return _last_erpm;
}
// --- Encode DShot TX Frame ---
// 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)
{
// Always start encoding from the top
count = 0;
//
uint32_t ticks_per_bit = 0;
uint32_t ticks_zero_high = 0;
uint32_t ticks_one_high = 0;
switch (_mode)
{
case DSHOT150:
2025-06-12 23:45:48 +01:00
ticks_per_bit = 64;
ticks_zero_high = 24;
ticks_one_high = 48;
break;
case DSHOT300:
2025-06-12 23:45:48 +01:00
ticks_per_bit = 32;
ticks_zero_high = 12;
2025-06-12 23:45:48 +01:00
ticks_one_high = 24;
break;
case DSHOT600:
2025-06-12 23:45:48 +01:00
ticks_per_bit = 16;
ticks_zero_high = 6;
2025-06-12 23:45:48 +01:00
ticks_one_high = 12;
break;
case DSHOT1200:
ticks_per_bit = 8;
ticks_zero_high = 3;
ticks_one_high = 6;
break;
2025-06-13 20:50:00 +01:00
// Safety first
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;
2023-04-15 06:45:14 +01:00
// Fill the 16 DShot-Bits Array with selected timings
for (int i = 15; i >= 0; i--)
2023-04-15 06:45:14 +01:00
{
bool bit = (dshot_packet >> i) & 0x01;
symbols[count].level0 = 1;
symbols[count].duration0 = bit ? ticks_one_high : ticks_zero_high;
symbols[count].level1 = 0;
symbols[count].duration1 = bit ? ticks_one_low : ticks_zero_low;
count++;
2023-04-15 06:45:14 +01:00
}
// Append the Pause Bits
symbols[count].level0 = 0;
symbols[count].duration0 = ticks_per_bit * PAUSE_BITS;
symbols[count].level1 = 0;
symbols[count].duration1 = 0;
count++;
2021-06-29 19:05:20 +01:00
}