|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
自己做了个小玩意,实现 PID 控制的可视化,这里直接搬了 EasyX 的图形库。
- #include <graphics.h>
- #include <deque>
- #include <cmath>
- #include <windows.h>
- #include <iostream>
- using namespace std;
- double var=0,stdvar=50;
- inline void screen_line(double x1,double y1,double x2,double y2)
- {
- line(static_cast<int>(round(x1)),110-static_cast<int>(round(y1)),static_cast<int>(round(x2)),110-static_cast<int>(round(y2)));
- return;
- }
- inline void draw_axis(void)
- {
- line(40,10,40,210);
- line(0,110,200,110);
- outtextxy(5,10,"100");
- outtextxy(5,60,"50");
- outtextxy(5,160,"-50");
- outtextxy(5,210,"-100");
- }
- inline void cause_error(void)
- {
- var-=10;
- return;
- }
- inline void pid_control(double kp,double ki,double kd)
- {
- static double sum_err=0,last_err=stdvar-var;
- sum_err+=(stdvar-var);
- cout<<"now_err: "<<stdvar-var<<" now_int: "<<sum_err<<endl;
- var+=(kp*(stdvar-var)+ki*sum_err+kd*(last_err-stdvar+var));
-
- last_err=stdvar-var;
- return;
- }
- int main()
- {
- initgraph(400,240);
- BeginBatchDraw();
- setbkcolor(RGB(255,255,255));
- setlinecolor(RGB(0,0,0));
- settextcolor(RGB(0,0,0));
- cleardevice();
-
-
- FlushBatchDraw();
- deque<int> q;
- int xax=0;
- double last_y=0;
- for(int i=0;i<50;i++)
- q.push_back(0);
- Sleep(1000);
- while(1)
- {
- cleardevice();
- draw_axis();
-
- pid_control(0.1,0.1,-0.1);
- cause_error();
-
- q.push_back(var);
- q.pop_front();
- xax=0;
- last_y=0;
- for(auto it=q.begin();it!=q.end();it++)
- {
- screen_line(xax-4,last_y,xax,*it);
- last_y=*it;
- xax+=4;
- }
- screen_line(0,stdvar,200,stdvar);
- line(0,60,200,60);
- FlushBatchDraw();
- Sleep(50);
- }
- EndBatchDraw();
- return 0;
- }
复制代码
下面是实际效果视频展示。
pidshow.zip
(321.99 KB, 下载次数: 1)
|
|