DShotRMT/examples/dshot300/dshot300.ino

109 lines
2.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @file dshot300.ino
* @brief Demo sketch for DShotRMT library
* @author Wastl Kraus
* @date 2025-06-11
* @license MIT
*/
#include <Arduino.h>
#include <DShotRMT.h>
// USB serial port settings
constexpr auto &USB_SERIAL = Serial0;
constexpr uint32_t USB_SERIAL_BAUD = 115200;
// Motor configuration
constexpr gpio_num_t MOTOR01_PIN = GPIO_NUM_17;
constexpr dshot_mode_t DSHOT_MODE = DSHOT300;
constexpr uint8_t MOTOR01_MAGNET_COUNT = 14;
// BiDirectional DShot Signal (default: false)
constexpr bool IS_BIDIRECTIONAL = false;
// Setup Motor Pin, DShot Mode and optional BiDirectional Support
DShotRMT motor01(MOTOR01_PIN, DSHOT_MODE, IS_BIDIRECTIONAL);
// 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();
//
void setup()
{
// Start the USB Serial Port
USB_SERIAL.begin(USB_SERIAL_BAUD);
// Initialize DShot Signal
motor01.begin();
USB_SERIAL.println("**********************");
USB_SERIAL.println("DShotRMT Demo started.");
USB_SERIAL.println("Enter a throttle value (48 2047):");
}
//
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);
}
}
// Reads throttle value from serial input
uint16_t readSerialThrottle()
{
static uint16_t last_throttle = DSHOT_THROTTLE_MIN;
if (USB_SERIAL.available() > 0)
{
String input = USB_SERIAL.readStringUntil('\n');
int throttle_input = input.toInt();
if (throttle_input < DSHOT_THROTTLE_MIN || throttle_input > DSHOT_THROTTLE_MAX)
{
USB_SERIAL.println("Invalid input. Please enter a value between 48 and 2047.");
return last_throttle;
}
else
{
last_throttle = throttle_input;
USB_SERIAL.print("Throttle set to: ");
USB_SERIAL.println(last_throttle);
}
USB_SERIAL.println("*********************************");
USB_SERIAL.println("Enter a throttle value (48 2047):");
}
return last_throttle;
}
// Prints RPM and throttle every 2 seconds
void printRPMPeriodically(uint16_t throttle)
{
static unsigned long last_print_time = 0;
if (millis() - last_print_time >= 2000)
{
uint32_t rpm = motor01.getMotorRPM(MOTOR01_MAGNET_COUNT);
USB_SERIAL.print("Throttle: ");
USB_SERIAL.print(throttle);
USB_SERIAL.print(" | RPM: ");
USB_SERIAL.println(rpm);
last_print_time = millis();
}
}