模拟直流电机速度环控制-基于位置式PID算法C语言

文章详细介绍了PID控制器的基本结构及其在C语言中的实现,通过迭代模拟了一个实际速度控制过程,展示了PID算法如何逐步调整电机输出以达到目标速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>

typedef struct {
    float target_val;  // 目标值
    float Kp;          // 比例系数
    float Ki;          // 积分系数
    float Kd;          // 微分系数
    float err;         // 误差
    float err_last;    // 上一次误差
    float integral;    // 误差累积
    float actual_val;  // 当前实际值
} PID_TypeDef;

PID_TypeDef pid;

/**
  * @brief  PID算法实现
  * @param  val    实际值
  * @note   无
  * @retval 通过PID计算后的输出
  */
float PID_realize(float temp_val)
{
    /* 计算目标值与实际值的误差 */
    pid.err = pid.target_val - temp_val;
    /* 误差累积 */
    pid.integral += pid.err;
    /* PID算法实现 */
    pid.actual_val = pid.Kp * pid.err + pid.Ki * pid.integral + pid.Kd * (pid.err - pid.err_last);
    /* 误差传递 */
    pid.err_last = pid.err;
    /* 返回当前实际值 */
    return pid.actual_val;
}

int main()
{
    // 初始化PID参数
    pid.target_val = 100.0;  // 设置目标速度
    pid.Kp = 0.01;            // 比例系数
    pid.Ki = 0.8;           // 积分系数
    pid.Kd = 0.04;           // 微分系数
    pid.err = 0.0;
    pid.err_last = 0.0;
    pid.integral = 0.0;
    pid.actual_val = 0.0;

    // 模拟编码器反馈的实际速度
    static float encoder_feedback_speed = 65.0;  // 假设编码器反馈的速度为65.0

    // 模拟控制过程,通过循环逐渐调节电机输出
    for (int i = 0; i < 100; i++) {
        // 调用PID算法进行速度环控制
        float control_output = PID_realize(encoder_feedback_speed);
        
        //模拟控制过程中速度变化  按理说编码器反馈值是实际测出来的值,因为手头上没有实物,这里就这样模拟了 
        encoder_feedback_speed = control_output; 

        printf("Iteration %d - Control Output: %f\n", i, control_output);
    }

    return 0;
}

运行结果:

Iteration 0 - Control Output: 29.750000
Iteration 1 - Control Output: 86.312508
Iteration 2 - Control Output: 93.024368
Iteration 3 - Control Output: 100.531784
Iteration 4 - Control Output: 99.999466
Iteration 5 - Control Output: 100.326805
Iteration 6 - Control Output: 100.027702
Iteration 7 - Control Output: 100.033585
Iteration 8 - Control Output: 99.994453
Iteration 9 - Control Output: 100.001083
Iteration 10 - Control Output: 99.998322
Iteration 11 - Control Output: 100.000061
Iteration 12 - Control Output: 99.999825
Iteration 13 - Control Output: 100.000038
Iteration 14 - Control Output: 99.999992
Iteration 15 - Control Output: 100.000008
Iteration 16 - Control Output: 100.000000
Iteration 17 - Control Output: 100.000000
Iteration 18 - Control Output: 100.000000
Iteration 19 - Control Output: 100.000000
Iteration 20 - Control Output: 100.000000
Iteration 21 - Control Output: 100.000000
Iteration 22 - Control Output: 100.000000
Iteration 23 - Control Output: 100.000000
Iteration 24 - Control Output: 100.000000
Iteration 25 - Control Output: 100.000000
Iteration 26 - Control Output: 100.000000
Iteration 27 - Control Output: 100.000000
Iteration 28 - Control Output: 100.000000
Iteration 29 - Control Output: 100.000000
Iteration 30 - Control Output: 100.000000
Iteration 31 - Control Output: 100.000000
Iteration 32 - Control Output: 100.000000
Iteration 33 - Control Output: 100.000000
Iteration 34 - Control Output: 100.000000
Iteration 35 - Control Output: 100.000000
Iteration 36 - Control Output: 100.000000
Iteration 37 - Control Output: 100.000000
Iteration 38 - Control Output: 100.000000
Iteration 39 - Control Output: 100.000000
Iteration 40 - Control Output: 100.000000
Iteration 41 - Control Output: 100.000000
Iteration 42 - Control Output: 100.000000
Iteration 43 - Control Output: 100.000000
Iteration 44 - Control Output: 100.000000
Iteration 45 - Control Output: 100.000000
Iteration 46 - Control Output: 100.000000
Iteration 47 - Control Output: 100.000000
Iteration 48 - Control Output: 100.000000
Iteration 49 - Control Output: 100.000000
Iteration 50 - Control Output: 100.000000
Iteration 51 - Control Output: 100.000000
Iteration 52 - Control Output: 100.000000
Iteration 53 - Control Output: 100.000000
Iteration 54 - Control Output: 100.000000
Iteration 55 - Control Output: 100.000000
Iteration 56 - Control Output: 100.000000
Iteration 57 - Control Output: 100.000000
Iteration 58 - Control Output: 100.000000
Iteration 59 - Control Output: 100.000000
Iteration 60 - Control Output: 100.000000
Iteration 61 - Control Output: 100.000000
Iteration 62 - Control Output: 100.000000
Iteration 63 - Control Output: 100.000000
Iteration 64 - Control Output: 100.000000
Iteration 65 - Control Output: 100.000000
Iteration 66 - Control Output: 100.000000
Iteration 67 - Control Output: 100.000000
Iteration 68 - Control Output: 100.000000
Iteration 69 - Control Output: 100.000000
Iteration 70 - Control Output: 100.000000
Iteration 71 - Control Output: 100.000000
Iteration 72 - Control Output: 100.000000
Iteration 73 - Control Output: 100.000000
Iteration 74 - Control Output: 100.000000
Iteration 75 - Control Output: 100.000000
Iteration 76 - Control Output: 100.000000
Iteration 77 - Control Output: 100.000000
Iteration 78 - Control Output: 100.000000
Iteration 79 - Control Output: 100.000000
Iteration 80 - Control Output: 100.000000
Iteration 81 - Control Output: 100.000000
Iteration 82 - Control Output: 100.000000
Iteration 83 - Control Output: 100.000000
Iteration 84 - Control Output: 100.000000
Iteration 85 - Control Output: 100.000000
Iteration 86 - Control Output: 100.000000
Iteration 87 - Control Output: 100.000000
Iteration 88 - Control Output: 100.000000
Iteration 89 - Control Output: 100.000000
Iteration 90 - Control Output: 100.000000
Iteration 91 - Control Output: 100.000000
Iteration 92 - Control Output: 100.000000
Iteration 93 - Control Output: 100.000000
Iteration 94 - Control Output: 100.000000
Iteration 95 - Control Output: 100.000000
Iteration 96 - Control Output: 100.000000
Iteration 97 - Control Output: 100.000000
Iteration 98 - Control Output: 100.000000
Iteration 99 - Control Output: 100.000000

--------------------------------
Process exited after 0.07146 seconds with return value 0
请按任意键继续. . .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

九层指针

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值