...readability adaptions
This commit is contained in:
parent
44e5a5cffe
commit
16ab6a6ef2
107
DShotRMT.cpp
107
DShotRMT.cpp
|
|
@ -18,36 +18,6 @@ static constexpr dshot_timing_t DSHOT_TIMINGS[] = {
|
||||||
{16, 8, 6, 2, 3, 5} // DSHOT1200
|
{16, 8, 6, 2, 3, 5} // DSHOT1200
|
||||||
};
|
};
|
||||||
|
|
||||||
// --- HELPERS ---
|
|
||||||
void printDShotResult(dshot_result_t &result, Stream &output)
|
|
||||||
{
|
|
||||||
if (result.success)
|
|
||||||
{
|
|
||||||
output.printf("Staus: SUCCESS - %s\n", result.msg);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
output.printf("Status: FAILED - %s\n", result.msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
output.println(" ");
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
void printDShotTelemetry(dshot_telemetry_result_t &result, Stream &output)
|
|
||||||
{
|
|
||||||
if (result.success)
|
|
||||||
{
|
|
||||||
output.printf("Telemetry: eRPM=%u, Motor RPM=%u \n", result.erpm, result.motor_rpm);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
output.printf("Telemetry: FAILED - %s\n", result.msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
output.println(" ");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Constructor with GPIO number
|
// Constructor with GPIO number
|
||||||
DShotRMT::DShotRMT(gpio_num_t gpio, dshot_mode_t mode, bool is_bidirectional)
|
DShotRMT::DShotRMT(gpio_num_t gpio, dshot_mode_t mode, bool is_bidirectional)
|
||||||
: _gpio(gpio),
|
: _gpio(gpio),
|
||||||
|
|
@ -70,7 +40,8 @@ DShotRMT::DShotRMT(gpio_num_t gpio, dshot_mode_t mode, bool is_bidirectional)
|
||||||
_tx_channel_config{},
|
_tx_channel_config{},
|
||||||
_rx_channel_config{},
|
_rx_channel_config{},
|
||||||
_transmit_config{},
|
_transmit_config{},
|
||||||
_receive_config{}
|
_receive_config{},
|
||||||
|
_result{false, UNKNOWN_ERROR}
|
||||||
{
|
{
|
||||||
// Calculate frame timing including switch/pause time
|
// Calculate frame timing including switch/pause time
|
||||||
_frame_timer_us = _timing_config.frame_length_us + DSHOT_SWITCH_TIME;
|
_frame_timer_us = _timing_config.frame_length_us + DSHOT_SWITCH_TIME;
|
||||||
|
|
@ -123,40 +94,37 @@ DShotRMT::~DShotRMT()
|
||||||
// Init DShotRMT
|
// Init DShotRMT
|
||||||
dshot_result_t DShotRMT::begin()
|
dshot_result_t DShotRMT::begin()
|
||||||
{
|
{
|
||||||
// Result container
|
|
||||||
dshot_result_t result = {false, INIT_FAILED};
|
|
||||||
|
|
||||||
// Init RX channel first
|
// Init RX channel first
|
||||||
if (_is_bidirectional)
|
if (_is_bidirectional)
|
||||||
{
|
{
|
||||||
if (!_initRXChannel().success)
|
if (!_initRXChannel().success)
|
||||||
{
|
{
|
||||||
result.msg = RX_INIT_FAILED;
|
_result.msg = RX_INIT_FAILED;
|
||||||
return result;
|
return _result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init TX channel
|
// Init TX channel
|
||||||
if (!_initTXChannel().success)
|
if (!_initTXChannel().success)
|
||||||
{
|
{
|
||||||
result.msg = TX_INIT_FAILED;
|
_result.msg = TX_INIT_FAILED;
|
||||||
return result;
|
return _result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init DShot encoder
|
// Init DShot encoder
|
||||||
if (!_initDShotEncoder().success)
|
if (!_initDShotEncoder().success)
|
||||||
{
|
{
|
||||||
result.msg = ENCODER_INIT_FAILED;
|
_result.msg = ENCODER_INIT_FAILED;
|
||||||
return result;
|
return _result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bit positions precalculation
|
// Bit positions precalculation
|
||||||
_preCalculateBitPositions();
|
_preCalculateBitPositions();
|
||||||
|
|
||||||
result.success = true;
|
_result.success = true;
|
||||||
result.msg = INIT_SUCCESS;
|
_result.msg = INIT_SUCCESS;
|
||||||
|
|
||||||
return result;
|
return _result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init RMT TX channel
|
// Init RMT TX channel
|
||||||
|
|
@ -258,30 +226,24 @@ bool IRAM_ATTR DShotRMT::_rmt_rx_done_callback(rmt_channel_handle_t rmt_rx_chann
|
||||||
// Initialize DShot encoder
|
// Initialize DShot encoder
|
||||||
dshot_result_t DShotRMT::_initDShotEncoder()
|
dshot_result_t DShotRMT::_initDShotEncoder()
|
||||||
{
|
{
|
||||||
// Result container
|
|
||||||
dshot_result_t result = {false, ENCODER_INIT_FAILED};
|
|
||||||
|
|
||||||
// Create copy encoder configuration
|
// Create copy encoder configuration
|
||||||
rmt_copy_encoder_config_t encoder_config = {};
|
rmt_copy_encoder_config_t encoder_config = {};
|
||||||
|
|
||||||
// Create encoder instance
|
// Create encoder instance
|
||||||
if (rmt_new_copy_encoder(&encoder_config, &_dshot_encoder) != DSHOT_OK)
|
if (rmt_new_copy_encoder(&encoder_config, &_dshot_encoder) != DSHOT_OK)
|
||||||
{
|
{
|
||||||
return result;
|
return _result;
|
||||||
}
|
}
|
||||||
|
|
||||||
result.success = true;
|
_result.success = true;
|
||||||
result.msg = ENCODER_INIT_SUCCESS;
|
_result.msg = ENCODER_INIT_SUCCESS;
|
||||||
|
|
||||||
return result;
|
return _result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send throttle value
|
// Send throttle value
|
||||||
dshot_result_t DShotRMT::sendThrottle(uint16_t throttle)
|
dshot_result_t DShotRMT::sendThrottle(uint16_t throttle)
|
||||||
{
|
{
|
||||||
// Result container
|
|
||||||
dshot_result_t result = {false, UNKNOWN_ERROR};
|
|
||||||
|
|
||||||
// Special case: if throttle is 0, use sendCommand() instead
|
// Special case: if throttle is 0, use sendCommand() instead
|
||||||
if (throttle == 0)
|
if (throttle == 0)
|
||||||
{
|
{
|
||||||
|
|
@ -291,7 +253,7 @@ dshot_result_t DShotRMT::sendThrottle(uint16_t throttle)
|
||||||
// Log only if throttle is out of range and different from last time
|
// Log only if throttle is out of range and different from last time
|
||||||
if ((throttle < DSHOT_THROTTLE_MIN || throttle > DSHOT_THROTTLE_MAX) && throttle != _last_throttle)
|
if ((throttle < DSHOT_THROTTLE_MIN || throttle > DSHOT_THROTTLE_MAX) && throttle != _last_throttle)
|
||||||
{
|
{
|
||||||
result.msg = THROTTLE_NOT_IN_RANGE;
|
_result.msg = THROTTLE_NOT_IN_RANGE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Always store the original throttle value
|
// Always store the original throttle value
|
||||||
|
|
@ -307,14 +269,11 @@ dshot_result_t DShotRMT::sendThrottle(uint16_t throttle)
|
||||||
// Send DShot command to ESC
|
// Send DShot command to ESC
|
||||||
dshot_result_t DShotRMT::sendCommand(uint16_t command)
|
dshot_result_t DShotRMT::sendCommand(uint16_t command)
|
||||||
{
|
{
|
||||||
// Result container
|
|
||||||
dshot_result_t result = {false, UNKNOWN_ERROR};
|
|
||||||
|
|
||||||
// Validate command is within DShot specification range
|
// Validate command is within DShot specification range
|
||||||
if (command < DSHOT_CMD_MOTOR_STOP || command > DSHOT_CMD_MAX)
|
if (command < DSHOT_CMD_MOTOR_STOP || command > DSHOT_CMD_MAX)
|
||||||
{
|
{
|
||||||
result.msg = COMMAND_NOT_VALID;
|
_result.msg = COMMAND_NOT_VALID;
|
||||||
return result;
|
return _result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build packet and transmit
|
// Build packet and transmit
|
||||||
|
|
@ -622,3 +581,33 @@ void DShotRMT::printCpuInfo(Stream &output) const
|
||||||
output.printf("XTAL Freq = %lu MHz\n", getXtalFrequencyMhz());
|
output.printf("XTAL Freq = %lu MHz\n", getXtalFrequencyMhz());
|
||||||
output.printf("APB Freq = %lu Hz\n", getApbFrequency());
|
output.printf("APB Freq = %lu Hz\n", getApbFrequency());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- HELPERS ---
|
||||||
|
void printDShotResult(dshot_result_t &result, Stream &output)
|
||||||
|
{
|
||||||
|
if (result.success)
|
||||||
|
{
|
||||||
|
output.printf("Staus: SUCCESS - %s\n", result.msg);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
output.printf("Status: FAILED - %s\n", result.msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
output.println(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
void printDShotTelemetry(dshot_telemetry_result_t &result, Stream &output)
|
||||||
|
{
|
||||||
|
if (result.success)
|
||||||
|
{
|
||||||
|
output.printf("Telemetry: eRPM=%u, Motor RPM=%u \n", result.erpm, result.motor_rpm);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
output.printf("Telemetry: FAILED - %s\n", result.msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
output.println(" ");
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -203,8 +203,11 @@ private:
|
||||||
volatile bool _telemetry_ready_flag;
|
volatile bool _telemetry_ready_flag;
|
||||||
static bool IRAM_ATTR _rmt_rx_done_callback(rmt_channel_handle_t rmt_rx_channel, const rmt_rx_done_event_data_t *edata, void *user_data);
|
static bool IRAM_ATTR _rmt_rx_done_callback(rmt_channel_handle_t rmt_rx_channel, const rmt_rx_done_event_data_t *edata, void *user_data);
|
||||||
|
|
||||||
|
// --- DSHOT RESULT HANDLER ---
|
||||||
|
dshot_result_t _result;
|
||||||
|
|
||||||
// --- DSHOT DEFAULTS ---
|
// --- DSHOT DEFAULTS ---
|
||||||
static constexpr auto DSHOT_TELEMETRY_INVALID = (0xffff);
|
static constexpr auto const DSHOT_TELEMETRY_INVALID = (0xffff);
|
||||||
|
|
||||||
// --- CONSTANTS & ERROR MESSAGES ---
|
// --- CONSTANTS & ERROR MESSAGES ---
|
||||||
static constexpr bool DSHOT_OK = 0;
|
static constexpr bool DSHOT_OK = 0;
|
||||||
|
|
|
||||||
|
|
@ -122,7 +122,7 @@ void handleSerialInput(const String &input, uint16_t &throttle, bool &continuous
|
||||||
{
|
{
|
||||||
// Stop motor
|
// Stop motor
|
||||||
throttle = 0;
|
throttle = 0;
|
||||||
continuous_throttle = true; // kill motor for sure
|
continuous_throttle = true;
|
||||||
dshot_result_t result = motor01.sendCommand(DSHOT_CMD_MOTOR_STOP);
|
dshot_result_t result = motor01.sendCommand(DSHOT_CMD_MOTOR_STOP);
|
||||||
printDShotResult(result);
|
printDShotResult(result);
|
||||||
}
|
}
|
||||||
|
|
@ -171,7 +171,7 @@ void handleSerialInput(const String &input, uint16_t &throttle, bool &continuous
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
USB_SERIAL.printf("Invalid input: '%s'\n", input.c_str());
|
USB_SERIAL.printf("Invalid input: '%s'\n", input);
|
||||||
USB_SERIAL.printf("Valid throttle range: %d - %d\n", DSHOT_THROTTLE_MIN, DSHOT_THROTTLE_MAX);
|
USB_SERIAL.printf("Valid throttle range: %d - %d\n", DSHOT_THROTTLE_MIN, DSHOT_THROTTLE_MAX);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue