...added getMotorRPM Function
This commit is contained in:
parent
c2f1bf84d4
commit
8557930d3e
17
DShotRMT.cpp
17
DShotRMT.cpp
|
|
@ -96,6 +96,8 @@ void DShotRMT::setThrottle(uint16_t throttle)
|
||||||
// --- Get eRPM from ESC ---
|
// --- Get eRPM from ESC ---
|
||||||
// Receives and decodes a response frame from ESC containing eRPM info
|
// Receives and decodes a response frame from ESC containing eRPM info
|
||||||
uint32_t DShotRMT::getERPM()
|
uint32_t DShotRMT::getERPM()
|
||||||
|
{
|
||||||
|
if (_isBidirectional)
|
||||||
{
|
{
|
||||||
static size_t rx_size = sizeof(_rx_symbols);
|
static size_t rx_size = sizeof(_rx_symbols);
|
||||||
|
|
||||||
|
|
@ -149,6 +151,21 @@ uint32_t DShotRMT::getERPM()
|
||||||
_last_erpm = throttle * 100;
|
_last_erpm = throttle * 100;
|
||||||
return _last_erpm;
|
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 ---
|
// --- Encode DShot TX Frame ---
|
||||||
// Converts a 16-bit packet into a valid DShot Frame for RMT
|
// Converts a 16-bit packet into a valid DShot Frame for RMT
|
||||||
|
|
|
||||||
11
DShotRMT.h
11
DShotRMT.h
|
|
@ -55,19 +55,17 @@ public:
|
||||||
// Initializes the RMT TX and RX channels
|
// Initializes the RMT TX and RX channels
|
||||||
void begin();
|
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);
|
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 getERPM();
|
||||||
|
uint32_t getMotorRPM(uint8_t magnet_count);
|
||||||
|
|
||||||
// Accessors for GPIO and DShot mode
|
// Accessors for GPIO and DShot mode
|
||||||
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; }
|
||||||
|
|
||||||
// Stores the last valid eRPM received from the ESC
|
|
||||||
uint32_t _last_erpm = 0;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Converts a 16-bit DShot packet into RMT symbols and appends pause
|
// 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);
|
void encodeDShotTX(uint16_t dshot_packet, rmt_symbol_word_t *symbols, size_t &count);
|
||||||
|
|
@ -96,4 +94,7 @@ private:
|
||||||
// --- RMT Symbol Buffer ---
|
// --- RMT Symbol Buffer ---
|
||||||
rmt_symbol_word_t _rx_symbols[RX_BUFFER_SIZE] = {};
|
rmt_symbol_word_t _rx_symbols[RX_BUFFER_SIZE] = {};
|
||||||
rmt_symbol_word_t _tx_symbols[TX_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;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,9 @@ constexpr auto DSHOT_MODE = DSHOT300;
|
||||||
// BiDirectional DShot Support (default: false)
|
// BiDirectional DShot Support (default: false)
|
||||||
constexpr auto IS_BIDIRECTIONAL = true;
|
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
|
// Setup Motor Pin, DShot Mode and optional BiDirectional Support
|
||||||
DShotRMT motor01(MOTOR01_PIN, DSHOT_MODE, IS_BIDIRECTIONAL);
|
DShotRMT motor01(MOTOR01_PIN, DSHOT_MODE, IS_BIDIRECTIONAL);
|
||||||
|
|
||||||
|
|
@ -58,12 +61,12 @@ void loop()
|
||||||
{
|
{
|
||||||
last_print_time = now;
|
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(throttle_input);
|
||||||
USB_Serial.print(" | eRPM: ");
|
USB_Serial.print(" | RPM: ");
|
||||||
USB_Serial.println(erpm);
|
USB_Serial.println(rpm);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue