Update dshot300.ino

Better structure and easier to understand.
This commit is contained in:
Wastl Kraus 2023-03-27 19:55:30 +02:00
parent 640604c146
commit 17b0fd017d
1 changed files with 23 additions and 15 deletions

View File

@ -1,49 +1,57 @@
// ...some very simple DShot example generating a DShot300 signal. // ...some very simple DShot example generating a DShot300 signal.
#include <Arduino.h> #include <Arduino.h>
#include <DShotRMT.h> #include <DShotRMT.h>
// ...clearly name usb port // Define USB serial port if available
#ifdef SERIAL #ifdef SERIAL
#define USB_Serial Serial #define USB_Serial Serial
constexpr auto USB_SERIAL_BAUD = 115200; constexpr auto USB_SERIAL_BAUD = 115200;
#endif // SERIAL #endif // SERIAL
DShotRMT motor01(GPIO_NUM_4, RMT_CHANNEL_0); // Define motor pin and DShot protocol
constexpr auto MOTOR_PIN = GPIO_NUM_4;
constexpr auto DSHOT_MODE = DSHOT300;
volatile uint16_t throttle_value = 0x30; // ...sending "48", the first throttle value // Define failsafe and initial throttle value
constexpr auto FAILSAVE_THROTTLE = 0x3E7; constexpr auto FAILSAFE_THROTTLE = 0x3E7;
constexpr auto INITIAL_THROTTLE = 0x30;
// Initialize DShotRMT object for the motor
DShotRMT motor01(MOTOR_PIN, RMT_CHANNEL_0);
void setup() void setup()
{ {
// ...always start the onboard usb support // Start USB serial port
USB_Serial.begin(USB_SERIAL_BAUD); USB_Serial.begin(USB_SERIAL_BAUD);
// ...start the dshot generation // Start DShot signal generation
motor01.begin(DSHOT300); motor01.begin(DSHOT_MODE);
} }
void loop() void loop()
{ {
readSerialThrottle(); // Read throttle value from serial input
auto throttle_input = readSerialThrottle();
// Set the throttle value
auto throttle_value = (throttle_input > 0) ? throttle_input : FAILSAFE_THROTTLE;
// Send the throttle value to the motor
motor01.sendThrottleValue(throttle_value); motor01.sendThrottleValue(throttle_value);
// ...print to console // Print the throttle value to the serial console
USB_Serial.println(throttle_value); USB_Serial.println(throttle_value);
} }
// // Read throttle value from USB serial input
//
uint16_t readSerialThrottle() uint16_t readSerialThrottle()
{ {
if (USB_Serial.available() > 0) if (USB_Serial.available() > 0)
{ {
auto throttle_input = (USB_Serial.readStringUntil('\n')).toInt(); return USB_Serial.readStringUntil('\n').toInt();
return throttle_input;
} }
else else
{ {
return FAILSAVE_THROTTLE; return 0;
} }
} }