From c37a66d5a5b2e666016cfc6037c83a8dfd4b8213 Mon Sep 17 00:00:00 2001 From: Wastl Kraus Date: Sun, 17 Aug 2025 18:58:40 +0200 Subject: [PATCH] ...keep things simple ...example sketch was getting out of hands --- DShotRMT.cpp | 39 +++++++++----------- DShotRMT.h | 2 +- examples/dshot300/dshot300.ino | 66 +++++++++++++--------------------- 3 files changed, 43 insertions(+), 64 deletions(-) diff --git a/DShotRMT.cpp b/DShotRMT.cpp index fb0a3d7..33cd41c 100644 --- a/DShotRMT.cpp +++ b/DShotRMT.cpp @@ -9,13 +9,13 @@ #include "DShotRMT.h" // --- DShot Timings --- -// frame_length_us, ticks_per_bit, ticks_one_high, ticks_zero_high, ticks_zero_low, ticks_one_low +// frame_length_us, ticks_per_bit, ticks_one_high, ticks_one_low, ticks_zero_high, ticks_zero_low constexpr dshot_timing_t DSHOT_TIMINGS[] = { {0, 0, 0, 0, 0, 0}, // DSHOT_OFF - {128, 64, 48, 24, 40, 16}, // DSHOT150 - {64, 32, 24, 12, 20, 8}, // DSHOT300 - {32, 16, 12, 6, 10, 4}, // DSHOT600 - {16, 8, 6, 3, 5, 2} // DSHOT1200 + {128, 64, 48, 16, 24, 40}, // DSHOT150 + {64, 32, 24, 8, 12, 20}, // DSHOT300 + {32, 16, 12, 4, 6, 10}, // DSHOT600 + {16, 8, 6, 2, 3, 5} // DSHOT1200 }; // --- DShot Config --- @@ -48,9 +48,10 @@ DShotRMT::DShotRMT(uint16_t pin_nr, dshot_mode_t mode, bool is_bidirectional): mode, is_bidirectional) { + // ...just to accept pin numbers and GPIO_NUMs } -// Init DShotRMT +// Setup and configure DShotRMT bool DShotRMT::begin() { // Init TX Channel @@ -78,7 +79,7 @@ bool DShotRMT::begin() return DSHOT_OK; } -// Deprecated, use "sendThrottle() instead" +// Deprecated, use "sendThrottle()"" instead bool DShotRMT::setThrottle(uint16_t throttle) { return sendThrottle(throttle); @@ -87,24 +88,17 @@ bool DShotRMT::setThrottle(uint16_t throttle) // Sends a valid throttle value bool DShotRMT::sendThrottle(uint16_t throttle) { - static uint16_t last_throttle = DSHOT_THROTTLE_MIN; + // Make sure throttle value is valid by force + auto value = constrain(throttle, DSHOT_THROTTLE_MIN, DSHOT_THROTTLE_MAX); - // Precheck throttle value - if (throttle < DSHOT_THROTTLE_MIN || throttle > DSHOT_THROTTLE_MAX) - { - Serial.println(DSHOT_MSG_06); - return DSHOT_ERROR; - } else { - last_throttle = throttle; - } - - // Converts throttle value to dshot packet - _packet = _buildDShotPacket(last_throttle); + // Converts throttle value to dshot packet RMT symbols + _packet = _buildDShotPacket(value); + // Actually send the RMT symbols return (_sendDShotFrame(_packet)); } -// Deprecated, use "sendCommand() instead" +// Deprecated, use "sendCommand()"" instead bool DShotRMT::sendDShotCommand(uint16_t command) { return sendCommand(command); @@ -192,8 +186,9 @@ bool DShotRMT::_initRXChannel() _rx_channel_config.resolution_hz = DSHOT_RMT_RESOLUTION; _rx_channel_config.mem_block_symbols = DSHOT_SYMBOLS_SIZE; - _receive_config.signal_range_min_ns = 300; - _receive_config.signal_range_max_ns = 5000; + // TODO: need to figure out + _receive_config.signal_range_min_ns = 2; + _receive_config.signal_range_max_ns = 128; // Creates and activates RMT TX Channel if (rmt_new_rx_channel(&_rx_channel_config, &_rmt_rx_channel) != DSHOT_OK) diff --git a/DShotRMT.h b/DShotRMT.h index fc8b9f9..7e8dd25 100644 --- a/DShotRMT.h +++ b/DShotRMT.h @@ -54,9 +54,9 @@ typedef struct dshot_timing_s uint16_t frame_length_us; uint16_t ticks_per_bit; uint16_t ticks_one_high; + uint16_t ticks_one_low; uint16_t ticks_zero_high; uint16_t ticks_zero_low; - uint16_t ticks_one_low; } dshot_timing_t; extern const dshot_timing_t DSHOT_TIMINGS[]; diff --git a/examples/dshot300/dshot300.ino b/examples/dshot300/dshot300.ino index 163661e..4ab2212 100644 --- a/examples/dshot300/dshot300.ino +++ b/examples/dshot300/dshot300.ino @@ -10,13 +10,13 @@ #include // USB serial port settings -static constexpr HardwareSerial &USB_SERIAL = Serial0; -static constexpr uint32_t USB_SERIAL_BAUD = 115200; +static constexpr auto &USB_SERIAL = Serial0; +static constexpr auto USB_SERIAL_BAUD = 115200; // Motor configuration // Pin number or GPIO_PIN // static constexpr gpio_num_t MOTOR01_PIN = GPIO_NUM_17; -static constexpr uint16_t MOTOR01_PIN = 17; +static constexpr auto MOTOR01_PIN = 17; // Supported: DSHOT150, DSHOT300, DSHOT600, (DSHOT1200) static constexpr dshot_mode_t DSHOT_MODE = DSHOT300; @@ -25,10 +25,10 @@ static constexpr dshot_mode_t DSHOT_MODE = DSHOT300; static constexpr bool IS_BIDIRECTIONAL = false; // Motor magnet count for RPM calculation -static constexpr uint8_t MOTOR01_MAGNET_COUNT = 14; +static constexpr auto MOTOR01_MAGNET_COUNT = 14; // -// +// Create the motor instance DShotRMT motor01(MOTOR01_PIN, DSHOT_MODE, IS_BIDIRECTIONAL); // @@ -51,48 +51,32 @@ void setup() // void loop() { - // Read value input from Serial - uint16_t throttle_input = readSerialThrottle(USB_SERIAL); - - // Send the value to the ESC - motor01.sendThrottle(throttle_input); - - // Prints out RPM if BiDirectional DShot is enabled every 2 seconds - printRPMPeriodically(2000); - - // Prints out "raw" DShot packet every 2 seconds - // printTXPacket(2000); -} - -// Reads throttle value from serial input -uint16_t readSerialThrottle(HardwareSerial &serial) -{ - static uint16_t last_throttle = DSHOT_THROTTLE_MIN; - - if (serial.available() > NULL) + static auto throttle = DSHOT_THROTTLE_MIN; + + if (USB_SERIAL.available() > NULL) { - // Reads a value - uint16_t throttle = (serial.readStringUntil('\n').toInt()); - - // Check for valid throttle value - if (throttle < DSHOT_THROTTLE_MIN || throttle > DSHOT_THROTTLE_MAX) - { - USB_SERIAL.println("Throttle value not in range (48 - 2047)!"); - return last_throttle; - } - - last_throttle = throttle; + auto new_throttle = (USB_SERIAL.readStringUntil('\n').toInt()); USB_SERIAL.println("*********************"); USB_SERIAL.print("Throttle set to: "); - USB_SERIAL.println(last_throttle); + USB_SERIAL.println(new_throttle); + + // + throttle = new_throttle; } - return last_throttle; + // Sends the value to the ESC + motor01.sendThrottle(throttle); + + // Prints out RPM if BiDirectional DShot is enabled every 2 seconds + // printRPMPeriodically(2000); + + // Debug: Prints out "raw" DShot packet every 2 seconds + print_RMT_packet(2000); } // Prints RPM every ms -void printRPMPeriodically(uint16_t timer_ms) +void printRPMPeriodically(auto timer_ms) { if (IS_BIDIRECTIONAL) { @@ -100,7 +84,7 @@ void printRPMPeriodically(uint16_t timer_ms) if (millis() - last_print_time >= timer_ms) { - uint32_t rpm = motor01.getMotorRPM(MOTOR01_MAGNET_COUNT); + auto rpm = motor01.getMotorRPM(MOTOR01_MAGNET_COUNT); USB_SERIAL.print("RPM: "); USB_SERIAL.println(rpm); @@ -111,13 +95,13 @@ void printRPMPeriodically(uint16_t timer_ms) } // Prints "raw" packet every ms -void printTXPacket(uint16_t timer_ms) +void print_RMT_packet(auto timer_ms) { static unsigned long last_print_time = 0; if (millis() - last_print_time >= timer_ms) { - uint16_t packet = motor01.getDShotPacket(); + auto packet = motor01.getDShotPacket(); // Print bit by bit for (int i = 15; i >= 0; --i)