2025-06-11 08:29:59 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* @file dshot300.ino
|
|
|
|
|
|
* @brief Demo sketch for continuous DShot signal using ESP32 and DShotRMT library
|
|
|
|
|
|
* @author Wastl Kraus
|
|
|
|
|
|
* @date 2025-06-07
|
|
|
|
|
|
* @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-06-12 11:24:27 +01:00
|
|
|
|
#define USB_Serial Serial0
|
|
|
|
|
|
constexpr auto USB_SERIAL_BAUD = 115200;
|
2021-06-30 02:06:02 +01:00
|
|
|
|
|
2025-06-11 08:29:59 +01:00
|
|
|
|
// Motor configuration
|
2025-06-12 11:24:27 +01:00
|
|
|
|
constexpr auto MOTOR01_PIN = GPIO_NUM_17;
|
|
|
|
|
|
constexpr auto DSHOT_MODE = DSHOT300;
|
2023-03-27 18:55:30 +01:00
|
|
|
|
|
2025-06-11 08:29:59 +01:00
|
|
|
|
// Create DShotRMT instance
|
|
|
|
|
|
DShotRMT motor01(MOTOR01_PIN, DSHOT_MODE);
|
2021-06-30 02:06:02 +01:00
|
|
|
|
|
2022-11-25 15:08:58 +00:00
|
|
|
|
void setup()
|
|
|
|
|
|
{
|
2025-05-14 14:19:38 +01:00
|
|
|
|
USB_Serial.begin(USB_SERIAL_BAUD);
|
2023-03-27 18:47:23 +01:00
|
|
|
|
|
2025-06-11 08:29:59 +01:00
|
|
|
|
// Wait for serial port
|
|
|
|
|
|
while (!USB_Serial)
|
|
|
|
|
|
delay(10);
|
|
|
|
|
|
|
|
|
|
|
|
USB_Serial.println("DShotRMT Demo started.");
|
|
|
|
|
|
USB_Serial.println("Enter a throttle value (48–2047):");
|
2025-06-10 20:40:45 +01:00
|
|
|
|
|
2025-06-11 08:29:59 +01:00
|
|
|
|
motor01.begin();
|
|
|
|
|
|
|
|
|
|
|
|
// Arm ESC with minimum throttle
|
|
|
|
|
|
motor01.setThrottle(DSHOT_THROTTLE_MIN);
|
2021-06-30 02:06:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-25 15:08:58 +00:00
|
|
|
|
void loop()
|
|
|
|
|
|
{
|
2025-06-11 08:29:59 +01:00
|
|
|
|
// Simple as can be
|
|
|
|
|
|
int throttle_input = readSerialThrottle();
|
2023-03-27 18:55:30 +01:00
|
|
|
|
|
2025-06-11 08:29:59 +01:00
|
|
|
|
motor01.setThrottle(throttle_input);
|
2021-06-30 02:06:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-11 08:29:59 +01:00
|
|
|
|
// Reads throttle value from serial input
|
|
|
|
|
|
int readSerialThrottle()
|
2022-11-25 15:08:58 +00:00
|
|
|
|
{
|
2025-06-12 11:24:27 +01:00
|
|
|
|
//
|
2025-06-11 08:29:59 +01:00
|
|
|
|
static int last_throttle = DSHOT_THROTTLE_MIN;
|
2025-06-10 20:40:45 +01:00
|
|
|
|
|
2025-05-14 14:19:38 +01:00
|
|
|
|
if (USB_Serial.available() > 0)
|
|
|
|
|
|
{
|
2025-06-11 08:29:59 +01:00
|
|
|
|
String input = USB_Serial.readStringUntil('\n');
|
|
|
|
|
|
int throttle_input = input.toInt();
|
|
|
|
|
|
|
|
|
|
|
|
// Clamp the value to the DShot range
|
|
|
|
|
|
throttle_input = constrain(throttle_input, 48, 2047);
|
2025-06-10 20:40:45 +01:00
|
|
|
|
last_throttle = throttle_input;
|
2025-06-11 08:29:59 +01:00
|
|
|
|
|
|
|
|
|
|
USB_Serial.print("Throttle set to: ");
|
|
|
|
|
|
USB_Serial.println(last_throttle);
|
|
|
|
|
|
|
|
|
|
|
|
USB_Serial.println("Enter a throttle value (48–2047):");
|
2025-05-15 12:00:35 +01:00
|
|
|
|
}
|
2025-06-12 11:24:27 +01:00
|
|
|
|
|
2025-06-10 20:40:45 +01:00
|
|
|
|
return last_throttle;
|
2025-06-11 08:29:59 +01:00
|
|
|
|
}
|