...more simplifications

This commit is contained in:
Wastl Kraus 2025-08-06 23:57:26 +02:00
parent bd68f227d4
commit b0373190bb
3 changed files with 87 additions and 64 deletions

View File

@ -18,7 +18,7 @@ constexpr dshot_timing_t DSHOT_TIMINGS[] = {
{16, 8, 6, 3, 5, 2} // DSHOT1200 {16, 8, 6, 3, 5, 2} // DSHOT1200
}; };
// // --- DShot Config ---
DShotRMT::DShotRMT(gpio_num_t gpio, dshot_mode_t mode, bool is_bidirectional): DShotRMT::DShotRMT(gpio_num_t gpio, dshot_mode_t mode, bool is_bidirectional):
_gpio(gpio), _gpio(gpio),
_mode(mode), _mode(mode),
@ -28,8 +28,9 @@ DShotRMT::DShotRMT(gpio_num_t gpio, dshot_mode_t mode, bool is_bidirectional):
_rmt_rx_channel(nullptr), _rmt_rx_channel(nullptr),
_dshot_encoder(nullptr), _dshot_encoder(nullptr),
_last_erpm(0), _last_erpm(0),
_last_transmission_time(0), _current_packet(0),
_current_packet(0) _packet{0},
_last_transmission_time(0)
{ {
// Double up frame time for bidirectional mode // Double up frame time for bidirectional mode
if (_is_bidirectional) if (_is_bidirectional)
@ -41,6 +42,14 @@ DShotRMT::DShotRMT(gpio_num_t gpio, dshot_mode_t mode, bool is_bidirectional):
_frame_time_us = _timing_config.frame_length_us + DSHOT_SWITCH_TIME; _frame_time_us = _timing_config.frame_length_us + DSHOT_SWITCH_TIME;
} }
// Easy Constructor
DShotRMT::DShotRMT(uint16_t pin_nr, dshot_mode_t mode, bool is_bidirectional):
DShotRMT((gpio_num_t)pin_nr,
mode,
is_bidirectional)
{
}
// Init DShotRMT // Init DShotRMT
bool DShotRMT::begin() bool DShotRMT::begin()
{ {
@ -69,36 +78,50 @@ bool DShotRMT::begin()
return DSHOT_OK; return DSHOT_OK;
} }
// // Deprecated, use "sendThrottle() instead"
bool DShotRMT::setThrottle(uint16_t throttle) bool DShotRMT::setThrottle(uint16_t throttle)
{ {
return sendThrottle(throttle);
}
// Sends a valid throttle value
bool DShotRMT::sendThrottle(uint16_t throttle)
{
static uint16_t last_throttle = DSHOT_THROTTLE_MIN;
// Precheck throttle value // Precheck throttle value
if (throttle < DSHOT_THROTTLE_MIN || throttle > DSHOT_THROTTLE_MAX) if (throttle < DSHOT_THROTTLE_MIN || throttle > DSHOT_THROTTLE_MAX)
{ {
Serial.println(DSHOT_MSG_06); Serial.println(DSHOT_MSG_06);
return DSHOT_ERROR; return DSHOT_ERROR;
} else {
last_throttle = throttle;
} }
// // Converts throttle value to dshot packet
dshot_packet_t packet = _buildDShotPacket(throttle); _packet = _buildDShotPacket(last_throttle);
return (_sendDShotFrame(packet)); return (_sendDShotFrame(_packet));
} }
// // Deprecated, use "sendCommand() instead"
bool DShotRMT::sendDShotCommand(uint16_t command) bool DShotRMT::sendDShotCommand(uint16_t command)
{ {
// Precheck command value return sendCommand(command);
}
bool DShotRMT::sendCommand(uint16_t command)
{
// Check for valid command
if (command < DSHOT_CMD_MOTOR_STOP || command > DSHOT_CMD_MAX) if (command < DSHOT_CMD_MOTOR_STOP || command > DSHOT_CMD_MAX)
{ {
Serial.println(DSHOT_MSG_07); Serial.println(DSHOT_MSG_07);
return DSHOT_ERROR; return DSHOT_ERROR;
} }
// _packet = _buildDShotPacket(command);
dshot_packet_t packet = _buildDShotPacket(command);
return (_sendDShotFrame(packet)); return (_sendDShotFrame(_packet));
} }
// //

