mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-15 12:25:20 +03:00
slight refactoring of PPM/PWM failsafe to make it actually work and honor failsafeThreshold value.
This commit is contained in:
parent
cce4d4975d
commit
2d248676f5
4 changed files with 26 additions and 24 deletions
|
@ -242,13 +242,27 @@ static pwmPortData_t *pwmInConfig(uint8_t port, timerCCCallbackPtr callback, uin
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void failsafeCheck(uint8_t channel, uint16_t pulse)
|
||||||
|
{
|
||||||
|
static uint8_t goodPulses;
|
||||||
|
|
||||||
|
if (channel < 4 && pulse > failsafeThreshold)
|
||||||
|
goodPulses |= (1 << channel); // if signal is valid - mark channel as OK
|
||||||
|
if (goodPulses == 0x0F) { // If first four chanells have good pulses, clear FailSafe counter
|
||||||
|
goodPulses = 0;
|
||||||
|
if (failsafeCnt > 20)
|
||||||
|
failsafeCnt -= 20;
|
||||||
|
else
|
||||||
|
failsafeCnt = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void ppmCallback(uint8_t port, uint16_t capture)
|
static void ppmCallback(uint8_t port, uint16_t capture)
|
||||||
{
|
{
|
||||||
uint16_t diff;
|
uint16_t diff;
|
||||||
static uint16_t now;
|
static uint16_t now;
|
||||||
static uint16_t last = 0;
|
static uint16_t last = 0;
|
||||||
static uint8_t chan = 0;
|
static uint8_t chan = 0;
|
||||||
static uint8_t GoodPulses;
|
|
||||||
|
|
||||||
last = now;
|
last = now;
|
||||||
now = capture;
|
now = capture;
|
||||||
|
@ -257,20 +271,11 @@ static void ppmCallback(uint8_t port, uint16_t capture)
|
||||||
if (diff > 2700) { // Per http://www.rcgroups.com/forums/showpost.php?p=21996147&postcount=3960 "So, if you use 2.5ms or higher as being the reset for the PPM stream start, you will be fine. I use 2.7ms just to be safe."
|
if (diff > 2700) { // Per http://www.rcgroups.com/forums/showpost.php?p=21996147&postcount=3960 "So, if you use 2.5ms or higher as being the reset for the PPM stream start, you will be fine. I use 2.7ms just to be safe."
|
||||||
chan = 0;
|
chan = 0;
|
||||||
} else {
|
} else {
|
||||||
if (diff > 750 && diff < 2250 && chan < MAX_INPUTS) { // 750 to 2250 ms is our 'valid' channel range
|
if (diff > PULSE_MIN && diff < PULSE_MAX && chan < MAX_INPUTS) { // 750 to 2250 ms is our 'valid' channel range
|
||||||
captures[chan] = diff;
|
captures[chan] = diff;
|
||||||
if (chan < 4 && diff > failsafeThreshold)
|
failsafeCheck(chan, diff);
|
||||||
GoodPulses |= (1 << chan); // if signal is valid - mark channel as OK
|
|
||||||
if (GoodPulses == 0x0F) { // If first four chanells have good pulses, clear FailSafe counter
|
|
||||||
GoodPulses = 0;
|
|
||||||
if (failsafeCnt > 20)
|
|
||||||
failsafeCnt -= 20;
|
|
||||||
else
|
|
||||||
failsafeCnt = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
chan++;
|
chan++;
|
||||||
failsafeCnt = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -284,12 +289,13 @@ static void pwmCallback(uint8_t port, uint16_t capture)
|
||||||
pwmPorts[port].fall = capture;
|
pwmPorts[port].fall = capture;
|
||||||
// compute capture
|
// compute capture
|
||||||
pwmPorts[port].capture = pwmPorts[port].fall - pwmPorts[port].rise;
|
pwmPorts[port].capture = pwmPorts[port].fall - pwmPorts[port].rise;
|
||||||
|
if (pwmPorts[port].capture > PULSE_MIN && pwmPorts[port].capture < PULSE_MAX) { // valid pulse width
|
||||||
captures[pwmPorts[port].channel] = pwmPorts[port].capture;
|
captures[pwmPorts[port].channel] = pwmPorts[port].capture;
|
||||||
|
failsafeCheck(pwmPorts[port].channel, pwmPorts[port].capture);
|
||||||
|
}
|
||||||
// switch state
|
// switch state
|
||||||
pwmPorts[port].state = 0;
|
pwmPorts[port].state = 0;
|
||||||
pwmICConfig(timerHardware[port].tim, timerHardware[port].channel, TIM_ICPolarity_Rising);
|
pwmICConfig(timerHardware[port].tim, timerHardware[port].channel, TIM_ICPolarity_Rising);
|
||||||
// reset failsafe
|
|
||||||
failsafeCnt = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,8 @@
|
||||||
#define MAX_SERVOS 8
|
#define MAX_SERVOS 8
|
||||||
#define MAX_INPUTS 8
|
#define MAX_INPUTS 8
|
||||||
#define PULSE_1MS (1000) // 1ms pulse width
|
#define PULSE_1MS (1000) // 1ms pulse width
|
||||||
|
#define PULSE_MIN (750) // minimum PWM pulse width which is considered valid
|
||||||
|
#define PULSE_MAX (2250) // maximum PWM pulse width which is considered valid
|
||||||
|
|
||||||
typedef struct drv_pwm_config_t {
|
typedef struct drv_pwm_config_t {
|
||||||
bool enableInput;
|
bool enableInput;
|
||||||
|
|
|
@ -85,7 +85,7 @@ static void ppmIRQHandler(TIM_TypeDef *tim)
|
||||||
if (diff > 4000) {
|
if (diff > 4000) {
|
||||||
chan = 0;
|
chan = 0;
|
||||||
} else {
|
} else {
|
||||||
if (diff > 750 && diff < 2250 && chan < 8) { // 750 to 2250 ms is our 'valid' channel range
|
if (diff > PULSE_MIN && diff < PULSE_MAX && chan < 8) { // 750 to 2250 ms is our 'valid' channel range
|
||||||
Inputs[chan].capture = diff;
|
Inputs[chan].capture = diff;
|
||||||
if (chan < 4 && diff > FAILSAFE_DETECT_TRESHOLD)
|
if (chan < 4 && diff > FAILSAFE_DETECT_TRESHOLD)
|
||||||
GoodPulses |= (1 << chan); // if signal is valid - mark channel as OK
|
GoodPulses |= (1 << chan); // if signal is valid - mark channel as OK
|
||||||
|
|
8
src/mw.c
8
src/mw.c
|
@ -226,13 +226,7 @@ void annexCode(void)
|
||||||
|
|
||||||
uint16_t pwmReadRawRC(uint8_t chan)
|
uint16_t pwmReadRawRC(uint8_t chan)
|
||||||
{
|
{
|
||||||
uint16_t data;
|
return pwmRead(mcfg.rcmap[chan]);
|
||||||
|
|
||||||
data = pwmRead(mcfg.rcmap[chan]);
|
|
||||||
if (data < 750 || data > 2250)
|
|
||||||
data = mcfg.midrc;
|
|
||||||
|
|
||||||
return data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void computeRC(void)
|
void computeRC(void)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue