2021-06-30 02:06:02 +01:00
|
|
|
// ...some very simple DShot example generating a DShot300 signal.
|
|
|
|
|
#include <Arduino.h>
|
2023-03-30 17:24:31 +01:00
|
|
|
#include "DShotRMT.h"
|
2021-06-30 02:06:02 +01:00
|
|
|
|
2023-03-27 18:55:30 +01:00
|
|
|
// Define USB serial port if available
|
2021-06-30 02:06:02 +01:00
|
|
|
#ifdef SERIAL
|
|
|
|
|
#define USB_Serial Serial
|
|
|
|
|
constexpr auto USB_SERIAL_BAUD = 115200;
|
|
|
|
|
#endif // SERIAL
|
|
|
|
|
|
2023-03-27 18:55:30 +01:00
|
|
|
// Define motor pin and DShot protocol
|
|
|
|
|
constexpr auto MOTOR_PIN = GPIO_NUM_4;
|
|
|
|
|
constexpr auto DSHOT_MODE = DSHOT300;
|
|
|
|
|
|
|
|
|
|
// Define failsafe and initial throttle value
|
|
|
|
|
constexpr auto FAILSAFE_THROTTLE = 0x3E7;
|
|
|
|
|
constexpr auto INITIAL_THROTTLE = 0x30;
|
2021-06-30 02:06:02 +01:00
|
|
|
|
2023-03-27 18:55:30 +01:00
|
|
|
// Initialize DShotRMT object for the motor
|
|
|
|
|
DShotRMT motor01(MOTOR_PIN, RMT_CHANNEL_0);
|
2021-06-30 02:06:02 +01:00
|
|
|
|
2022-11-25 15:08:58 +00:00
|
|
|
void setup()
|
|
|
|
|
{
|
2023-03-27 18:55:30 +01:00
|
|
|
// Start USB serial port
|
2023-03-27 18:47:23 +01:00
|
|
|
USB_Serial.begin(USB_SERIAL_BAUD);
|
|
|
|
|
|
2023-03-27 18:55:30 +01:00
|
|
|
// Start DShot signal generation
|
|
|
|
|
motor01.begin(DSHOT_MODE);
|
2021-06-30 02:06:02 +01:00
|
|
|
}
|
|
|
|
|
|
2022-11-25 15:08:58 +00:00
|
|
|
void loop()
|
|
|
|
|
{
|
2023-03-27 18:55:30 +01:00
|
|
|
// Read throttle value from serial input
|
|
|
|
|
auto throttle_input = readSerialThrottle();
|
|
|
|
|
|
|
|
|
|
// Set the throttle value
|
|
|
|
|
auto throttle_value = (throttle_input > 0) ? throttle_input : FAILSAFE_THROTTLE;
|
2021-06-30 05:43:07 +01:00
|
|
|
|
2023-03-27 18:55:30 +01:00
|
|
|
// Send the throttle value to the motor
|
2023-03-27 18:47:23 +01:00
|
|
|
motor01.sendThrottleValue(throttle_value);
|
2021-06-30 05:43:07 +01:00
|
|
|
|
2023-03-27 18:55:30 +01:00
|
|
|
// Print the throttle value to the serial console
|
2023-03-27 18:47:23 +01:00
|
|
|
USB_Serial.println(throttle_value);
|
2021-06-30 02:06:02 +01:00
|
|
|
}
|
|
|
|
|
|
2023-03-27 18:55:30 +01:00
|
|
|
// Read throttle value from USB serial input
|
2023-03-27 18:47:23 +01:00
|
|
|
uint16_t readSerialThrottle()
|
2022-11-25 15:08:58 +00:00
|
|
|
{
|
2023-03-27 18:47:23 +01:00
|
|
|
if (USB_Serial.available() > 0)
|
|
|
|
|
{
|
2023-03-27 18:55:30 +01:00
|
|
|
return USB_Serial.readStringUntil('\n').toInt();
|
2023-03-27 18:47:23 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-03-27 18:55:30 +01:00
|
|
|
return 0;
|
2023-03-27 18:47:23 +01:00
|
|
|
}
|
2021-06-30 02:06:02 +01:00
|
|
|
}
|