Hubert_M

Untitled

May 29th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.10 KB | None | 0 0
  1. #include "MUX74HC4067.h"
  2.  
  3. #define WYP  0.63 // współczynnik wypływu dla ostrych brzegów
  4. //#define PI  3.14
  5. #define G 9.81
  6. #define TM 6 // 100ms/1 cykl symulacji = 6 sekund dla rzeczywistego obiektu
  7. #define QM 11 // maks wartosć potencjometrów
  8. #define X1 0.6
  9. #define X2 1.3
  10. #define X3 1.9
  11.  
  12.  
  13. // Creates a MUX74HC4067 instance
  14. // 1st argument is the Arduino PIN to which the EN pin connects
  15. // 2nd-5th arguments are the Arduino PINs to which the S0-S3 pins connect
  16. MUX74HC4067 mux(7, 8, 9, 10, 11);
  17.  
  18. unsigned long currentTime = 0;
  19. unsigned long previousTime = 0;
  20. unsigned long deltaTime = 0;
  21. double data[10];
  22.  
  23. /* --- Przyciski --- */
  24. int button1Pin = 2, button2Pin = 3, button3Pin =4, button4Pin = 5, button5Pin = 6,  button6Pin = 7;
  25. int button1State = LOW, button2State = LOW, button3State = LOW, button4State = LOW, button5State = LOW, button6State = LOW;
  26. int button1Previous = LOW, button2Previous = LOW, button3Previous = LOW, button4Previous = LOW, button5Previous = LOW, button6Previous = LOW;
  27. unsigned long time1 = 0, time2 = 0, time3 = 0, time4 = 0, time5 = 0, time6 = 0;
  28. long debounce = 200;    
  29. /* --- Przyciski --- */
  30.      
  31.     double d1 = 0, d2 = 0, d3 = 0, d4 = 0; // różnica w poziomie wodym w całym cyklu
  32.     class Tank // zmiany tu
  33.     {
  34.       public:
  35.       int x1 = 0, x2 = 0, x3 = 0;
  36.       double hT = 2; // wysokość zbiornika w m2
  37.       double r = 0.5; // promień zbiornika w m
  38.       double sT = PI * r * r; // powierzchnia podstawy zbiornika w m2
  39.       double vT = sT * hT; // objętość zbiornika w m3
  40.       double hW = 0.0; // poziom wody w zbiorniku w m
  41.       double vW = 0; // objętość wody w zbiorniku w m3 50litra max objętość
  42.       double tWp = 20;
  43.       double tWn = 20;
  44.       int m = 0; // mieszadło on/off
  45.       int overflowed = 0; // czujnik przelania
  46.       void check();
  47.      
  48.     };
  49.      void Tank::check()
  50.      {
  51.       if (hW>=hT)
  52.       {
  53.         hW=2;
  54.         overflowed = 1;
  55.       }
  56.       else overflowed = 0;
  57.       (hW>=X1)?x1 = 1:x1 = 0;
  58.       (hW>=X2)?x2 = 1:x2 = 0;
  59.       (hW>=X3)?x3 = 1:x3 = 0;
  60.       vW=sT*hW;
  61.      }
  62.      
  63.     class Valve
  64.     {
  65.       public:
  66.       int status = 0; // 1 nalewa/ 0 nic nie robi
  67.       double r = 0.03; // promień przekorju zaworu w m
  68.       double d = 1; // procent otwarcia zaworu
  69.       double sV = PI * r * r; // przekrój otworu w m2
  70.       double rSV = sV; // Obecny przekrój
  71.       void qV(double x)
  72.       {
  73.         if (x != QM)
  74.         {
  75.         rSV = sV / x;
  76.         }
  77.         else
  78.         {
  79.           rSV = 0;
  80.         }
  81.       }
  82.     };
  83.  
  84.     class Heater // zmiany tu
  85.     {
  86.       public:
  87.       int status = 0; // 1 grzeje/ o nie grzeje
  88.       int p=100;
  89.       double alfa1=2.8;
  90.       double alfa2=-0.8;
  91.       double alfa3=0.04;
  92.       double tHp = 20;
  93.       double tHn = 20;
  94.      
  95.       void dH(double x)
  96.       {
  97.           alfa2=-x;
  98.       }
  99.     };
  100.  
  101.     void heat(Tank& t, Heater& h) // zmiany tu
  102.     {
  103.       h.tHn = h.tHp + 0.1 * (h.p-h.alfa1*(h.tHp - t.tWp));
  104.       if(t.vW)
  105.       t.tWn = t.tWp + 0.1 * (h.alfa2 * 1/t.vW * (t.tWp - h.tHp) + h.alfa3*(t.tWp - 20));
  106.       else t.tWn = 20;
  107.       if(t.tWn > 100)
  108.       {
  109.         t.tWn = 100;
  110.       }
  111.       t.tWp = t.tWn;
  112.       h.tHp = h.tHn;
  113.      
  114.     }
  115.  
  116.     void cool(Tank& t, Heater& h) // zmiany tu
  117.     {
  118.       t.tWn=20+(t.tWp-20)*exp(-0.017*1/t.vW);
  119.       t.tWp = t.tWn;
  120.       h.tHp = t.tWn;
  121.      
  122.     }
  123.  
  124.     void mixTemp(Tank& tP,Tank& tS, double d) // zmiany tu
  125.     {
  126.       if(d>0)
  127.       tP.tWp = (tP.vW*997*tP.tWp + d*tS.sT*997*tS.tWp) / (tP.vW*997 + d*tS.sT*997);
  128.     }
  129.      
  130.     double bind(Tank t, Valve v,int s, double h = 1000)
  131.     {
  132.         double d,q;
  133.         if (v.rSV == 0)
  134.         return 0;
  135.         if (h==1000)
  136.         q =WYP * v.rSV * sqrt(2*G*(h/1000))
  137.         else
  138.         q =WYP * v.rSV * sqrt(2*G*h); // natężenie wypływu w m3/s
  139.        
  140.         d = TM * q / t.sT;
  141.         if (h<1000) if (d>=h) d = h;
  142.         if (d>=h) d = h;
  143.         if(s==-1)
  144.         {
  145.             return -d;
  146.         }
  147.         return d;
  148.     }
  149.  
  150.     void muxRead() // zmiany tu
  151.     {
  152.  
  153.       for (int i = 0; i < 10; ++i)
  154.       {
  155.       // Reads from channel i. Returns a value from 0 to 1023
  156.       //data[i] = mux.read(i);
  157.       if(i<6)
  158.       data[i] = mux.read(i)*10/1023+1;    
  159.       else  
  160.       {
  161.         data[i] = mux.read(i);
  162.         data[i] = (data[i]*75/1023+5);
  163.         data[i] = map(data[i], 5, 80, 80, 5);
  164.         data[i] = data[i]/100;  
  165.       }
  166.       }
  167.     }
  168.      
  169.         //int i=0;
  170.  
  171.         Heater h1,h2,h3,h4;
  172.         Tank t1,t2,t3,t4;
  173.         Valve v1,v2,v3,v4,v5,v6;
  174.        
  175.     void setup() {
  176.       // put your setup code here, to run once:
  177.       Serial.begin(9600);
  178.         pinMode(button1Pin, INPUT);
  179.         pinMode(button2Pin, INPUT);
  180.         pinMode(button3Pin, INPUT);
  181.         pinMode(button4Pin, INPUT);
  182.        
  183.   // Configures how the SIG pin will be interfaced
  184.   // e.g. The SIG pin connects to PIN A0 on the Arduino,
  185.       //      and PIN A0 is a analog input
  186.        mux.signalPin(A0, INPUT, ANALOG);
  187.  
  188.     }
  189.        
  190.     void loop() {
  191.  
  192.     currentTime = millis();
  193.     if (currentTime - previousTime >= 100UL) // co 100ms
  194.     {
  195.            { // obsługa guzików
  196.       button1State = digitalRead(button1Pin);
  197.       if (button1State == HIGH && button1Previous == LOW && millis() - time1 > debounce)
  198.       {
  199.  
  200.         if (v1.status == HIGH)
  201.         {
  202.           v1.status = LOW;
  203.         }
  204.         else
  205.           v1.status = HIGH;
  206.        
  207.         time1 = millis();
  208.       }
  209.       button1Previous = button1State;
  210.  
  211.      /* button2State = digitalRead(button2Pin);
  212.       if (button2State == HIGH && button2Previous == LOW && millis() - time2 > debounce)
  213.       {
  214.         if (v2.status == HIGH)
  215.           v2.status = LOW;
  216.         else
  217.           v2.status = HIGH;
  218.  
  219.         time2 = millis();
  220.       }
  221.       button2Previous = button2State;
  222.  
  223.       button3State = digitalRead(button3Pin);
  224.       if (button3State == HIGH && button3Previous == LOW && millis() - time3 > debounce)
  225.       {
  226.         if (v3.status == HIGH)
  227.           v3.status = LOW;
  228.         else
  229.           v3.status = HIGH;
  230.         time3 = millis();
  231.       }
  232.       button3Previous = button3State;
  233.  
  234.      
  235.       button4State = digitalRead(button4Pin);
  236.       if (button4State == HIGH && button4Previous == LOW && millis() - time4 > debounce)
  237.       {
  238.         if (v4.status == HIGH)
  239.           v4.status = LOW;
  240.         else
  241.           v4.status = HIGH;
  242.         time4 = millis();
  243.       }
  244.       button4Previous = button4State;
  245.  
  246.      
  247.       button5State = digitalRead(button5Pin);
  248.       if (button5State == HIGH && button5Previous == LOW && millis() - time5 > debounce)
  249.       {
  250.         if (v5.status == HIGH)
  251.           v5.status = LOW;
  252.         else
  253.           v5.status = HIGH;
  254.         time5 = millis();
  255.       }
  256.       button5Previous = button5State;
  257.  
  258.      
  259.       button6State = digitalRead(button6Pin);
  260.       if (button6State == HIGH && button6Previous == LOW && millis() - time6 > debounce)
  261.       {
  262.         if (v6.status == HIGH)
  263.           v6.status = LOW;
  264.         else
  265.           v6.status = HIGH;
  266.         time6 = millis();
  267.       }
  268.       button6Previous = button6State;  
  269.       */
  270.     } // koniec obsługi guzikówm
  271.        
  272.         muxRead();// zmiany tu
  273.         v1.qV(data[0]);
  274.         v2.qV(data[1]);
  275.         v3.qV(data[2]);
  276.         v4.qV(data[3]);
  277.         v5.qV(data[4]);
  278.         v6.qV(data[5]);
  279.         h1.dH(data[6]);
  280.         h2.dH(data[7]);
  281.         h3.dH(data[8]);
  282.         h4.dH(data[9]);
  283.  
  284.         // zmiany tu
  285.         if (v1.status == HIGH) d1 += bind(t1, v1, 1);
  286.         if (v2.status == HIGH)  d1 += bind(t1, v2, -1, t1.hW);
  287.        
  288.         if (v2.status == HIGH) d2 += bind(t2, v2, 1, t1.hW);
  289.         if (h2.status == 1)mixTemp(t2,t1,d2);
  290.         t1.hW += d1;
  291.         d1=0;
  292.         if (v3.status == HIGH) d1 += bind(t1, v3, -1, t1.hW);
  293.                
  294.         if (v4.status == HIGH) d2 += bind(t2, v4, -1, t2.hW);
  295.        
  296.         if (v3.status == HIGH) d3 += bind(t3, v3, 1, t1.hW);    
  297.         if (h3.status == 1)mixTemp(t3,t1,d3);
  298.         if (v5.status == HIGH) d3 += bind(t3, v5, -1, t3.hW);
  299.        
  300.         if (v4.status == HIGH) d4 += bind(t4, v4, 1, t2.hW);
  301.         if (h4.status == 1)mixTemp(t4,t2,d4);
  302.         t4.hW += d4;
  303.         d4=0;
  304.         if (v5.status == HIGH) d4 += bind(t4, v5, 1, t3.hW);
  305.         if (h4.status == 1)mixTemp(t4,t3,d4);  
  306.         if (v6.status == HIGH) d4 += bind(t4, v6, -1, t4.hW);
  307.  
  308.        
  309.         t1.hW += d1; // zmiany tu
  310.         t2.hW += d2;
  311.         t3.hW += d3;
  312.         t4.hW += d4;
  313.          
  314.          t1.check();
  315.          t2.check();
  316.          t3.check();
  317.          t4.check();    
  318.          if (t1.overflowed==1)
  319.          {
  320.           t1.hW=2;
  321.           }
  322.          if (t2.overflowed==1)
  323.          {
  324.           t2.hW=2;
  325.          }      
  326.              
  327.          if (h1.status == 1) heat(t1,h1); // zmiany tu
  328.          else cool(t1,h1);
  329.          if (h2.status == 1) heat(t2,h2);
  330.          else cool(t2,h2);
  331.          if (h3.status == 1) heat(t3,h3);
  332.          else cool(t3,h3);
  333.          if (h4.status == 1) heat(t4,h4);
  334.          else cool(t4,h4);
  335.       // put your main code here, to run repeatedly:
  336.       Serial.print(t1.tWp);
  337.       Serial.print(" | ");
  338.       Serial.print(h1.tHp);
  339.       Serial.print(" | ");
  340.       Serial.print(t1.hW,5);
  341.       Serial.print(" | ");
  342.       Serial.print(t2.hW,5);
  343.       Serial.print(" | ");
  344.       //Serial.print(t1.tW);
  345.       Serial.print(" | ");
  346.       Serial.print(data[6]);
  347.       Serial.print(" | ");
  348.       Serial.print(data[1]);
  349.       Serial.print(" | ");
  350.       Serial.print(v2.status);
  351.       Serial.print(" | ");
  352.       Serial.print(v5.status);
  353.       Serial.print(" | ");
  354.       Serial.print(t4.hW,6);
  355.       Serial.print(" | ");
  356.       //Serial.print(d1);
  357.       //int dp=analogRead(A1);
  358.       //Serial.println(dp);
  359.      
  360.      
  361.       Serial.println(v6.status);
  362.       //Serial.println(button1State);
  363.       //Serial.print(" | ");
  364.       //Serial.println(t1.sZb,9);
  365.       d1 = 0; d2 = 0; d3 = 0; d4 = 0;
  366.  
  367.       previousTime = currentTime; // do loopa co 100ms
  368.   } // co 100ms
  369.     }
Advertisement
Add Comment
Please, Sign In to add comment