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
128 lines (87 loc) · 2.52 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
#include <iostream>
#include <math.h>
using namespace std;
/*
Создаем абстрактный класс Model, от которого будут наследоватся LinearModel и NonLinearModel
*/
class Model {
public:
virtual double Iteration(double roomTemp, double inputWarm) = 0;
};
/*
Класс линейной модели поведения
*/
class LinearModel : public Model {
private:
const double A = 0.333, B = 0.667;
public:
double Iteration(double roomTemp, double inputWarm) {
double tempNext;
tempNext = A * roomTemp + B * inputWarm;
return tempNext;
}
};
/*
Класс линейной модели поведения
*/
class NonLinearModel : public Model
{
private:
const double A = 1, B = 0.0033, C = 0.525, D = 0.525;
double tempPrev = 0, warmPrev = 0;
public:
double Iteration(double roomTemp, double inputWarm) {
double tempNext = A * roomTemp - B * pow(tempPrev, 2) + C * inputWarm + D * sin(warmPrev);
warmPrev = inputWarm;
tempPrev = roomTemp;
return tempNext;
}
};
/*
Создаем абстрактный класс Model, от которого будут наследоватся LinearModel и NonLinearModel
*/
class Regulator {
private:
double T = 10, T0 = 10, TD = 50, K = 0.1;
double u = 0;
double num = 100;
public:
double Regulate(double e, double e1, double e2) {
double q0 = K * (1 + TD / T0);
double q1 = -K * (1 + 2 * TD / T0 - T0 / T);
double q2 = K * TD / T0;
u += q0 * e + q1 * e1 + q2 * e2;
return u;
}
void ResetU()
{
u = 0;
}
void pidRegulator(double w, double y0, Model* model)
{
double errorPrev = 0, error2Prev = 0, y = y0;
for (int i = 0; i < num; i++) {
double error, u;
error = w - y;
u = Regulate(error, errorPrev, error2Prev);
y = model->Iteration(y0, u);
std::cout << "E = " << error << ", Y = " << y << ", U = " << u << std::endl;
error2Prev = errorPrev;
errorPrev = error;
}
}
};
int main()
{
setlocale(LC_ALL, "Russian");
double w = 24, y = 18;
Regulator* regulator = new Regulator;
LinearModel* lModel = new LinearModel;
NonLinearModel* nlModel = new NonLinearModel;
cout << "Linear: " << endl;
regulator->pidRegulator(w, y, lModel);
regulator->ResetU();
cout << "NonLinear: " << endl;
regulator->pidRegulator(w, y, nlModel);
return 0;
}