diff --git a/Inc/config.h b/Inc/config.h index 7ca79e4..8d5b158 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -7,6 +7,7 @@ #define DEAD_TIME 32 // PWM deadtime #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. #define TIMEOUT 5 // number of wrong / missing input commands before emergency off +#define A2BIT_CONV 50 // bits per A on ADC. Example: 50 = 1 A, 100 = 2 A, etc // ADC conversion time definitions #define ADC_CONV_TIME_1C5 (14) //Total ADC clock cycles / conversion = ( 1.5+12.5) @@ -93,13 +94,13 @@ // ############################### MOTOR CONTROL (overwrite) ######################### -#define CTRL_TYP_SEL 1 // [-] Control type selection: 0 = Commutation , 1 = FOC Field Oriented Control (default) -#define CTRL_MOD_REQ 1 // [-] Control mode request: 0 = Open mode, 1 = Voltage mode (default), 2 = Speed mode, 3 = Torque mode -#define DIAG_ENA 1 // [-] Motor Diagnostics enable flag: 0 = Disabled, 1 = Enabled (default) -#define FIELD_WEAK_ENA 0 // [-] Field Weakening enable flag: 0 = Disabled (default), 1 = Enabled -#define I_MOT_MAX (15 * 50) << 4 // [A] Maximum motor current (change only the first number, the rest is needed for fixed-point conversion) -#define DC_CUR_LIMIT 17 // [A] DC current limit in amps per motor. (Final current protection. Above this value, current chopping is applied. To avoid this make sure that DC_CUR_LIMIT = I_MOT_MAX + 2A ) -#define N_MOT_MAX 800 << 4 // [rpm] Maximum motor speed (change only the first number, the rest is needed for fixed-point conversion) +#define CTRL_TYP_SEL 1 // [-] Control type selection: 0 = Commutation , 1 = FOC Field Oriented Control (default) +#define CTRL_MOD_REQ 1 // [-] Control mode request: 0 = Open mode, 1 = Voltage mode (default), 2 = Speed mode, 3 = Torque mode +#define DIAG_ENA 1 // [-] Motor Diagnostics enable flag: 0 = Disabled, 1 = Enabled (default) +#define FIELD_WEAK_ENA 0 // [-] Field Weakening enable flag: 0 = Disabled (default), 1 = Enabled +#define I_MOT_MAX (15 * A2BIT_CONV) << 4 // [A] Maximum motor current limit (Change only the first number, the rest is needed for fixed-point conversion) +#define I_DC_MAX (17 * A2BIT_CONV) // [A] Maximum DC Link current limit (This is the final current protection. Above this value, current chopping is applied. To avoid this make sure that I_DC_MAX = I_MOT_MAX + 2A ) +#define N_MOT_MAX 800 << 4 // [rpm] Maximum motor speed (change only the first number, the rest is needed for fixed-point conversion) // GENERAL NOTES: diff --git a/Inc/defines.h b/Inc/defines.h index d99da31..131e756 100644 --- a/Inc/defines.h +++ b/Inc/defines.h @@ -123,15 +123,13 @@ #define DELAY_TIM_FREQUENCY_US 1000000 -#define MOTOR_AMP_CONV_DC_AMP 0.02f // A per bit (12) on ADC. - #define MILLI_R (R * 1000) #define MILLI_PSI (PSI * 1000) #define MILLI_V (V * 1000) #define NO 0 #define YES 1 -#define ABS(a) (((a) < 0.0f) ? -(a) : (a)) +#define ABS(a) (((a) < 0) ? -(a) : (a)) #define LIMIT(x, lowhigh) (((x) > (lowhigh)) ? (lowhigh) : (((x) < (-lowhigh)) ? (-lowhigh) : (x))) #define SAT(x, lowhigh) (((x) > (lowhigh)) ? (1.0f) : (((x) < (-lowhigh)) ? (-1.0f) : (0.0f))) #define SAT2(x, low, high) (((x) > (high)) ? (1.0f) : (((x) < (low)) ? (-1.0f) : (0.0f))) diff --git a/Src/bldc.c b/Src/bldc.c index 00904db..6ef9bcd 100644 --- a/Src/bldc.c +++ b/Src/bldc.c @@ -1,5 +1,5 @@ /* -* This file has been re-implemented FOC motor control. +* This file implements FOC motor control. * This control method offers superior performanace * compared to previous cummutation method. The new method features: * ► reduced noise and vibrations @@ -66,7 +66,7 @@ static uint32_t buzzerTimer = 0; uint8_t enable = 0; static uint8_t enableFin = 0; -static const uint16_t pwm_res = 64000000 / 2 / PWM_FREQ; // = 2000 +static const uint16_t pwm_res = 64000000 / 2 / PWM_FREQ; // = 2000 static uint16_t offsetcount = 0; static int16_t offsetrl1 = 2000; @@ -113,7 +113,7 @@ void DMA1_Channel1_IRQHandler(void) { curR_DC = (int16_t)(adc_buffer.dcr - offsetdcr); // Disable PWM when current limit is reached (current chopping) - if(ABS(curL_DC * MOTOR_AMP_CONV_DC_AMP) > DC_CUR_LIMIT || timeout > TIMEOUT || enable == 0) { + if(ABS(curL_DC) > I_DC_MAX || timeout > TIMEOUT || enable == 0) { LEFT_TIM->BDTR &= ~TIM_BDTR_MOE; //HAL_GPIO_WritePin(LED_PORT, LED_PIN, 1); } else { @@ -121,7 +121,7 @@ void DMA1_Channel1_IRQHandler(void) { //HAL_GPIO_WritePin(LED_PORT, LED_PIN, 0); } - if(ABS(curR_DC * MOTOR_AMP_CONV_DC_AMP) > DC_CUR_LIMIT || timeout > TIMEOUT || enable == 0) { + if(ABS(curR_DC) > I_DC_MAX || timeout > TIMEOUT || enable == 0) { RIGHT_TIM->BDTR &= ~TIM_BDTR_MOE; } else { RIGHT_TIM->BDTR |= TIM_BDTR_MOE;