...added getMotorRPM Function

This commit is contained in:
Wastl Kraus 2025-06-14 18:16:45 +02:00
parent c2f1bf84d4
commit 8557930d3e
3 changed files with 76 additions and 55 deletions

View File

@ -97,6 +97,8 @@ void DShotRMT::setThrottle(uint16_t throttle)
// Receives and decodes a response frame from ESC containing eRPM info
uint32_t DShotRMT::getERPM()
{
if (_isBidirectional)
{
static size_t rx_size = sizeof(_rx_symbols);
if (_rmt_rx_channel == nullptr)
@ -148,6 +150,21 @@ uint32_t DShotRMT::getERPM()
// Approximate eRPM (ESC dependent, scale factor can be tuned)
_last_erpm = throttle * 100;
return _last_erpm;
}
// Nothing to do here
return _last_erpm;
}
// Translate eRPM value to RPM taking magnet count as parameter
uint32_t DShotRMT::getMotorRPM(uint8_t magnet_count)
{
uint8_t pole_count = magnet_count / 2;
if (pole_count == 0)
pole_count = 1;
uint32_t rpm = getERPM() / pole_count;
return rpm;
}
// --- Encode DShot TX Frame ---

View File

@ -55,19 +55,17 @@ public:
// Initializes the RMT TX and RX channels
void begin();
// Sets a new throttle value (0-2047) and sends it repeatedly
// Sets a new throttle value (48-2047) and sends it repeatedly
void setThrottle(uint16_t throttle);
// Receives and decodes the latest eRPM value from ESC, if available
// Receives and decodes the latest value from ESC, if available
uint32_t getERPM();
uint32_t getMotorRPM(uint8_t magnet_count);
// Accessors for GPIO and DShot mode
gpio_num_t getGPIO() const { return _gpio; }
dshot_mode_t getDShotMode() const { return _mode; }
// Stores the last valid eRPM received from the ESC
uint32_t _last_erpm = 0;
private:
// Converts a 16-bit DShot packet into RMT symbols and appends pause
void encodeDShotTX(uint16_t dshot_packet, rmt_symbol_word_t *symbols, size_t &count);
@ -96,4 +94,7 @@ private:
// --- RMT Symbol Buffer ---
rmt_symbol_word_t _rx_symbols[RX_BUFFER_SIZE] = {};
rmt_symbol_word_t _tx_symbols[TX_BUFFER_SIZE] = {};
// Stores the last valid eRPM received from the ESC
uint32_t _last_erpm = 0;
};

View File

@ -20,6 +20,9 @@ constexpr auto DSHOT_MODE = DSHOT300;
// BiDirectional DShot Support (default: false)
constexpr auto IS_BIDIRECTIONAL = true;
// Motor Magnet count for RPM calculation
constexpr auto MOTOR01_MAGNET_COUNT = 14;
// Setup Motor Pin, DShot Mode and optional BiDirectional Support
DShotRMT motor01(MOTOR01_PIN, DSHOT_MODE, IS_BIDIRECTIONAL);
@ -37,7 +40,7 @@ void setup()
//
USB_Serial.println("**********************");
USB_Serial.println("DShotRMT Demo started.");
USB_Serial.println("Enter a throttle value (48 2047):");
USB_Serial.println("Enter a throttle value (482047):");
}
void loop()
@ -58,12 +61,12 @@ void loop()
{
last_print_time = now;
uint32_t erpm = motor01.getERPM();
uint32_t rpm = motor01.getMotorRPM(MOTOR01_MAGNET_COUNT);
USB_Serial.print("Sent Throttle: ");
USB_Serial.print("Throttle: ");
USB_Serial.print(throttle_input);
USB_Serial.print(" | eRPM: ");
USB_Serial.println(erpm);
USB_Serial.print(" | RPM: ");
USB_Serial.println(rpm);
}
}
}
@ -86,7 +89,7 @@ int readSerialThrottle()
USB_Serial.print("Throttle set to: ");
USB_Serial.println(last_throttle);
USB_Serial.println("***********************************");
USB_Serial.println("Enter a throttle value (48 2047):");
USB_Serial.println("Enter a throttle value (482047):");
}
return last_throttle;