From 22984a7fd60b164609481768f952b8aa529c6f3d Mon Sep 17 00:00:00 2001 From: EmanuelFeru Date: Wed, 1 Jul 2020 19:50:32 +0200 Subject: [PATCH] Fixed PPM, PWM timeout - fixed the general timeout handling: there is no need to disable the MOSFETs timer because it will confuse the BLDC_controller, making him consider the timeout as a false MOSFET defective error. - renamed `timeout` to `timeoutCnt` for consistency and to ease the search #61 --- Inc/config.h | 2 +- Src/bldc.c | 6 ++---- Src/control.c | 14 +++++++------- Src/main.c | 22 ++++++++++++++-------- Src/util.c | 8 ++++---- 5 files changed, 28 insertions(+), 24 deletions(-) diff --git a/Inc/config.h b/Inc/config.h index d327114..3435575 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -31,7 +31,7 @@ #else #define DELAY_IN_MAIN_LOOP 5 // in ms. default 5. it is independent of all the timing critical stuff. do not touch if you do not know what you are doing. #endif -#define TIMEOUT 5 // number of wrong / missing input commands before emergency off +#define TIMEOUT 20 // number of wrong / missing input commands before emergency off #define A2BIT_CONV 50 // A to bit for current conversion on ADC. Example: 1 A = 50, 2 A = 100, etc // ADC conversion time definitions diff --git a/Src/bldc.c b/Src/bldc.c index 639d8bd..cf0c17c 100644 --- a/Src/bldc.c +++ b/Src/bldc.c @@ -57,8 +57,6 @@ volatile int pwmr = 0; extern volatile adc_buf_t adc_buffer; -extern volatile uint32_t timeout; - uint8_t buzzerFreq = 0; uint8_t buzzerPattern = 0; static uint32_t buzzerTimer = 0; @@ -116,13 +114,13 @@ void DMA1_Channel1_IRQHandler(void) { // Disable PWM when current limit is reached (current chopping) // This is the Level 2 of current protection. The Level 1 should kick in first given by I_MOT_MAX - if(ABS(curL_DC) > curDC_max || timeout > TIMEOUT || enable == 0) { + if(ABS(curL_DC) > curDC_max || enable == 0) { LEFT_TIM->BDTR &= ~TIM_BDTR_MOE; } else { LEFT_TIM->BDTR |= TIM_BDTR_MOE; } - if(ABS(curR_DC) > curDC_max || timeout > TIMEOUT || enable == 0) { + if(ABS(curR_DC) > curDC_max || enable == 0) { RIGHT_TIM->BDTR &= ~TIM_BDTR_MOE; } else { RIGHT_TIM->BDTR |= TIM_BDTR_MOE; diff --git a/Src/control.c b/Src/control.c index ce489df..8f0c541 100644 --- a/Src/control.c +++ b/Src/control.c @@ -10,7 +10,7 @@ TIM_HandleTypeDef TimHandle; TIM_HandleTypeDef TimHandle2; uint8_t ppm_count = 0; uint8_t pwm_count = 0; -uint32_t timeout = 100; +uint32_t timeoutCnt = 0; uint8_t nunchuk_data[6] = {0}; uint8_t i2cBuffer[2]; @@ -40,7 +40,7 @@ void PPM_ISR_Callback(void) { ppm_count = 0; } else if (ppm_count < PPM_NUM_CHANNELS && IN_RANGE(rc_delay, 900, 2100)){ - timeout = 0; + timeoutCnt = 0; ppm_captured_value_buffer[ppm_count++] = CLAMP(rc_delay, 1000, 2000) - 1000; } else { ppm_valid = false; @@ -113,7 +113,7 @@ void PWM_ISR_CH1_Callback(void) { } else { // Falling Edge interrupt -> measure pulse duration uint16_t rc_signal = TIM2->CNT - pwm_CNT_prev_ch1; if (IN_RANGE(rc_signal, 900, 2100)){ - timeout = 0; + timeoutCnt = 0; pwm_timeout_ch1 = 0; pwm_captured_ch1_value = CLAMP(rc_signal, 1000, 2000) - 1000; } @@ -132,7 +132,7 @@ void PWM_ISR_CH2_Callback(void) { } else { // Falling Edge interrupt -> measure pulse duration uint16_t rc_signal = TIM2->CNT - pwm_CNT_prev_ch2; if (IN_RANGE(rc_signal, 900, 2100)){ - timeout = 0; + timeoutCnt = 0; pwm_timeout_ch2 = 0; pwm_captured_ch2_value = CLAMP(rc_signal, 1000, 2000) - 1000; } @@ -143,7 +143,7 @@ void PWM_ISR_CH2_Callback(void) { void PWM_SysTick_Callback(void) { pwm_timeout_ch1++; pwm_timeout_ch2++; - // Stop after 500 ms without PPM signal + // Stop after 500 ms without PWM signal if(pwm_timeout_ch1 > 500) { pwm_captured_ch1_value = 500; pwm_timeout_ch1 = 0; @@ -228,11 +228,11 @@ void Nunchuk_Read(void) { HAL_I2C_Master_Transmit(&hi2c2,0xA4,(uint8_t*)i2cBuffer, 1, 10); HAL_Delay(3); if (HAL_I2C_Master_Receive(&hi2c2,0xA4,(uint8_t*)nunchuk_data, 6, 10) == HAL_OK) { - timeout = 0; + timeoutCnt = 0; } #ifndef TRANSPOTTER - if (timeout > 3) { + if (timeoutCnt > 3) { HAL_Delay(50); Nunchuk_Init(); } diff --git a/Src/main.c b/Src/main.c index bd9ad01..3bef838 100644 --- a/Src/main.c +++ b/Src/main.c @@ -65,6 +65,7 @@ extern int16_t cmd2; // normalized input value. -1000 to 1000 extern int16_t speedAvg; // Average measured speed extern int16_t speedAvgAbs; // Average measured speed in absolute +extern volatile uint32_t timeoutCnt; // Timeout counter for the General timeout (PPM, PWM, Nunchuck) extern uint8_t timeoutFlagADC; // Timeout Flag for for ADC Protection: 0 = OK, 1 = Problem detected (line disconnected or wrong ADC data) extern uint8_t timeoutFlagSerial; // Timeout Flag for Rx Serial command: 0 = OK, 1 = Problem detected (line disconnected or wrong Rx data) @@ -76,7 +77,6 @@ extern uint8_t buzzerPattern; // global variable for the buzzer patter extern uint8_t enable; // global variable for motor enable -extern volatile uint32_t timeout; // global variable for timeout extern int16_t batVoltage; // global variable for battery voltage #if defined(SIDEBOARD_SERIAL_USART2) @@ -236,6 +236,12 @@ int main(void) { } #endif + // ####### GENERAL TIMEOUT ####### + if (timeoutCnt > TIMEOUT) { // Bring the system to a Safe State + cmd1 = 0; + cmd2 = 0; + } + // ####### LOW-PASS FILTER ####### rateLimiter16(cmd1, RATE, &steerRateFixdt); rateLimiter16(cmd2, RATE, &speedRateFixdt); @@ -259,7 +265,7 @@ int main(void) { mixerFcn(speed << 4, steer << 4, &speedR, &speedL); // This function implements the equations above // ####### SET OUTPUTS (if the target change is less than +/- 100) ####### - if ((speedL > lastSpeedL-100 && speedL < lastSpeedL+100) && (speedR > lastSpeedR-100 && speedR < lastSpeedR+100) && timeout < TIMEOUT) { + if ((speedL > lastSpeedL-100 && speedL < lastSpeedL+100) && (speedR > lastSpeedR-100 && speedR < lastSpeedR+100)) { #ifdef INVERT_R_DIRECTION pwmr = speedR; #else @@ -308,10 +314,10 @@ int main(void) { enable = 0; } } - timeout = 0; + timeoutCnt = 0; } - if (timeout > TIMEOUT) { + if (timeoutCnt > TIMEOUT) { pwml = 0; pwmr = 0; enable = 0; @@ -345,7 +351,7 @@ int main(void) { #ifdef SUPPORT_LCD LCD_SetLocation(&lcd, 0, 0); LCD_WriteString(&lcd, "Nunchuk Control"); #endif - timeout = 0; + timeoutCnt = 0; HAL_Delay(1000); nunchuk_connected = 1; } @@ -452,7 +458,7 @@ int main(void) { enable = 0; buzzerFreq = 8; buzzerPattern = 1; - } else if (timeoutFlagADC || timeoutFlagSerial) { // beep in case of ADC or Serial timeout - fast beep + } else if (timeoutFlagADC || timeoutFlagSerial || timeoutCnt > TIMEOUT) { // beep in case of ADC timeout, Serial timeout or General timeout - fast beep buzzerFreq = 24; buzzerPattern = 1; } else if (TEMP_WARNING_ENABLE && board_temp_deg_c >= TEMP_WARNING) { // beep if mainboard gets hot @@ -479,7 +485,7 @@ int main(void) { if (abs(speedL) > 50 || abs(speedR) > 50) { inactivity_timeout_counter = 0; } else { - inactivity_timeout_counter ++; + inactivity_timeout_counter++; } if (inactivity_timeout_counter > (INACTIVITY_TIMEOUT * 60 * 1000) / (DELAY_IN_MAIN_LOOP + 1)) { // rest of main loop needs maybe 1ms poweroff(); @@ -490,7 +496,7 @@ int main(void) { lastSpeedL = speedL; lastSpeedR = speedR; main_loop_counter++; - timeout++; + timeoutCnt++; } } diff --git a/Src/util.c b/Src/util.c index 50334ee..b299e10 100644 --- a/Src/util.c +++ b/Src/util.c @@ -52,7 +52,7 @@ extern uint8_t buzzerPattern; // global variable for the buzzer patter extern uint8_t enable; // global variable for motor enable extern uint8_t nunchuk_data[6]; -extern volatile uint32_t timeout; // global variable for timeout +extern volatile uint32_t timeoutCnt; // global variable for general timeout counter extern volatile uint32_t main_loop_counter; #if defined(CONTROL_PPM_LEFT) || defined(CONTROL_PPM_RIGHT) @@ -746,7 +746,7 @@ void readCommand(void) { button1 = !HAL_GPIO_ReadPin(BUTTON1_PORT, BUTTON1_PIN); button2 = !HAL_GPIO_ReadPin(BUTTON2_PORT, BUTTON2_PIN); #endif - timeout = 0; + timeoutCnt = 0; #endif #if defined(CONTROL_SERIAL_USART2) || defined(CONTROL_SERIAL_USART3) @@ -776,7 +776,7 @@ void readCommand(void) { button1 = !HAL_GPIO_ReadPin(BUTTON1_PORT, BUTTON1_PIN); button2 = !HAL_GPIO_ReadPin(BUTTON2_PORT, BUTTON2_PIN); #endif - timeout = 0; + timeoutCnt = 0; #endif #if defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) @@ -798,7 +798,7 @@ void readCommand(void) { #endif #ifdef VARIANT_HOVERCAR - brakePressed = (uint8_t)(cmd1 > 50); + brakePressed = (uint8_t)(cmd1 > 50); #endif #ifdef VARIANT_TRANSPOTTER