Merge pull request #6 from derdoktor667/dev

...Pause duration configuration
This commit is contained in:
Wastl Kraus 2025-07-19 13:28:26 +02:00 committed by GitHub
commit 314e888a38
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 124 additions and 81 deletions

View File

@ -1,4 +1,4 @@
name: Arduino CI
name: DShotRMT Example Sketch
on:
push:

View File

@ -11,28 +11,12 @@
// --- DShotRMT Class ---
// 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)
: _gpio(gpio), _mode(mode), _isBidirectional(isBidirectional) {}
DShotRMT::DShotRMT(gpio_num_t gpio, dshot_mode_t mode, bool isBidirectional, uint8_t pauseDuration)
: _gpio(gpio), _mode(mode), _isBidirectional(isBidirectional), _pauseDuration(pauseDuration) {}
// Sets up RMT TX and RX channels as well as encoder configuration
void DShotRMT::begin()
{
// TX RMT Channel Configuration
_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 = 2,
// .flags = {
// invert Signal if BiDirectional DShot Mode
// .invert_out = _isBidirectional,
// .with_dma = false}
};
rmt_new_tx_channel(&_rmt_tx_channel_config, &_rmt_tx_channel);
rmt_enable(_rmt_tx_channel);
// RX RMT Channel Configuration (for BiDirectional DShot)
if (_isBidirectional)
{
@ -41,28 +25,57 @@ void DShotRMT::begin()
.clk_src = DSHOT_CLOCK_SRC_DEFAULT,
.resolution_hz = DSHOT_RMT_RESOLUTION,
.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);
if (rmt_new_rx_channel(&_rmt_rx_channel_config, &_rmt_rx_channel) != 0)
{
Serial.println("Failed to create RX channel");
return;
}
if (rmt_enable(_rmt_rx_channel) != 0)
{
Serial.println("Failed to enable RX channel");
return;
}
_receive_config.signal_range_min_ns = 300;
_receive_config.signal_range_max_ns = 5000;
}
// TX RMT Channel Configuration
_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 = 10,
};
// Configure transmission looping
_transmit_config.loop_count = 0;
_transmit_config.flags.eot_level = _isBidirectional;
if (rmt_new_tx_channel(&_rmt_tx_channel_config, &_rmt_tx_channel) != 0)
{
Serial.println("Failed to create TX channel");
return;
}
if (rmt_enable(_rmt_tx_channel) != 0)
{
Serial.println("Failed to enable TX channel");
return;
}
// Use a copy encoder to send raw symbols
if (!_dshot_encoder)
{
rmt_copy_encoder_config_t enc_cfg = {};
rmt_new_copy_encoder(&enc_cfg, &_dshot_encoder);
if (rmt_new_copy_encoder(&enc_cfg, &_dshot_encoder) != 0)
{
Serial.println("Failed to create copy encoder");
return;
}
}
// Configure transmission looping
_transmit_config.loop_count = 0;
_transmit_config.flags.eot_level = _isBidirectional;
}
// Encodes and transmits a valid DShot Throttle value (48 - 2047)
@ -81,10 +94,14 @@ void DShotRMT::setThrottle(uint16_t throttle)
encodeDShotTX(_tx_packet, _tx_symbols, count);
// Send the packet
rmt_transmit(_rmt_tx_channel, _dshot_encoder, _tx_symbols, count * sizeof(rmt_symbol_word_t), &_transmit_config);
if (rmt_transmit(_rmt_tx_channel, _dshot_encoder, _tx_symbols, count * sizeof(rmt_symbol_word_t), &_transmit_config) != 0)
{
Serial.println("Failed to transmit DShot packet");
return;
}
// Take a break
esp_rom_delay_us(120);
esp_rom_delay_us(_pauseDuration);
}
// --- Get eRPM from ESC ---
@ -100,7 +117,6 @@ uint32_t DShotRMT::getERPM()
if (!rmt_receive(_rmt_rx_channel, _rx_symbols, sizeof(_rx_symbols), &_receive_config))
return _last_erpm;
//
_last_erpm = decodeDShotRX(_rx_symbols, DSHOT_BITS_PER_FRAME);
return _last_erpm;
}
@ -204,7 +220,7 @@ void DShotRMT::encodeDShotTX(uint16_t dshot_packet, rmt_symbol_word_t *symbols,
// Fill the 16 DShot-Bits Array with selected timings
for (int i = 15; i >= 0; i--)
{
bool bit = (dshot_packet >> i) & 0x01;
bool bit = (dshot_packet >> i) & 0b0000000000000001;
if (_isBidirectional)
{
symbols[count].level0 = 0;
@ -236,20 +252,21 @@ uint16_t DShotRMT::decodeDShotRX(const rmt_symbol_word_t *symbols, uint32_t coun
_rec_frame = (_rec_frame << 1) | bit;
}
// Store the received CRC for checking
// Cut the received CRC for checking
uint16_t _temp = _rec_frame >> 4;
// Masking the received CRC
uint8_t crc_recv = _rec_frame & 0x0F;
// Store the received CRC
uint8_t crc_recv = _rec_frame & 0b0000000000001111;
// Calculate CRC for received frame again
uint8_t crc_calc = (_temp ^ (_temp >> 4) ^ (_temp >> 8)) & 0b0000000000001111;
if (_isBidirectional)
crc_calc = (~crc_calc) & 0x0F;
crc_calc = (~crc_calc) & 0b0000000000001111;
// Checking CRC
if (crc_recv != crc_calc)
Serial.println("RX - CRC check failed.");
return _last_erpm;
// Cut "telemetric" bit leaving "raw" value

View File

@ -15,13 +15,10 @@
#include <driver/rmt_rx.h>
// --- DShot Protocol Constants ---
// Constants to define timing and encoding rules for DShot Protocol
static constexpr auto DSHOT_THROTTLE_FAILSAVE = 0;
static constexpr auto DSHOT_THROTTLE_MIN = 48;
static constexpr auto DSHOT_THROTTLE_MAX = 2047;
static constexpr auto DSHOT_BITS_PER_FRAME = 16;
static constexpr auto PAUSE_BITS = 21;
static constexpr auto SWITCH_PAUSE = 4;
static constexpr auto DSHOT_NULL_PACKET = 0b0000000000000000;
static constexpr auto DSHOT_FULL_PACKET = 0b1111111111111111;
@ -34,8 +31,15 @@ static constexpr auto DSHOT_RMT_RESOLUTION = 10 * 1000 * 1000; // 10 MHz Clock
static constexpr auto TX_BUFFER_SIZE = DSHOT_BITS_PER_FRAME;
static constexpr auto RX_BUFFER_SIZE = 32; // Padding for RX decoding
// DShot Packet
typedef struct dshot_packet_s
{
uint16_t throttle_value : 11;
bool telemetric_request : 1;
uint16_t checksum : 4;
} dshot_packet_t;
// --- DShot Mode Selection ---
// Select the appropriate bit timing for the protocol
typedef enum dshot_mode_e
{
DSHOT_OFF,
@ -46,13 +50,11 @@ typedef enum dshot_mode_e
} dshot_mode_t;
// --- DShotRMT Class ---
// 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.
class DShotRMT
{
public:
// Constructor: initializes configuration state
DShotRMT(gpio_num_t gpio, dshot_mode_t mode = DSHOT300, bool isBidirectional = false);
DShotRMT(gpio_num_t gpio, dshot_mode_t mode = DSHOT300, bool isBidirectional = false, uint8_t pauseDuration = 120);
// Initializes the RMT TX and RX channels
void begin();
@ -64,15 +66,17 @@ public:
uint32_t getERPM();
uint32_t getMotorRPM(uint8_t magnet_count);
// Accessors for GPIO and DShot mode
// 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; }
private:
// Calculate the checksum for throttle value
// Calculates the checksum for throttle value
uint16_t calculateCRC(uint16_t dshot_packet);
// Assamble DShot Paket (10 bit throttle + 1 bit telemetry request + 4 bit crc)
// Assembles DShot packet (11 bit throttle + 1 bit telemetry request + 4 bit crc)
uint16_t assambleDShotPaket(uint16_t value);
// Converts a 16-bit DShot packet into RMT symbols and appends pause
@ -81,18 +85,20 @@ private:
// Decodes the ESC answer
uint16_t decodeDShotRX(const rmt_symbol_word_t *symbols, uint32_t count);
// --- Configuration Parameter ---
// --- 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 _received_packet = DSHOT_NULL_PACKET;
uint16_t _rx_packet = DSHOT_NULL_PACKET;
uint16_t _tx_packet = DSHOT_NULL_PACKET;
uint8_t _packet_crc = 0;
dshot_packet_t _dshot_packet = {};
// --- RMT Channel ---
// --- RMT Channel Handles ---
rmt_channel_handle_t _rmt_rx_channel = nullptr;
rmt_channel_handle_t _rmt_tx_channel = nullptr;
rmt_rx_channel_config_t _rmt_rx_channel_config = {};
@ -105,7 +111,7 @@ private:
rmt_receive_config_t _receive_config = {};
rmt_transmit_config_t _transmit_config = {};
// --- RMT Symbol Buffer ---
// --- RMT Symbol Buffers ---
rmt_symbol_word_t _rx_symbols[RX_BUFFER_SIZE] = {};
rmt_symbol_word_t _tx_symbols[TX_BUFFER_SIZE] = {};

View File

@ -2,8 +2,8 @@
## DShotRMT - ESP32 Library (Rewrite for ESP-IDF 5)
This is a complete rewrite of the original DShotRMT library to support the new ESP-IDF 5 RMT encoder API (`rmt_tx.h`).
The library sends continuous DShot frames with a configurable pause between them and supports all standard DShot modes (150, 300, 600).
This is a complete rewrite of the original DShotRMT library to support the new ESP-IDF 5 RMT encoder API (`rmt_tx.h` / `rmt_rx.h`).
The library sends continuous DShot frames with a pause between them and supports all standard DShot modes (150, 300, 600).
### Now with BiDirectional DShot Support!!!
@ -27,7 +27,7 @@ Data is transmitted MSB-first. Pulse timing depends on the selected DShot mode.
| 300 | 300 kbit/s | 2.50 | 1.25 | 3.33 | ~53.28 |
| 600 | 600 kbit/s | 1.25 | 0.625 | 1.67 | ~26.72 |
Each frame is followed by a 21-bit time pause. This helps ESCs detect separate frames.
Each frame is followed by a pause. This helps ESCs detect separate frames.
![DShotRMT](https://raw.githubusercontent.com/derdoktor667/DShotRMT/refs/heads/main/img/dshot300.png)

View File

@ -26,6 +26,13 @@ constexpr auto MOTOR01_MAGNET_COUNT = 14;
// Setup Motor Pin, DShot Mode and optional BiDirectional Support
DShotRMT motor01(MOTOR01_PIN, DSHOT_MODE, IS_BIDIRECTIONAL);
// Prints RPM and throttle every 2 seconds if BiDirectional is enabled
void printRPMPeriodically(uint16_t throttle);
// Reads throttle value from serial input
uint16_t readSerialThrottle();
//
void setup()
{
// Start the USB Serial Port
@ -37,23 +44,61 @@ void setup()
// Arm ESC with minimum throttle
motor01.setThrottle(DSHOT_THROTTLE_MIN);
//
USB_SERIAL.println("**********************");
USB_SERIAL.println("DShotRMT Demo started.");
USB_SERIAL.println("Enter a throttle value (482047):");
}
//
void loop()
{
// Read value input from Serial
int throttle_input = readSerialThrottle();
uint16_t throttle_input = readSerialThrottle();
// Now send the value
// Send the value to the ESC
motor01.setThrottle(throttle_input);
// BiDirectional DShot: print out the received eRPMs every 2 seconds
// Print RPM if BiDirectional DShot is enabled
if (IS_BIDIRECTIONAL)
{
printRPMPeriodically(throttle_input);
}
}
// Reads throttle value from serial input
uint16_t readSerialThrottle()
{
static uint16_t last_throttle = DSHOT_THROTTLE_MIN;
if (USB_SERIAL.available() > 0)
{
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");
}
else
{
last_throttle = throttle_input;
USB_SERIAL.print("Throttle set to: ");
USB_SERIAL.println(last_throttle);
}
USB_SERIAL.println("*********************************");
USB_SERIAL.println("Enter a throttle value (482047):");
}
return last_throttle;
}
// Prints RPM and throttle every 2 seconds
void printRPMPeriodically(uint16_t throttle)
{
static unsigned long last_print_time = 0;
unsigned long now = millis();
@ -64,33 +109,8 @@ void loop()
uint32_t rpm = motor01.getMotorRPM(MOTOR01_MAGNET_COUNT);
USB_SERIAL.print("Throttle: ");
USB_SERIAL.print(throttle_input);
USB_SERIAL.print(throttle);
USB_SERIAL.print(" | RPM: ");
USB_SERIAL.println(rpm);
}
}
}
// Reads throttle value from serial input
int readSerialThrottle()
{
//
static int last_throttle = DSHOT_THROTTLE_MIN;
if (USB_SERIAL.available() > 0)
{
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);
last_throttle = throttle_input;
USB_SERIAL.print("Throttle set to: ");
USB_SERIAL.println(last_throttle);
USB_SERIAL.println("***********************************");
USB_SERIAL.println("Enter a throttle value (482047):");
}
return last_throttle;
}

View File

@ -1,5 +1,5 @@
name=DShotRMT
version=0.4.0
version=0.5.0
author=derdoktor667
maintainer=derdoktor667
sentence=DShotRMT Library supporting all DShot Types and speeds. Tested with BlHeli_S.