2025-06-11 08:29:59 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* @file dshot300.ino
|
2025-06-12 23:45:48 +01:00
|
|
|
|
* @brief Demo sketch for DShotRMT library
|
2025-06-11 08:29:59 +01:00
|
|
|
|
* @author Wastl Kraus
|
2025-06-12 23:45:48 +01:00
|
|
|
|
* @date 2025-06-11
|
2025-06-11 08:29:59 +01:00
|
|
|
|
* @license MIT
|
2023-04-13 20:10:51 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
2021-06-30 02:06:02 +01:00
|
|
|
|
#include <Arduino.h>
|
2025-09-27 22:17:43 +01:00
|
|
|
|
#include <DShotRMT.h>
|
2021-06-30 02:06:02 +01:00
|
|
|
|
|
2025-06-11 08:29:59 +01:00
|
|
|
|
// USB serial port settings
|
2025-08-17 17:58:40 +01:00
|
|
|
|
static constexpr auto &USB_SERIAL = Serial0;
|
|
|
|
|
|
static constexpr auto USB_SERIAL_BAUD = 115200;
|
2021-06-30 02:06:02 +01:00
|
|
|
|
|
2025-08-17 22:00:43 +01:00
|
|
|
|
// Motor configuration - Pin number or GPIO_PIN
|
2025-09-20 14:41:08 +01:00
|
|
|
|
static constexpr gpio_num_t MOTOR01_PIN = GPIO_NUM_27;
|
2025-09-17 10:35:07 +01:00
|
|
|
|
// static constexpr auto MOTOR01_PIN = 17;
|
2025-08-08 14:44:19 +01:00
|
|
|
|
|
|
|
|
|
|
// Supported: DSHOT150, DSHOT300, DSHOT600, (DSHOT1200)
|
2025-10-01 08:48:26 +01:00
|
|
|
|
static constexpr dshot_mode_t DSHOT_MODE = DSHOT300;
|
2023-03-27 18:55:30 +01:00
|
|
|
|
|
2025-08-02 15:11:10 +01:00
|
|
|
|
// BiDirectional DShot Support (default: false)
|
2025-09-25 15:15:36 +01:00
|
|
|
|
// Note: Bidirectional DShot is currently not officially supported
|
2025-09-23 22:48:18 +01:00
|
|
|
|
// due to instability and external hardware requirements.
|
2025-09-25 15:15:36 +01:00
|
|
|
|
static constexpr auto IS_BIDIRECTIONAL = false;
|
2025-06-12 23:45:48 +01:00
|
|
|
|
|
2025-08-02 15:11:10 +01:00
|
|
|
|
// Motor magnet count for RPM calculation
|
2025-10-01 08:48:26 +01:00
|
|
|
|
// static constexpr auto MOTOR01_MAGNET_COUNT = 14;
|
2025-08-02 15:11:10 +01:00
|
|
|
|
|
2025-08-17 22:00:43 +01:00
|
|
|
|
// Creates the motor instance
|
2025-10-01 23:31:24 +01:00
|
|
|
|
DShotRMT motor01(MOTOR01_PIN, DSHOT_MODE, IS_BIDIRECTIONAL, DEFAULT_MOTOR_MAGNET_COUNT);
|
2021-06-30 02:06:02 +01:00
|
|
|
|
|
2025-07-30 13:44:01 +01:00
|
|
|
|
//
|
2022-11-25 15:08:58 +00:00
|
|
|
|
void setup()
|
|
|
|
|
|
{
|
2025-08-17 22:00:43 +01:00
|
|
|
|
// Starts the USB Serial Port
|
2025-07-19 15:41:04 +01:00
|
|
|
|
USB_SERIAL.begin(USB_SERIAL_BAUD);
|
2023-03-27 18:47:23 +01:00
|
|
|
|
|
2025-09-05 11:16:19 +01:00
|
|
|
|
// Initialize DShot Signal
|
2025-07-19 15:41:04 +01:00
|
|
|
|
motor01.begin();
|
2025-06-11 08:29:59 +01:00
|
|
|
|
|
2025-08-30 20:56:34 +01:00
|
|
|
|
// Print CPU Info
|
2025-10-01 08:48:26 +01:00
|
|
|
|
printCpuInfo(USB_SERIAL);
|
2025-08-28 13:37:02 +01:00
|
|
|
|
|
2025-09-05 11:16:19 +01:00
|
|
|
|
//
|
|
|
|
|
|
printMenu();
|
2021-06-30 02:06:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-30 13:44:01 +01:00
|
|
|
|
//
|
2022-11-25 15:08:58 +00:00
|
|
|
|
void loop()
|
|
|
|
|
|
{
|
2025-09-05 11:16:19 +01:00
|
|
|
|
// Safety first
|
2025-09-25 15:32:40 +01:00
|
|
|
|
static uint16_t throttle = DSHOT_CMD_MOTOR_STOP;
|
2025-09-16 12:41:32 +01:00
|
|
|
|
|
|
|
|
|
|
// Initialize the esc with "0"
|
2025-09-05 11:16:19 +01:00
|
|
|
|
static bool continuous_throttle = true;
|
2025-08-28 12:22:42 +01:00
|
|
|
|
|
2025-08-30 20:56:34 +01:00
|
|
|
|
// Time Measurement
|
2025-09-07 12:48:48 +01:00
|
|
|
|
static uint64_t last_stats_print = 0;
|
2025-08-28 12:22:42 +01:00
|
|
|
|
|
2025-09-05 11:16:19 +01:00
|
|
|
|
// Handle serial input
|
2025-08-28 12:22:42 +01:00
|
|
|
|
if (USB_SERIAL.available() > 0)
|
2025-07-19 12:26:54 +01:00
|
|
|
|
{
|
2025-09-05 11:16:19 +01:00
|
|
|
|
String input = USB_SERIAL.readStringUntil('\n');
|
|
|
|
|
|
input.trim();
|
|
|
|
|
|
|
|
|
|
|
|
if (input.length() > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
handleSerialInput(input, throttle, continuous_throttle);
|
|
|
|
|
|
}
|
2025-07-19 12:26:54 +01:00
|
|
|
|
}
|
2025-06-12 11:24:27 +01:00
|
|
|
|
|
2025-09-05 11:16:19 +01:00
|
|
|
|
// Send throttle value in continuous mode
|
|
|
|
|
|
if (continuous_throttle)
|
|
|
|
|
|
{
|
|
|
|
|
|
motor01.sendThrottle(throttle);
|
|
|
|
|
|
}
|
2025-08-17 17:58:40 +01:00
|
|
|
|
|
2025-09-07 12:48:48 +01:00
|
|
|
|
// Print motor stats every 3 seconds in continuous mode
|
|
|
|
|
|
if (continuous_throttle && (esp_timer_get_time() - last_stats_print >= 3000000))
|
2025-07-19 15:41:04 +01:00
|
|
|
|
{
|
2025-10-01 08:48:26 +01:00
|
|
|
|
printDShotInfo(motor01, USB_SERIAL);
|
2025-07-30 13:44:01 +01:00
|
|
|
|
|
2025-09-05 22:28:08 +01:00
|
|
|
|
USB_SERIAL.println(" ");
|
|
|
|
|
|
|
2025-09-05 11:16:19 +01:00
|
|
|
|
// Get Motor RPM if bidirectional
|
2025-08-31 10:43:48 +01:00
|
|
|
|
if (IS_BIDIRECTIONAL)
|
|
|
|
|
|
{
|
2025-09-20 15:47:39 +01:00
|
|
|
|
dshot_result_t telem_result = motor01.getTelemetry();
|
2025-09-26 18:01:55 +01:00
|
|
|
|
printDShotResult(telem_result);
|
2025-08-31 10:43:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-07 12:48:48 +01:00
|
|
|
|
USB_SERIAL.println("Type 'help' to show Menu");
|
|
|
|
|
|
|
2025-08-30 20:56:34 +01:00
|
|
|
|
// Time Stamp
|
2025-09-07 12:48:48 +01:00
|
|
|
|
last_stats_print = esp_timer_get_time();
|
2025-08-04 22:29:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-05 11:16:19 +01:00
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
void printMenu()
|
|
|
|
|
|
{
|
|
|
|
|
|
USB_SERIAL.println(" ");
|
2025-09-05 22:28:08 +01:00
|
|
|
|
USB_SERIAL.println("*******************************************");
|
|
|
|
|
|
USB_SERIAL.println(" DShotRMT Demo ");
|
|
|
|
|
|
USB_SERIAL.println("*******************************************");
|
2025-09-05 11:16:19 +01:00
|
|
|
|
USB_SERIAL.println(" <value> - Set throttle (48 – 2047)");
|
|
|
|
|
|
USB_SERIAL.println(" 0 - Stop motor");
|
2025-09-05 22:28:08 +01:00
|
|
|
|
USB_SERIAL.println("*******************************************");
|
|
|
|
|
|
USB_SERIAL.println(" cmd <number> - Send DShot command (0 - 47)");
|
2025-09-05 11:16:19 +01:00
|
|
|
|
USB_SERIAL.println(" info - Show motor info");
|
|
|
|
|
|
if (IS_BIDIRECTIONAL)
|
|
|
|
|
|
{
|
|
|
|
|
|
USB_SERIAL.println(" rpm - Get telemetry data");
|
|
|
|
|
|
}
|
2025-09-05 22:28:08 +01:00
|
|
|
|
USB_SERIAL.println("*******************************************");
|
2025-09-05 11:16:19 +01:00
|
|
|
|
USB_SERIAL.println(" h / help - Show this Menu");
|
2025-09-05 22:28:08 +01:00
|
|
|
|
USB_SERIAL.println("*******************************************");
|
2025-09-05 11:16:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
void handleSerialInput(const String &input, uint16_t &throttle, bool &continuous_throttle)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (input == "0")
|
|
|
|
|
|
{
|
|
|
|
|
|
// Stop motor
|
|
|
|
|
|
throttle = 0;
|
2025-09-08 08:05:54 +01:00
|
|
|
|
continuous_throttle = true;
|
2025-09-25 15:32:40 +01:00
|
|
|
|
dshot_result_t result = motor01.sendCommand(DSHOT_CMD_MOTOR_STOP);
|
2025-09-26 18:01:55 +01:00
|
|
|
|
printDShotResult(result);
|
2025-09-05 11:16:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
else if (input == "info")
|
|
|
|
|
|
{
|
2025-10-01 08:48:26 +01:00
|
|
|
|
printDShotInfo(motor01, USB_SERIAL);
|
2025-09-05 11:16:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
else if (input == "rpm" && IS_BIDIRECTIONAL)
|
|
|
|
|
|
{
|
2025-09-20 15:47:39 +01:00
|
|
|
|
dshot_result_t result = motor01.getTelemetry();
|
2025-09-26 18:01:55 +01:00
|
|
|
|
printDShotResult(result);
|
2025-09-05 11:16:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
else if (input.startsWith("cmd "))
|
|
|
|
|
|
{
|
|
|
|
|
|
continuous_throttle = false;
|
|
|
|
|
|
|
|
|
|
|
|
// Send DShot command
|
|
|
|
|
|
int cmd_num = input.substring(4).toInt();
|
|
|
|
|
|
|
2025-09-25 15:32:40 +01:00
|
|
|
|
if (cmd_num >= DSHOT_CMD_MOTOR_STOP && cmd_num <= DSHOT_CMD_MAX)
|
2025-09-05 11:16:19 +01:00
|
|
|
|
{
|
|
|
|
|
|
dshot_result_t result = motor01.sendCommand(cmd_num);
|
2025-09-26 18:01:55 +01:00
|
|
|
|
printDShotResult(result);
|
2025-09-05 11:16:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-09-25 15:32:40 +01:00
|
|
|
|
USB_SERIAL.printf("Invalid command: %d (valid range: 0 - %d)\n", cmd_num, DSHOT_CMD_MAX);
|
2025-09-05 11:16:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (input == "h" || input == "help")
|
|
|
|
|
|
{
|
|
|
|
|
|
printMenu();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// Parse input throttle value
|
|
|
|
|
|
int throttle_value = input.toInt();
|
|
|
|
|
|
|
|
|
|
|
|
if (throttle_value >= DSHOT_THROTTLE_MIN && throttle_value <= DSHOT_THROTTLE_MAX)
|
|
|
|
|
|
{
|
|
|
|
|
|
throttle = throttle_value;
|
|
|
|
|
|
continuous_throttle = true;
|
|
|
|
|
|
|
|
|
|
|
|
dshot_result_t result = motor01.sendThrottle(throttle);
|
2025-09-26 18:01:55 +01:00
|
|
|
|
printDShotResult(result);
|
2025-09-05 11:16:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-09-08 20:10:15 +01:00
|
|
|
|
USB_SERIAL.println(" ");
|
2025-09-08 08:05:54 +01:00
|
|
|
|
USB_SERIAL.printf("Invalid input: '%s'\n", input);
|
2025-09-05 11:16:19 +01:00
|
|
|
|
USB_SERIAL.printf("Valid throttle range: %d - %d\n", DSHOT_THROTTLE_MIN, DSHOT_THROTTLE_MAX);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|