[牛人杂谈] 我让chatGPT帮我写PID,它写的好像不对呀

[复制链接]
1005|9
 楼主| gaoyang9992006 发表于 2023-2-15 08:53 | 显示全部楼层 |阅读模式
gp, pi, pid
4528763ec2c4e9d63d.png

3217963ec2c5f2d865.png
增量式PID控制的C程序: 以下为一个使用C语言编写的基本增量式PID控制程序:
  1. #include <stdio.h>
  2. //Define Variables
  3. int target;
  4. int KP;
  5. int KI;
  6. int KD;

  7. //Function definition
  8. void pid_control(int Error){
  9.     int Proportional_Control, Integral_Control, Derivative_Control, Output;
  10.    
  11.     Proportional_Control = Error * KP;        //Proportional Control

  12.     Integral_Control = Error * KI;           //Integral Control

  13.     Derivative_Control = Error * KD;         //Derivative Control

  14.     Output = Proportional_Control + Integral_Control + Derivative_Control;      //Calculate the Output

  15.     if (Output > 0){                                  //Limit the output
  16.        printf("Increase output");                     //Do appropriate action
  17.     }
  18.     else {
  19.        printf("Decrease output");                     //Do appropriate action
  20.     }
  21. }

  22. int main(){
  23.     printf("Enter the Target Value:");
  24.     scanf("%d", &target);                             //Read the target value

  25.     //Calculate PID coefficients
  26.     KP = 2.5;
  27.     KI = 0.5;
  28.     KD = 0.1;

  29.     pid_control(target);                              //Call the function to control the system

  30.     return 0;
  31. }
然后我让它再写一份

  1. #include <stdio.h>

  2. int main()
  3. {
  4.     float error, prevError;
  5.     float Kp, Ki, Kd;
  6.     float output;

  7.     // Proportional gain
  8.     Kp = 0.5;

  9.     // Integral gain
  10.     Ki = 0.1;

  11.     // Derivative gain
  12.     Kd = 0.05;

  13.     while (1)
  14.     {
  15.         // Compute Error
  16.         prevError = error;
  17.         error = getError();  // This should return the process error as a float

  18.         // Compute output from PID algorithm
  19.         float pTerm = Kp * error;
  20.         float iTerm = Ki * (error + prevError);
  21.         float dTerm = Kd * (error - prevError);
  22.         output = pTerm + iTerm + dTerm;
  23.          
  24.         // Send output to the system to be controlled
  25.         outputControl(output);   // This should send the processed output to the system being controlled
  26.      }
  27.      return 0;
  28. }
我怎么看第一个不对,大家说它写的对吗?

 楼主| gaoyang9992006 发表于 2023-2-15 09:03 | 显示全部楼层
又给我来了一段
  1. /*
  2. * Program to illustrate Incremental PID control
  3. */
  4. # include <stdio.h>
  5. int main()
  6. {
  7.      double Kp, Ki, Kd; // Proportional, Integral, Derivative constants
  8.      int i;

  9.      // Get the user input
  10.      printf("Enter the Proportional(Kp), Integral(Ki), and Derivative(Kd) values:");
  11.      scanf("%lf %lf %lf", &Kp, &Ki, &Kd);

  12.      // Compute the incremental PID output
  13.      double increment = Kp + Ki + Kd;

  14.      // Apply the incremental PID output
  15.      for (i=0; i<1000; i++) {
  16.          printf("Incremental PID Output at Timestep %d = %f\n", i, increment);

  17.      }

  18.      // Exit gracefully
  19.      return 0;
  20. }
小明的同学 发表于 2023-2-19 18:15 | 显示全部楼层
哈哈,这东西参考还可以,用我是不敢。
小小蚂蚁举千斤 发表于 2023-2-23 16:11 | 显示全部楼层
chatGPT说是这个写论文太可以了
huangcunxiake 发表于 2023-2-23 21:54 | 显示全部楼层
好玩,怎么安装的,分享个key吧
AdaMaYun 发表于 2023-2-24 08:48 | 显示全部楼层
这是科技还是狠活,文字类看过文章好像效果不错,这么看代码不行
 楼主| gaoyang9992006 发表于 2023-2-24 08:49 | 显示全部楼层
AdaMaYun 发表于 2023-2-24 08:48
这是科技还是狠活,文字类看过文章好像效果不错,这么看代码不行

是的,这东西其实并没真正理解什么是PID算法。只是胡乱匹配数据库内的资料。
AloneKaven 发表于 2023-3-1 20:16 | 显示全部楼层
这个只适合搞点文章吧
phoenixwhite 发表于 2023-3-11 21:58 | 显示全部楼层
就当人脑看就行了。              
janewood 发表于 2023-3-14 15:48 | 显示全部楼层
不知道在哪里搜索来的呢              
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:如果你觉得我的分享或者答复还可以,请给我点赞,谢谢。

2055

主题

16424

帖子

222

粉丝
快速回复 在线客服 返回列表 返回顶部