本文记录了我学习FOC的第二步,基于上一篇程序,使用SVPWM控制无刷电机,本文参考了B站UP主 热心市民翔先生 的视频。
1.SVPWM算法篇
1.1软件宏定义部分
其中 PWM_Period 是用户自己设置的定时器ARR的值,以及一些二分之π,三分之π的数值
1.计算参考电压矢量
定义了一个函数,包括三个形参,D轴电压Ud,Q轴电压Uq,以及电角度。
1.1 Uref
定义了一个Uref,上图中的蓝色所指的直线
Uref按比例缩小,标幺
把Uref限制再三分之根号三中
1.2 float U_alpha,U_beta;
用户输入的Ud.Uq,先使用Park逆变换,算出U_alpha和U_alpha
1.3 angle_el
加上二分之Π,因为Q轴和D轴相差90°
2.扇区判断
2.1 sector
矢量的扇区=(参考电压的相位/三分之Π)+1
例:
第一扇区:0~60度,也就是三分之Π,在这个区间内,都表示在第一扇区。
3.计算相邻矢量和零矢量作用时间
3.1 T1、T2、T0
T1和T2可以当成T4和T6,当sector = 1,在第一扇区时,将下图公式代入可以得到
然后在一个周期内减去相邻两个电压时间就得到剩下的0矢量作用时间
4.计算各桥臂开关时间
4.1 Ta Tb Tc
Ta = T1+T2+T0/2,为什么呢?
由上图可以看出 当V4到V6时,三个桥的状态就是100~110,由下列注释可以得到
5. 配置PWM占空比
Ta*PWM_Period
a相的打开时间再乘PWM的周期就得到a相的占空比
6.主函数
延迟记得一定要加
2.现象
这里数字电源给的是12V,3A
使用VOFA+打印出来波形有点奇怪,并不是想要的马鞍波,并且电机在旋转的时候有明显的噪音,并且发热发烫。
参考代码:
svpwm.c
#include "tim.h"
#include <math.h>
#define PWM_Period 5000
#define _PI 3.14159265359f//圆周率
#define _PI_2 1.57079632679f
#define _PI_3 1.0471975512f
#define _2PI 6.28318530718f
#define _3PI_2 4.71238898038f
#define _PI_6 0.52359877559f
#define _SQRT3 1.73205080757f//根号三
float voltage_power_supply;//直流母线电压值=24V
/*将角度控制再0~2Π之间*/
float _normalizeAngle(float angle)
{
float a = fmod(angle, _2PI); //fmod()对浮点数取模
return a >= 0 ? a : (a + _2PI);
}
//FOC核心函数:输入Ud、Uq和电角度,输出PWM
void setPhaseVoltage(float Uq, float Ud, float angle_el)
{
float Uref;//参考电压
float U_alpha,U_beta;
float T0,T1,T2;//相邻两个矢量开关的时间
float Ta,Tb,Tc;//开关桥臂的时间
int sector;//扇区判断
/*先进行反park变换*/
U_alpha=Ud*cos(angle_el)-Uq*sin(angle_el); //反park变换
U_beta=Ud*sin(angle_el)+Uq*cos(angle_el);
Uref=_sqrt(U_alpha*U_alpha + U_beta*U_beta) / voltage_power_supply; //计算参考电压矢量的幅值
if(Uref> 0.577f)Uref= 0.577f; //六边形的内切圆(SVPWM最大不失真旋转电压矢量赋值)根号3/3
if(Uref<-0.577f)Uref=-0.577f;
//判断参考电压矢量所在扇区:
angle_el = _normalizeAngle(angle_el+_PI_2); //标准化电角度值【0,2pi】,加90度后是参考电压矢量的位置
sector = (angle_el / _PI_3) + 1; //=参考电压的相位/三分之Π+1
T1 = _SQRT3*sin(sector*_PI_3 - angle_el) * Uref; //计算两个相邻电压矢量作用时间
T2 = _SQRT3*sin(angle_el - (sector-1.0)*_PI_3) * Uref;
T0 = 1 - T1 - T2; //零矢量作用时间
/*第一扇区
Ta Tb TC
T1= 1 0 0 ~
Ta Tb TC
T2= 1 1 0
Ta = T1+T2
Tb = T2
Tc = 0
第二扇区
Ta Tb TC
T1= 1 1 0
Ta Tb TC
T2= 0 1 0
Ta = T1
Tb = T1+T2
Tc = 0
*/
switch(sector)
{
case 1:
Ta = T1 + T2 + T0/2;
Tb = T2 + T0/2;//
Tc = T0/2;//
break;
case 2:
Ta = T1 + T0/2;
Tb = T1 + T2 + T0/2;
Tc = T0/2;
break;
case 3:
Ta = T0/2;
Tb = T1 + T2 + T0/2;
Tc = T2 + T0/2;
break;
case 4:
Ta = T0/2;
Tb = T1+ T0/2;
Tc = T1 + T2 + T0/2;
break;
case 5:
Ta = T2 + T0/2;
Tb = T0/2;
Tc = T1 + T2 + T0/2;
break;
case 6:
Ta = T1 + T2 + T0/2;
Tb = T0/2;
Tc = T1 + T0/2;
break;
default: // possible error state
Ta = 0;
Tb = 0;
Tc = 0;
}
printf("%.6f,%.6f,%.6f\r\n",Ta,Tb,Tc);
__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,Ta*PWM_Period); //输出PWM,配置占空比
__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_2,Tb*PWM_Period);
__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_3,Tc*PWM_Period);
}
main.c
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
void setPhaseVoltage(float Uq, float Ud, float angle_el);
extern float voltage_power_supply;
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MPU_Config(void);
/**
* 函数功能: 重定向c库函数printf到DEBUG_USARTx
* 输入参数: 无
* 返 回 值: 无
* 说 明:无
*/
int fputc(int ch, FILE *f)
{
HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, 0xffff);
return ch;
}
/**
* 函数功能: 重定向c库函数getchar,scanf到DEBUG_USARTx
* 输入参数: 无
* 返 回 值: 无
* 说 明:无
*/
int fgetc(FILE * f)
{
uint8_t ch = 0;
HAL_UART_Receive(&huart3,&ch, 1, 0xffff);
return ch;
}
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
float angle_el = 0.0f;//电角度
/* USER CODE END 1 */
/* MPU Configuration--------------------------------------------------------*/
MPU_Config();
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM1_Init();
MX_USART3_UART_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_3);
HAL_TIMEx_PWMN_Start(&htim1,TIM_CHANNEL_1);
HAL_TIMEx_PWMN_Start(&htim1,TIM_CHANNEL_2);
HAL_TIMEx_PWMN_Start(&htim1,TIM_CHANNEL_3);
voltage_power_supply = 24;
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
setPhaseVoltage(0,2,angle_el); //开环控制Ud<2
angle_el += 2; //估计电角度
HAL_Delay(1);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Supply configuration update enable
*/
HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
/** Configure the main internal regulator output voltage
*/
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 5;
RCC_OscInitStruct.PLL.PLLN = 160;
RCC_OscInitStruct.PLL.PLLP = 2;
RCC_OscInitStruct.PLL.PLLQ = 2;
RCC_OscInitStruct.PLL.PLLR = 2;
RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_2;
RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
RCC_OscInitStruct.PLL.PLLFRACN = 0;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
|RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/* MPU Configuration */
void MPU_Config(void)
{
MPU_Region_InitTypeDef MPU_InitStruct = {0};
/* Disables the MPU */
HAL_MPU_Disable();
/** Initializes and configures the Region and the memory to be protected
*/
MPU_InitStruct.Enable = MPU_REGION_ENABLE;
MPU_InitStruct.Number = MPU_REGION_NUMBER0;
MPU_InitStruct.BaseAddress = 0x0;
MPU_InitStruct.Size = MPU_REGION_SIZE_4GB;
MPU_InitStruct.SubRegionDisable = 0x87;
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
MPU_InitStruct.AccessPermission = MPU_REGION_NO_ACCESS;
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE;
MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
HAL_MPU_ConfigRegion(&MPU_InitStruct);
/* Enables the MPU */
HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
}
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
总结
虽然电机旋转起来了,但是依旧是有问题的,希望随着后续的学习能解决掉这些问题。
参考文章:
1.彻底吃透SVPWM如此简单 - 知乎 (zhihu.com)