标题:增量式PID/位置式PID(仅供学习)
只看楼主
Mr_Rain
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2022-4-29
 问题点数:0 回复次数:1 
增量式PID/位置式PID(仅供学习)
程序代码:
#include <stdio.h>
/************************************/
//增量式PID
/*************************************/

//初始化变量
struct _PID{
    float SetSpeed;       //设定值
    float ActualSpeed;    //检测值
    float Err;            //当前误差
    float Err_last;       //上一次误差
    float Err_llast;      //上上次的误差
    float KP,KI,KD;       //PID参数
}PID;

void PID_Init(void)
{
    printf("PID_Value_Init_Begin \n");
    
    PID.SetSpeed = 0.0;
    PID.ActualSpeed = 0.0;
    PID.Err = 0.0;        
    PID.Err_last = 0.0;   
    PID.Err_llast = 0.0; 
    PID.KP = 0.2;
    PID.KI = 0.015;
    PID.KD = 0.2;   
    printf("PID_Init_End \n");
}

void delay(int count)
{
    int i;
    for(i = 1;i <= count;i++);
}

float PID_Realize(float Speed)
{
    PID.SetSpeed = Speed;
    PID.Err = PID.SetSpeed - PID.ActualSpeed;
    float Increment_Speed = PID.KP * (PID.Err - PID.Err_last) + PID.KI * PID.Err + PID.KD * (PID.Err - 2 * PID.Err_last + PID.Err_llast);
    PID.ActualSpeed += Increment_Speed;
    PID.Err_llast = PID.Err_last;
    PID.Err_last = PID.Err;
    return PID.ActualSpeed;
}

int main() 
{
    int count = 0;
    
    printf("Please begin \n");
    PID_Init();
    
    while(count < 1000)
    {
        float Speed = PID_Realize(1000.0);
        
        printf("Value is %f\n",Speed);
        count++;
        delay(3000);
    }
    
    return 0;
}

程序代码:
#include <stdio.h>
/************************************/
//位置式PID
/*************************************/

//初始化变量
struct _PID{
    float SetSpeed;       //设定值
    float ActualSpeed;    //检测值
    float Err;            //当前误差
    float Err_last;       //上一次误差
    float KP,KI,KD;       //PID参数
    float Voltage;        //输出值
    float Intergral;      //误差积分
}PID;

void PID_Init(void)
{
    printf("PID_Value_Init_Begin \n");
    
    PID.SetSpeed = 0;
    PID.ActualSpeed = 0;
    PID.Err = 0;        
    PID.Err_last = 0;   
    PID.Voltage = 0;    
    PID.Intergral = 0; 
    PID.KP = 0.1;
    PID.KI = 0.15;
    PID.KD = 0.1;   
    printf("PID_Init_End \n");
}

void delay(int count)
{
    int i;
    for(i = 1;i <= count;i++);
}

float PID_Realize(float Speed)
{
    PID.SetSpeed = Speed;
    PID.Err = PID.SetSpeed - PID.ActualSpeed;
    PID.Intergral += PID.Err;
    PID.Voltage = PID.KP*PID.Err + PID.KI*PID.Intergral + PID.KD*(PID.Err-PID.Err_last);
    PID.Err_last = PID.Err;
    PID.ActualSpeed = PID.Voltage;
    return PID.ActualSpeed;
}

int main() 
{
    int count = 0;
    
    printf("Please begin \n");
    PID_Init();
    
    while(count < 1000)
    {
        float Speed = PID_Realize(1000.0);
        
        printf("Value is %f\n",Speed);
        count++;
        delay(3000);
    }
    
    return 0;
}
搜索更多相关主题的帖子: float int count printf PID 
2022-08-09 11:02
an99fang
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2022-8-12
得分:0 
2022-08-12 17:35



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-509747-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.279900 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved