diff --git a/applications/app_sten.c b/applications/app_sten.c index 13b8bdbf..5219d9e9 100644 --- a/applications/app_sten.c +++ b/applications/app_sten.c @@ -50,6 +50,7 @@ static volatile systime_t last_uart_update_time; // Private functions static void set_output(float output); +static uint16_t middle_of_3(uint16_t a, uint16_t b, uint16_t c); /* * This callback is invoked when a transmission buffer has been completely @@ -82,7 +83,15 @@ static void rxerr(UARTDriver *uartp, uartflags_t e) { static void rxchar(UARTDriver *uartp, uint16_t c) { (void)uartp; - out_received = ((float)c / 128) - 1.0; + static uint16_t c1 = 128; + static uint16_t c2 = 128; + + uint16_t med = middle_of_3(c, c1, c2); + + c2 = c1; + c1 = c; + + out_received = ((float)med / 128) - 1.0; last_uart_update_time = chTimeNow(); } @@ -183,4 +192,17 @@ static void set_output(float output) { } } +static uint16_t middle_of_3(uint16_t a, uint16_t b, uint16_t c) { + uint16_t middle; + + if ((a <= b) && (a <= c)) { + middle = (b <= c) ? b : c; + } else if ((b <= a) && (b <= c)) { + middle = (a <= c) ? a : c; + } else { + middle = (a <= b) ? a : b; + } + return middle; +} + #endif diff --git a/comm.c b/comm.c index 156b84a3..3c16781d 100644 --- a/comm.c +++ b/comm.c @@ -245,8 +245,6 @@ void comm_printf(char* format, ...) { if(len>0) { packet_send_packet((unsigned char*)print_buffer, (len<254)? len+1: 255, 0); } - - return; } void comm_send_samples(uint8_t *data, int len) { diff --git a/mcconf/mcconf_sten.h b/mcconf/mcconf_sten.h index aed304f2..858b6e78 100644 --- a/mcconf/mcconf_sten.h +++ b/mcconf/mcconf_sten.h @@ -30,7 +30,8 @@ */ #define MCPWM_CURRENT_MAX 35.0 // Current limit in Amperes (Upper) #define MCPWM_CURRENT_MIN -30.0 // Current limit in Amperes (Lower) -#define MCPWM_MAX_ABS_CURRENT 100.0 // The maximum absolute current above which a fault is generated +#define MCPWM_MAX_ABS_CURRENT 80.0 // The maximum absolute current above which a fault is generated +#define MCPWM_SLOW_ABS_OVERCURRENT 1 // Use the filtered (and hence slower) current for the overcurrent fault detection #define MCPWM_IN_CURRENT_MAX 25.0 // Input current limit in Amperes (Upper) #define MCPWM_IN_CURRENT_MIN -20.0 // Input current limit in Amperes (Lower) #define MCPWM_RPM_MAX 50000.0 // The motor speed limit (Upper) diff --git a/mcpwm.c b/mcpwm.c index 77fca0d3..857937c2 100644 --- a/mcpwm.c +++ b/mcpwm.c @@ -1939,10 +1939,10 @@ static void commutate(void) { } static void set_next_timer_settings(mc_timer_struct *settings) { - memcpy((void*)&timer_struct, settings, sizeof(mc_timer_struct)); - chSysLock(); + memcpy((void*)&timer_struct, settings, sizeof(mc_timer_struct)); + volatile uint32_t cnt = TIM1->CNT; volatile uint32_t top = TIM1->ARR;