DShotRMT/examples/dshot300/dshot300.ino

87 lines
2.0 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
// Debug output
static constexpr auto DEBUG = false;
// USB serial port settings
static constexpr auto &USB_SERIAL = Serial0;
static constexpr auto USB_SERIAL_BAUD = 115200;
2021-06-30 02:06:02 +01:00
// Motor configuration - 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 auto 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;
// BiDirectional DShot Support (default: false)
static constexpr auto IS_BIDIRECTIONAL = false;
2025-06-12 23:45:48 +01:00
// Motor magnet count for RPM calculation
static constexpr auto MOTOR01_MAGNET_COUNT = 14;
// Creates the motor instance
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()
{
// Starts the USB Serial Port
USB_SERIAL.begin(USB_SERIAL_BAUD);
// Initializes DShot Signal
motor01.begin();
// Print CPU Info
motor01.printCpuInfo();
2025-08-28 13:37:02 +01:00
USB_SERIAL.println(" ");
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()
{
/// ...safety first
static uint16_t throttle = DSHOT_CMD_MOTOR_STOP;
// Time Measurement
static uint32_t last_stats_print = 0;
// Read throttle value
if (USB_SERIAL.available() > 0)
{
throttle = USB_SERIAL.readStringUntil('\n').toInt();
}
// Send the current throttle value
motor01.sendThrottle(throttle);
// Print motor stats every 2 seconds
if (millis() - last_stats_print >= 2000)
{
motor01.printDShotInfo();
2025-07-30 13:44:01 +01:00
2025-08-31 10:43:48 +01:00
// Get Motor RPM
if (IS_BIDIRECTIONAL)
{
uint32_t rpm = motor01.getMotorRPM(MOTOR01_MAGNET_COUNT);
USB_SERIAL.printf("Motor RPM: %u\n", rpm);
}
// Time Stamp
last_stats_print = millis();
}
}