1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-26 01:35:41 +03:00

Prevent DSHOT commands during protocol initialization/detection (#8927)

Prevent DSHOT commands during protocol initialization/detection
This commit is contained in:
Michael Keller 2019-09-23 18:57:44 +12:00 committed by GitHub
commit e2c569269a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 2 deletions

View file

@ -37,6 +37,7 @@
#include "drivers/dshot_command.h" #include "drivers/dshot_command.h"
#include "drivers/pwm_output.h" #include "drivers/pwm_output.h"
#define DSHOT_PROTOCOL_DETECTION_DELAY_MS 3000
#define DSHOT_INITIAL_DELAY_US 10000 #define DSHOT_INITIAL_DELAY_US 10000
#define DSHOT_COMMAND_DELAY_US 1000 #define DSHOT_COMMAND_DELAY_US 1000
#define DSHOT_ESCINFO_DELAY_US 12000 #define DSHOT_ESCINFO_DELAY_US 12000
@ -150,9 +151,18 @@ static bool allMotorsAreIdle(void)
return true; return true;
} }
bool dshotCommandsAreEnabled(void)
{
if (motorIsEnabled() && motorGetMotorEnableTimeMs() && millis() > motorGetMotorEnableTimeMs() + DSHOT_PROTOCOL_DETECTION_DELAY_MS) {
return true;
} else {
return false;
}
}
void dshotCommandWrite(uint8_t index, uint8_t motorCount, uint8_t command, bool blocking) void dshotCommandWrite(uint8_t index, uint8_t motorCount, uint8_t command, bool blocking)
{ {
if (!isMotorProtocolDshot() || (command > DSHOT_MAX_COMMAND) || dshotCommandQueueFull()) { if (!isMotorProtocolDshot() || !dshotCommandsAreEnabled() || (command > DSHOT_MAX_COMMAND) || dshotCommandQueueFull()) {
return; return;
} }

View file

@ -68,3 +68,4 @@ bool dshotCommandQueueEmpty(void);
bool dshotCommandIsProcessing(void); bool dshotCommandIsProcessing(void);
uint8_t dshotCommandGetCurrent(uint8_t index); uint8_t dshotCommandGetCurrent(uint8_t index);
bool dshotCommandOutputIsEnabled(uint8_t motorCount); bool dshotCommandOutputIsEnabled(uint8_t motorCount);
bool dshotCommandsAreEnabled(void);

View file

@ -50,6 +50,7 @@ void motorShutdown(void)
{ {
motorDevice->vTable.shutdown(); motorDevice->vTable.shutdown();
motorDevice->enabled = false; motorDevice->enabled = false;
motorDevice->motorEnableTimeMs = 0;
motorDevice->initialized = false; motorDevice->initialized = false;
delayMicroseconds(1500); delayMicroseconds(1500);
} }
@ -246,6 +247,7 @@ void motorDevInit(const motorDevConfig_t *motorConfig, uint16_t idlePulse, uint8
if (motorDevice) { if (motorDevice) {
motorDevice->count = motorCount; motorDevice->count = motorCount;
motorDevice->initialized = true; motorDevice->initialized = true;
motorDevice->motorEnableTimeMs = 0;
motorDevice->enabled = false; motorDevice->enabled = false;
} else { } else {
motorNullDevice.vTable = motorNullVTable; motorNullDevice.vTable = motorNullVTable;
@ -257,12 +259,14 @@ void motorDisable(void)
{ {
motorDevice->vTable.disable(); motorDevice->vTable.disable();
motorDevice->enabled = false; motorDevice->enabled = false;
motorDevice->motorEnableTimeMs = 0;
} }
void motorEnable(void) void motorEnable(void)
{ {
if (motorDevice->initialized && motorDevice->vTable.enable()) { if (motorDevice->initialized && motorDevice->vTable.enable()) {
motorDevice->enabled = true; motorDevice->enabled = true;
motorDevice->motorEnableTimeMs = millis();
} }
} }
@ -281,6 +285,13 @@ bool isMotorProtocolDshot(void)
return isDshot; return isDshot;
} }
#ifdef USE_DSHOT
timeMs_t motorGetMotorEnableTimeMs(void)
{
return motorDevice->motorEnableTimeMs;
}
#endif
#ifdef USE_DSHOT_BITBANG #ifdef USE_DSHOT_BITBANG
bool isDshotBitbangActive(const motorDevConfig_t *motorConfig) { bool isDshotBitbangActive(const motorDevConfig_t *motorConfig) {
return motorConfig->useDshotBitbang == DSHOT_BITBANG_ON || return motorConfig->useDshotBitbang == DSHOT_BITBANG_ON ||

View file

@ -22,6 +22,8 @@
#pragma once #pragma once
#include "common/time.h"
typedef enum { typedef enum {
PWM_TYPE_STANDARD = 0, PWM_TYPE_STANDARD = 0,
PWM_TYPE_ONESHOT125, PWM_TYPE_ONESHOT125,
@ -62,6 +64,7 @@ typedef struct motorDevice_s {
uint8_t count; uint8_t count;
bool initialized; bool initialized;
bool enabled; bool enabled;
timeMs_t motorEnableTimeMs;
} motorDevice_t; } motorDevice_t;
void motorPostInitNull(); void motorPostInitNull();
@ -85,6 +88,7 @@ void motorDisable(void);
void motorEnable(void); void motorEnable(void);
bool motorIsEnabled(void); bool motorIsEnabled(void);
bool motorIsMotorEnabled(uint8_t index); bool motorIsMotorEnabled(uint8_t index);
timeMs_t motorGetMotorEnableTimeMs(void);
void motorShutdown(void); // Replaces stopPwmAllMotors void motorShutdown(void); // Replaces stopPwmAllMotors
#ifdef USE_DSHOT_BITBANG #ifdef USE_DSHOT_BITBANG

View file

@ -220,7 +220,14 @@ void updateArmingStatus(void)
LED0_ON; LED0_ON;
} else { } else {
// Check if the power on arming grace time has elapsed // Check if the power on arming grace time has elapsed
if ((getArmingDisableFlags() & ARMING_DISABLED_BOOT_GRACE_TIME) && (millis() >= systemConfig()->powerOnArmingGraceTime * 1000)) { if ((getArmingDisableFlags() & ARMING_DISABLED_BOOT_GRACE_TIME) && (millis() >= systemConfig()->powerOnArmingGraceTime * 1000)
#ifdef USE_DSHOT
// We also need to prevent arming until it's possible to send DSHOT commands.
// Otherwise if the initial arming is in crash-flip the motor direction commands
// might not be sent.
&& dshotCommandsAreEnabled()
#endif
) {
// If so, unset the grace time arming disable flag // If so, unset the grace time arming disable flag
unsetArmingDisabled(ARMING_DISABLED_BOOT_GRACE_TIME); unsetArmingDisabled(ARMING_DISABLED_BOOT_GRACE_TIME);
} }