DShotRMT/examples/dshot300/dshot300.ino

56 lines
1.4 KiB
Arduino
Raw Normal View History

2023-04-13 20:10:51 +01:00
/*
* Title: dshot300.ino
* Author: derdoktor667
* Date: 2023-04-13
2023-04-15 23:50:32 +01:00
*
2023-04-13 20:10:51 +01:00
* Description: A simple example of using the DShotRMT library to
2023-04-15 23:50:32 +01:00
* generate a DShot300 signal for blheli_s escs.
2023-04-13 20:10:51 +01:00
*/
2021-06-30 02:06:02 +01:00
#include <Arduino.h>
#include "DShotRMT.h"
2021-06-30 02:06:02 +01:00
2023-04-13 20:10:51 +01:00
// USB serial port needed for this example
2023-04-15 07:12:37 +01:00
const auto USB_SERIAL_BAUD = 115200;
2023-04-13 20:10:51 +01:00
#define USB_Serial Serial
2021-06-30 02:06:02 +01:00
2023-04-13 20:10:51 +01:00
// Define the GPIO pin connected to the motor and the DShot protocol used
2023-04-15 07:12:37 +01:00
const auto MOTOR01_PIN = GPIO_NUM_4;
const auto DSHOT_MODE = DSHOT300;
2023-04-13 20:10:51 +01:00
// Define the failsafe and initial throttle values
2023-04-15 07:12:37 +01:00
const auto FAILSAFE_THROTTLE = 999;
const auto INITIAL_THROTTLE = 48;
2021-06-30 02:06:02 +01:00
2023-04-13 20:10:51 +01:00
// Initialize a DShotRMT object for the motor
DShotRMT motor01(MOTOR01_PIN, RMT_CHANNEL_0);
2021-06-30 02:06:02 +01:00
2022-11-25 15:08:58 +00:00
void setup()
{
USB_Serial.begin(USB_SERIAL_BAUD);
2023-04-13 20:10:51 +01:00
// Start generating DShot signal for the motor
motor01.begin(DSHOT_MODE);
2021-06-30 02:06:02 +01:00
}
2022-11-25 15:08:58 +00:00
void loop()
{
2023-04-13 20:10:51 +01:00
// Read the throttle value from the USB serial input
auto throttle_input = readSerialThrottle();
2023-04-13 20:10:51 +01:00
// Set the throttle value to either the value received from the serial input or the failsafe throttle value
auto throttle_value = (throttle_input > 0) ? throttle_input : FAILSAFE_THROTTLE;
2021-06-30 05:43:07 +01:00
// Send the throttle value to the motor
motor01.sendThrottleValue(throttle_value);
2021-06-30 02:06:02 +01:00
}
2023-04-13 20:10:51 +01:00
// Read the throttle value from the USB serial input
uint16_t readSerialThrottle()
2022-11-25 15:08:58 +00:00
{
if (USB_Serial.available() > 0)
{
return USB_Serial.readStringUntil('\n').toInt();
}
2021-06-30 02:06:02 +01:00
}