Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

算法大作业提交 #56

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions C_PID/Nxersty/PID.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "PID.h"
#include <stdlib.h>
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;
}
18 changes: 18 additions & 0 deletions C_PID/Nxersty/PID.h
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions C_PID/Nxersty/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include<stdio.h>
#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",&current_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;
}