电机开环跑
This commit is contained in:
parent
b5f566c940
commit
2676f1efe1
@ -51,6 +51,8 @@ ${SRC_PATH}/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c
|
|||||||
${SRC_PATH}/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c
|
${SRC_PATH}/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c
|
||||||
${SRC_PATH}/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c
|
${SRC_PATH}/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c
|
||||||
${SRC_PATH}/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c
|
${SRC_PATH}/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c
|
||||||
|
|
||||||
|
${SRC_PATH}/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_tim.c
|
||||||
)
|
)
|
||||||
|
|
||||||
set(RTThread_SRC
|
set(RTThread_SRC
|
||||||
@ -92,7 +94,7 @@ ${SRC_PATH}/Bsp/stm32f4xx_it.c
|
|||||||
)
|
)
|
||||||
|
|
||||||
set(Foc_SRC
|
set(Foc_SRC
|
||||||
${SRC_PATH}/Foc/math.c
|
${SRC_PATH}/Foc/foc_math.c
|
||||||
${SRC_PATH}/Foc/hall.c
|
${SRC_PATH}/Foc/hall.c
|
||||||
${SRC_PATH}/Foc/svpwm.c
|
${SRC_PATH}/Foc/svpwm.c
|
||||||
)
|
)
|
||||||
@ -111,6 +113,7 @@ add_executable(${PROJECT_NAME}.elf
|
|||||||
#${RTThread_SRC}
|
#${RTThread_SRC}
|
||||||
#${FinSH_SRC}
|
#${FinSH_SRC}
|
||||||
${Common_SRC}
|
${Common_SRC}
|
||||||
|
${Foc_SRC}
|
||||||
${Bsp_SRC}
|
${Bsp_SRC}
|
||||||
${App_SRC}
|
${App_SRC}
|
||||||
)
|
)
|
||||||
|
@ -23,6 +23,26 @@ foc_handle_t foc_handle;
|
|||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
board_init();
|
board_init();
|
||||||
|
|
||||||
|
foc_handle.pwm_period = 168 * 1000000 / 16000;
|
||||||
|
// while (1)
|
||||||
|
// {
|
||||||
|
// int segment = 60;
|
||||||
|
// for (int i = 0; i <= segment; i++)
|
||||||
|
// {
|
||||||
|
// foc_handle.Vqd.d = 0;
|
||||||
|
// foc_handle.Vqd.q = (int16_t)(13.86f * 32767); // 24 / SQRT_3 = 13.86f
|
||||||
|
// // 电角度归一化[-PI, PI] -> Q15 [-32767, 32767]
|
||||||
|
// hall_handle.control_elec_angle = (int16_t)((i - (float)(segment / 2)) / (float)(segment / 2) * 32767);
|
||||||
|
// foc_handle.Valphabeta = math_rev_park(foc_handle.Vqd, hall_handle.control_elec_angle);
|
||||||
|
// svpwm(&foc_handle);
|
||||||
|
// LL_TIM_OC_SetCompareCH1(TIM1, foc_handle.pwmA);
|
||||||
|
// LL_TIM_OC_SetCompareCH2(TIM1, foc_handle.pwmB);
|
||||||
|
// LL_TIM_OC_SetCompareCH3(TIM1, foc_handle.pwmC);
|
||||||
|
// HAL_Delay(2);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
// // FOC控制流程
|
// // FOC控制流程
|
||||||
// foc_handle.Vqd.d = 0;
|
// foc_handle.Vqd.d = 0;
|
||||||
// foc_handle.Vqd.q = 1;
|
// foc_handle.Vqd.q = 1;
|
||||||
@ -39,7 +59,7 @@ int main(void)
|
|||||||
// foc_handle.Valphabeta = math_rev_park(foc_handle.Vqd, hall_handle.control_elec_angle);
|
// foc_handle.Valphabeta = math_rev_park(foc_handle.Vqd, hall_handle.control_elec_angle);
|
||||||
// svmpw(foc_handle);
|
// svmpw(foc_handle);
|
||||||
// 测试PWM输出
|
// 测试PWM输出
|
||||||
|
// __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 42 * 1000000 / 16000);
|
||||||
led_ctrl(1);
|
led_ctrl(1);
|
||||||
HAL_Delay(500);
|
HAL_Delay(500);
|
||||||
led_ctrl(0);
|
led_ctrl(0);
|
||||||
|
@ -71,11 +71,11 @@ static void MX_NVIC_Init(void)
|
|||||||
// HAL_NVIC_SetPriority(ADC_IRQn, 2, 0);
|
// HAL_NVIC_SetPriority(ADC_IRQn, 2, 0);
|
||||||
// HAL_NVIC_EnableIRQ(ADC_IRQn);
|
// HAL_NVIC_EnableIRQ(ADC_IRQn);
|
||||||
/* TIM1_UP_TIM10_IRQn interrupt configuration */
|
/* TIM1_UP_TIM10_IRQn interrupt configuration */
|
||||||
// HAL_NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 0, 0);
|
HAL_NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 0, 0);
|
||||||
// HAL_NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn);
|
HAL_NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn);
|
||||||
/* TIM1_BRK_TIM9_IRQn interrupt configuration */
|
/* TIM1_BRK_TIM9_IRQn interrupt configuration */
|
||||||
// HAL_NVIC_SetPriority(TIM1_BRK_TIM9_IRQn, 4, 1);
|
HAL_NVIC_SetPriority(TIM1_BRK_TIM9_IRQn, 4, 1);
|
||||||
// HAL_NVIC_EnableIRQ(TIM1_BRK_TIM9_IRQn);
|
HAL_NVIC_EnableIRQ(TIM1_BRK_TIM9_IRQn);
|
||||||
/* TIM3_IRQn interrupt configuration */
|
/* TIM3_IRQn interrupt configuration */
|
||||||
HAL_NVIC_SetPriority(TIM3_IRQn, 3, 0);
|
HAL_NVIC_SetPriority(TIM3_IRQn, 3, 0);
|
||||||
HAL_NVIC_EnableIRQ(TIM3_IRQn);
|
HAL_NVIC_EnableIRQ(TIM3_IRQn);
|
||||||
@ -87,6 +87,47 @@ static void MX_NVIC_Init(void)
|
|||||||
// HAL_NVIC_EnableIRQ(EXTI2_IRQn);
|
// HAL_NVIC_EnableIRQ(EXTI2_IRQn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FOC_Init()
|
||||||
|
{
|
||||||
|
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); // 开启三个PWM通道输出
|
||||||
|
HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1);
|
||||||
|
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2); // 开启三个PWM通道输出
|
||||||
|
HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2);
|
||||||
|
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3); // 开启三个PWM通道输出
|
||||||
|
HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_3);
|
||||||
|
|
||||||
|
// LL_TIM_OC_SetCompareCH1(TIM1, 42 * 1000000 / 16000);
|
||||||
|
// LL_TIM_OC_SetCompareCH2(TIM1, 42 * 1000000 / 16000);
|
||||||
|
// LL_TIM_OC_SetCompareCH3(TIM1, 42 * 1000000 / 16000);
|
||||||
|
// LL_TIM_OC_SetCompareCH1(TIM1, 42 * 1000000 / 16000);
|
||||||
|
// __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 2400);
|
||||||
|
// __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 2400);
|
||||||
|
|
||||||
|
// HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
|
||||||
|
// HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);
|
||||||
|
|
||||||
|
/* wait for a new PWM period */
|
||||||
|
// LL_TIM_ClearFlag_UPDATE(TIM1);
|
||||||
|
// // while (LL_TIM_IsActiveFlag_UPDATE(TIM1) == 0)
|
||||||
|
// // {
|
||||||
|
// // }
|
||||||
|
// /* Clear Update Flag */
|
||||||
|
// LL_TIM_ClearFlag_UPDATE(TIM1);
|
||||||
|
|
||||||
|
/* Main PWM Output Enable */
|
||||||
|
// TIM1->BDTR |= LL_TIM_OSSI_ENABLE;
|
||||||
|
// LL_TIM_EnableAllOutputs(TIM1);
|
||||||
|
|
||||||
|
// /* Clear Update Flag */
|
||||||
|
// LL_TIM_ClearFlag_UPDATE(TIM1);
|
||||||
|
// /* Enable Update IRQ */
|
||||||
|
// LL_TIM_EnableIT_UPDATE(TIM1);
|
||||||
|
|
||||||
|
// LL_TIM_OC_SetCompareCH1(TIM1, 42 * 1000000 / 16000);
|
||||||
|
// LL_TIM_OC_SetCompareCH2(TIM1, 42 * 1000000 / 16000);
|
||||||
|
// LL_TIM_OC_SetCompareCH3(TIM1, 42 * 1000000 / 16000);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function will initial your board.
|
* This function will initial your board.
|
||||||
*/
|
*/
|
||||||
@ -98,10 +139,14 @@ void board_init(void)
|
|||||||
HAL_Init();
|
HAL_Init();
|
||||||
SystemClock_Config();
|
SystemClock_Config();
|
||||||
SystemCoreClockUpdate();
|
SystemCoreClockUpdate();
|
||||||
|
|
||||||
MX_GPIO_Init();
|
MX_GPIO_Init();
|
||||||
MX_USART1_UART_Init();
|
MX_USART1_UART_Init();
|
||||||
// MX_TIM1_Init();
|
MX_TIM1_Init();
|
||||||
MX_TIM3_Init();
|
// MX_TIM3_Init();
|
||||||
|
MX_NVIC_Init();
|
||||||
|
|
||||||
|
FOC_Init();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -19,6 +19,7 @@ extern "C"
|
|||||||
/* Includes ------------------------------------------------------------------*/
|
/* Includes ------------------------------------------------------------------*/
|
||||||
|
|
||||||
#include "stm32f4xx_hal.h"
|
#include "stm32f4xx_hal.h"
|
||||||
|
#include "stm32f4xx_ll_tim.h"
|
||||||
|
|
||||||
|
|
||||||
#include "usart.h"
|
#include "usart.h"
|
||||||
|
@ -13,15 +13,16 @@
|
|||||||
|
|
||||||
void MX_GPIO_Init(void)
|
void MX_GPIO_Init(void)
|
||||||
{
|
{
|
||||||
|
|
||||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||||
|
|
||||||
/* GPIO Ports Clock Enable */
|
/* GPIO Ports Clock Enable */
|
||||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||||
__HAL_RCC_GPIOE_CLK_ENABLE();
|
__HAL_RCC_GPIOE_CLK_ENABLE();
|
||||||
|
__HAL_RCC_GPIOG_CLK_ENABLE();
|
||||||
|
|
||||||
/*Configure GPIO pin Output Level */
|
/*Configure GPIO pin Output Level */
|
||||||
HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_RESET);
|
HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_RESET);
|
||||||
|
HAL_GPIO_WritePin(SD_GPIO_Port, SD_Pin, GPIO_PIN_SET);
|
||||||
|
|
||||||
/*Configure GPIO pins : PFPin PFPin */
|
/*Configure GPIO pins : PFPin PFPin */
|
||||||
GPIO_InitStruct.Pin = LED0_Pin;
|
GPIO_InitStruct.Pin = LED0_Pin;
|
||||||
@ -35,6 +36,12 @@ void MX_GPIO_Init(void)
|
|||||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||||
HAL_GPIO_Init(LED1_GPIO_Port, &GPIO_InitStruct);
|
HAL_GPIO_Init(LED1_GPIO_Port, &GPIO_InitStruct);
|
||||||
|
|
||||||
|
GPIO_InitStruct.Pin = SD_Pin;
|
||||||
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||||
|
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||||
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||||
|
HAL_GPIO_Init(SD_GPIO_Port, &GPIO_InitStruct);
|
||||||
}
|
}
|
||||||
// INIT_BOARD_EXPORT(MX_GPIO_Init);
|
// INIT_BOARD_EXPORT(MX_GPIO_Init);
|
||||||
|
|
||||||
|
@ -25,6 +25,9 @@ extern "C"
|
|||||||
#define LED1_Pin GPIO_PIN_2
|
#define LED1_Pin GPIO_PIN_2
|
||||||
#define LED1_GPIO_Port GPIOE
|
#define LED1_GPIO_Port GPIOE
|
||||||
|
|
||||||
|
#define SD_Pin GPIO_PIN_12
|
||||||
|
#define SD_GPIO_Port GPIOG
|
||||||
|
|
||||||
void MX_GPIO_Init(void);
|
void MX_GPIO_Init(void);
|
||||||
void led_ctrl(uint8_t cmd);
|
void led_ctrl(uint8_t cmd);
|
||||||
|
|
||||||
|
125
src/Bsp/timer.c
125
src/Bsp/timer.c
@ -16,6 +16,11 @@
|
|||||||
TIM_HandleTypeDef htim1;
|
TIM_HandleTypeDef htim1;
|
||||||
TIM_HandleTypeDef htim3;
|
TIM_HandleTypeDef htim3;
|
||||||
|
|
||||||
|
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
|
||||||
|
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim_base);
|
||||||
|
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *htim_base);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief TIM1 Initialization Function
|
* @brief TIM1 Initialization Function
|
||||||
* @param None
|
* @param None
|
||||||
@ -46,12 +51,12 @@ void MX_TIM1_Init(void)
|
|||||||
{
|
{
|
||||||
Error_Handler();
|
Error_Handler();
|
||||||
}
|
}
|
||||||
sSlaveConfig.SlaveMode = TIM_SLAVEMODE_TRIGGER;
|
// sSlaveConfig.SlaveMode = TIM_SLAVEMODE_TRIGGER;
|
||||||
sSlaveConfig.InputTrigger = TIM_TS_ITR1;
|
// sSlaveConfig.InputTrigger = TIM_TS_ITR1;
|
||||||
if (HAL_TIM_SlaveConfigSynchro(&htim1, &sSlaveConfig) != HAL_OK)
|
// if (HAL_TIM_SlaveConfigSynchro(&htim1, &sSlaveConfig) != HAL_OK)
|
||||||
{
|
// {
|
||||||
Error_Handler();
|
// Error_Handler();
|
||||||
}
|
// }
|
||||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
||||||
@ -86,7 +91,6 @@ void MX_TIM1_Init(void)
|
|||||||
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_ENABLE;
|
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_ENABLE;
|
||||||
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_ENABLE;
|
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_ENABLE;
|
||||||
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_1;
|
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_1;
|
||||||
// sBreakDeadTimeConfig.DeadTime = ((DEAD_TIME_COUNTS) / 2);
|
|
||||||
sBreakDeadTimeConfig.DeadTime = 100;
|
sBreakDeadTimeConfig.DeadTime = 100;
|
||||||
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
|
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
|
||||||
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_LOW;
|
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_LOW;
|
||||||
@ -95,6 +99,11 @@ void MX_TIM1_Init(void)
|
|||||||
{
|
{
|
||||||
Error_Handler();
|
Error_Handler();
|
||||||
}
|
}
|
||||||
|
// if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
||||||
|
// {
|
||||||
|
// Error_Handler();
|
||||||
|
// }
|
||||||
|
HAL_TIM_MspPostInit(&htim1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -138,6 +147,62 @@ void MX_TIM3_Init(void)
|
|||||||
{
|
{
|
||||||
Error_Handler();
|
Error_Handler();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Set IC filter for Channel 1 (ICF1) */
|
||||||
|
LL_TIM_IC_SetFilter(TIM3, LL_TIM_CHANNEL_CH1, (uint32_t)(11) << 20);
|
||||||
|
|
||||||
|
/* Force the TIMx prescaler with immediate access (gen update event)
|
||||||
|
*/
|
||||||
|
LL_TIM_SetPrescaler(TIM3, 65535);
|
||||||
|
LL_TIM_GenerateEvent_UPDATE(TIM3);
|
||||||
|
|
||||||
|
LL_TIM_SetUpdateSource(TIM3, LL_TIM_UPDATESOURCE_COUNTER);
|
||||||
|
|
||||||
|
LL_TIM_EnableIT_CC1(TIM3);
|
||||||
|
LL_TIM_EnableIT_UPDATE(TIM3);
|
||||||
|
LL_TIM_SetCounter(TIM3, 0);
|
||||||
|
|
||||||
|
LL_TIM_CC_EnableChannel(TIM3, LL_TIM_CHANNEL_CH1);
|
||||||
|
LL_TIM_EnableCounter(TIM3);
|
||||||
|
}
|
||||||
|
|
||||||
|
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim)
|
||||||
|
{
|
||||||
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||||
|
if (htim->Instance == TIM1)
|
||||||
|
{
|
||||||
|
/* USER CODE BEGIN TIM1_MspPostInit 0 */
|
||||||
|
|
||||||
|
/* USER CODE END TIM1_MspPostInit 0 */
|
||||||
|
|
||||||
|
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||||
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||||
|
/**TIM1 GPIO Configuration
|
||||||
|
PB13 ------> TIM1_CH1N
|
||||||
|
PB14 ------> TIM1_CH2N
|
||||||
|
PB15 ------> TIM1_CH3N
|
||||||
|
PA8 ------> TIM1_CH1
|
||||||
|
PA9 ------> TIM1_CH2
|
||||||
|
PA10 ------> TIM1_CH3
|
||||||
|
*/
|
||||||
|
GPIO_InitStruct.Pin = GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
|
||||||
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||||
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||||
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||||
|
GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
|
||||||
|
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||||
|
|
||||||
|
GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10;
|
||||||
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||||
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||||
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||||
|
GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
|
||||||
|
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||||
|
|
||||||
|
/* USER CODE BEGIN TIM1_MspPostInit 1 */
|
||||||
|
|
||||||
|
/* USER CODE END TIM1_MspPostInit 1 */
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -151,32 +216,32 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim_base)
|
|||||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||||
if (htim_base->Instance == TIM1)
|
if (htim_base->Instance == TIM1)
|
||||||
{
|
{
|
||||||
/* Peripheral clock enable */
|
// /* Peripheral clock enable */
|
||||||
__HAL_RCC_TIM1_CLK_ENABLE();
|
__HAL_RCC_TIM1_CLK_ENABLE();
|
||||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
// __HAL_RCC_GPIOA_CLK_ENABLE();
|
||||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
// __HAL_RCC_GPIOB_CLK_ENABLE();
|
||||||
|
|
||||||
/**TIM1 GPIO Configuration
|
// /**TIM1 GPIO Configuration
|
||||||
PA10 ------> TIM1_CH3
|
// PA10 ------> TIM1_CH3
|
||||||
PA9 ------> TIM1_CH2
|
// PA9 ------> TIM1_CH2
|
||||||
PA8 ------> TIM1_CH1
|
// PA8 ------> TIM1_CH1
|
||||||
PB13 ------> TIM1_CH1N
|
// PB13 ------> TIM1_CH1N
|
||||||
PB14 ------> TIM1_CH2N
|
// PB14 ------> TIM1_CH2N
|
||||||
PB15 ------> TIM1_CH3N
|
// PB15 ------> TIM1_CH3N
|
||||||
*/
|
// */
|
||||||
GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10;
|
// GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10;
|
||||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
// GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||||
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
|
// GPIO_InitStruct.Pull = GPIO_PULLDOWN;
|
||||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
// GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||||
GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
|
// GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
|
||||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
// HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||||
|
|
||||||
GPIO_InitStruct.Pin = GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
|
// GPIO_InitStruct.Pin = GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
|
||||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
// GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||||
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
|
// GPIO_InitStruct.Pull = GPIO_PULLDOWN;
|
||||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
// GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||||
GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
|
// GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
|
||||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
// HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||||
}
|
}
|
||||||
else if (htim_base->Instance == TIM3)
|
else if (htim_base->Instance == TIM3)
|
||||||
{
|
{
|
||||||
|
@ -20,6 +20,9 @@ extern "C"
|
|||||||
|
|
||||||
#include "stm32f4xx_hal.h"
|
#include "stm32f4xx_hal.h"
|
||||||
|
|
||||||
|
extern TIM_HandleTypeDef htim1;
|
||||||
|
extern TIM_HandleTypeDef htim3;
|
||||||
|
|
||||||
void MX_TIM1_Init(void);
|
void MX_TIM1_Init(void);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
@ -13,23 +13,10 @@
|
|||||||
|
|
||||||
#include "stm32f407xx.h"
|
#include "stm32f407xx.h"
|
||||||
|
|
||||||
#include "type.h"
|
#include "foc_type.h"
|
||||||
#include "math.h"
|
#include "foc_math.h"
|
||||||
|
|
||||||
#include "hall.h"
|
#include "hall.h"
|
||||||
#include "svpwm.h"
|
#include "svpwm.h"
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
uint8_t sector; // 扇区
|
|
||||||
|
|
||||||
qd_t Vqd;
|
|
||||||
alphabeta_t Valphabeta;
|
|
||||||
|
|
||||||
uint16_t pwmA;
|
|
||||||
uint16_t pwmB;
|
|
||||||
uint16_t pwmC;
|
|
||||||
uint16_t pwm_period;
|
|
||||||
|
|
||||||
} foc_handle_t;
|
|
||||||
|
|
||||||
#endif /* __FOC_H__ */
|
#endif /* __FOC_H__ */
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include "math.h"
|
#include "foc_math.h"
|
||||||
|
|
||||||
#define SIN_MASK 0x0300u
|
#define SIN_MASK 0x0300u
|
||||||
#define U0_90 0x0200u
|
#define U0_90 0x0200u
|
||||||
@ -41,14 +41,12 @@ const int16_t hSin_Cos_Table[256] = {
|
|||||||
0x7FD8, 0x7FE1, 0x7FE9, 0x7FF0, 0x7FF5, 0x7FF9, 0x7FFD, 0x7FFE};
|
0x7FD8, 0x7FE1, 0x7FE9, 0x7FF0, 0x7FF5, 0x7FF9, 0x7FFD, 0x7FFE};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief This function transforms stator voltage qVq and qVd, that belong to
|
* @brief 反Park变换
|
||||||
* a rotor flux synchronous rotating frame, to a stationary reference
|
* Valpha= Vq*Cos(theta)+ Vd*Sin(theta)
|
||||||
* frame, so as to obtain qValpha and qVbeta:
|
* Vbeta=-Vq*Sin(theta)+ Vd*Cos(theta)
|
||||||
* Valfa= Vq*Cos(theta)+ Vd*Sin(theta)
|
|
||||||
* Vbeta=-Vq*Sin(theta)+ Vd*Cos(theta)
|
|
||||||
* @param Input: stator voltage Vq and Vd in qd_t format
|
* @param Input: stator voltage Vq and Vd in qd_t format
|
||||||
* @param Theta: rotating frame angular position in q1.15 format
|
* @param Theta: rotating frame angular position in q1.15 format
|
||||||
* @retval Stator voltage Valpha and Vbeta in qd_t format
|
* @retval stator voltage Valpha and Vbeta in qd_t format
|
||||||
*/
|
*/
|
||||||
alphabeta_t math_rev_park(qd_t input, int16_t theta)
|
alphabeta_t math_rev_park(qd_t input, int16_t theta)
|
||||||
{
|
{
|
@ -4,17 +4,18 @@
|
|||||||
* @date 2023-08-29
|
* @date 2023-08-29
|
||||||
* @version 0.0.1
|
* @version 0.0.1
|
||||||
* @copyright XiazhiTech Copyright (c) 2023
|
* @copyright XiazhiTech Copyright (c) 2023
|
||||||
*
|
*
|
||||||
* @brief
|
* @brief
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef __FOC_MATH_H__
|
#ifndef __FOC_MATH_H__
|
||||||
#define __FOC_MATH_H__
|
#define __FOC_MATH_H__
|
||||||
|
|
||||||
#include "type.h"
|
#include "foc_type.h"
|
||||||
|
|
||||||
|
#define PI (3.1415926f)
|
||||||
#define SQRT_2 1.4142
|
#define SQRT_2 1.4142
|
||||||
#define SQRT_3 1.732
|
#define SQRT_3 (1.732051f)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Macro to compute logarithm of two
|
* @brief Macro to compute logarithm of two
|
@ -4,8 +4,8 @@
|
|||||||
* @date 2023-08-29
|
* @date 2023-08-29
|
||||||
* @version 0.0.1
|
* @version 0.0.1
|
||||||
* @copyright XiazhiTech Copyright (c) 2023
|
* @copyright XiazhiTech Copyright (c) 2023
|
||||||
*
|
*
|
||||||
* @brief
|
* @brief
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef __FOC_TYPE_H__
|
#ifndef __FOC_TYPE_H__
|
||||||
@ -13,8 +13,6 @@
|
|||||||
|
|
||||||
#include "stm32f407xx.h"
|
#include "stm32f407xx.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Two components q, d type definition
|
* @brief Two components q, d type definition
|
||||||
*/
|
*/
|
||||||
@ -40,4 +38,18 @@ typedef struct
|
|||||||
int16_t beta;
|
int16_t beta;
|
||||||
} alphabeta_t;
|
} alphabeta_t;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
uint8_t sector; // 扇区
|
||||||
|
|
||||||
|
qd_t Vqd;
|
||||||
|
alphabeta_t Valphabeta;
|
||||||
|
|
||||||
|
uint16_t pwmA;
|
||||||
|
uint16_t pwmB;
|
||||||
|
uint16_t pwmC;
|
||||||
|
uint16_t pwm_period;
|
||||||
|
|
||||||
|
} foc_handle_t;
|
||||||
|
|
||||||
#endif /* __FOC_TYPE_H__ */
|
#endif /* __FOC_TYPE_H__ */
|
@ -65,6 +65,6 @@ void svpwm(foc_handle_t *handle)
|
|||||||
}
|
}
|
||||||
|
|
||||||
handle->pwmA = (uint16_t)wTimePhA;
|
handle->pwmA = (uint16_t)wTimePhA;
|
||||||
handle->pwmA = (uint16_t)wTimePhB;
|
handle->pwmB = (uint16_t)wTimePhB;
|
||||||
handle->pwmA = (uint16_t)wTimePhC;
|
handle->pwmC = (uint16_t)wTimePhC;
|
||||||
}
|
}
|
@ -12,6 +12,7 @@
|
|||||||
#define __FOC_SVPWM_H__
|
#define __FOC_SVPWM_H__
|
||||||
|
|
||||||
#include "stm32f407xx.h"
|
#include "stm32f407xx.h"
|
||||||
|
#include "foc_type.h"
|
||||||
|
|
||||||
#define SECTOR_1 0u
|
#define SECTOR_1 0u
|
||||||
#define SECTOR_2 1u
|
#define SECTOR_2 1u
|
||||||
@ -20,4 +21,6 @@
|
|||||||
#define SECTOR_5 4u
|
#define SECTOR_5 4u
|
||||||
#define SECTOR_6 5u
|
#define SECTOR_6 5u
|
||||||
|
|
||||||
|
extern void svpwm(foc_handle_t *handle);
|
||||||
|
|
||||||
#endif /* __FOC_SVPWM_H__ */
|
#endif /* __FOC_SVPWM_H__ */
|
Loading…
Reference in New Issue
Block a user