Alberto Terrazas / Mbed 2 deprecated Lupe

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "mbed.h"
00003 #include "pinout.h"
00004 
00005 #include "CUTTER.h"
00006 #include "EXTRUDER.h"
00007 #include "GRIDDLE.h"
00008 #include "INTERFACE.h"
00009 #include "MACHINE.h"
00010 #include "OVEN.h"
00011 #include "TORTILLA.h"
00012 
00013 Ticker timer;
00014 INTERFACE interface(StartStopPin,StartStopRPin,StartStopGPin,StartStopBPin,TortillaPin,TempReadyPin);
00015 EXTRUDER extruder(ExtruderStartPin,ExtruderPWMPin);
00016 CUTTER cutter(CutterUpPin,CutterDownPin,CutterMotorDirectionPin,CutterMotorEnablePin);
00017 GRIDDLE griddle(GriddlePWMPin,GriddleUpPin,GriddleDownPin,GriddleHomePin);
00018 OVEN oven(OvenPWMPin);
00019 MACHINE Lupe(BandPWMPin,TempPin,CapsuleDoorPin,CartridgePin,CuttedPin);
00020 TORTILLA tortilla;
00021 
00022 #if defined(DEBUG_MODE)
00023     static Serial pc(USBTX, USBRX);
00024     int DebugTimer=0;
00025 #endif
00026 
00027 void MANAGER(void);
00028 void init(void);
00029 void checkUp(void);
00030 void ExtruderProduction(void);
00031 void CutterProduction(void);
00032 void GriddleProduction(void);
00033 void OvenProduction(void);
00034 
00035 int STATUS=-1;
00036 int GENERAL_TIME=-1;
00037 
00038 
00039 int main(){
00040     #if defined(DEBUG_MODE)
00041         pc.printf("\n\n***************************\nMCU variable init OK!\r\n");
00042     #endif
00043     init();
00044     while(1);
00045 }//main
00046 
00047 void MANAGER(void){
00048     GENERAL_TIME++;
00049     /******************************STATUS CONTROL******************************/
00050     if(STATUS==HOME){/****************************************************HOME*/
00051         if(GENERAL_TIME==0){
00052             #if defined(DEBUG_MODE)
00053                 pc.printf("\nHOME rutine start...\r\n");
00054             #endif
00055             interface.setLED(1,1,0);
00056             interface.setFlash(true);
00057             cutter.down();
00058             extruder.Init();
00059             griddle.Home();
00060             oven.Home();
00061         }//if
00062         if(cutter.available()&&griddle.isFree()){//falta temperatura
00063             #if defined(DEBUG_MODE)
00064                 pc.printf("\nHOME rutine end...\r\n");
00065             #endif
00066             GENERAL_TIME=-1;
00067             STATUS=IDLE;
00068             interface.setLED(0,1,0);
00069         }//if
00070     }else if(STATUS==IDLE){/**********************************************IDLE*/
00071         if(IDLE_TIMER==GENERAL_TIME){
00072             GENERAL_TIME=0;
00073             if((Lupe.StartCheck())&&(interface.getFlash()))
00074                 interface.setFlash(false);
00075             if((!Lupe.StartCheck())&&(!interface.getFlash()))
00076                 interface.setFlash(true);
00077             if(interface.getStart()&&(!interface.getFlash())){
00078                 #if defined(DEBUG_MODE)
00079                     pc.printf("\nIDLE Done\r\n");
00080                 #endif
00081                 STATUS=INIT_PRODUCTION;
00082                 GENERAL_TIME=-1;
00083                 interface.setLED(1,1,1);
00084                 interface.setFlash(true);
00085             }//if
00086         }//if
00087     }else if(STATUS==INIT_PRODUCTION){/*************************INIT_PRODUCTION*/
00088         if(GENERAL_TIME==0){//INIT             
00089             #if defined(DEBUG_MODE)
00090                 pc.printf("\nFinding producton start point................r\n");
00091             #endif
00092         }//if
00093         if(extruder.getSwitch()){//No dough detected
00094             if(extruder.getTimer()>=1000){//Check every 1s
00095                 extruder.setTimer(0);
00096                 extruder.GetHome();                     
00097                 #if defined(DEBUG_MODE)
00098                     pc.printf("\nExtruder finding home on:%f\r\n",extruder.getPosition());
00099                 #endif
00100                 if(extruder.getPosition()>=0.1){
00101                     GENERAL_TIME=-1;
00102                     STATUS=HOME;
00103                     interface.setLED(1,1,0);
00104                     interface.setFlash(true);
00105                 }//if
00106             }//if
00107         }else{
00108             extruder.setTimer(EXTRUDER_CYCLE);
00109             cutter.setTimer(-CUTTER_FIRST_TIME);
00110             griddle.setTimer(-GRIDDLE_FIRST_TIME-CUTTER_WAIT_TIMER);
00111             oven.setTimer(-OVEN_FIRST_TIME-GRIDDLE_WAIT_TIMER-CUTTER_WAIT_TIMER);
00112             STATUS=PRODUCTION;
00113             GENERAL_TIME=-1;
00114             tortilla.setDP(interface.getTortilla());                
00115             #if defined(DEBUG_MODE)
00116                 pc.printf("\nProduction of %d Tortillas...\r\n",interface.getTortilla());
00117             #endif
00118         }//else
00119     }else if(STATUS==PRODUCTION){/**********************************PRODUCTION*/
00120         if(GENERAL_TIME==0){//INIT             
00121             #if defined(DEBUG_MODE)
00122                 pc.printf("\nProducton started!\r\n");
00123             #endif
00124         }//if
00125         if(tortilla.getExtruder()){ExtruderProduction();}
00126         if(tortilla.getCutter()){CutterProduction();}
00127         if(tortilla.getGriddle()){GriddleProduction();}
00128         if(tortilla.getOven()){OvenProduction();}
00129         if(!tortilla.getTortilla()){
00130             GENERAL_TIME=-1;
00131             STATUS=HOME;
00132             interface.setLED(1,1,0);
00133             interface.setFlash(true);
00134         }//if
00135     }//else if
00136     checkUp();
00137     return;
00138 }//MANAGER
00139 
00140 void init(void){
00141     #if defined(DEBUG_MODE)
00142         pc.printf("\nMachine modules is starting...\r\n");
00143     #endif
00144     interface.Init();
00145     #if defined(DEBUG_MODE)
00146         pc.printf("INTERFACE OK!\r\n");
00147     #endif
00148     Lupe.Init();
00149     #if defined(DEBUG_MODE)
00150         pc.printf("MACHINE OK!\r\n");
00151     #endif
00152     cutter.Init();
00153     #if defined(DEBUG_MODE)
00154         pc.printf("CUTTER OK!\r\n");
00155     #endif
00156     extruder.Init();
00157     #if defined(DEBUG_MODE)
00158         pc.printf("EXTRUDER OK!\r\n");
00159     #endif
00160     griddle.Init();
00161     #if defined(DEBUG_MODE)
00162         pc.printf("GRIDDLE OK!\r\n");
00163     #endif
00164     oven.Init();
00165     #if defined(DEBUG_MODE)
00166         pc.printf("OVEN OK!\r\n");
00167     #endif
00168     timer.attach(&MANAGER,0.001);
00169     #if defined(DEBUG_MODE)
00170         pc.printf("Time Manager OK!\r\n");
00171     #endif
00172     wait(5);
00173     #if defined(DEBUG_MODE)
00174         pc.printf("\nInit process done...\r\n");
00175     #endif
00176     STATUS=HOME;
00177     GENERAL_TIME=0;
00178     return;
00179 }//init
00180 
00181 void checkUp(void){
00182     #if defined(DEBUG_MODE)
00183         if((GENERAL_TIME%5000==0)&&(STATUS==PRODUCTION))
00184             pc.printf("Time: %ds\r\n",GENERAL_TIME/1000);
00185     #endif
00186     /*************************GENERAL INTERFACE CONTROL************************/
00187     interface.check();  //Flash_control; Update_start_button_latch_each 50ms
00188        
00189     /*************************GENERAL MACHINE CONTROL**************************/
00190     if(Lupe.check()&&(STATUS==PRODUCTION)){   //Error at the machine
00191         interface.setLED(1,0,0);
00192         interface.setFlash(true);
00193         tortilla.setDesiredProduction(tortilla.getExtruderCount());
00194     }//if
00195     
00196     /*************************GENERAL CUTTER CONTROL***************************/
00197     cutter.check();     //Add_production_timer; Check_cutt_status;
00198                         //Manage_cutting_process
00199     
00200     /*************************GENERAL EXTRUDER CONTROL*************************/
00201     extruder.check();   //Add_timer
00202        
00203     /*************************GENERAL GRIDDLE CONTROL**************************/
00204     griddle.check();    //Add_timer; Manage_Griddle_press
00205     
00206     /*************************GENERAL OVEN CONTROL*****************************/
00207     oven.check();
00208     
00209     /***********************GENERAL TORITLLA CONTROL***************************/
00210     #if defined(DEBUG_MODE)
00211         bool latchExtruder=tortilla.getExtruder();
00212         bool latchCutter=tortilla.getCutter();
00213         bool latchGriddle=tortilla.getGriddle();
00214         bool latchOven=tortilla.getOven();
00215         //bool latchProduction=tortilla.getTortilla();
00216         int END=tortilla.check();
00217         if((END==1)&&(latchExtruder!=tortilla.getExtruder()))
00218             pc.printf("\n..........Extruder END..........\r\n");
00219         else if((END==2)&&(latchCutter!=tortilla.getCutter()))
00220             pc.printf("\n..........Cutter END..........\r\n");
00221         else if((END==3)&&(latchGriddle!=tortilla.getGriddle()))
00222             pc.printf("\n..........Griddle END..........\r\n");
00223         else if((END==4)&&(latchOven!=tortilla.getOven()))
00224             pc.printf("\n..........Oven END..........\r\n");
00225         else if(END==5){
00226             pc.printf("\n!!.......FULL PRODUCTION FINALIZATION.......!!\r\n");
00227         }//else if
00228     #else
00229         tortilla.check();
00230     #endif
00231     return;
00232 }//checkUp
00233 
00234 void ExtruderProduction(void){
00235     if((extruder.getTimer()>=EXTRUDER_CYCLE)&&(extruder.getPosition()<0.1)){
00236         int addTime=GENERAL_TIME+CUTTER_WAIT_TIMER;
00237         if(tortilla.getExtruderCount()==0)
00238             addTime=addTime+CUTTER_FIRST_TIME;
00239         tortilla.setCutterTrigger(tortilla.getExtruderCount(),addTime);
00240         extruder.setTimer(0);
00241         extruder.Step();
00242         tortilla.addExtruder();
00243         #if defined(DEBUG_MODE)
00244             pc.printf("Extruded dough number %d in position:%f\r\n",tortilla.getExtruderCount(),extruder.getPosition());
00245         #endif
00246     }//if
00247     if(extruder.getPosition()>=0.1){
00248         //tortilla.setExtruder(false);
00249         tortilla.setDesiredProduction(tortilla.getExtruderCount());
00250         #if defined(DEBUG_MODE)
00251             pc.printf("Extruder reached end position\r\n");
00252         #endif
00253     }//if
00254     return;
00255 }//ExtruderProduction
00256 
00257 void CutterProduction(void){
00258     int cutterMomentum=tortilla.getCutterTrigger(tortilla.getCutterCount());
00259     if((cutterMomentum<=GENERAL_TIME)&&(cutterMomentum!=0)){
00260         int addTime=GENERAL_TIME+GRIDDLE_WAIT_TIMER;
00261         if(tortilla.getCutterCount()==0)
00262             addTime=addTime+GRIDDLE_FIRST_TIME;
00263         tortilla.setGriddleTrigger(tortilla.getCutterCount(),addTime);
00264         cutter.cutt();
00265         tortilla.addCutter();
00266         #if defined(DEBUG_MODE)
00267             pc.printf("Cutted tortilla number:%d\r\n",tortilla.getCutterCount());
00268         #endif
00269     }//if
00270     return;
00271 }//CutterProduction
00272 
00273 void GriddleProduction(void){
00274     int griddleMomentum=tortilla.getGriddleTrigger(tortilla.getGriddleCount());
00275     if((griddleMomentum<=GENERAL_TIME)&&(griddleMomentum!=0)){
00276         int addTime=GENERAL_TIME+OVEN_WAIT_TIMER;
00277         if(tortilla.getGriddleCount()==0)
00278             addTime=addTime+OVEN_FIRST_TIME;
00279         tortilla.setOvenTrigger(tortilla.getGriddleCount(),addTime);
00280         griddle.press();
00281         tortilla.addGriddle();
00282         #if defined(DEBUG_MODE)
00283             pc.printf("Griddled dough process number:%d\r\n",tortilla.getGriddleCount());
00284         #endif
00285     }//if
00286     return;
00287 }//GriddleProduction
00288 
00289 void OvenProduction(void){
00290     int ovenMomentum=tortilla.getOvenTrigger(tortilla.getOvenCount());
00291     if((ovenMomentum<=GENERAL_TIME)&&(ovenMomentum!=0)){
00292         oven.open();
00293         tortilla.addOven();
00294         #if defined(DEBUG_MODE)
00295             pc.printf("Oven passing tortilla number:%d\r\n",tortilla.getOvenCount());
00296         #endif
00297     }//if
00298     return;
00299 }//OvenProduction