From cdc949c74e7b9dabfb3709e9048f41c1a9c25e4f Mon Sep 17 00:00:00 2001 From: Nxersty <144417963+Nxersty@users.noreply.github.com> Date: Thu, 23 Nov 2023 16:08:46 +0800 Subject: [PATCH 1/3] Create main.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 算法大作业 --- C_PID/Nxersty/main.c | 45 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 C_PID/Nxersty/main.c diff --git a/C_PID/Nxersty/main.c b/C_PID/Nxersty/main.c new file mode 100644 index 0000000..ab363eb --- /dev/null +++ b/C_PID/Nxersty/main.c @@ -0,0 +1,45 @@ +#include +#include "pid.h" +int main() +{ + float kp, ki, kd; + printf("输入本次使用的PID参数\n"); + printf("KP:"); + scanf("%lf",&kp); + printf("KI:"); + scanf("%lf",&ki); + printf("KD:"); + scanf("%lf",&kd); + + PID my_pid; + float current_value; + float target; + printf("请输入目标值:"); // 预期的值 + scanf("%lf",&target); + printf("请输入当前值:"); // 目前的值 + scanf("%lf",¤t_value); + + float pos_err,inc_err; + printf("普通位置式pid 普通增量式pid\n"); + printf("误差表:\n"); + + // 初始化PID结构体 + PID_Init(&my_pid, kp, ki, kd, target); + PID_SetParameters(&my_pid, kp, ki, kd); + + + // 模拟执行PID控制器 + for (int i = 0; i < 10; ++i) { + current_value += 5; // 模拟当前值增加 + + // 位置式PID控制 + float pos_pid_output = PID_PositionalPID(&my_pid, current_value); + // 增量式PID控制 + float inc_pid_output = PID_IncrementalPID(&my_pid, current_value); + + printf("E:%.2f E:%.2f\n",pos_pid_output,inc_pid_output); + + } + + return 0; +} From 4cfb78f670527c86f8f6e6852de46a41f989a463 Mon Sep 17 00:00:00 2001 From: Nxersty <144417963+Nxersty@users.noreply.github.com> Date: Thu, 23 Nov 2023 16:09:32 +0800 Subject: [PATCH 2/3] Create PID.h MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 算法大作业 --- C_PID/Nxersty/PID.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 C_PID/Nxersty/PID.h diff --git a/C_PID/Nxersty/PID.h b/C_PID/Nxersty/PID.h new file mode 100644 index 0000000..6b4869f --- /dev/null +++ b/C_PID/Nxersty/PID.h @@ -0,0 +1,18 @@ +#ifndef PID_H +#define PID_H + +typedef struct { + float kp; + float ki; + float kd; + float target; + float error_sum; + float last_error; +} PID; + +int PID_Init(PID *pid, float kp, float ki, float kd, float target); +int PID_SetParameters(PID *pid, float kp, float ki, float kd); +float PID_PositionalPID(PID *pid, float current_value); +float PID_IncrementalPID(PID *pid, float current_value); + +#endifm From f6f95282592bbc7ce9e1e3456a118c6191b3f948 Mon Sep 17 00:00:00 2001 From: Nxersty <144417963+Nxersty@users.noreply.github.com> Date: Thu, 23 Nov 2023 16:10:28 +0800 Subject: [PATCH 3/3] Create PID.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 算法大作业 --- C_PID/Nxersty/PID.c | 61 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 C_PID/Nxersty/PID.c diff --git a/C_PID/Nxersty/PID.c b/C_PID/Nxersty/PID.c new file mode 100644 index 0000000..823126d --- /dev/null +++ b/C_PID/Nxersty/PID.c @@ -0,0 +1,61 @@ +#include "PID.h" +#include +int PID_Init(PID *pid, float kp, float ki, float kd, float target) { + if (pid == NULL) return -1; // 检查PID结构体是否为空 + + // 初始化PID结构体参数 + pid->kp = kp; + pid->ki = ki; + pid->kd = kd; + pid->target = target; + pid->error_sum = 0; + pid->last_error = 0; + + return 0; +} + +int PID_SetParameters(PID *pid, float kp, float ki, float kd) { + if (pid == NULL) return -1; + + // 设置PID结构体参数 + pid->kp = kp; + pid->ki = ki; + pid->kd = kd; + + return 0; +} + +float PID_PositionalPID(PID *pid, float current_value) { + if (pid == NULL) return 0; + + // 计算误差项 + float error = pid->target - current_value; + + // 计算比例项、积分项、微分项 + float p_term = pid->kp * error; + pid->error_sum += error; + float i_term = pid->ki * pid->error_sum; + float d_term = pid->kd * (error - pid->last_error); + + pid->last_error = error; + + // 计算PID输出 + float output = p_term + i_term + d_term; + + return output; +} + +float PID_IncrementalPID(PID *pid, float current_value) { + if (pid == NULL) return 0; + + // 计算误差项 + float error = pid->target - current_value; + + // 计算增量式PID输出 + float output = pid->kp * (error - pid->last_error) + pid->ki * error + pid->kd * (error - 2 * pid->last_error + pid->error_sum); + + pid->last_error = error; + pid->error_sum += error; + + return output; +}