#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
请按任意键继续. . .