# FOC-DRIVE-STM32 **Repository Path**: vasoconstriction/foc-drive-stm32 ## Basic Information - **Project Name**: FOC-DRIVE-STM32 - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-11-07 - **Last Updated**: 2025-07-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # FOC 最新的FOC库在工程目录下 ****************************************************************************** * @project FOC-Control * @version V1.0.0 * @date 2024/12/26 * @brief The file define foc_BLDCMotor interface driver functions. * @copyright (c) 2024 LiHuashi. All rights reserved. * All information contained herein is, and remains, the property of LiHuashi. The intellectual and technical concepts contained herein are proprietary to LiHuashi and may be covered by U.S. and foreign patents, patents in process, and protected by trade secret or copyright law. Dissemination of this information, including but not limited to data and other proprietary material(s) incorporated within the information, in any form, is strictly prohibited without the express written consent of LiHuashi. * If you receive this source code without LiHuashi authorization, you may not further disseminate the information, and you must immediately remove the source code and notify LiHuashi of its removal. LiHuashi reserves the right to pursue legal actions against you for any loss(es) or damage(s) caused by your failure to do so. ****************************************************************************** ## 相关参数 FOC_MAIN_F405 这个驱动库集成位置环 速度环 电流环,电流环默认频率为10KHz,位置环、速度环默认频率为1KHz,你可以通过定时器来重新修改频率(TIM3),其中通道1到通道3用于驱动电机的PWM,通道四用于触发ADC采样,工程中已经配置好ADC在TIM3 CH4的下降沿进行采样,默认为1.5个周期(可修改)。用户在修改默认参数时请确保系统运行稳定,ADC采样的时刻必须要在TIM3 CH1 CH2 CH3为低电平的时候进行。 ### 系统错误代码 > @brief SIM error code complete works. Users can search all error messages here. > @details Please get error description, error reason and error recovery suggestion of every error code from > ::SIM_ERROR_OBJECTS macro. ``` enum FocErrorCode { ROR_SYS_HANDLER_CODE_SUCCES = 0X0001, FOC_ERROR_SYS_HANDLER_CODE_NULL = 0X0002, FOC_ERROR_SYS_DATAPRO_CODE_SUCCES = 0X0010, FOC_ERROR_SYS_DATAPRO_CODE_NULL = 0X0020, FOC_ERROR_SYS_INITPARAM_CODE_NULL = 0X0200, FOC_ERROR_SYS_INIT_CODE_SUCCES = 0X2000 }; ``` > @brief Error objects, specifying error code, error description, error reason and error recovery suggestion below. > @attention Require arrange by error code from small to large. ``` #define FOC_ERROR_OBJECTS {FOC_ERROR_SYS_HANDLER_CODE_SUCCES, "Handler is SUCCESS.", NULL}, {FOC_ERROR_SYS_HANDLER_CODE_NULL, "Handler is NULL.", NULL}, {FOC_ERROR_SYS_DATAPRO_CODE_SUCCES, "Data Processing SUCCES.", NULL}, {FOC_ERROR_SYS_DATAPRO_CODE_NULL, "Data Processing FAILURE.", NULL}, {FOC_ERROR_SYS_INITPARAM_CODE_NULL, "The Initialization Parameter Incorrect.", NULL}, {FOC_ERROR_SYS_DEVICEINIT_CODE_SUCCES, "Device Init SUCCESS.", NULL}, ``` ### 相关结构体 > T_FocDelayConstHandler ``` typedef struct { (*Delay)(foc_uint32_t ms);//延时函数指针 } T_FocDelayConstHandler; ``` > T_FocPowerOutputConstHandler ``` typedef struct { foc_f32_t POWER_SUPPLY;//驱动器供电电压 foc_f32_t POWER_LIMIT;//电机限制电压 void (*PWM_Updata)(foc_f32_t Duty_U,foc_f32_t Duty_V,foc_f32_t Duty_w);//PWM更新函数指针 } T_FocPowerOutputConstHandler; ``` > T_FocEncoderConstHandler ``` typedef struct { foc_int32_t PP;//极对数 FOC_DIR DIR;//极性 foc_uint8_t Bit;//编码器位数 foc_f32_t I_TIM;//用于计算速度的时间间隔 foc_uint32_t (*EncoderGetRawData)(void);//编码器原始数据读取函数指针 } T_FocEncoderConstHandler; ``` > T_FocCurrentConstHandler ``` typedef struct { foc_f32_t RES;//采样电阻 foc_uint8_t DB;//放大倍数 FOC_DIR DIR;//电流方向 foc_uint8_t Bit;//ADC位数 foc_f32_t ADC_U;//ADC参考电压 foc_uint32_t *Raw_ADC_U;//指向ADC原始数据的指针 foc_uint32_t *Raw_ADC_V;//指向ADC原始数据的指针 CCOTypedef (*CurrentCalibrationOffest)(void);//ADC校准偏置函数指针 } T_FocCurrentConstHandler; ``` > T_FocBLDCMotorHandler ``` typedef struct { T_FocDelayConstHandler DelayConstHandler; T_FocPowerOutputConstHandler PowerOutputConstHandler; T_FocEncoderConstHandler EncoderConstHandler; T_FocCurrentConstHandler CurrentConstHandler; foc_f32_t P_K;//电压限幅系数 foc_f32_t E_K;//角度系数 foc_f32_t Z_Angle;//零机械角度 foc_int32_t N_full_rotations; // 总圈数计数 foc_int32_t L_full_rotations; // 先前完整旋转圈数 foc_f32_t LM_Angle;//上一个机械角度 foc_f32_t M_Angles;//多圈机械角度 foc_f32_t M_Angle;//单圈机械角度 foc_f32_t E_Angle;//电角度 foc_f32_t speed;//角速度 foc_f32_t C_K;//电流系数 foc_uint32_t Offest_U;//ADC_U偏置 foc_uint32_t Offest_V;//ADC_V偏置 foc_f32_t I_U;//U相电流 foc_f32_t I_V;//V相电流 foc_f32_t I_Alpha;//alpha轴电流 foc_f32_t I_Beta;//beat轴电流 foc_f32_t I_D;//D轴电流 foc_f32_t I_Q;//Q轴电流 foc_f32_t U_Q;//Q轴输入 foc_f32_t U_D;//D轴输入 foc_f32_t U_Alpha;//alpha轴电压 foc_f32_t U_Beta;//beat轴电压 foc_f32_t Duty_U;//U相占空比(0~1) foc_f32_t Duty_V;//V相占空比(0~1) foc_f32_t Duty_W;//W相占空比(0~1) foc_f32_t sine;//电角度sine值 foc_f32_t cose;//电角度cose值 } T_FocBLDCMotorHandler; ``` ### 相关函数 > 注册FOC设备初始化句柄 ```T_FocReturnCode Foc_Platform_RegBLDCMotorHandler(T_FocBLDCMotorHandler *BLDCMotorHandler, T_FocDelayConstHandler *DelayConstHandler, T_FocPowerOutputConstHandler *PowerOutputConstHandler,T_FocEncoderConstHandler *EncoderConstHandler, T_FocCurrentConstHandler *CurrentConstHandler)``` > 校准电流偏置 ```T_FocReturnCode Foc_Platform_CurrentBiasInit(T_FocBLDCMotorHandler *Handler)``` > 校准电机零点 ```T_FocReturnCode Foc_Platform_MotorZeroAngleInit(T_FocBLDCMotorHandler *Handler)``` > 反Park变换 ```T_FocReturnCode Foc_Voltage_iPark_Transf(T_FocBLDCMotorHandler *Handler)``` > SVPWM调制 ```T_FocReturnCode Foc_Voltage_SVPWM_Transf(T_FocBLDCMotorHandler *Handler)``` > 更新PWM ```T_FocReturnCode Foc_PWM_Updata(T_FocBLDCMotorHandler *Handler)``` > U_Q U_D等比例缩小 ```T_FocReturnCode Foc_Voltage_ScalingDown_Transf(T_FocBLDCMotorHandler *Handler)``` > 更新机械角度电角度 ```T_FocReturnCode Foc_Encoder_MEAngle_Updata(T_FocBLDCMotorHandler *Handler)``` > 更新速度 ```T_FocReturnCode Foc_Encoder_Speed_Updata(T_FocBLDCMotorHandler *Handler)``` > 更新电角度正弦余弦值 ```T_FocReturnCode Foc_Encoder_SinCos_Updata(T_FocBLDCMotorHandler *Handler)``` > 获取单圈机械角度 ```foc_f32_t Foc_Encoder_Get_MAngle(T_FocBLDCMotorHandler *Handler)``` > 获取多圈机械角度 ```foc_f32_t Foc_Encoder_Get_MAngles(T_FocBLDCMotorHandler *Handler)``` > 获取角速度 ```foc_f32_t Foc_Encoder_Get_Speed(T_FocBLDCMotorHandler *Handler)``` > 更新UV相电流 ```T_FocReturnCode Foc_Current_UV_Updata(T_FocBLDCMotorHandler *Handler)``` > 更新alpha bate轴电流 ```T_FocReturnCode Foc_Current_ClarkeTransf_Updata(T_FocBLDCMotorHandler *Handler)``` > 更新D Q轴电流 ```T_FocReturnCode Foc_Current_ParkTransf_Updata(T_FocBLDCMotorHandler *Handler)``` > 更新电流 ```T_FocReturnCode Foc_Currents_Updata(T_FocBLDCMotorHandler *Handler)``` ## 初始化示例 ### 初始化结构体 ``` //初始化结构体 T_FocDelayConstHandler T_FocPowerOutputConstHandler T_FocEncoderConstHandler T_FocCurrentConstHandler T_FocDelayConstHandler m0_DelayConstHandler = { .Delay = HAL_Delay }; T_FocPowerOutputConstHandler m0_PowerOutputConstHandler = { .POWER_SUPPLY = 12.0f, .POWER_LIMIT = 12.0f, .PWM_Updata = m0_PWM_Updata }; T_FocEncoderConstHandler m0_EncoderConstHandler = { .PP = 14, .DIR = INVERSION, .Bit = 14, .I_TIM = 0.001060f, .EncoderGetRawData = m0_EncoderGetRawData, }; T_FocCurrentConstHandler m0_CurrentConstHandler = { .RES = 0.005f, .DB = 50, .DIR = INVERSION, .Bit = 12, .ADC_U = 3.3f, .Raw_ADC_U = &adc_data[0], .Raw_ADC_V = &adc_data[1], .CurrentCalibrationOffest = m0_CurrentCalibrationOffest }; //注册设备参数到控制系统 Foc_Platform_RegBLDCMotorHandler(&m0_BLDCMotorHandler, \ &m0_DelayConstHandler, \ &m0_PowerOutputConstHandler, \ &m0_EncoderConstHandler, \ &m0_CurrentConstHandler); //进行校准 Foc_Platform_CurrentBiasInit(&m0_BLDCMotorHandler); //电流校准 Foc_Platform_MotorZeroAngleInit(&m0_BLDCMotorHandler); //电机零角度校准 ``` ## 控制流程(以进入ADC中断后开始执行为一个周期) ### 控制总流程 ``` adc_data[0] = hadc->Instance->JDR1; adc_data[1] = hadc->Instance->JDR2; Foc_Encoder_MEAngle_Updata(&m0_BLDCMotorHandler); Foc_Encoder_SinCos_Updata(&m0_BLDCMotorHandler); Foc_Voltage_iPark_Transf(&m0_BLDCMotorHandler); Foc_Voltage_SVPWM_Transf(&m0_BLDCMotorHandler); Foc_PWM_Updata(&m0_BLDCMotorHandler); ``` ### 读取adc值 ``` adc_data[0] = hadc->Instance->JDR1; adc_data[1] = hadc->Instance->JDR2; ``` ### 读取编码器并进行角度转换 ``` Foc_Encoder_MEAngle_Updata(&m0_BLDCMotorHandler); ``` ### 计算sine cose ``` Foc_Encoder_SinCos_Updata(&m0_BLDCMotorHandler); ``` ### 计算电流 ``` 暂未开发 ``` ### 电流环PI控制 ``` 暂未开发 ``` ### 10个周期进一次速度环、位置环 #### 速度环、位置换PI控制 ``` 暂未开发 ``` ### PARK反变换 ``` Foc_Voltage_iPark_Transf(&m0_BLDCMotorHandler); ``` ### SVPWM调制 ``` Foc_Voltage_SVPWM_Transf(&m0_BLDCMotorHandler); ``` ### PWM输出到逆变器 ``` Foc_PWM_Updata(&m0_BLDCMotorHandler); ```