parent
4db92a9b31
commit
90eaecd61d
47
DShotRMT.cpp
47
DShotRMT.cpp
|
|
@ -12,8 +12,38 @@
|
||||||
// This class provides an abstraction for sending and optionally receiving DShot frames.
|
// 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.
|
// 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)
|
DShotRMT::DShotRMT(gpio_num_t gpio, dshot_mode_t mode, bool isBidirectional)
|
||||||
: _gpio(gpio), _mode(mode), _isBidirectional(isBidirectional), _pauseDuration(pauseDuration) {}
|
: _gpio(gpio), _mode(mode), _isBidirectional(isBidirectional)
|
||||||
|
{
|
||||||
|
|
||||||
|
// Setting up fixed DShot Frame length
|
||||||
|
switch (_mode)
|
||||||
|
{
|
||||||
|
case DSHOT_OFF:
|
||||||
|
_frameLenght = 0;
|
||||||
|
break;
|
||||||
|
case DSHOT150:
|
||||||
|
_frameLenght = 128;
|
||||||
|
break;
|
||||||
|
case DSHOT300:
|
||||||
|
_frameLenght = 64;
|
||||||
|
break;
|
||||||
|
case DSHOT600:
|
||||||
|
_frameLenght = 32;
|
||||||
|
break;
|
||||||
|
case DSHOT1200:
|
||||||
|
_frameLenght = 16;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// DShot Frame length incl. DShot answer duration
|
||||||
|
if (_isBidirectional)
|
||||||
|
{
|
||||||
|
_frameLenght += _frameLenght;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Initializes RMT TX and RX channels and encoder configuration
|
// Initializes RMT TX and RX channels and encoder configuration
|
||||||
void DShotRMT::begin()
|
void DShotRMT::begin()
|
||||||
|
|
@ -85,13 +115,11 @@ void DShotRMT::begin()
|
||||||
void DShotRMT::setThrottle(uint16_t throttle)
|
void DShotRMT::setThrottle(uint16_t throttle)
|
||||||
{
|
{
|
||||||
// Simple timer
|
// Simple timer
|
||||||
static long last_time = 0;
|
static unsigned long last_time = NULL;
|
||||||
|
|
||||||
// Keep a pause between the frames
|
// Ensure frame lenght for compatibility
|
||||||
if (micros() - last_time >= _pauseDuration)
|
if (micros() - last_time >= _frameLenght)
|
||||||
{
|
{
|
||||||
last_time = micros();
|
|
||||||
|
|
||||||
// Clamp input range for throttle value
|
// Clamp input range for throttle value
|
||||||
_dshot_packet.throttle_value = constrain(throttle, DSHOT_THROTTLE_MIN, DSHOT_THROTTLE_MAX) & 0b0000011111111111;
|
_dshot_packet.throttle_value = constrain(throttle, DSHOT_THROTTLE_MIN, DSHOT_THROTTLE_MAX) & 0b0000011111111111;
|
||||||
|
|
||||||
|
|
@ -99,7 +127,7 @@ void DShotRMT::setThrottle(uint16_t throttle)
|
||||||
calculateCRC(&_dshot_packet);
|
calculateCRC(&_dshot_packet);
|
||||||
|
|
||||||
// Encode RMT symbols
|
// Encode RMT symbols
|
||||||
size_t count = 0;
|
size_t count = NULL;
|
||||||
encodeDShotTX(&_dshot_packet, _tx_symbols, count);
|
encodeDShotTX(&_dshot_packet, _tx_symbols, count);
|
||||||
|
|
||||||
// Transmit the packet
|
// Transmit the packet
|
||||||
|
|
@ -108,6 +136,9 @@ void DShotRMT::setThrottle(uint16_t throttle)
|
||||||
Serial.println("Failed to transmit DShot packet");
|
Serial.println("Failed to transmit DShot packet");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Timestamp
|
||||||
|
last_time = micros();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,7 @@ class DShotRMT
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// Constructor: initializes configuration state
|
// 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
|
// Initializes the RMT TX and RX channels
|
||||||
void begin();
|
void begin();
|
||||||
|
|
@ -69,8 +69,7 @@ public:
|
||||||
// Accessors for GPIO and DShot settings
|
// Accessors for GPIO and DShot settings
|
||||||
gpio_num_t getGPIO() const { return _gpio; }
|
gpio_num_t getGPIO() const { return _gpio; }
|
||||||
dshot_mode_t getDShotMode() const { return _mode; }
|
dshot_mode_t getDShotMode() const { return _mode; }
|
||||||
uint8_t getPauseDuration() const { return _pauseDuration; }
|
uint8_t getFrameLenght() const { return _frameLenght; }
|
||||||
void setPauseDuration(uint8_t pauseDuration) { _pauseDuration = pauseDuration; }
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// Calculates the checksum for a DShot packet
|
// Calculates the checksum for a DShot packet
|
||||||
|
|
@ -90,7 +89,7 @@ private:
|
||||||
gpio_num_t _gpio;
|
gpio_num_t _gpio;
|
||||||
dshot_mode_t _mode;
|
dshot_mode_t _mode;
|
||||||
bool _isBidirectional;
|
bool _isBidirectional;
|
||||||
uint8_t _pauseDuration;
|
uint16_t _frameLenght;
|
||||||
|
|
||||||
// --- DShot Packets Container ---
|
// --- DShot Packets Container ---
|
||||||
uint16_t _rx_packet = DSHOT_NULL_PACKET;
|
uint16_t _rx_packet = DSHOT_NULL_PACKET;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue