Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 22.68 KB | None | 0 0
  1. #include "stationen.h"
  2.  
  3. /*******************************  Kommentare und Änderungen ***********************************/
  4.  
  5. //  Änderungen FB1:
  6. //      Übergang aus Standby bei count > 0 statt count == 1
  7. //      "statepulse" entfernt
  8. //      hatKiste = 1 schon einen State früher (state 2)
  9. //  Änderungen DT1:
  10. //      Übergang aus Standby bei fbi.typ > 0 gelöscht (tritt nie ein wegen hatKiste)
  11. //  Änderungen Waage:
  12. //      waage.bereit = 0 wird später gesetzt (state 5)
  13. //      timer_info gelöscht
  14. //      In case 9 springt er immer zurück auf "Standby" statt auf "Hole Kiste".
  15. //          (Einheitlicher) Ist dt1.typ > 0 springt er dann direkt weiter.
  16. //      Hab noch eine Filterung reingebaut, die extreme Werte raushaut
  17. //  Änderungen DT2:
  18. //      dt2.bereit = 0 wird später gesetzt (state 5)
  19. //  Änderungen Mixer:
  20. //      Timer_1_def gelöscht
  21. //      Timer_0 und Timer_1 zusammengefasst und umbennant in timer
  22. //      mixer.bereit = 1 wird früher gesetzt (state 8)
  23. //      Rücksprung aus Rausholen nur nach Standby (wie bei Waage)
  24. //  Änderungen TempReg:
  25. //      irgendwo ein temp < temp_max ersetzt durch temp <= temp_max
  26. //  Änderungen Ofen:
  27. //      ofenzeit umbenannt in timer
  28. //      bei case 3 if-Bedingung geändert: ofen.SIO == 0 statt ofen.SIO == 1
  29. //          (sonst springt er immer direkt weiter)
  30. //      bei case 4 if-Bedingung geändert: ofen.SIO == 1 statt ofen.SIO == 0
  31. //          (kann sein dass invertierte Logik?? -> umändern! / erledigt)
  32. //      ofen.bereit = 0 erst bei state 5
  33. // 
  34. //  Ansonsten habe ich alle "Typ" durch "typ" und "Z" durch "z"s ersetzt.
  35. //  Alle restlichen printfs durch idsci_printfs ersetzt.   
  36. //  -Mathias-
  37. //
  38. //
  39. //
  40. //
  41. //
  42.  
  43. /********************************   Zähler   **************************************************/
  44.  
  45. void f_zaehler(void){
  46.     static char state;
  47.     //init:
  48.         if (erster_aufruf)
  49.         {   state = 2;
  50.             fb1.count = 0;
  51.             if (states) {idsci_printf("Zaehler:      Lichtschranke frei\n");}
  52.         }
  53.  
  54.     switch (state) {
  55.     case 1:     //blockiert
  56.         if (fb1.SLE == 0)
  57.         {   state = 2;
  58.             if (states) {idsci_printf("Zaehler:      Lichtschranke frei\n");}
  59.         }
  60.     break;
  61.     case 2:     //frei
  62.         if (fb1.SLE == 1)
  63.         {   fb1.count++;
  64.             state = 1;
  65.             if (states) {idsci_printf("Zaehler:      Lichtschranke blockiert\n");}
  66.         }
  67.     break;
  68.     }
  69. }
  70.  
  71. /*********************************   FB1   ***************************************************/
  72.  
  73. void f_FB1(void){
  74.     static char state;
  75.     static char pulse;
  76.     static int timer;
  77.  
  78.     //idsci_printf("fb1 timer:%i\n",timer);
  79.     //init:
  80.         if (erster_aufruf)
  81.         {   fb1.typ = 0;
  82.             fb1.hatKiste = 0;
  83.             state = 0;
  84.             pulse = 1;
  85.             timer = 0;
  86.             if (states) {idsci_printf("FB:           Warte auf Kiste\n");}
  87.         }
  88.  
  89.     switch (state){
  90.     case 0:     //standby
  91.         fb1.MFB1 = 0;
  92.         if (fb1.count > 0)
  93.         {   state = 1;
  94.             if (states) {idsci_printf("FB:           Transpotiere\n");}
  95.         }
  96.     break;
  97.     case 1:     //fährt
  98.         fb1.MFB1 = 1;
  99.         if (fb1.SIF1 == 1)
  100.         {   state = 2;
  101.             //if (states) {idsci_printf("FB1:           Kiste am Ende\n");}
  102.         }
  103.     break;
  104.     case 2:
  105.         fb1.MFB1 = 1;
  106.         if (fb1.SIF1 == 0)
  107.         {   state = 3;
  108.             fb1.hatKiste = 1;
  109.             if (states) {idsci_printf("FB:           Kiste bereit\n");}
  110.         }
  111.     break;
  112.     case 3:     //Drehtisch bereit?
  113.         fb1.MFB1 = 0;
  114.         if (dt1.bereit == 1)
  115.         {   timer = 500 * 1000/T_syst;                                // Beim Zählen Xms warten
  116.             pulse = 1;
  117.             state = 4;         
  118.             if (states) {idsci_printf("FB:           Zaehle Pulse\n");}
  119.         }
  120.     break;
  121.     case 4:     //zähle Pulse
  122.         fb1.MFB1 = 1;
  123.         timer--;
  124.         if (timer < 0)
  125.         {   timer = 2000 *1000/T_syst;                                // Beim Rausschieben Xms warten
  126.             fb1.typ = 1;
  127.             fb1.count--;
  128.             state = 6;
  129.             if (states) {idsci_printf("FB:           Schiebe raus, Typ:  %i\n",fb1.typ);}
  130.         }
  131.         if (fb1.SIF1 == 1)
  132.         {   timer = 750 *1000/T_syst;
  133.             fb1.typ = 2;
  134.             fb1.count--;
  135.             state = 5;
  136.             if (states) {idsci_printf("FB:           Schiebe raus, Typ:  %i\n",fb1.typ);}
  137.         }
  138.     break;
  139.     case 5:
  140.         fb1.MFB1 = 1;
  141.         timer--;
  142.         if (timer < 0)
  143.         {   timer = 2000 *1000/T_syst;                                // Beim Rausschieben Xms warten
  144.             state = 6;
  145.         }
  146.     break;
  147.     case 6:     //Rausschieben
  148.         fb1.MFB1 = 1;
  149.         timer--;
  150.         if (fb1.SIF1 == 1)
  151.         {   fb1.typ = 0;
  152.             state = 2;
  153.             fb1.hatKiste = 0;
  154.             //if (states) {idsci_printf("FB1:           Kiste am Ende\n");}
  155.         }
  156.         if (timer < 0)
  157.         {   fb1.typ = 0;
  158.             state = 0;
  159.             fb1.hatKiste = 0;
  160.             if (states) {idsci_printf("FB:           Warte auf Kiste\n");}
  161.         }
  162.     }
  163. }
  164.  
  165. /*********************************   DT1   ***************************************************/
  166.  
  167. void f_DT1(void){
  168.     static char state;
  169.     static char typ;
  170.     static char z;
  171.     static int timer;
  172.  
  173.     //init
  174.         if (erster_aufruf)
  175.         {   dt1.typ = 0;
  176.             dt1.bereit = 0;
  177.             state = 0;
  178.             typ = 0;
  179.             z = 0;
  180.             timer = 0;
  181.             if (states) {idsci_printf("DT1:          Drehe zum Band\n");}
  182.         }
  183.  
  184.  
  185.     switch (state){
  186.     case 0:     //Turn left
  187.         if (dt1.STD1B == 1)
  188.         {   dt1.bereit = 1;
  189.             state = 1;
  190.             if (states) {idsci_printf("DT1:          Bin bereit\n");}
  191.         }else{
  192.             dt1.MD1F = 1;
  193.             dt1.MD1W = 0;
  194.             dt1.MFBD1 = 0;
  195.             break;
  196.         }
  197.     case 1:     //Standby
  198.         dt1.MD1F = 0;
  199.         dt1.MD1W = 0;
  200.         dt1.MFBD1 = 0;
  201.         if(fb1.hatKiste){
  202.             state = 99;
  203.             dt1.MFBD1 = 1;
  204.             if (states) {idsci_printf("DT1:          Hole Kiste\n");}
  205.        
  206.         }
  207.     break;
  208.     case 99:    // Hole Kiste bevor Typ bekannt
  209.         if (fb1.typ)
  210.         {   typ = fb1.typ;
  211.             z = typ;
  212.             dt1.bereit = 0;
  213.             state = 2;
  214.             dt1.MFBD1 = 1;
  215.         }
  216.     break;
  217.  
  218.     case 2:     //Hole Kiste
  219.         dt1.MD1F = 0;
  220.         dt1.MD1W = 0;
  221.         dt1.MFBD1 = 1;
  222.         if (dt1.SID1 == 1)
  223.         {   z--;
  224.             state = 3;
  225.         }
  226.     break;
  227.     case 3:     //Unterscheide Typ
  228.         dt1.MD1F = 0;
  229.         dt1.MD1W = 0;
  230.         dt1.MFBD1 = 1;
  231.         if (z == 1) {state = 4;}
  232.         if (z == 0)
  233.         {   state = 5;
  234.             if (states) {idsci_printf("DT1:          Drehe zur Waage\n");}
  235.         }
  236.     break;
  237.     case 4:
  238.         dt1.MD1F = 0;
  239.         dt1.MD1W = 0;
  240.         dt1.MFBD1 = 1;
  241.         if (dt1.SID1 == 0) {state = 2;}
  242.     break;
  243.     case 5:     //Turn rightt.elf
  244.         if (dt1.STD1W == 1)
  245.         {   dt1.typ = typ;
  246.             state = 6;
  247.             if (states) {idsci_printf("DT1:          Warte auf Waage\n");}
  248.         }else{
  249.             dt1.MD1F = 0;
  250.             dt1.MD1W = 1;
  251.             dt1.MFBD1 = 0;
  252.             break;
  253.         }
  254.     case 6:     //Warte auf Waage
  255.         dt1.MD1F = 0;
  256.         dt1.MD1W = 0;
  257.         dt1.MFBD1 = 0;
  258.         if (waage.bereit)
  259.         {   timer = 2000 * 1000/T_syst;                           // Beim Rausschieben 2000ms warten
  260.             state = 7;
  261.             if (states) {idsci_printf("DT1:          Schiebe raus\n");}
  262.         }
  263.     break;
  264.     case 7:     //Rausschieben
  265.         dt1.MD1F = 0;
  266.         dt1.MD1W = 0;
  267.         dt1.MFBD1 = 1;
  268.         timer--;
  269.         if (timer < 0)
  270.         {   dt1.typ = 0;
  271.             typ = 0;
  272.             z = 0;
  273.             timer = 0;
  274.             state = 0;
  275.             if (states) {idsci_printf("DT1:          Drehe zum Band\n");}
  276.         }
  277.     }
  278. }
  279.  
  280. /********************************   Waage   **************************************************/
  281.  
  282. void f_Waage(void){
  283.     static char state;
  284.     static char typ;
  285.     static char z;
  286.     static int timer;
  287.     static int n0;
  288.     int n;
  289.     static int ntief;
  290.  
  291.     //init
  292.     if (erster_aufruf)
  293.     {   waage.bereit = 1;
  294.         waage.typ = 0;
  295.         typ = 0;
  296.         z = 0;
  297.         n0 = 0;
  298.         ntief  = TPU_GetPPWACount(0); //44000;
  299.         //f = 0;
  300.         timer = 0;
  301.         state = 0;
  302.         if (states) {idsci_printf("Waage:        Warte auf DT1\n");}
  303.     }
  304.  
  305.         n = TPU_GetPPWACount(0);
  306.         if ((n>ntief*0.99) && (n<ntief*1.01))
  307.         {
  308.             ntief = (ntief*49 + TPU_GetPPWACount(0))/50;    // Holt letzten gültigen Wert, wird dann alles tiefpassgefiltert
  309.         }
  310.         //f = 255 * 2083750 / ntief;            // f0 = N * fTCR / n ; mit fTCR = Systemfrequenz * 1/8
  311.     //idsci_printf("ntief = %i\n",ntief);
  312.  
  313.  
  314. switch (state) {
  315.     case 0:     //Standby
  316.         waage.MFS1 = 0;
  317.         waage.MFS2 = 0;
  318.         waage.MFS3 = 0;
  319.         waage.MFBW = 0;
  320.         if (dt1.typ > 0) {
  321.             state = 1;
  322.             typ = dt1.typ;
  323.             z = typ;
  324.             if (states) {idsci_printf("Waage:        Hole Kiste\n");}
  325.         }
  326.     break;
  327.  
  328.     case 1:     //Hole Kiste
  329.         waage.MFS1 = 0;
  330.         waage.MFS2 = 0;
  331.         waage.MFS3 = 0;
  332.         waage.MFBW = 1;
  333.         if (waage.SIFW == 1) {
  334.             z--;
  335.             state = 2;
  336.         }
  337.     break;
  338.  
  339.     case 2:
  340.         waage.MFS1 = 0;
  341.         waage.MFS2 = 0;
  342.         waage.MFS3 = 0;
  343.         waage.MFBW = 1;
  344.         if (z == 0) {
  345.             state = 3;
  346.             waage.bereit = 0;
  347.             if (states) {idsci_printf("Waage:        Silos checken\n");}
  348.             }
  349.         if (waage.SIFW == 0) {
  350.             state = 1;
  351.         }
  352.     break;
  353.  
  354.     case 3:     //Silos gefüllt?
  355.         waage.MFS1 = 0;
  356.         waage.MFS2 = 0;
  357.         waage.MFS3 = 0;
  358.         waage.MFBW = 0;
  359.         if (waage.SLS1*waage.SLS2*waage.SLS3 == 1) {
  360.             state = 4;
  361.             timer = 1000 * 1000/T_syst; //Kalibriere 1s lang
  362.             if (states) {idsci_printf("Waage:        Kalibriere\n");}
  363.         }else{
  364.             //idsci_printf("Silos prüfen und nachfüllen!\n");
  365.         }
  366.     break;
  367.  
  368.     case 4:     //Kalibriere
  369.         waage.MFS1 = 0;
  370.         waage.MFS2 = 0;
  371.         waage.MFS3 = 0;
  372.         waage.MFBW = 0;
  373.         if (timer-- < 0) {
  374.             n0 = ntief;
  375.             state = 5;
  376.             if (states) {idsci_printf("Waage:        Einfüllen Zutat 1\n");}
  377.         }
  378.     break;
  379.  
  380.     case 5:     //Fülle Zutat 1
  381.         waage.MFS1 = 1;
  382.         waage.MFS2 = 0;
  383.         waage.MFS3 = 0;
  384.         waage.MFBW = 0;
  385.         if ((n0-ntief)>=rezept[typ-1].menge1 *10) {
  386.             state = 10;
  387.             timer = 1000 * 1000/T_syst; //Warte
  388.             if (states) {idsci_printf("Waage:        Einfüllen Zutat 2\n");}
  389.         }
  390.     break;
  391.  
  392.     case 10:    //Kalibriere
  393.         waage.MFS1 = 0;
  394.         waage.MFS2 = 0;
  395.         waage.MFS3 = 0;
  396.         waage.MFBW = 0;
  397.         if (timer-- < 0) {
  398.             n0 = ntief;
  399.             state = 6;
  400.         }
  401.     break;
  402.  
  403.     case 6:     //Fülle Zutat 2
  404.         waage.MFS1 = 0;
  405.         waage.MFS2 = 1;
  406.         waage.MFS3 = 0;
  407.         waage.MFBW = 0;
  408.  
  409.         if ((n0-ntief)>=rezept[typ-1].menge2 *10) {
  410.             state = 11;
  411.             timer = 1000 * 1000/T_syst; //Warte
  412.             if (states) {idsci_printf("Waage:        Einfüllen Zutat 3\n");}
  413.         }
  414.     break;
  415.    
  416.     case 11:    //Kalibriere
  417.         waage.MFS1 = 0;
  418.         waage.MFS2 = 0;
  419.         waage.MFS3 = 0;
  420.         waage.MFBW = 0;
  421.         if (timer-- < 0) {
  422.             n0 = ntief;
  423.             state = 7;
  424.         }
  425.     break;
  426.    
  427.     case 7:     //Fülle Zutat 3
  428.         waage.MFS1 = 0;
  429.         waage.MFS2 = 0;
  430.         waage.MFS3 = 1;
  431.         waage.MFBW = 0;
  432.  
  433.         if ((n0-ntief)>=rezept[typ-1].menge3 *10) {
  434.             state = 8;
  435.             waage.typ = typ;
  436.             if (states) {idsci_printf("Waage:        Warte auf DT2\n");}
  437.         }
  438.     break;
  439.  
  440.     case 8:     //Warte auf DT2
  441.         waage.MFS1 = 0;
  442.         waage.MFS2 = 0;
  443.         waage.MFS3 = 0;
  444.         waage.MFBW = 0;
  445.  
  446.         if ( dt2.bereit ) {
  447.             state = 9;
  448.             waage.bereit = 1;
  449.             timer = 2000 * 1000/T_syst; //Rausschieben: 2s
  450.             if (states) {idsci_printf("Waage:        Kiste rausschieben\n");}
  451.         }
  452.     break;
  453.  
  454.     case 9:     //Kiste rausschieben
  455.         waage.MFS1 = 0;
  456.         waage.MFS2 = 0;
  457.         waage.MFS3 = 0;
  458.         waage.MFBW = 1;
  459.        
  460.         timer--;
  461.         if( dt1.typ == 0 ) {
  462.             if ( timer < 0 ) {
  463.                 state = 0;
  464.                 waage.typ = 0;
  465.                 if (states) {idsci_printf("Waage:        Warte auf DT1\n");}
  466.             }
  467.         }
  468.         else {
  469.             if( timer < 1500 * 1000/T_syst ) {
  470.                 state = 0;
  471.                 waage.typ = 0;
  472.             }
  473.         }
  474.  
  475.     break;
  476.  
  477.     }
  478. }
  479.  
  480. /***********************************   DT2  **************************************************/
  481.  
  482. void f_DT2(void){
  483.     static char state;
  484.     static char typ;
  485.     static char z;
  486.     static int timer;
  487.  
  488.     //init
  489.         if (erster_aufruf)
  490.         {   dt2.typ = 0;
  491.             dt2.bereit = 0;
  492.             state = 0;
  493.             typ = 0;
  494.             z = 0;
  495.             timer = 0;
  496.             if (states) {idsci_printf("DT2:          Drehe zur Waage\n");}
  497.         }
  498.  
  499.  
  500.     switch (state){
  501.     case 0:     //Turn left
  502.         if (dt2.STD2W == 1)
  503.         {   dt2.bereit = 1;
  504.             state = 1;
  505.             if (states) {idsci_printf("DT2:          Bin bereit\n");}
  506.         }else{
  507.             dt2.MD2W = 1;
  508.             dt2.MD2M = 0;
  509.             dt2.MFBD2 = 0;
  510.             break;
  511.         }
  512.     case 1:     //Standby
  513.         dt2.MD2W = 0;
  514.         dt2.MD2M = 0;
  515.         dt2.MFBD2 = 0;
  516.         if (waage.typ)
  517.         {   typ = waage.typ;
  518.             z = typ;
  519.             state = 2;
  520.             dt2.MFBD2 = 1;
  521.             if (states) {idsci_printf("DT2:          Hole Kiste\n");}
  522.         }
  523.     break;
  524.     case 2:     //Hole Kiste
  525.         dt2.MD2W = 0;
  526.         dt2.MD2M = 0;
  527.         dt2.MFBD2 = 1;
  528.         if (dt2.SID2 == 1)
  529.         {   z--;
  530.             state = 3;
  531.         }
  532.     break;
  533.     case 3:     //Unterscheide Typ
  534.         dt2.MD2W = 0;
  535.         dt2.MD2M = 0;
  536.         dt2.MFBD2 = 1;
  537.         if (z == 1) {state = 4;}
  538.         if (z == 0)
  539.         {   state = 5;
  540.             dt2.bereit = 0;
  541.             if (states) {idsci_printf("DT2:          Drehe zum Mixer\n");}
  542.         }
  543.     break;
  544.     case 4:
  545.         dt2.MD2W = 0;
  546.         dt2.MD2M = 0;
  547.         dt2.MFBD2 = 1;
  548.         if (dt2.SID2 == 0) {state = 2;}
  549.     break;
  550.     case 5:     //Turn right
  551.         if (dt2.STD2M == 1)
  552.         {   dt2.typ = typ;
  553.             state = 6;
  554.             if (states) {idsci_printf("DT2:          Warte auf Mixer\n");}
  555.         }else{
  556.             dt2.MD2W = 0;
  557.             dt2.MD2M = 1;
  558.             dt2.MFBD2 = 0;
  559.             break;
  560.         }
  561.     case 6:     //Warte auf Mixer
  562.         dt2.MD2W = 0;
  563.         dt2.MD2M = 0;
  564.         dt2.MFBD2 = 0;
  565.         if (mixer.bereit == 1)
  566.         {   timer = 2000 * 1000/T_syst;                           // Beim Rausschieben 2000ms warten
  567.             state = 7;
  568.             if (states) {idsci_printf("DT2:          Schiebe raus\n");}
  569.         }
  570.     break;
  571.     case 7:     //Rausschieben
  572.         dt2.MD2W = 0;
  573.         dt2.MD2M = 0;
  574.         dt2.MFBD2 = 1;
  575.         timer--;
  576.         if (timer < 0)
  577.         {   dt2.typ = 0;
  578.             state = 0;
  579.             typ = 0;
  580.             z = 0;
  581.             timer = 0;
  582.             if (states) {idsci_printf("DT2:          Drehe zur Waage\n");}
  583.         }
  584.     }
  585. }
  586.  
  587.  
  588.  
  589. /***********************************   Mixer  **************************************************/
  590.  
  591. void f_Mixer(void){
  592.     // lokale Variablen:
  593.     static char state;              // Zustand der state machine
  594.     static char typ;                // Art der Kiste die gerade im Mixer ist
  595.     static char z;                  // Zähler für den Kistentyp
  596.     static int timer;               // Zählvariable für Timeouts
  597.  
  598.     if (erster_aufruf)
  599.     {   state = 0;
  600.         mixer.bereit = 0;
  601.         mixer.typ = 0;
  602.         if (states) {idsci_printf("Mixer:        Mixer hochfahren\n");}
  603.     }
  604.  
  605.  
  606.  
  607. /* globale Variablen auf die zugegriffen wird:
  608. INPUT:
  609.         SENSOREN:
  610.                 STMG    Sensor Taster Mixer Grundposition
  611.                 STMA    Sensor Taster Mixer Arbeitsposition
  612.                 SIFM    Sensor Induktion F rderband Mixer
  613.         int DT2_typ
  614.         int O_bereit
  615.         int mixzeit()   legt die Zeit fest die gebacken werden soll, Array über Kistentyp
  616. OUTPUT:
  617.         AKTOREN:
  618.                 MFBM    Motor Förderband Mixer
  619.                 MMAN    Motor Mixer AN
  620.                 MMAP    Motor Mixer Arbeitsposition     -> runter
  621.                 MMG  rezept[typ - 1].mixzeit *1000/T_syst;   Motor Mixer Grundposition       -> hoch
  622.         int M_bereit
  623.         int M_typ
  624. */
  625.  
  626. // state machine
  627.     switch (state) {
  628.         // HOCHFAHREN
  629.         case 0:
  630.             mixer.MFBM = 0;
  631.         mixer.MMAN = 0;
  632.         mixer.MMAP = 0;
  633.         mixer.MMG = 1;
  634.                 mixer.typ = 0;
  635.                 if (mixer.STMG == 0) {}
  636.                 else {
  637.                         mixer.bereit = 1;
  638.                         state = 1;
  639.             if (states) {idsci_printf("Mixer:        Warte auf Drehtisch\n");}
  640.         }
  641.                 break;
  642.         // STAND BY
  643.         case 1:
  644.             mixer.MFBM = 0;
  645.         mixer.MMAN = 0;
  646.         mixer.MMAP = 0;
  647.         mixer.MMG = 0;
  648.                 if (dt2.typ == 0) {}
  649.                 else {
  650.                         typ = dt2.typ;
  651.                         z = dt2.typ;
  652.             tempregelung.vorheizen_typ = dt2.typ;
  653.                         state = 2;
  654.             if (states) {idsci_printf("Mixer:        Hole Kiste\n");}
  655.         }
  656.                 break;
  657.         // HOLE KISTE
  658.         case 2:
  659.             mixer.MFBM = 1;
  660.         mixer.MMAN = 0;
  661.         mixer.MMAP = 0;
  662.         mixer.MMG = 0;
  663.                 if (mixer.SIFM == 1) {
  664.                         z--;
  665.                         if (z == 0) {
  666.                                 state = 4;
  667.                 mixer.bereit = 0;
  668.                             if (states) {idsci_printf("Mixer:        Mixer runterfahren\n");}
  669.                 }
  670.                         else {
  671.                                 state = 3; }
  672.                 }
  673.                 else {
  674.             state = 2;
  675.         }
  676.                 break;
  677.         // KISTE WEITERFAHREN
  678.         case 3:
  679.             mixer.MFBM = 1;
  680.         mixer.MMAN = 0;
  681.         mixer.MMAP = 0;
  682.         mixer.MMG = 0;
  683.                 if (mixer.SIFM == 0) {
  684.                         state = 2;
  685.         }
  686.         else {
  687.                         state = 3;
  688.         }
  689.                 break;
  690.         // MIXER RUNTERFAHREN
  691.         case 4:
  692.             mixer.MFBM = 0;
  693.         mixer.MMAN = 0;
  694.         mixer.MMAP = 1;
  695.         mixer.MMG = 0;
  696.                 if (mixer.STMA == 0) {
  697.             state = 4;
  698.         }
  699.                 else {
  700.                         state = 5;
  701.             timer = rezept[typ - 1].mixzeit *1000/T_syst;
  702.             if (states) {idsci_printf("Mixer:        Mixen\n");}
  703.             }
  704.                 break;
  705.         // MIXEN
  706.         case 5:
  707.             mixer.MFBM = 0;
  708.         mixer.MMAN = 1;
  709.         mixer.MMAP = 0;
  710.         mixer.MMG = 0;
  711.         timer--;
  712.         if (timer < 0) {
  713.                 state = 6;
  714.             if (states) {idsci_printf("Mixer:        Mixer hochfahren\n");}
  715.             }
  716.             else {
  717.                 state = 5;
  718.         }
  719.  
  720.                 break;
  721.         // MIXER HOCHFAHREN
  722.         case 6:
  723.             mixer.MFBM = 0;
  724.         mixer.MMAN = 0;
  725.         mixer.MMAP = 0;
  726.         mixer.MMG = 1;
  727.                 if (mixer.STMG == 0) {
  728.                         state = 6;
  729.         }
  730.                 else {
  731.                         mixer.MMG = 0;
  732.                         mixer.typ = typ;
  733.                         state = 7;
  734.             if (states) {
  735.                 idsci_printf("Mixer:        Warte auf Ofen\n");
  736.             }
  737.         }
  738.                 break;
  739.         // WARTE AUF OFEN
  740.         case 7:
  741.             mixer.MFBM = 0;
  742.         mixer.MMAN = 0;
  743.         mixer.MMAP = 0;
  744.         mixer.MMG = 0;
  745.                 if (ofen.bereit == 0) {
  746.                         state = 7;
  747.             }
  748.                 else {
  749.                         state = 8;
  750.                     mixer.bereit = 1;
  751.             timer = 2000 *1000/T_syst;
  752.             if (states) {idsci_printf("Mixer:        Kiste rausfahren\n");}
  753.             }
  754.                 break;
  755.         // KISTE RAUSFAHREN
  756.         case 8:
  757.             mixer.MFBM = 1;
  758.         mixer.MMAN = 0;
  759.         mixer.MMAP = 0;
  760.         mixer.MMG = 0;
  761.             timer--;
  762.             if (timer < 0) {                        // Rücksprung in Stand By
  763.             state = 1;
  764.                     mixer.typ = 0;
  765.             tempregelung.vorheizen_typ = 0;        
  766.             if (states) {idsci_printf("Mixer:        Warte auf Drehtisch\n");}
  767.         }
  768.         else if ((timer < 1500*1000/T_syst) && (dt2.typ > 0)) {     // Rücksprung in Hole Kiste
  769.             state = 1;                     
  770.             mixer.typ = 0;     
  771.         }
  772.         else {
  773.             state = 8;
  774.         }
  775.     break;
  776.     }
  777. }
  778.  
  779. /***********************************   TemperaturRegelung  **************************************************/
  780.  
  781. // Temperature Lookup Table
  782.  
  783.  
  784.  
  785. char getvaloftemp(char temp){
  786.     char TempLUT[][2] = {
  787.         {27, 59},
  788.         {28, 65},
  789.         {29, 69},
  790.         {30, 73},
  791.         {31, 78},
  792.         {32, 81},
  793.         {33, 85},
  794.         {34, 88},
  795.         {35, 91},
  796.         {36, 94},
  797.         {37, 97},
  798.         {38, 99},
  799.         {39, 101},
  800.         {40, 104}
  801.     };
  802.     if(temp < 27){
  803.         return 59;
  804.      }
  805.      else if(temp > 40){
  806.         return 104;
  807.     }else{
  808.         return TempLUT[temp-27][1];
  809.     }
  810. }
  811.  
  812.  
  813. void f_TempO (void) {
  814. // steuert die Temperatur des Ofens
  815. // lokale Variablen:           
  816. static int temp;            // digitalgewandelte Temperatur des Ofens
  817. static char state;      // Zustand der state machine
  818. static int temp_max;
  819. static int temp_min;
  820. static int temp_tolH;
  821. static int temp_tolL;
  822.  
  823.  
  824.  
  825. /* globale Variablen auf die zugegriffen wird:
  826. INPUT:
  827. int backen          startet den Backvorgang
  828. int temp_max        maximale Temperatur bei der gebacken wird
  829. int temp_min        minimale Temperatur bei der gebacken wird
  830. OUTPUT:
  831. int temp_ok         zeigt an ob der Ofen im zulässigen Temperaturbereich zum backen ist
  832. */
  833.  
  834. // state machine
  835.  
  836. if (erster_aufruf){
  837.     state = 0;
  838.     tempregelung.temp_ok = 0;
  839.     tempregelung.vorheizen_typ = 0;
  840.     temp_tolH = 2;
  841.     temp_tolL = 1;
  842.     if (states) {idsci_printf("TempRegelung: Abgeschaltet\n");}
  843. }
  844.  
  845. temp = tempregelung.PT1000/256;//ftemp();
  846. //idsci_printf("%i\n",temp); //AD-wandler wert ausgeben
  847. //if (states) {idsci_printf("temp = %i\n",temp);}
  848. switch (state) {
  849.     // Standby
  850.     case 0:
  851.         tempregelung.Lampe = 0;
  852.         if (tempregelung.vorheizen_typ > 0) {
  853.             state = 1;
  854.             temp_max = getvaloftemp(rezept[tempregelung.vorheizen_typ-1].temp) + temp_tolH;
  855.             temp_min = getvaloftemp(rezept[tempregelung.vorheizen_typ-1].temp) - temp_tolL;
  856.             if (states) {idsci_printf("TempRegelung: Heize vor\n");}
  857.             }
  858.         break;
  859.  
  860.     // Vorheizen (Licht an)
  861.     case 1:
  862.         tempregelung.Lampe = 1;
  863.         if (temp >= temp_max) {
  864.             state = 2;
  865.             if (states) {idsci_printf("TempRegelung: Vorheizen, Ofen heiss\n");}
  866.             }
  867.         break;
  868.  
  869.     // Vorheizen Temp stimmt (Licht aus)
  870.     case 2:
  871.         tempregelung.Lampe = 0;
  872.         if(temp <= temp_max) {
  873.             tempregelung.temp_ok = 1;
  874.             if (ofen.backen > 0) {
  875.                 state = 3;
  876.                 if (states) {idsci_printf("TempRegelung: Backen, Temperatur stimmt\n");}
  877.             }
  878.             if (temp  <= temp_min) {
  879.                 tempregelung.temp_ok = 0;
  880.                 state = 1;
  881.                 if (states) {idsci_printf("TempRegelung: Heize Vor\n");}
  882.                 }
  883.         }
  884.         break;
  885.  
  886.     // Backen Licht aus
  887.     case 3:
  888.         tempregelung.Lampe = 0;
  889.         if (ofen.backen == 0) {
  890.             tempregelung.temp_ok = 0;
  891.             state = 0;
  892.             if (states) {idsci_printf("TempRegelung: Abgeschaltet\n");}
  893.         }
  894.         if (temp  <= temp_min) {
  895.             state = 4;
  896.             if (states) {idsci_printf("TempRegelung: Backen, anheizen\n");}
  897.         }
  898.         break;
  899.  
  900.     // Backen Licht an
  901.     case 4:
  902.         tempregelung.Lampe = 1;
  903.         if (ofen.backen == 0) {
  904.             tempregelung.temp_ok = 0;
  905.             state = 0;
  906.             if (states) {idsci_printf("TempRegelung: Abgeschaltet\n");}
  907.         }
  908.         if (temp >= temp_max) {
  909.             state = 3;
  910.             if (states) {idsci_printf("TempRegelung: Backen, Temperatur stimmt\n");}
  911.         }
  912.         break;
  913.     }
  914. }
  915.  
  916. /***********************************   Ofen  **************************************************/
  917.  
  918.  
  919. void f_Ofen (void) {
  920.     // steuert das Förderband des Ofens
  921.     //-----------------------------------------------------------------------------------------------------
  922.     // lokale Variablen:
  923.     static char state;      // Zustand der state machine
  924.     static char typ;                // Kistentyp im Ofen
  925.     static char z;              // Zähler für den Kistentyp
  926.     static int timer;       // Zähler der die Zeit im Ofen misst
  927.  
  928. /* globale Variablen auf die zugegriffen wird:
  929. INPUT:
  930. int backzeit()      legt die Zeit fest die gebacken werden soll, Array über Kistentyp
  931. int temp_ok         zeigt an ob der Ofen im zulässigen Temperaturbereich zum backen ist
  932. int M_typ           Typ der Kiste die aus dem Mixer kommt
  933.     SENSOR DATEN:
  934.     int SLA         Lichtschranke Ablageplatz
  935.     int SIO         Initiator Ofen
  936. OUTPUT:
  937. int backen          startet den Backvorgang
  938. int O_bereit        signalisiert (dem Mixer) dass der Ofen bereit zum backen ist
  939.     AKTOR DATEN:
  940.     int MFBO        Motor Förderband Ofen
  941. */
  942.  
  943. // state machine
  944. //idsci_printf("ofen.SIO: %i\n", ofen.SIO);
  945.  
  946. if (erster_aufruf){
  947.     state = 0;
  948.     typ = 0;
  949.     z = 0;
  950.     timer = 0;
  951.     ofen.bereit = 0;
  952.     if (states){idsci_printf("Ofen:         Ausgang frei?\n");}
  953. }
  954.  
  955. switch (state) {
  956.     // Ausgang frei?
  957.     case 0:
  958.         ofen.MFBO = 0;
  959.         if (ofen.SLA == 0) {
  960.             state = 1;
  961.             if (states){idsci_printf("Ofen:         Warte auf Mixer\n");}
  962.         }
  963.     break;
  964.     // STAND BY (Warte auf Mixer)
  965.     case 1:
  966.         ofen.MFBO = 0;
  967.         if (ofen.SLA == 1){
  968.             state = 0;
  969.             if (states){idsci_printf("Ofen:         Ausgang blockiert\n");}
  970.         }
  971.         if (mixer.typ > 0) {
  972.             state = 2;
  973.             if (states){idsci_printf("Ofen:         Warte auf richtige Temperatur\n");}
  974.             }
  975.         break;
  976.     // WARTE AUF TempRegelung
  977.     case 2:
  978.         ofen.MFBO = 0;
  979.         if (ofen.SLA == 1){
  980.             state = 0;
  981.             if (states){idsci_printf("Ofen:         Ausgang blockiert\n");}
  982.         }
  983.         if (tempregelung.temp_ok > 0) {
  984.             z = mixer.typ;
  985.             typ = mixer.typ;
  986.             ofen.bereit = 1;
  987.             ofen.backen = 1;
  988.             state = 3;
  989.             if (states){idsci_printf("Ofen:         Temperatur ok, hole Kiste\n");}
  990.         }
  991.     break;
  992.     // HOLE KISTE
  993.     case 3:
  994.         ofen.MFBO = 1;
  995.         if (ofen.SIO == 0) {
  996.             state = 3; }
  997.         else {
  998.             z--;
  999.             state = 4; }
  1000.     break;
  1001.     // FÖRDERBAND AN
  1002.     case 4:
  1003.         ofen.MFBO = 1;
  1004.         if (z <= 0) {
  1005.             state = 5;
  1006.             ofen.bereit = 0;
  1007.             timer = rezept[typ - 1].backzeit *1000/T_syst;
  1008.             if (states){idsci_printf("Ofen:         Backen\n");}
  1009.             }
  1010.         else {
  1011.             if (ofen.SIO == 1) {
  1012.                 state = 4; }
  1013.             else {
  1014.                 state = 3; }
  1015.         }
  1016.     break;
  1017.     // BACKEN
  1018.     case 5:
  1019.         ofen.MFBO = 0;
  1020.         // Timeout:
  1021.         if (timer-- > 0) {
  1022.         state = 5; }
  1023.         else {
  1024.             ofen.backen = 0;
  1025.             state = 6;
  1026.             timer = 3000 *1000/T_syst;
  1027.             if (states){idsci_printf("Ofen:         Kiste rausfahren\n");}
  1028.         }
  1029.     break;
  1030.     // KISTE RAUSFAHREN
  1031.     case 6:
  1032.         ofen.MFBO = 1;
  1033.         if (timer-- < 0) {
  1034.             state = 0;
  1035.             if (states){idsci_printf("Ofen:         Ausgang blockiert\n");}
  1036.             }
  1037.     break;
  1038. }   // end of switch state
  1039. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement