Advertisement
makispaiktis

PI controller

Feb 7th, 2020 (edited)
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <vector>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. // Auxiliary Functions
  10. // 1. askForZero
  11. double askForZero(int n){
  12.     cout << "Give me zero" << n+1 << ": \n";
  13.     double zero;
  14.     cin >> zero;
  15.     while(zero < 0){
  16.         cout << "Give me zero" << n+1 << ": \n";
  17.         cin >> zero;
  18.     }
  19.     return zero;
  20. }
  21.  
  22.  
  23. // 1. askForPole
  24. double askForPole(int n){
  25.     cout << "Give me pole" << n+1 << ": \n";
  26.     double pole;
  27.     cin >> pole;
  28.     while(pole < 0){
  29.         cout << "Give me pole" << n+1 << ": \n";
  30.         cin >> pole;
  31.     }
  32.     return pole;
  33. }
  34.  
  35. int main()
  36. {
  37.     cout << "****************************************************************************\n";
  38.     cout << "Let's create a PI controller given some specifications. You will give me the \n";
  39.     cout << "the plant function Hp(s) and then the set of specifications.\n";
  40.     cout << "****************************************************************************\n\n";
  41.  
  42.  
  43.     // 1. Create the Hp(s) function
  44.     cout << "**** 1. CREATING THE PLANT FUNCTION ****\n\n";
  45.     cout << "a)\n";
  46.     cout << "First, give me the DC gain of Hp(s): \n";
  47.     double G;
  48.     cin >> G;
  49.     while(G <= 0){
  50.         cout << "Give me a positive G: ";
  51.         cin >> G;
  52.     }
  53.     cout << "\nb)\n";
  54.     cout << "Now give me the numbers of 'zeros': ";
  55.     int numOfZeros;
  56.     cin >> numOfZeros;
  57.     while(numOfZeros < 0){
  58.         cout << "Give me the number of zeros: ";
  59.         cin >> numOfZeros;
  60.     }
  61.     vector <double> zeros = vector <double> ();
  62.     if(numOfZeros > 0){
  63.         cout << "\nNow, write down the zeros: Be careful. You have to insert positive values\n";
  64.         cout << "symbolizing the opposite number of zeros in reality!\n";
  65.         for(int i=0; i<numOfZeros; i++){
  66.             zeros.push_back(askForZero(i));
  67.         }
  68.     }
  69.  
  70.     cout << "\nc)\n";
  71.     cout << "Now give me the numbers of 'poles': ";
  72.     int numOfPoles;
  73.     cin >> numOfPoles;
  74.     while(numOfPoles < numOfZeros){
  75.         cout << "Give me the number of poles: ";
  76.         cin >> numOfPoles;
  77.     }
  78.     vector <double> poles = vector <double> ();
  79.     if(numOfPoles > 0){
  80.         cout << "\nNow, write down the poles: Be careful. You have to insert positive values\n";
  81.         cout << "symbolizing the opposite number of poles in reality!\n";
  82.         for(int i=0; i<numOfPoles; i++){
  83.             poles.push_back(askForPole(i));
  84.         }
  85.     }
  86.     // Showing the Hp(s) function, that user created
  87.     cout << endl << endl;
  88.     cout << "G = " << G << ", ";
  89.     for(unsigned int i=0; i<zeros.size(); i++){
  90.         cout << "z" << i << " = " << zeros[i] << ", ";
  91.     }
  92.     for(unsigned int i=0; i<poles.size(); i++){
  93.         cout << "p" << i << " = " << poles[i] << ", ";
  94.     }
  95.     cout << " ----> \n\n";
  96.     if(numOfZeros != 0){
  97.         cout << " Hp(s) = " << G << "*";
  98.     }
  99.     else{
  100.         cout << " Hp(s) = " << G << " / [ ";
  101.     }
  102.     for(unsigned int i=0; i<zeros.size(); i++){
  103.         if(i != zeros.size()-1){
  104.             cout << "(s+" << zeros[i] << ")*" ;
  105.         }
  106.         else{
  107.             cout << "(s+" << zeros[i] << ") / [ ";
  108.         }
  109.     }
  110.  
  111.     for(unsigned int i=0; i<poles.size(); i++){
  112.         if(i != poles.size()-1){
  113.             cout << "(s+" << poles[i] << ")*" ;
  114.         }
  115.         else{
  116.             cout << "(s+" << poles[i] << ") ] \n";
  117.         }
  118.     }
  119.  
  120.     cout << "\nPI controller = G(s) = k*(s+c)/s\n\n";
  121.     cout << "So, A(s) = Hp(s) * G(s) = ";
  122.  
  123.     if(numOfZeros != 0){
  124.         cout << G << "*k*(s+c)*";
  125.     }
  126.     else{
  127.         cout << G << "*k*(s+c) / [ ";
  128.     }
  129.     for(unsigned int i=0; i<zeros.size(); i++){
  130.         if(i != zeros.size()-1){
  131.             cout << "(s+" << zeros[i] << ")*" ;
  132.         }
  133.         else{
  134.             cout << "(s+" << zeros[i] << ") / [ ";
  135.         }
  136.     }
  137.     cout << "s*";
  138.     for(unsigned int i=0; i<poles.size(); i++){
  139.         if(i != poles.size()-1){
  140.             cout << "(s+" << poles[i] << ")*" ;
  141.         }
  142.         else{
  143.             cout << "(s+" << poles[i] << ") ] \n";
  144.         }
  145.     }
  146.  
  147.  
  148.     // 2. Set the specifications
  149.     cout << "\n\n\nPerfect! Now it is time to select the set of specifications. Press one number\n";
  150.     cout << "as shown underneath this message, to choose the appropriate set for you!\n";
  151.     cout << "1. 1) essv, 2) Bandwidth fb, 3) Disturbs rejection in a low frequency (<=1 rad/s),\n";
  152.     cout << "4) Noise depreciation and 5) Phase margin Ph,m\n";
  153.     cout << "2. 1) essa, 2) Bandwidth fb, 3) Disturbs rejection in a low frequency (<=1 rad/s),\n";
  154.     cout << "4) Noise depreciation and 5) Phase margin Ph,m\n";
  155.  
  156.     int choice;
  157.     cin >> choice;
  158.     switch(choice){
  159.  
  160.     case 1:
  161.         cout << "1. essv <= ?\n";
  162.         double essv;
  163.         cin >> essv;
  164.         while(essv < 0){
  165.             cout << " essv <= ?\n";
  166.             cin >> essv;
  167.         }
  168.         // First, I check if there is
  169.         break;
  170.     case 2:
  171.         break;
  172.     default:
  173.         break;
  174.     }
  175.  
  176.     return 0;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement