This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathmain.cpp
132 lines (127 loc) · 3.37 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <algorithm>
#include <cmath>
#include <fstream>
using namespace std;
/**
* @mainpage
* @brief ПИД-регулятор
* @author Бурда Дмитрий Сергеевич
*/
/**
* @class Function
* @brief Абстрактный класс моделей вычисления
*/
class Function{
public:
virtual double function (double wh_t, double wh_w ) = 0;
};
/**
* @class LinealFunction
* @brief Класс линейной модели
* @details Является подклассом Function
*/
class LineFunction: public Function{
private:
/**
* @brief Константы линейной модели
*
* @param a Параметр А
* @param b Параметр B
*/
const double a = 0.2, b = 0.1;
public:
double function ( double wh_t, double wh_w) override{
double rezult;
rezult = a * wh_w + b* wh_w;
return rezult;
}
};
/**
* @class NoLinealFunction
* @brief Класс нелинейной модели
* @details Является подклассом Function
*/
class NoLineFunction:public Function{
private:
/**
* @brief Константы нелинейной модели
*
* @param a Параметр А
* @param b Параметр B
* @param c Параметр С
* @param d Параметр D
*/
const double a = 1, b = 0.0033, c = 0.525, d =0.525;
double wh_w_1 = 0;
double wh_t_1 = 0;
public:
double function (double wh_t,double wh_w) override{
double rezult;
rezult = a * wh_t - b * pow(wh_t_1, 2) + c * wh_w + d * sin(wh_w_1);
wh_w_1 = wh_w;
wh_t_1 = wh_t;
return rezult;
}
};
/**
* @class PID
* @brief Абстрактный класс пид-контроллера
*/
class PID{
private:
/**
* @brief controller
*
* @param T Параметр Т
* @param T0 Параметр Т0
* @param Td Параметр Td
* @param K Параметр k
*/
double u = 0, Td = 50, T =10, T0 = 10, k = 0.1;
public:
double regulator(double e, double e1, double e2){
double q0 = k*(1+(Td/T0));
double q1 = -1*k*(1+2*(Td/T0)-(T0/T));
double q2 = k * (Td/T0);
u += q0*e + q1*e1 + q2*e2;
return u;
}
double PID_contr(double w, double y0, Function* model,ofstream& file){
double buf1 = 0, buf2 = 0, y = y0;
for (int i = 0; i <100; i++) {
double er, u;
er = w - y;
u = regulator(er, buf1, buf2);
y = model->function(y0, u);
file<<er<<"\t"<<u<<"\t"<<y;
buf2 = buf1;
buf1 = er;
}
}
};
int main(){
double w = 80, y = 10;
ofstream file;
file.open("text.txt");
int number;
cout<<"Выберите тип функции ( 1 - линейная, 2 - нелинейная): ";
cin>>number;
switch(number){
case 1:{
PID* pid = new PID;
LineFunction* lfunct = new LineFunction;
pid->PID_contr(w,y,lfunct,file);
break;
}
case 2:{
PID* pid = new PID;
NoLineFunction* nlfunct = new NoLineFunction;
pid->PID_contr(w,y,nlfunct,file);
break;
}
case 0:{
break;
}
}
}