slight refactoring of PPM/PWM failsafe to make it actually work and honor failsafeThreshold value.

This commit is contained in:
dongie 2014-05-02 16:12:31 +09:00
parent cce4d4975d
commit 2d248676f5
4 changed files with 26 additions and 24 deletions

View File

@ -242,13 +242,27 @@ static pwmPortData_t *pwmInConfig(uint8_t port, timerCCCallbackPtr callback, uin
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)
{
uint16_t diff;
static uint16_t now;
static uint16_t last = 0;
static uint8_t chan = 0;
static uint8_t GoodPulses;
last = now;
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."
chan = 0;
} 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;
if (chan < 4 && diff > failsafeThreshold)
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;
}
failsafeCheck(chan, diff);
}
chan++;
failsafeCnt = 0;
}
}
@ -284,12 +289,13 @@ static void pwmCallback(uint8_t port, uint16_t capture)
pwmPorts[port].fall = capture;
// compute capture
pwmPorts[port].capture = pwmPorts[port].fall - pwmPorts[port].rise;
captures[pwmPorts[port].channel] = pwmPorts[port].capture;
if (pwmPorts[port].capture > PULSE_MIN && pwmPorts[port].capture < PULSE_MAX) { // valid pulse width
captures[pwmPorts[port].channel] = pwmPorts[port].capture;
failsafeCheck(pwmPorts[port].channel, pwmPorts[port].capture);
}
// switch state
pwmPorts[port].state = 0;
pwmICConfig(timerHardware[port].tim, timerHardware[port].channel, TIM_ICPolarity_Rising);
// reset failsafe
failsafeCnt = 0;
}
}

View File

@ -3,7 +3,9 @@
#define MAX_MOTORS 12
#define MAX_SERVOS 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 {
bool enableInput;

View File

@ -85,7 +85,7 @@ static void ppmIRQHandler(TIM_TypeDef *tim)
if (diff > 4000) {
chan = 0;
} 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;
if (chan < 4 && diff > FAILSAFE_DETECT_TRESHOLD)
GoodPulses |= (1 << chan); // if signal is valid - mark channel as OK

View File

@ -226,13 +226,7 @@ void annexCode(void)
uint16_t pwmReadRawRC(uint8_t chan)
{
uint16_t data;
data = pwmRead(mcfg.rcmap[chan]);
if (data < 750 || data > 2250)
data = mcfg.midrc;
return data;
return pwmRead(mcfg.rcmap[chan]);
}
void computeRC(void)