Add electrical specs to motor structs.

More #defines for PWM settings.
This commit is contained in:
2025-10-05 15:06:14 -05:00
parent f8d98bda05
commit fb42baa4c3
5 changed files with 55 additions and 32 deletions

View File

@@ -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

View File

@@ -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;

View File

@@ -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() {