View File

@ -59,24 +59,26 @@ typedef struct dshot_timing_s
uint16_t ticks_one_low; uint16_t ticks_one_low;
} dshot_timing_t; } dshot_timing_t;
// --- DShot Timing Config ---
extern const dshot_timing_t DSHOT_TIMINGS[]; extern const dshot_timing_t DSHOT_TIMINGS[];
// // --- DShotRMT Class ---
class DShotRMT class DShotRMT
{ {
public: public:
// // --- DShot Config ---
DShotRMT(gpio_num_t gpio, dshot_mode_t mode = DSHOT300, bool is_bidirectional = false); DShotRMT(gpio_num_t gpio, dshot_mode_t mode = DSHOT300, bool is_bidirectional = false);
DShotRMT(uint16_t pin_nr, dshot_mode_t mode, bool is_bidirectional);
// --- Init RMT Module --- // --- Init RMT Module ---
bool begin(); bool begin();
// Sets the throttle value and transmits // Sets the throttle value and transmits
bool setThrottle(uint16_t throttle); bool setThrottle(uint16_t throttle); // deprecated
bool sendThrottle(uint16_t throttle);
// Sends a DShot Command // Sends a DShot Command
bool sendDShotCommand(uint16_t command); bool sendDShotCommand(uint16_t command); // deprecated
bool sendCommand(uint16_t command);
// Gets eRPM from ESC telemetry // Gets eRPM from ESC telemetry
uint16_t getERPM(); uint16_t getERPM();
@ -117,6 +119,7 @@ private:
// --- Buffers --- // --- Buffers ---
uint16_t _last_erpm; uint16_t _last_erpm;
uint16_t _current_packet; uint16_t _current_packet;
dshot_packet_t _packet;
unsigned long _last_transmission_time; unsigned long _last_transmission_time;
// //

View File

@ -14,7 +14,8 @@ static constexpr HardwareSerial &USB_SERIAL = Serial0;
static constexpr uint32_t USB_SERIAL_BAUD = 115200; static constexpr uint32_t USB_SERIAL_BAUD = 115200;
// Motor configuration // Motor configuration
static constexpr gpio_num_t MOTOR01_PIN = GPIO_NUM_17; // static constexpr gpio_num_t MOTOR01_PIN = GPIO_NUM_17;
static constexpr uint16_t MOTOR01_PIN = 17;
static constexpr dshot_mode_t DSHOT_MODE = DSHOT300; static constexpr dshot_mode_t DSHOT_MODE = DSHOT300;
// BiDirectional DShot Support (default: false) // BiDirectional DShot Support (default: false)
@ -26,14 +27,14 @@ static constexpr uint8_t 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);
// Prints RPM and throttle every 2 seconds if BiDirectional is enabled // Prints RPM if BiDirectional is enabled every ms
void printRPMPeriodically(uint16_t throttle); void printRPMPeriodically(uint16_t timer_ms);
// Reads throttle value from serial input // Reads throttle value from serial input
uint16_t readSerialThrottle(); uint16_t readSerialThrottle(HardwareSerial &serial);
// Prints out the dshot packet bitwise (Debug) // Prints out the dshot packet bitwise every ms (Debug)
void printPacket(); void printTXPacket(uint16_t timer_ms);
// //
void setup() void setup()
@ -45,10 +46,10 @@ void setup()
motor01.begin(); motor01.begin();
// Arm ESC with minimum throttle // Arm ESC with minimum throttle
motor01.setThrottle(DSHOT_THROTTLE_MIN); // motor01.sendThrottle(DSHOT_THROTTLE_MIN);
USB_SERIAL.println("**********************"); USB_SERIAL.println("***********************************");
USB_SERIAL.println("DShotRMT Demo started."); USB_SERIAL.println(" === DShotRMT Demo started. === ");
USB_SERIAL.println("Enter a throttle value (48 2047):"); USB_SERIAL.println("Enter a throttle value (48 2047):");
} }
@ -56,78 +57,74 @@ void setup()
void loop() void loop()
{ {
// Read value input from Serial // Read value input from Serial
uint16_t throttle_input = readSerialThrottle(); uint16_t throttle_input = readSerialThrottle(USB_SERIAL);
// Send the value to the ESC // Send the value to the ESC
motor01.setThrottle(throttle_input); motor01.sendThrottle(throttle_input);
// Print RPM if BiDirectional DShot is enabled // Prints RPM every 2 if BiDirectional DShot is enabled
if (IS_BIDIRECTIONAL) printRPMPeriodically(2000);
{
printRPMPeriodically(throttle_input);
}
// Prints out "raw" DShot packet // Prints out "raw" DShot packet every 2 seconds
// printPacket(); // printTXPacket(2000);
} }
// Reads throttle value from serial input // Reads throttle value from serial input
uint16_t readSerialThrottle() uint16_t readSerialThrottle(HardwareSerial &serial)
{ {
static uint16_t last_throttle = DSHOT_THROTTLE_MIN; static uint16_t last_throttle = DSHOT_THROTTLE_MIN;
if (USB_SERIAL.available() > 0) if (serial.available() > NULL)
{ {
String input = USB_SERIAL.readStringUntil('\n'); // Reads a value
int throttle_input = input.toInt(); uint16_t throttle = (serial.readStringUntil('\n').toInt());
if (throttle_input < DSHOT_THROTTLE_MIN || throttle_input > DSHOT_THROTTLE_MAX) // Check for valid throttle value
if (throttle < DSHOT_THROTTLE_MIN || throttle > DSHOT_THROTTLE_MAX)
{ {
USB_SERIAL.println("Invalid input. Please enter a value between 48 and 2047."); USB_SERIAL.println("Throttle value not in range (48 - 2047)!");
return last_throttle; return last_throttle;
} }
else
{
last_throttle = throttle_input;
USB_SERIAL.print("Throttle set to: ");
USB_SERIAL.println(last_throttle);
}
USB_SERIAL.println("*********************************"); last_throttle = throttle;
USB_SERIAL.println("Enter a throttle value (48 2047):");
USB_SERIAL.println("*********************");
USB_SERIAL.print("Throttle set to: ");
USB_SERIAL.println(last_throttle);
} }
return last_throttle; return last_throttle;
} }
// Prints RPM and throttle every 2 seconds // Prints RPM every ms
void printRPMPeriodically(uint16_t throttle) void printRPMPeriodically(uint16_t timer_ms)
{ {
static unsigned long last_print_time = 0; if (IS_BIDIRECTIONAL)
if (millis() - last_print_time >= 2000)
{ {
uint32_t rpm = motor01.getMotorRPM(MOTOR01_MAGNET_COUNT); static unsigned long last_print_time = 0;
USB_SERIAL.print("Throttle: "); if (millis() - last_print_time >= timer_ms)
USB_SERIAL.print(throttle); {
USB_SERIAL.print(" | RPM: "); uint32_t rpm = motor01.getMotorRPM(MOTOR01_MAGNET_COUNT);
USB_SERIAL.println(rpm);
last_print_time = millis(); USB_SERIAL.print("RPM: ");
USB_SERIAL.println(rpm);
last_print_time = millis();
}
} }
} }
// // Prints "raw" packet every ms
void printPacket() void printTXPacket(uint16_t timer_ms)
{ {
static unsigned long last_print_time = 0; static unsigned long last_print_time = 0;
if (millis() - last_print_time >= 2000) if (millis() - last_print_time >= timer_ms)
{ {
// Prints out actual DShot Packet bitwise
uint16_t packet = motor01.getDShotPacket(); uint16_t packet = motor01.getDShotPacket();
// Print bit by bit
for (int i = 15; i >= 0; --i) for (int i = 15; i >= 0; --i)
{ {
if ((packet >> i) & 1) if ((packet >> i) & 1)