parent
66482aeadb
commit
33bfb6a0be
23
DShotRMT.cpp
23
DShotRMT.cpp
|
|
@ -13,7 +13,7 @@ DShotRMT::DShotRMT(gpio_num_t gpio, dshot_mode_t mode, bool isBidirectional)
|
||||||
|
|
||||||
void DShotRMT::begin()
|
void DShotRMT::begin()
|
||||||
{
|
{
|
||||||
rmt_tx_channel_config_t tx_config = {
|
rmt_tx_channel_config_t rmt_tx_channel_config = {
|
||||||
.gpio_num = _gpio,
|
.gpio_num = _gpio,
|
||||||
.clk_src = RMT_CLK_SRC_DEFAULT,
|
.clk_src = RMT_CLK_SRC_DEFAULT,
|
||||||
.resolution_hz = DEFAULT_RES_HZ,
|
.resolution_hz = DEFAULT_RES_HZ,
|
||||||
|
|
@ -23,8 +23,8 @@ void DShotRMT::begin()
|
||||||
.invert_out = _isBidirectional,
|
.invert_out = _isBidirectional,
|
||||||
.with_dma = false}};
|
.with_dma = false}};
|
||||||
|
|
||||||
rmt_new_tx_channel(&tx_config, &_channel);
|
rmt_new_tx_channel(&rmt_tx_channel_config, &_rmt_channel);
|
||||||
rmt_enable(_channel);
|
rmt_enable(_rmt_channel);
|
||||||
|
|
||||||
// Create encoder only once
|
// Create encoder only once
|
||||||
if (!_encoder)
|
if (!_encoder)
|
||||||
|
|
@ -33,19 +33,22 @@ void DShotRMT::begin()
|
||||||
rmt_new_copy_encoder(&enc_cfg, &_encoder);
|
rmt_new_copy_encoder(&enc_cfg, &_encoder);
|
||||||
}
|
}
|
||||||
|
|
||||||
_tx_config.loop_count = -1; // Infinite loop
|
_transmit_config.loop_count = -1; // Infinite loop
|
||||||
_tx_config.flags.eot_level = 0;
|
_transmit_config.flags.eot_level = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DShotRMT::setThrottle(uint16_t throttle, bool telemetry)
|
void DShotRMT::setThrottle(uint16_t throttle, bool telemetry)
|
||||||
{
|
{
|
||||||
|
// Send only new Throttle values
|
||||||
|
static uint16_t _lastThrottle = 0;
|
||||||
|
|
||||||
// Clamp to 11 bits
|
// Clamp to 11 bits
|
||||||
throttle &= 0x07FF;
|
throttle &= 0x07FF;
|
||||||
if (throttle == _lastThrottle && telemetry == _lastTelemetry)
|
|
||||||
|
if (throttle == _lastThrottle)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_lastThrottle = throttle;
|
_lastThrottle = throttle;
|
||||||
_lastTelemetry = telemetry;
|
|
||||||
|
|
||||||
// Build 16-bit DShot packet
|
// Build 16-bit DShot packet
|
||||||
uint16_t packet = (throttle << 1) | (telemetry ? 1 : 0);
|
uint16_t packet = (throttle << 1) | (telemetry ? 1 : 0);
|
||||||
|
|
@ -59,10 +62,10 @@ void DShotRMT::setThrottle(uint16_t throttle, bool telemetry)
|
||||||
buildFrameSymbols(packet, symbols, count);
|
buildFrameSymbols(packet, symbols, count);
|
||||||
|
|
||||||
// Transmit
|
// Transmit
|
||||||
rmt_disable(_channel); // Ensure safe restart
|
rmt_disable(_rmt_channel); // Ensure safe restart
|
||||||
rmt_enable(_channel);
|
rmt_enable(_rmt_channel);
|
||||||
|
|
||||||
rmt_transmit(_channel, _encoder, symbols, count * sizeof(rmt_symbol_word_t), &_tx_config);
|
rmt_transmit(_rmt_channel, _encoder, symbols, count * sizeof(rmt_symbol_word_t), &_transmit_config);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DShotRMT::buildFrameSymbols(uint16_t dshot_packet, rmt_symbol_word_t *symbols, size_t &count)
|
void DShotRMT::buildFrameSymbols(uint16_t dshot_packet, rmt_symbol_word_t *symbols, size_t &count)
|
||||||
|
|
|
||||||
|
|
@ -42,12 +42,9 @@ private:
|
||||||
dshot_mode_t _mode;
|
dshot_mode_t _mode;
|
||||||
bool _isBidirectional;
|
bool _isBidirectional;
|
||||||
|
|
||||||
rmt_channel_handle_t _channel = nullptr;
|
rmt_channel_handle_t _rmt_channel = nullptr;
|
||||||
rmt_encoder_handle_t _encoder = nullptr;
|
rmt_encoder_handle_t _encoder = nullptr;
|
||||||
rmt_transmit_config_t _tx_config = {};
|
rmt_transmit_config_t _transmit_config = {};
|
||||||
|
|
||||||
uint16_t _lastThrottle = 0;
|
|
||||||
bool _lastTelemetry = false;
|
|
||||||
|
|
||||||
void buildFrameSymbols(uint16_t frame, rmt_symbol_word_t *symbols, size_t &count);
|
void buildFrameSymbols(uint16_t frame, rmt_symbol_word_t *symbols, size_t &count);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -10,12 +10,12 @@
|
||||||
#include <DShotRMT.h>
|
#include <DShotRMT.h>
|
||||||
|
|
||||||
// USB serial port settings
|
// USB serial port settings
|
||||||
#define USB_Serial Serial
|
#define USB_Serial Serial0
|
||||||
const uint32_t USB_SERIAL_BAUD = 115200;
|
constexpr auto USB_SERIAL_BAUD = 115200;
|
||||||
|
|
||||||
// Motor configuration
|
// Motor configuration
|
||||||
const gpio_num_t MOTOR01_PIN = GPIO_NUM_17;
|
constexpr auto MOTOR01_PIN = GPIO_NUM_17;
|
||||||
const dshot_mode_t DSHOT_MODE = DSHOT300;
|
constexpr auto DSHOT_MODE = DSHOT300;
|
||||||
|
|
||||||
// Create DShotRMT instance
|
// Create DShotRMT instance
|
||||||
DShotRMT motor01(MOTOR01_PIN, DSHOT_MODE);
|
DShotRMT motor01(MOTOR01_PIN, DSHOT_MODE);
|
||||||
|
|
@ -48,6 +48,7 @@ void loop()
|
||||||
// Reads throttle value from serial input
|
// Reads throttle value from serial input
|
||||||
int readSerialThrottle()
|
int readSerialThrottle()
|
||||||
{
|
{
|
||||||
|
//
|
||||||
static int last_throttle = DSHOT_THROTTLE_MIN;
|
static int last_throttle = DSHOT_THROTTLE_MIN;
|
||||||
|
|
||||||
if (USB_Serial.available() > 0)
|
if (USB_Serial.available() > 0)
|
||||||
|
|
@ -64,5 +65,6 @@ int readSerialThrottle()
|
||||||
|
|
||||||
USB_Serial.println("Enter a throttle value (48–2047):");
|
USB_Serial.println("Enter a throttle value (48–2047):");
|
||||||
}
|
}
|
||||||
|
|
||||||
return last_throttle;
|
return last_throttle;
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue