Skip to content

Commit

Permalink
esp32 async esc fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rtlopez committed Jul 20, 2023
1 parent 491023f commit ded51c8
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 31 deletions.
Binary file modified docs/calculations.ods
Binary file not shown.
5 changes: 2 additions & 3 deletions lib/EscDriver/src/EscDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ enum EscProtocol {
ESC_PROTOCOL_DSHOT150,
ESC_PROTOCOL_DSHOT300,
ESC_PROTOCOL_DSHOT600,
// ESC_PROTOCOL_DSHOT1200, // not used anymore
ESC_PROTOCOL_PROSHOT,
ESC_PROTOCOL_DISABLED,
ESC_PROTOCOL_COUNT
Expand Down Expand Up @@ -68,7 +67,7 @@ class EscDriverBase
#define EscDriver EscDriverEsp32

#define ESC_DRIVER_MOTOR_TIMER 0
#define ESC_DRIVER_SERVO_TIMER 0
#define ESC_DRIVER_SERVO_TIMER 1

#elif defined(ARCH_RP2040)

Expand All @@ -85,7 +84,7 @@ class EscDriverBase
#define EscDriver EscDriverBase

#define ESC_DRIVER_MOTOR_TIMER 0
#define ESC_DRIVER_SERVO_TIMER 0
#define ESC_DRIVER_SERVO_TIMER 1

#else

Expand Down
9 changes: 9 additions & 0 deletions lib/EscDriver/src/EscDriverEsp32.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#if defined(ESP32)

#include "EscDriverEsp32.h"

bool EscDriverEsp32::_tx_end_installed = false;

EscDriverEsp32* EscDriverEsp32::instances[] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };

#endif
48 changes: 24 additions & 24 deletions lib/EscDriver/src/EscDriverEsp32.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ static const int32_t DURATION_MAX = 0x7fff; // max in 15 bits
#define TO_INTERVAL(v) (1 * 1000 * 1000 / (v)) // [us]

// faster esc response, but unsafe (no task synchronisation)
// set to 0 in case of issues
#define ESPFC_RMT_BYPASS_WRITE_SYNC 1

#if ESPFC_RMT_BYPASS_WRITE_SYNC
Expand Down Expand Up @@ -54,10 +55,6 @@ IRAM_ATTR static esp_err_t _rmt_tx_start(rmt_channel_t channel, bool tx_idx_rst)
#define _rmt_fill_tx_items rmt_fill_tx_items
#endif


class EscDriverEsp32;
static EscDriverEsp32* instances[] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };

class EscDriverEsp32: public EscDriverBase
{
public:
Expand Down Expand Up @@ -117,13 +114,12 @@ class EscDriverEsp32: public EscDriverBase
}
};

EscDriverEsp32(): _protocol(ESC_PROTOCOL_PWM), _async(true), _rate(50)
EscDriverEsp32(): _protocol(ESC_PROTOCOL_PWM), _async(true), _rate(50), _digital(false)
{
for(size_t i = 0; i < ESC_CHANNEL_COUNT; i++)
{
_channel[i].dev.gpio_num = gpio_num_t(-1);
}
//begin(_protocol, _async, _rate);
}

void end()
Expand All @@ -138,17 +134,15 @@ class EscDriverEsp32: public EscDriverBase
int begin(EscProtocol protocol, bool async, int32_t rate, int timer = 0)
{
(void)timer; // unused
if(_async) rmt_register_tx_end_callback(NULL, NULL); // unregister old callback

_protocol = protocol;
_async = async;// && false;
_digital = isDigital(protocol);
_async = async;
_rate = rate;
_interval = TO_INTERVAL(_rate);

if(_async) rmt_register_tx_end_callback(&txDoneCallback, NULL);

return 1;
}
}

int attach(size_t channel, int pin, int pulse)
{
Expand Down Expand Up @@ -216,20 +210,27 @@ class EscDriverEsp32: public EscDriverBase

rmt_config(&_channel[i].dev);
rmt_driver_install(_channel[i].dev.channel, 0, 0);

if(_async) txDoneCallback((rmt_channel_t)i, NULL);
if (_async && !_tx_end_installed)
{
_tx_end_installed = true;
rmt_register_tx_end_callback(txDoneCallback, NULL);
}
if(_async)
{
rmt_set_tx_intr_en(_channel[i].dev.channel, true);
txDoneCallback((rmt_channel_t)i, NULL); // start generating pulses
}
}

static void txDoneCallback(rmt_channel_t channel, void *arg)
{
if(!instances[channel] || !instances[channel]->_async) return;
instances[channel]->transmitOne(channel);
if(instances[channel] && instances[channel]->_async) instances[channel]->transmitOne(channel);
}

void transmitOne(uint8_t i)
{
if(!_channel[i].attached()) return;
if(isDigital())
if(_digital)
{
writeDshotCommand(i, _channel[i].pulse);
}
Expand All @@ -242,11 +243,10 @@ class EscDriverEsp32: public EscDriverBase

void transmitAll()
{
bool digital = isDigital();
for(size_t i = 0; i < ESC_CHANNEL_COUNT; i++)
{
if(!_channel[i].attached()) continue;
if(digital)
if(_digital)
{
writeDshotCommand(i, _channel[i].pulse);
}
Expand Down Expand Up @@ -341,7 +341,6 @@ class EscDriverEsp32: public EscDriverBase
case ESC_PROTOCOL_DSHOT150:
case ESC_PROTOCOL_DSHOT300:
case ESC_PROTOCOL_DSHOT600:
//case ESC_PROTOCOL_DSHOT1200:
default:
return 0;
}
Expand All @@ -359,7 +358,6 @@ class EscDriverEsp32: public EscDriverBase
case ESC_PROTOCOL_DSHOT150:
case ESC_PROTOCOL_DSHOT300:
case ESC_PROTOCOL_DSHOT600:
//case ESC_PROTOCOL_DSHOT1200:
default:
return 2048;
}
Expand All @@ -384,7 +382,6 @@ class EscDriverEsp32: public EscDriverBase
case ESC_PROTOCOL_DSHOT150: return (width / (div * DURATION_CLOCK / 2)) * 4;
case ESC_PROTOCOL_DSHOT300: return (width / (div * DURATION_CLOCK / 2)) * 2;
case ESC_PROTOCOL_DSHOT600: return (width / (div * DURATION_CLOCK / 2)) * 1;
//case ESC_PROTOCOL_DSHOT1200: return (width / (div * DURATION_CLOCK / 2)) / 2;
case ESC_PROTOCOL_BRUSHED:
case ESC_PROTOCOL_PWM:
case ESC_PROTOCOL_ONESHOT125:
Expand All @@ -395,14 +392,13 @@ class EscDriverEsp32: public EscDriverBase
}
}

bool isDigital() const
bool isDigital(EscProtocol protocol) const
{
switch(_protocol)
switch(protocol)
{
case ESC_PROTOCOL_DSHOT150:
case ESC_PROTOCOL_DSHOT300:
case ESC_PROTOCOL_DSHOT600:
//case ESC_PROTOCOL_DSHOT1200:
return true;
default:
return false;
Expand All @@ -414,6 +410,10 @@ class EscDriverEsp32: public EscDriverBase
int32_t _async;
int32_t _rate;
int32_t _interval;
bool _digital;

static bool _tx_end_installed;
static EscDriverEsp32* instances[];
};

#endif
Expand Down
2 changes: 0 additions & 2 deletions lib/EscDriver/src/EscDriverEsp8266.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ uint32_t EscDriverEsp8266::usToTicks(uint32_t us)
case ESC_PROTOCOL_DSHOT150:
case ESC_PROTOCOL_DSHOT300:
case ESC_PROTOCOL_DSHOT600:
//case ESC_PROTOCOL_DSHOT1200:
ticks = us;
break;
default:
Expand Down Expand Up @@ -373,7 +372,6 @@ int EscDriverEsp8266::begin(EscProtocol protocol, bool async, int16_t rate, EscD
}
break;
case ESC_PROTOCOL_DSHOT600:
//case ESC_PROTOCOL_DSHOT1200:
case ESC_PROTOCOL_PROSHOT:
{
const float base = 47;
Expand Down
1 change: 0 additions & 1 deletion lib/EscDriver/src/EscDriverRP2040.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ uint32_t EscDriverRP2040::usToTicks(uint32_t us)
case ESC_PROTOCOL_DSHOT150:
case ESC_PROTOCOL_DSHOT300:
case ESC_PROTOCOL_DSHOT600:
//case ESC_PROTOCOL_DSHOT1200:
ticks = constrain(us, 1000, 2000);
break;
default:
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ build_flags =
-DESPFC_DEBUG_SERIAL
; -DDEBUG_RP2040_PORT=Serial
; -DDEBUG_RP2040_SPI
; -DESPFC_DEBUG_PIN=4 ; specify pin number (board specific)
; -DESPFC_DEBUG_PIN=2 ; specify pin number (board specific)
; -DNO_GLOBAL_INSTANCES
; -DESPFC_DEV_PRESET_MODES
; -DESPFC_DEV_PRESET_BLACKBOX=1 ; specify port number (board specific)
Expand Down

0 comments on commit ded51c8

Please sign in to comment.