DShotRMT/examples/dshot300/dshot300.ino

117 lines
2.8 KiB
Arduino
Raw Normal View History

/**
* @file dshot300.ino
2025-06-12 23:45:48 +01:00
* @brief Demo sketch for DShotRMT library
* @author Wastl Kraus
2025-06-12 23:45:48 +01:00
* @date 2025-06-11
* @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
// USB serial port settings
2025-07-11 22:51:46 +01:00
constexpr auto &USB_SERIAL = Serial0;
constexpr auto USB_SERIAL_BAUD = 115200;
2021-06-30 02:06:02 +01:00
// Motor configuration
constexpr auto MOTOR01_PIN = GPIO_NUM_17;
constexpr auto DSHOT_MODE = DSHOT300;
// BiDirectional DShot Support (default: false)
2025-07-11 22:51:46 +01:00
constexpr auto IS_BIDIRECTIONAL = false;
2025-06-12 23:45:48 +01:00
2025-06-14 17:16:45 +01:00
// Motor Magnet count for RPM calculation
constexpr auto MOTOR01_MAGNET_COUNT = 14;
2025-06-12 23:45:48 +01:00
// Setup Motor Pin, DShot Mode and optional BiDirectional Support
DShotRMT motor01(MOTOR01_PIN, DSHOT_MODE, IS_BIDIRECTIONAL);
2021-06-30 02:06:02 +01:00
// Prints RPM and throttle every 2 seconds if BiDirectional is enabled
void printRPMPeriodically(uint16_t throttle);
// Reads throttle value from serial input
uint16_t readSerialThrottle();
//
2022-11-25 15:08:58 +00:00
void setup()
{
// Start the USB Serial Port
2025-07-11 22:51:46 +01:00
USB_SERIAL.begin(USB_SERIAL_BAUD);
2025-06-12 23:45:48 +01:00
// Initialize DShot Signal
motor01.begin();
// Arm ESC with minimum throttle
motor01.setThrottle(DSHOT_THROTTLE_MIN);
2025-06-12 23:45:48 +01:00
2025-07-11 22:51:46 +01:00
USB_SERIAL.println("**********************");
USB_SERIAL.println("DShotRMT Demo started.");
USB_SERIAL.println("Enter a throttle value (482047):");
2021-06-30 02:06:02 +01:00
}
//
2022-11-25 15:08:58 +00:00
void loop()
{
// Read value input from Serial
uint16_t throttle_input = readSerialThrottle();
// Send the value to the ESC
motor01.setThrottle(throttle_input);
// Print RPM if BiDirectional DShot is enabled
if (IS_BIDIRECTIONAL)
{
printRPMPeriodically(throttle_input);
}
2021-06-30 02:06:02 +01:00
}
// Reads throttle value from serial input
uint16_t readSerialThrottle()
2022-11-25 15:08:58 +00:00
{
static uint16_t last_throttle = DSHOT_THROTTLE_MIN;
2025-06-10 20:40:45 +01:00
2025-07-11 22:51:46 +01:00
if (USB_SERIAL.available() > 0)
2025-05-14 14:19:38 +01:00
{
2025-07-11 22:51:46 +01:00
String input = USB_SERIAL.readStringUntil('\n');
int throttle_input = input.toInt();
// Clamp the value to the DShot range
2025-06-13 20:50:00 +01:00
throttle_input = constrain(throttle_input, DSHOT_THROTTLE_MIN, DSHOT_THROTTLE_MAX);
if (throttle_input < DSHOT_THROTTLE_MIN || throttle_input > DSHOT_THROTTLE_MAX)
{
USB_SERIAL.println("Invalid input. Please enter a value between 48 and 2047");
}
else
{
last_throttle = throttle_input;
USB_SERIAL.print("Throttle set to: ");
USB_SERIAL.println(last_throttle);
}
USB_SERIAL.println("*********************************");
2025-07-11 22:51:46 +01:00
USB_SERIAL.println("Enter a throttle value (482047):");
}
2025-06-10 20:40:45 +01:00
return last_throttle;
2025-06-12 23:45:48 +01:00
}
// Prints RPM and throttle every 2 seconds
void printRPMPeriodically(uint16_t throttle)
{
static unsigned long last_print_time = 0;
unsigned long now = millis();
if (now - last_print_time >= 2000)
{
last_print_time = now;
uint32_t rpm = motor01.getMotorRPM(MOTOR01_MAGNET_COUNT);
USB_SERIAL.print("Throttle: ");
USB_SERIAL.print(throttle);
USB_SERIAL.print(" | RPM: ");
USB_SERIAL.println(rpm);
}
}