...keep things simple
...example sketch was getting out of hands
This commit is contained in:
parent
8edbe8c5eb
commit
c37a66d5a5
39
DShotRMT.cpp
39
DShotRMT.cpp
|
|
@ -9,13 +9,13 @@
|
||||||
#include "DShotRMT.h"
|
#include "DShotRMT.h"
|
||||||
|
|
||||||
// --- DShot Timings ---
|
// --- DShot Timings ---
|
||||||
// frame_length_us, ticks_per_bit, ticks_one_high, ticks_zero_high, ticks_zero_low, ticks_one_low
|
// frame_length_us, ticks_per_bit, ticks_one_high, ticks_one_low, ticks_zero_high, ticks_zero_low
|
||||||
constexpr dshot_timing_t DSHOT_TIMINGS[] = {
|
constexpr dshot_timing_t DSHOT_TIMINGS[] = {
|
||||||
{0, 0, 0, 0, 0, 0}, // DSHOT_OFF
|
{0, 0, 0, 0, 0, 0}, // DSHOT_OFF
|
||||||
{128, 64, 48, 24, 40, 16}, // DSHOT150
|
{128, 64, 48, 16, 24, 40}, // DSHOT150
|
||||||
{64, 32, 24, 12, 20, 8}, // DSHOT300
|
{64, 32, 24, 8, 12, 20}, // DSHOT300
|
||||||
{32, 16, 12, 6, 10, 4}, // DSHOT600
|
{32, 16, 12, 4, 6, 10}, // DSHOT600
|
||||||
{16, 8, 6, 3, 5, 2} // DSHOT1200
|
{16, 8, 6, 2, 3, 5} // DSHOT1200
|
||||||
};
|
};
|
||||||
|
|
||||||
// --- DShot Config ---
|
// --- DShot Config ---
|
||||||
|
|
@ -48,9 +48,10 @@ DShotRMT::DShotRMT(uint16_t pin_nr, dshot_mode_t mode, bool is_bidirectional):
|
||||||
mode,
|
mode,
|
||||||
is_bidirectional)
|
is_bidirectional)
|
||||||
{
|
{
|
||||||
|
// ...just to accept pin numbers and GPIO_NUMs
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init DShotRMT
|
// Setup and configure DShotRMT
|
||||||
bool DShotRMT::begin()
|
bool DShotRMT::begin()
|
||||||
{
|
{
|
||||||
// Init TX Channel
|
// Init TX Channel
|
||||||
|
|
@ -78,7 +79,7 @@ bool DShotRMT::begin()
|
||||||
return DSHOT_OK;
|
return DSHOT_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deprecated, use "sendThrottle() instead"
|
// Deprecated, use "sendThrottle()"" instead
|
||||||
bool DShotRMT::setThrottle(uint16_t throttle)
|
bool DShotRMT::setThrottle(uint16_t throttle)
|
||||||
{
|
{
|
||||||
return sendThrottle(throttle);
|
return sendThrottle(throttle);
|
||||||
|
|
@ -87,24 +88,17 @@ bool DShotRMT::setThrottle(uint16_t throttle)
|
||||||
// Sends a valid throttle value
|
// Sends a valid throttle value
|
||||||
bool DShotRMT::sendThrottle(uint16_t throttle)
|
bool DShotRMT::sendThrottle(uint16_t throttle)
|
||||||
{
|
{
|
||||||
static uint16_t last_throttle = DSHOT_THROTTLE_MIN;
|
// Make sure throttle value is valid by force
|
||||||
|
auto value = constrain(throttle, DSHOT_THROTTLE_MIN, DSHOT_THROTTLE_MAX);
|
||||||
|
|
||||||
// Precheck throttle value
|
// Converts throttle value to dshot packet RMT symbols
|
||||||
if (throttle < DSHOT_THROTTLE_MIN || throttle > DSHOT_THROTTLE_MAX)
|
_packet = _buildDShotPacket(value);
|
||||||
{
|
|
||||||
Serial.println(DSHOT_MSG_06);
|
|
||||||
return DSHOT_ERROR;
|
|
||||||
} else {
|
|
||||||
last_throttle = throttle;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Converts throttle value to dshot packet
|
|
||||||
_packet = _buildDShotPacket(last_throttle);
|
|
||||||
|
|
||||||
|
// Actually send the RMT symbols
|
||||||
return (_sendDShotFrame(_packet));
|
return (_sendDShotFrame(_packet));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deprecated, use "sendCommand() instead"
|
// Deprecated, use "sendCommand()"" instead
|
||||||
bool DShotRMT::sendDShotCommand(uint16_t command)
|
bool DShotRMT::sendDShotCommand(uint16_t command)
|
||||||
{
|
{
|
||||||
return sendCommand(command);
|
return sendCommand(command);
|
||||||
|
|
@ -192,8 +186,9 @@ bool DShotRMT::_initRXChannel()
|
||||||
_rx_channel_config.resolution_hz = DSHOT_RMT_RESOLUTION;
|
_rx_channel_config.resolution_hz = DSHOT_RMT_RESOLUTION;
|
||||||
_rx_channel_config.mem_block_symbols = DSHOT_SYMBOLS_SIZE;
|
_rx_channel_config.mem_block_symbols = DSHOT_SYMBOLS_SIZE;
|
||||||
|
|
||||||
_receive_config.signal_range_min_ns = 300;
|
// TODO: need to figure out
|
||||||
_receive_config.signal_range_max_ns = 5000;
|
_receive_config.signal_range_min_ns = 2;
|
||||||
|
_receive_config.signal_range_max_ns = 128;
|
||||||
|
|
||||||
// Creates and activates RMT TX Channel
|
// Creates and activates RMT TX Channel
|
||||||
if (rmt_new_rx_channel(&_rx_channel_config, &_rmt_rx_channel) != DSHOT_OK)
|
if (rmt_new_rx_channel(&_rx_channel_config, &_rmt_rx_channel) != DSHOT_OK)
|
||||||
|
|
|
||||||
|
|
@ -54,9 +54,9 @@ typedef struct dshot_timing_s
|
||||||
uint16_t frame_length_us;
|
uint16_t frame_length_us;
|
||||||
uint16_t ticks_per_bit;
|
uint16_t ticks_per_bit;
|
||||||
uint16_t ticks_one_high;
|
uint16_t ticks_one_high;
|
||||||
|
uint16_t ticks_one_low;
|
||||||
uint16_t ticks_zero_high;
|
uint16_t ticks_zero_high;
|
||||||
uint16_t ticks_zero_low;
|
uint16_t ticks_zero_low;
|
||||||
uint16_t ticks_one_low;
|
|
||||||
} dshot_timing_t;
|
} dshot_timing_t;
|
||||||
|
|
||||||
extern const dshot_timing_t DSHOT_TIMINGS[];
|
extern const dshot_timing_t DSHOT_TIMINGS[];
|
||||||
|
|
|
||||||
|
|
@ -10,13 +10,13 @@
|
||||||
#include <DShotRMT.h>
|
#include <DShotRMT.h>
|
||||||
|
|
||||||
// USB serial port settings
|
// USB serial port settings
|
||||||
static constexpr HardwareSerial &USB_SERIAL = Serial0;
|
static constexpr auto &USB_SERIAL = Serial0;
|
||||||
static constexpr uint32_t USB_SERIAL_BAUD = 115200;
|
static constexpr auto USB_SERIAL_BAUD = 115200;
|
||||||
|
|
||||||
// Motor configuration
|
// Motor configuration
|
||||||
// Pin number or GPIO_PIN
|
// Pin number or GPIO_PIN
|
||||||
// static constexpr gpio_num_t MOTOR01_PIN = GPIO_NUM_17;
|
// static constexpr gpio_num_t MOTOR01_PIN = GPIO_NUM_17;
|
||||||
static constexpr uint16_t MOTOR01_PIN = 17;
|
static constexpr auto MOTOR01_PIN = 17;
|
||||||
|
|
||||||
// Supported: DSHOT150, DSHOT300, DSHOT600, (DSHOT1200)
|
// Supported: DSHOT150, DSHOT300, DSHOT600, (DSHOT1200)
|
||||||
static constexpr dshot_mode_t DSHOT_MODE = DSHOT300;
|
static constexpr dshot_mode_t DSHOT_MODE = DSHOT300;
|
||||||
|
|
@ -25,10 +25,10 @@ static constexpr dshot_mode_t DSHOT_MODE = DSHOT300;
|
||||||
static constexpr bool IS_BIDIRECTIONAL = false;
|
static constexpr bool IS_BIDIRECTIONAL = false;
|
||||||
|
|
||||||
// Motor magnet count for RPM calculation
|
// Motor magnet count for RPM calculation
|
||||||
static constexpr uint8_t MOTOR01_MAGNET_COUNT = 14;
|
static constexpr auto MOTOR01_MAGNET_COUNT = 14;
|
||||||
|
|
||||||
//
|
//
|
||||||
//
|
// Create the motor instance
|
||||||
DShotRMT motor01(MOTOR01_PIN, DSHOT_MODE, IS_BIDIRECTIONAL);
|
DShotRMT motor01(MOTOR01_PIN, DSHOT_MODE, IS_BIDIRECTIONAL);
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
@ -51,48 +51,32 @@ void setup()
|
||||||
//
|
//
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
// Read value input from Serial
|
static auto throttle = DSHOT_THROTTLE_MIN;
|
||||||
uint16_t throttle_input = readSerialThrottle(USB_SERIAL);
|
|
||||||
|
if (USB_SERIAL.available() > NULL)
|
||||||
// Send the value to the ESC
|
|
||||||
motor01.sendThrottle(throttle_input);
|
|
||||||
|
|
||||||
// Prints out RPM if BiDirectional DShot is enabled every 2 seconds
|
|
||||||
printRPMPeriodically(2000);
|
|
||||||
|
|
||||||
// Prints out "raw" DShot packet every 2 seconds
|
|
||||||
// printTXPacket(2000);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reads throttle value from serial input
|
|
||||||
uint16_t readSerialThrottle(HardwareSerial &serial)
|
|
||||||
{
|
|
||||||
static uint16_t last_throttle = DSHOT_THROTTLE_MIN;
|
|
||||||
|
|
||||||
if (serial.available() > NULL)
|
|
||||||
{
|
{
|
||||||
// Reads a value
|
auto new_throttle = (USB_SERIAL.readStringUntil('\n').toInt());
|
||||||
uint16_t throttle = (serial.readStringUntil('\n').toInt());
|
|
||||||
|
|
||||||
// Check for valid throttle value
|
|
||||||
if (throttle < DSHOT_THROTTLE_MIN || throttle > DSHOT_THROTTLE_MAX)
|
|
||||||
{
|
|
||||||
USB_SERIAL.println("Throttle value not in range (48 - 2047)!");
|
|
||||||
return last_throttle;
|
|
||||||
}
|
|
||||||
|
|
||||||
last_throttle = throttle;
|
|
||||||
|
|
||||||
USB_SERIAL.println("*********************");
|
USB_SERIAL.println("*********************");
|
||||||
USB_SERIAL.print("Throttle set to: ");
|
USB_SERIAL.print("Throttle set to: ");
|
||||||
USB_SERIAL.println(last_throttle);
|
USB_SERIAL.println(new_throttle);
|
||||||
|
|
||||||
|
//
|
||||||
|
throttle = new_throttle;
|
||||||
}
|
}
|
||||||
|
|
||||||
return last_throttle;
|
// Sends the value to the ESC
|
||||||
|
motor01.sendThrottle(throttle);
|
||||||
|
|
||||||
|
// Prints out RPM if BiDirectional DShot is enabled every 2 seconds
|
||||||
|
// printRPMPeriodically(2000);
|
||||||
|
|
||||||
|
// Debug: Prints out "raw" DShot packet every 2 seconds
|
||||||
|
print_RMT_packet(2000);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints RPM every ms
|
// Prints RPM every ms
|
||||||
void printRPMPeriodically(uint16_t timer_ms)
|
void printRPMPeriodically(auto timer_ms)
|
||||||
{
|
{
|
||||||
if (IS_BIDIRECTIONAL)
|
if (IS_BIDIRECTIONAL)
|
||||||
{
|
{
|
||||||
|
|
@ -100,7 +84,7 @@ void printRPMPeriodically(uint16_t timer_ms)
|
||||||
|
|
||||||
if (millis() - last_print_time >= timer_ms)
|
if (millis() - last_print_time >= timer_ms)
|
||||||
{
|
{
|
||||||
uint32_t rpm = motor01.getMotorRPM(MOTOR01_MAGNET_COUNT);
|
auto rpm = motor01.getMotorRPM(MOTOR01_MAGNET_COUNT);
|
||||||
|
|
||||||
USB_SERIAL.print("RPM: ");
|
USB_SERIAL.print("RPM: ");
|
||||||
USB_SERIAL.println(rpm);
|
USB_SERIAL.println(rpm);
|
||||||
|
|
@ -111,13 +95,13 @@ void printRPMPeriodically(uint16_t timer_ms)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints "raw" packet every ms
|
// Prints "raw" packet every ms
|
||||||
void printTXPacket(uint16_t timer_ms)
|
void print_RMT_packet(auto timer_ms)
|
||||||
{
|
{
|
||||||
static unsigned long last_print_time = 0;
|
static unsigned long last_print_time = 0;
|
||||||
|
|
||||||
if (millis() - last_print_time >= timer_ms)
|
if (millis() - last_print_time >= timer_ms)
|
||||||
{
|
{
|
||||||
uint16_t packet = motor01.getDShotPacket();
|
auto packet = motor01.getDShotPacket();
|
||||||
|
|
||||||
// Print bit by bit
|
// Print bit by bit
|
||||||
for (int i = 15; i >= 0; --i)
|
for (int i = 15; i >= 0; --i)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue