You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:基于STM32的直流电机浪涌电流PID控制C语言程序开发

求助:STM32控制直流电机PWM时的浪涌电流限制PID实现

我正在开发一款C语言程序,用于控制直流电机端子PWM的同时限制浪涌电流。涉及的电机为50W 24V型号,目标是将电流限制在2.5A。我制作了一块搭载意法半导体STM32F31C4T6微控制器的电路板,通过ADC测量电流,但编写PID函数时遇到困难,不知道如何在限制电流的同时管理PWM。以下是我编写的初始代码,目前缺少需要作为函数参数传入的初始PWM值:

#include "PID.h"


/* constants for PID */
const float Kp = 0.01;  // The value for Proportional gain
const float Ki = 0.01;  // The value for Integral gain
const float Kd = 0.001; // The value for Differential gain
const int Set_Point = 1950; // The ADC reference point we are aiming to regulate to

uint32_t tabDuty[50];

int i =0;

void PWM_Duty_Change(current)
{
    /* Global Variables for PID */
    float d_Temp = 0;   // This stores the old ADC value
    float i_Temp = 0;   // This stores the accumulated Integral value
    float PWM_Temp = 0; 
    
    
    //Local variables for PID
    float iMax = 100;       // Used to prevent integral wind-up
    float iMin = -100;  // Used to prevent integral wind-up
    float Err_Value;        // Holds the calculated Error value
    float P_Term;               // Holds the calculated Proportional value
    float I_Term;               // Holds the calculated Integral value
    float D_Term;               // Holds the calculated Differential value
    int new_current_ADC_value;  // Holds the new ADC value
    float PWM_Duty; // Holds the new PWM value

    
    new_current_ADC_value = current;

    Err_Value = (Set_Point - new_current_ADC_value);

    // This calculates Proportional value, Kp is 
    //multiplied with Err_Value and the result is assigned to P_Term
    P_Term = Kp * Err_Value;

    // Prepare Integral value, add the current error 
    //value to the integral value and assign the total to i_Temp
    i_Temp += Err_Value;

    // Prevents integral wind-up, limits i_Temp 
    //from getting too positive or negative
    if (i_Temp > iMax)
        {i_Temp = iMax;}
    else if (i_Temp < iMin)
        {i_Temp = iMin;}

    // Calculates the Integral value, Ki is 
    //multiplied with i_Temp and the result is assigned to I_Term
    I_Term = Ki * i_Temp;

    // Calculates Differential value, Kd is multiplied with 
    //(d_Temp minus new_ADC_value) and the result is assigned to D_Term
        
    // The new_ADC_value will become the old ADC value 
    //on the next function call, this is assigned to d_Temp so it can be used
    D_Term = Kd * (d_Temp - Err_Value);
    d_Temp = Err_Value;

    /****** Now we have the P_Term, I_Term and D_Term *****/
    PWM_Duty = PWM_Temp - (P_Term + I_Term + D_Term);
        
    // PWM overflow prevention
    if (PWM_Duty > 90)
        {PWM_Duty = 90;}
    else if (PWM_Duty < 10)
        {PWM_Duty = 10;}

    // Adjusts the PWM duty cycle
    adjust_PWM(PWM_Duty);
    // Assigns the current PWM duty cycle value to PWM_Temp
    PWM_Temp = PWM_Duty;
    
}

若需更多信息,请随时告知。感谢各位提供的帮助。

内容的提问来源于stack exchange,提问作者Kolia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 18:10:31