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-03-26 12:49:56 +00: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-03 21:15:38 +01:00
|
|
|
|
static constexpr HardwareSerial &USB_SERIAL = Serial0;
|
2025-08-02 16:20:32 +01:00
|
|
|
|
static constexpr uint32_t USB_SERIAL_BAUD = 115200;
|
2021-06-30 02:06:02 +01:00
|
|
|
|
|
2025-06-11 08:29:59 +01:00
|
|
|
|
// Motor configuration
|
2025-08-08 14:44:19 +01:00
|
|
|
|
// Pin number or GPIO_PIN
|
2025-08-06 22:57:26 +01:00
|
|
|
|
// static constexpr gpio_num_t MOTOR01_PIN = GPIO_NUM_17;
|
|
|
|
|
|
static constexpr uint16_t MOTOR01_PIN = 17;
|
2025-08-08 14:44:19 +01:00
|
|
|
|
|
|
|
|
|
|
// Supported: DSHOT150, DSHOT300, DSHOT600, (DSHOT1200)
|
2025-08-02 16:20:32 +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-08-02 16:20:32 +01:00
|
|
|
|
static constexpr bool 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-08-02 16:20:32 +01:00
|
|
|
|
static constexpr uint8_t MOTOR01_MAGNET_COUNT = 14;
|
2025-08-02 15:11:10 +01:00
|
|
|
|
|
2025-08-08 14:44:19 +01:00
|
|
|
|
//
|
|
|
|
|
|
//
|
2025-06-12 23:45:48 +01:00
|
|
|
|
DShotRMT motor01(MOTOR01_PIN, DSHOT_MODE, IS_BIDIRECTIONAL);
|
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-07-19 15:41:04 +01:00
|
|
|
|
// Start the USB Serial Port
|
|
|
|
|
|
USB_SERIAL.begin(USB_SERIAL_BAUD);
|
2023-03-27 18:47:23 +01:00
|
|
|
|
|
2025-07-19 15:41:04 +01:00
|
|
|
|
// Initialize DShot Signal
|
|
|
|
|
|
motor01.begin();
|
2025-06-11 08:29:59 +01:00
|
|
|
|
|
2025-08-02 15:11:10 +01:00
|
|
|
|
// Arm ESC with minimum throttle
|
2025-08-06 22:57:26 +01:00
|
|
|
|
// motor01.sendThrottle(DSHOT_THROTTLE_MIN);
|
2025-08-02 15:11:10 +01:00
|
|
|
|
|
2025-08-06 22:57:26 +01:00
|
|
|
|
USB_SERIAL.println("***********************************");
|
|
|
|
|
|
USB_SERIAL.println(" === DShotRMT Demo started. === ");
|
2025-07-30 13:47:40 +01:00
|
|
|
|
USB_SERIAL.println("Enter a throttle value (48 – 2047):");
|
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-07-19 15:41:04 +01:00
|
|
|
|
// Read value input from Serial
|
2025-08-06 22:57:26 +01:00
|
|
|
|
uint16_t throttle_input = readSerialThrottle(USB_SERIAL);
|
2023-03-27 18:55:30 +01:00
|
|
|
|
|
2025-07-19 15:41:04 +01:00
|
|
|
|
// Send the value to the ESC
|
2025-08-06 22:57:26 +01:00
|
|
|
|
motor01.sendThrottle(throttle_input);
|
2025-06-13 12:50:10 +01:00
|
|
|
|
|
2025-08-08 14:44:19 +01:00
|
|
|
|
// Prints out RPM if BiDirectional DShot is enabled every 2 seconds
|
2025-08-06 22:57:26 +01:00
|
|
|
|
printRPMPeriodically(2000);
|
2025-08-04 22:29:49 +01:00
|
|
|
|
|
2025-08-06 22:57:26 +01:00
|
|
|
|
// Prints out "raw" DShot packet every 2 seconds
|
|
|
|
|
|
// printTXPacket(2000);
|
2021-06-30 02:06:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-11 08:29:59 +01:00
|
|
|
|
// Reads throttle value from serial input
|
2025-08-06 22:57:26 +01:00
|
|
|
|
uint16_t readSerialThrottle(HardwareSerial &serial)
|
2022-11-25 15:08:58 +00:00
|
|
|
|
{
|
2025-07-19 15:41:04 +01:00
|
|
|
|
static uint16_t last_throttle = DSHOT_THROTTLE_MIN;
|
2025-06-10 20:40:45 +01:00
|
|
|
|
|
2025-08-06 22:57:26 +01:00
|
|
|
|
if (serial.available() > NULL)
|
2025-07-19 12:26:54 +01:00
|
|
|
|
{
|
2025-08-06 22:57:26 +01:00
|
|
|
|
// Reads a value
|
|
|
|
|
|
uint16_t throttle = (serial.readStringUntil('\n').toInt());
|
2025-07-19 15:41:04 +01:00
|
|
|
|
|
2025-08-06 22:57:26 +01:00
|
|
|
|
// Check for valid throttle value
|
|
|
|
|
|
if (throttle < DSHOT_THROTTLE_MIN || throttle > DSHOT_THROTTLE_MAX)
|
2025-07-19 15:41:04 +01:00
|
|
|
|
{
|
2025-08-06 22:57:26 +01:00
|
|
|
|
USB_SERIAL.println("Throttle value not in range (48 - 2047)!");
|
2025-07-29 23:40:09 +01:00
|
|
|
|
return last_throttle;
|
2025-07-19 15:41:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-06 22:57:26 +01:00
|
|
|
|
last_throttle = throttle;
|
|
|
|
|
|
|
|
|
|
|
|
USB_SERIAL.println("*********************");
|
|
|
|
|
|
USB_SERIAL.print("Throttle set to: ");
|
|
|
|
|
|
USB_SERIAL.println(last_throttle);
|
2025-07-19 12:26:54 +01:00
|
|
|
|
}
|
2025-06-12 11:24:27 +01:00
|
|
|
|
|
2025-07-19 15:41:04 +01:00
|
|
|
|
return last_throttle;
|
2025-06-12 23:45:48 +01:00
|
|
|
|
}
|
2025-07-19 12:26:54 +01:00
|
|
|
|
|
2025-08-06 22:57:26 +01:00
|
|
|
|
// Prints RPM every ms
|
|
|
|
|
|
void printRPMPeriodically(uint16_t timer_ms)
|
2025-07-19 12:26:54 +01:00
|
|
|
|
{
|
2025-08-06 22:57:26 +01:00
|
|
|
|
if (IS_BIDIRECTIONAL)
|
2025-07-19 15:41:04 +01:00
|
|
|
|
{
|
2025-08-06 22:57:26 +01:00
|
|
|
|
static unsigned long last_print_time = 0;
|
2025-07-19 12:26:54 +01:00
|
|
|
|
|
2025-08-06 22:57:26 +01:00
|
|
|
|
if (millis() - last_print_time >= timer_ms)
|
|
|
|
|
|
{
|
|
|
|
|
|
uint32_t rpm = motor01.getMotorRPM(MOTOR01_MAGNET_COUNT);
|
2025-07-30 13:44:01 +01:00
|
|
|
|
|
2025-08-06 22:57:26 +01:00
|
|
|
|
USB_SERIAL.print("RPM: ");
|
|
|
|
|
|
USB_SERIAL.println(rpm);
|
|
|
|
|
|
|
|
|
|
|
|
last_print_time = millis();
|
|
|
|
|
|
}
|
2025-07-19 15:41:04 +01:00
|
|
|
|
}
|
2025-07-19 12:26:54 +01:00
|
|
|
|
}
|
2025-08-04 22:29:49 +01:00
|
|
|
|
|
2025-08-06 22:57:26 +01:00
|
|
|
|
// Prints "raw" packet every ms
|
|
|
|
|
|
void printTXPacket(uint16_t timer_ms)
|
2025-08-04 22:29:49 +01:00
|
|
|
|
{
|
|
|
|
|
|
static unsigned long last_print_time = 0;
|
|
|
|
|
|
|
2025-08-06 22:57:26 +01:00
|
|
|
|
if (millis() - last_print_time >= timer_ms)
|
2025-08-04 22:29:49 +01:00
|
|
|
|
{
|
|
|
|
|
|
uint16_t packet = motor01.getDShotPacket();
|
|
|
|
|
|
|
2025-08-06 22:57:26 +01:00
|
|
|
|
// Print bit by bit
|
2025-08-04 22:29:49 +01:00
|
|
|
|
for (int i = 15; i >= 0; --i)
|
|
|
|
|
|
{
|
|
|
|
|
|
if ((packet >> i) & 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
USB_SERIAL.print("1");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
USB_SERIAL.print("0");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
USB_SERIAL.println("");
|
|
|
|
|
|
last_print_time = millis();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|