Skip to content
This repository has been archived by the owner on Jan 23, 2025. It is now read-only.

Commit

Permalink
Все переделано, работает нормально.
Browse files Browse the repository at this point in the history
(!) Вывод у нелинейной модели странный
  • Loading branch information
gleb7499 committed Sep 21, 2024
1 parent 53c7568 commit 9000fb2
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 107 deletions.
49 changes: 24 additions & 25 deletions tasks/task_02/src/PID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,34 @@
*/

#include "PID.h"

/**
* @brief The constructor of the PID controller.
*
* @param K The coefficient P in the PID controller.
* @param T The coefficient I in the PID controller.
* @param TD The coefficient D in the PID controller.
*/

PID::PID(double K, double T, double TD) : kp(K), ki(T), kd(TD) {}
#include "models.h"

/**
* @brief The method to calculate the control signal by PID.
*
* @param error The vector of errors.
*/
void PID::calculate(const std::vector<double> &error) {
double integralError = 0.0;
double derivativeError = 0.0;
double controlSignal = 0.0;
double previous_error = 0.0;
for (int i = 0; i < error.size(); ++i) {


integralError += error[i];
derivativeError = error[i] - previous_error;
previous_error = error[i];
controlSignal = kp * error[i] + ki * integralError + kd * derivativeError;
this->control_signals.push_back(controlSignal);
void PID::calculate(const double& w, const double& T0, const std::vector<double>& temp) {
double q0;
double q1;
double q2;
double e0;
double e1 = 0;
double e2 = 0;
double Uk;
double delta_Uk;
double Uk_1 = 0;
for (const auto& y0 : temp) {
e0 = w - y0;
q0 = K * (1 + TD / T0);
q1 = -K * (1 + 2 * TD / T0 - T0 / T);
q2 = K * TD / T0;
delta_Uk = q0 * e0 + q1 * e1 + q2 * e2;
Uk = Uk_1 + delta_Uk;
Uk_1 = Uk;
e2 = e1;
e1 = e0;
res_liner.push_back({e0, y0, Uk});
}
}

Expand All @@ -48,7 +47,7 @@ void PID::calculate(const std::vector<double> &error) {
*
* @return The vector of control signals.
*/
std::vector<double> PID::getControlSignals() const {
return this->control_signals;
std::vector<std::vector<double>> PID::getControlSignals() const {
return this->res_liner;
}

20 changes: 6 additions & 14 deletions tasks/task_02/src/PID.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,27 @@
class PID {
private:
/// The coefficient K in the PID controller.
double kp;
const double K = 10;
/// The coefficient T in the PID controller.
double ki;
const double T = 0.1;
/// The coefficient TD in the PID controller.
double kd;
const double TD = 50;
/// The vector of control signals.
std::vector <double> control_signals;
std::vector<std::vector<double>> res_liner;

public:
/**
* @brief The constructor of the PID controller.
*
* @param K The coefficient P in the PID controller.
* @param T The coefficient I in the PID controller.
* @param TD The coefficient D in the PID controller.
*/
PID(double K, double T, double TD);
/**
* @brief The method to calculate the control signal by PID.
*
* @param error The vector of errors.
*/
void calculate(const std::vector <double>& error);
void calculate(const double& w, const double& T0, const std::vector <double>& temps_linear);
/**
* @brief The method to get the control signals.
*
* @return The vector of control signals.
*/
std::vector<double> getControlSignals() const;
std::vector<std::vector<double>> getControlSignals() const;
};

#endif //PID_S
116 changes: 48 additions & 68 deletions tasks/task_02/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ using namespace std;

void start();
bool choice_input(int &choice);
PID get_pig();
LinearModel get_liner_model();
NonLinearModel get_nonlinear_model();
vector <double> get_error(const vector <double>& temps_linear, const vector <double>& temps_nonlinear);
LinearModel get_liner_model(const int &T0);
NonLinearModel get_nonlinear_model(const int &T0);
template<typename T> bool input_value(T &value, const string &name);
void output(const vector <vector<double>>& control_signals, const string& name);

/**
* @brief The main function.
Expand Down Expand Up @@ -55,36 +55,55 @@ int main() {
* calculate the error, calculate the control signal by PID and output the results.
*/
void start() {
double w;
int T0;

cout << "Enter the algorithm of the system functioning\n";
input_value(w, "w(t)");

cout << "Enter the step\n";
input_value(T0, "T0");

cout << "\tFill in the data for the Linear model\n";
LinearModel liner_model = get_liner_model();
cout << "\tFill in the data for the Nonlinear model\n";
NonLinearModel non_liner_model = get_nonlinear_model();
LinearModel liner_model = get_liner_model(T0);

vector <double> temps_linear = liner_model.getTemp();

PID pid_liner, pid_nonlinear;

pid_liner.calculate(w, T0, temps_linear);

vector <vector<double>> control_signals_liner = pid_liner.getControlSignals();

cout << "\tFill in the data for the Nonlinear model\n";
PID pid = get_pig();
NonLinearModel non_liner_model = get_nonlinear_model(T0);

vector <double> temps_linear = liner_model.getTemp();
vector <double> temps_nonlinear = non_liner_model.getTemp();
vector <double> error = get_error(temps_linear, temps_nonlinear);

pid.calculate(error);
pid_nonlinear.calculate(w, T0, temps_nonlinear);

vector <double> control_signals = pid.getControlSignals();
vector <vector<double>> control_signals_nonlinear = pid_nonlinear.getControlSignals();

// Output
output(control_signals_liner, "Linear model");
output(control_signals_nonlinear, "Nonlinear model");

}

void output(const vector <vector<double>>& control_signals, const string& name) {
cout << "\n\t\t\t\t\t\t\t\tRESULTS\n\n";
cout << "\t\t\t\t\t\t\t" << name << "\n\n";
cout.setf(ios::left);
cout << setw(10) << "TIME";
cout << setw(17) << "LINAR MODEL";
cout << setw(20) << "NONLINAR MODEL";
cout << setw(17) << "ERROR";
cout << setw(19) << "CONTROL SIGNAL" << endl;
cout << setfill('=') << setw(80) << "" << setfill(' ') << endl;
for (int i = 0; i < control_signals.size(); ++i) {
cout << setw(10) << (i + 1);
cout << setw(17) << temps_linear[i];
cout << setw(20) << temps_nonlinear[i];
cout << setw(17) << error[i];
cout << setw(19) << control_signals[i] << endl;
cout << setw(15) << "TIME (T0)";
cout << setw(20) << "DEVIATION (e)";
cout << setw(27) << "OUTPUT VARIABLE (Yt)";
cout << setw(35) << "CONTROLLING INFLUENCE (Uk)" << endl;
cout << setfill('=') << setw(85) << "" << setfill(' ') << endl;
int i = 1;
for (const auto& signal : control_signals) {
cout << setw(15) << i++;
cout << setw(20) << signal[0];
cout << setw(27) << signal[1];
cout << setw(35) << signal[2] << endl;
}
}

Expand Down Expand Up @@ -115,17 +134,14 @@ template<typename T> bool input_value(T &value, const string &name) {
*
* @return The Linear Model.
*/
LinearModel get_liner_model() {
LinearModel get_liner_model(const int& T0) {
double A, B, current_temperature, warm;
int time;
input_value(A, "A");
input_value(B, "B");
input_value(current_temperature, "current_temperature");
input_value(warm, "warm");
input_value(time, "time");
LinearModel model(A, B, current_temperature, warm);
model.calculate(time);
model.print();
model.calculate(T0);
return model;
}

Expand All @@ -134,55 +150,19 @@ LinearModel get_liner_model() {
*
* @return The Nonlinear Model.
*/
NonLinearModel get_nonlinear_model() {
NonLinearModel get_nonlinear_model(const int& T0) {
double A, B, C, D, current_temperature, warm;
int time;
input_value(A, "A");
input_value(B, "B");
input_value(C, "C");
input_value(D, "D");
input_value(current_temperature, "current_temperature");
input_value(warm, "warm");
input_value(time, "time");
NonLinearModel model(A, B, C, D, current_temperature, warm);
model.calculate(time);
model.print();
model.calculate(T0);
return model;
}

/**
* @brief The function to calculate the error.
*
* @param temps_linear The temperatures of the Linear Model.
* @param temps_nonlinear The temperatures of the Nonlinear Model.
*
* @return The error.
*/
vector <double> get_error(const vector <double>& temps_linear, const vector <double>& temps_nonlinear) {
if (temps_linear.size() != temps_nonlinear.size()) {
cerr << "\n\a\t\t*The sizes of the models are not equal*\n\n";
}
vector <double> error;
for (int i = 0; i < temps_linear.size() && i < temps_nonlinear.size(); ++i) {
error.push_back(temps_linear[i] - temps_nonlinear[i]);
}
return error;
}

/**
* @brief The function to input data for the PID.
*
* @return The PID.
*/
PID get_pig() {
double P, I, D;
input_value(P, "P");
input_value(I, "I");
input_value(D, "D");
PID pid(P, I, D);
return pid;
}

/**
* @brief The function to input choice.
*
Expand Down

0 comments on commit 9000fb2

Please sign in to comment.