diff --git a/BLDC-Motor-Controller.ioc b/BLDC-Motor-Controller.ioc index 51aa86d..40c22c7 100755 --- a/BLDC-Motor-Controller.ioc +++ b/BLDC-Motor-Controller.ioc @@ -212,14 +212,14 @@ TIM1.Channel-PWM\ Generation2\ CH2\ CH2N=TIM_CHANNEL_2 TIM1.Channel-PWM\ Generation3\ CH3\ CH3N=TIM_CHANNEL_3 TIM1.DeadTime=PWM_DEADTIME_COUNTS TIM1.IPParameters=Period,Prescaler,BreakState,AutomaticOutput,Channel-PWM Generation2 CH2 CH2N,OCFastMode_PWM-PWM Generation2 CH2 CH2N,DeadTime,OffStateRunMode,OffStateIDLEMode,Channel-PWM Generation1 CH1 CH1N,Channel-PWM Generation3 CH3 CH3N,OCFastMode_PWM-PWM Generation1 CH1 CH1N,OCFastMode_PWM-PWM Generation3 CH3 CH3N -TIM1.IPParametersWithoutCheck=DeadTime,Period +TIM1.IPParametersWithoutCheck=DeadTime,Prescaler,Period TIM1.OCFastMode_PWM-PWM\ Generation1\ CH1\ CH1N=TIM_OCFAST_ENABLE TIM1.OCFastMode_PWM-PWM\ Generation2\ CH2\ CH2N=TIM_OCFAST_ENABLE TIM1.OCFastMode_PWM-PWM\ Generation3\ CH3\ CH3N=TIM_OCFAST_ENABLE TIM1.OffStateIDLEMode=TIM_OSSI_ENABLE TIM1.OffStateRunMode=TIM_OSSR_ENABLE TIM1.Period=PWM_COUNTS-1 -TIM1.Prescaler=1 +TIM1.Prescaler=TIM1_PRESCALER TIM2.IPParameters=Prescaler,Period TIM2.Period=999 TIM2.Prescaler=31 diff --git a/Core/Inc/motor_controller.h b/Core/Inc/motor_controller.h index 8f3e622..c7ed8b1 100644 --- a/Core/Inc/motor_controller.h +++ b/Core/Inc/motor_controller.h @@ -10,13 +10,24 @@ typedef struct { uint32_t U_Phase_TIM_Channel; uint32_t V_Phase_TIM_Channel; uint32_t W_Phase_TIM_Channel; -}Cont_Mot_Interface_TypeDef; +} Cont_Mot_PWMInterface_TypeDef; typedef struct { - Cont_Mot_Interface_TypeDef Motor_Interface; + uint32_t rpm_rated_speed; + uint32_t mA_rated_current; + uint8_t num_poles; + uint16_t v_rated_voltage; + uint16_t w_rated_power; + float vkrpm_bemf_constant; + uint32_t Nmm_rated_torque; +} Electrical_Specs_TypeDef; + +typedef struct { + Cont_Mot_PWMInterface_TypeDef PWM_Interface; + Electrical_Specs_TypeDef Electrical_Specs; } Motor_TypeDef; -void BLDC_Init(Cont_Mot_Interface_TypeDef Interface); +void BLDC_PWM_Init(Cont_Mot_PWMInterface_TypeDef* PWM_Interface); void BLDC_Loop(); #endif \ No newline at end of file diff --git a/Core/Src/main.c b/Core/Src/main.c index 4fe84f1..d440b0c 100755 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -36,9 +36,12 @@ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ -#define PWM_COUNTS 2000 - #define CLOCK_SPEED_MHZ 32UL + +#define TIM1_PRESCALER 1 +#define PWM_COUNTS 2000 +#define PWM_FREQUENCY (CLOCK_SPEED_MHZ * 1000UL / (TIM1_PRESCALER + 1UL) * 1000 / PWM_COUNTS) + #define PWM_DEADTIME_NS 1000UL #define PWM_DEADTIME_COUNTS (CLOCK_SPEED_MHZ * 1000UL / PWM_DEADTIME_NS) @@ -62,6 +65,24 @@ DMA_HandleTypeDef hdma_usart1_tx; /* USER CODE BEGIN PV */ +Motor_TypeDef Motor1 = { + .Electrical_Specs = { + .mA_rated_current = 4800, + .v_rated_voltage = 24, + .rpm_rated_speed = 3000, + .w_rated_power = 75, + .num_poles = 8, + .vkrpm_bemf_constant = 4.41, + .Nmm_rated_torque = 240, + }, + .PWM_Interface = { + .Driver_PWM_TIM = &htim1, + .U_Phase_TIM_Channel = TIM_CHANNEL_1, + .V_Phase_TIM_Channel = TIM_CHANNEL_2, + .W_Phase_TIM_Channel = TIM_CHANNEL_3, + } +}; + /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ @@ -127,28 +148,17 @@ int main(void) static uint32_t loops; HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4); - // HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); - // HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1); - // HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2); - // HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2); - // HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3); - // HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_3); - Cont_Mot_Interface_TypeDef MotorInterface = { - .Driver_PWM_TIM = &htim1, - .U_Phase_TIM_Channel = TIM_CHANNEL_1, - .V_Phase_TIM_Channel = TIM_CHANNEL_2, - .W_Phase_TIM_Channel = TIM_CHANNEL_3, - }; - - BLDC_Init(MotorInterface); - - HAL_ADC_Start(&hadc); + //These configure overcurrent protection. TODO HAL_GPIO_WritePin(OC_TH_STBY1_GPIO_Port, OC_TH_STBY1_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(OC_TH_STBY2_GPIO_Port, OC_TH_STBY2_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(PWM_LSW_GPIO_Port, PWM_LSW_Pin, GPIO_PIN_SET); + //Initialize the PWM for gate drivers. + BLDC_PWM_Init(&Motor1.PWM_Interface); + + //Start timer with interrupt for control loop. HAL_TIM_Base_Start_IT(&htim2); while (1) @@ -296,7 +306,7 @@ static void MX_TIM1_Init(void) /* USER CODE END TIM1_Init 1 */ htim1.Instance = TIM1; - htim1.Init.Prescaler = 1; + htim1.Init.Prescaler = TIM1_PRESCALER; htim1.Init.CounterMode = TIM_COUNTERMODE_UP; htim1.Init.Period = PWM_COUNTS-1; htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; diff --git a/Core/Src/motor_controller.c b/Core/Src/motor_controller.c index b272adb..647c8c5 100644 --- a/Core/Src/motor_controller.c +++ b/Core/Src/motor_controller.c @@ -3,13 +3,15 @@ #include "motor_controller.h" -void BLDC_Init(Cont_Mot_Interface_TypeDef Interface) { - HAL_TIM_PWM_Start(Interface.Driver_PWM_TIM, Interface.U_Phase_TIM_Channel); - HAL_TIMEx_PWMN_Start(Interface.Driver_PWM_TIM, Interface.U_Phase_TIM_Channel); - HAL_TIM_PWM_Start(Interface.Driver_PWM_TIM, Interface.V_Phase_TIM_Channel); - HAL_TIMEx_PWMN_Start(Interface.Driver_PWM_TIM, Interface.V_Phase_TIM_Channel); - HAL_TIM_PWM_Start(Interface.Driver_PWM_TIM, Interface.W_Phase_TIM_Channel); - HAL_TIMEx_PWMN_Start(Interface.Driver_PWM_TIM, Interface.W_Phase_TIM_Channel); +void BLDC_PWM_Init(Cont_Mot_PWMInterface_TypeDef* PWM_Interface) { + + //Start High-Side and Low-Side PWM Drivers + HAL_TIM_PWM_Start(PWM_Interface->Driver_PWM_TIM, PWM_Interface->U_Phase_TIM_Channel); + HAL_TIMEx_PWMN_Start(PWM_Interface->Driver_PWM_TIM, PWM_Interface->U_Phase_TIM_Channel); + HAL_TIM_PWM_Start(PWM_Interface->Driver_PWM_TIM, PWM_Interface->V_Phase_TIM_Channel); + HAL_TIMEx_PWMN_Start(PWM_Interface->Driver_PWM_TIM, PWM_Interface->V_Phase_TIM_Channel); + HAL_TIM_PWM_Start(PWM_Interface->Driver_PWM_TIM, PWM_Interface->W_Phase_TIM_Channel); + HAL_TIMEx_PWMN_Start(PWM_Interface->Driver_PWM_TIM, PWM_Interface->W_Phase_TIM_Channel); } void BLDC_Loop() { diff --git a/Makefile b/Makefile index afeb73a..3a00e3c 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ ########################################################################################################################## -# File automatically-generated by tool: [projectgenerator] version: [4.7.0-B52] date: [Sun Oct 05 13:27:16 CDT 2025] +# File automatically-generated by tool: [projectgenerator] version: [4.7.0-B52] date: [Sun Oct 05 14:55:26 CDT 2025] ########################################################################################################################## # ------------------------------------------------