PROJ515 / Mbed 2 deprecated CoilingRigV13

Dependencies:   mbed mbed-rtos TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Interface.cpp Source File

Interface.cpp

00001 #include "Interface.hpp"          //Include the header file, this acts like a series of forward declarations 
00002 //Constructor
00003 INTERFACE::INTERFACE(){}//Set all values to 0 
00004 INTERFACE::~INTERFACE(){}   //Destructor
00005 void INTERFACE::Interface_Init()//Set all values to 0 bar the default turns
00006 {
00007     Led_Select_Left_mutex.lock();
00008     Led_Select_Left = 0;
00009     Led_Select_Left_mutex.unlock();
00010         
00011     Led_Select_Right_mutex.lock();
00012     Led_Select_Right= 0;
00013     Led_Select_Right_mutex.unlock();
00014     
00015     _Function_mutex.lock();
00016     _Function = Type_of_Rig;
00017     _Function_mutex.unlock();
00018     
00019     _Twist_Go_mutex.lock();
00020     _Twist_Go =0;
00021     _Twist_Go_mutex.unlock();
00022     
00023     _Anneal_Go_mutex.lock();
00024     _Anneal_Go =0;
00025     _Anneal_Go_mutex.unlock();
00026     
00027     _Test_Go_mutex.lock();
00028     _Test_Go =0;
00029     _Test_Go_mutex.unlock();
00030     
00031     _Select_mutex.lock();
00032     _Select =0;
00033     _Select_mutex.unlock();
00034     
00035     _Turns_Done_mutex.lock();
00036     _Turns_Done =0;
00037     _Turns_Done_mutex.unlock();
00038     
00039     _Loop_mutex.lock();
00040     _Loop =6;
00041     _Loop_mutex.unlock();
00042     
00043     _On_Time_mutex.lock();
00044     _On_Time =6;
00045     _On_Time_mutex.unlock();
00046     
00047     _Off_Time_mutex.lock();
00048     _Off_Time =6;
00049     _Off_Time_mutex.unlock();
00050     
00051     _Duty_Cycle_mutex.lock();
00052     _Duty_Cycle =0;
00053     _Duty_Cycle_mutex.unlock();
00054     
00055     _Power_Time_mutex.lock();
00056     _Power_Time =0;
00057     _Power_Time_mutex.unlock();
00058     
00059      Led_Select_Left_mutex.lock();
00060      Led_Select_Left = 0;
00061      Led_Select_Left_mutex.unlock();
00062      
00063      Led_Select_Right_mutex.lock();
00064      Led_Select_Right= 0;
00065      Led_Select_Right_mutex.unlock();
00066      
00067      Led_Power_mutex.lock();
00068      Led_Power =0;
00069      Led_Power_mutex.unlock();
00070      
00071      _System_Running_mutex.lock();
00072      _System_Running =0;
00073      _System_Running_mutex.unlock();
00074      
00075      
00076      Led_Select_Left_mutex.lock();
00077      Led_Select_Left = 0;
00078      Led_Select_Left_mutex.unlock();
00079         
00080      Led_Select_Right_mutex.lock();
00081      Led_Select_Right= 1;
00082      Led_Select_Right_mutex.unlock();
00083      
00084      _Turns_Todo_mutex.lock();
00085     if(Type_of_Rig == 2)
00086     {
00087         _Turns_Todo = Default_Coiling_Turns;
00088     }
00089     else if(Type_of_Rig == 3)
00090     {
00091         _Turns_Todo = Default_Twisting_Turns;
00092     }
00093     _Turns_Todo_mutex.unlock();
00094 }
00095 void INTERFACE::Up()//Up Routine
00096 {
00097     printf("In up routine\n");
00098     printf("Function value is: %d\n",_Function);
00099     printf("Select Value is :%d\n",_Select);
00100     if (_Function == 2 || _Function == 3 )//Coiling or twist Function
00101     {
00102         _Turns_Todo_mutex.lock();
00103         _Turns_Todo = _Turns_Todo +1;              
00104         _Turns_Todo_mutex.unlock();
00105     }
00106     else if (_Function == 0)//Annealing Function
00107     {
00108         if (_Select == 0)//Select = 0
00109         {   
00110             if(_On_Time > 0)
00111             {
00112                 _On_Time_mutex.lock();
00113                 _On_Time =  _On_Time+ 1;  //Increments On time
00114                 _On_Time_mutex.unlock();
00115             }
00116         }
00117         else if (_Select == 1)//Select = 1
00118         {
00119             if(_Loop > 0)
00120             {
00121                 _Loop_mutex.lock();
00122                 _Loop = _Loop + 1;  //Increments Repetitions in Annealing
00123                 _Loop_mutex.unlock();
00124             }
00125         }
00126         else if (_Select == 2)//Select = 2
00127         {
00128             if(_Off_Time > 0)
00129             {
00130                 _Off_Time_mutex.lock();
00131                 _Off_Time = _Off_Time + 1;  //Increments Off time
00132                 _Off_Time_mutex.unlock();
00133             }
00134         }
00135     }
00136     else if (_Function == 1)//Test Function
00137     {
00138         if (_Select == 1)//Select = 1
00139         {
00140             if (_Duty_Cycle < 100)
00141             {
00142                 _Duty_Cycle_mutex.lock();
00143                 _Duty_Cycle = _Duty_Cycle + 10; //Increases Testing Duty Cycle
00144                 _Duty_Cycle_mutex.unlock();
00145             }
00146         }
00147         else if (_Select == 0)//Select = 0
00148         {
00149             _Power_Time_mutex.lock();
00150             _Power_Time = _Power_Time + 1;    //Increases time on
00151             _Power_Time_mutex.unlock();
00152         }
00153     }
00154 }
00155 void INTERFACE::Down()//Down Routine
00156 {
00157     if (_Function == 2 || _Function == 3)//Coiling or twist Function
00158     {
00159         _Turns_Todo_mutex.lock();
00160         _Turns_Todo = _Turns_Todo -1;  
00161         _Turns_Todo_mutex.unlock();
00162     }
00163     else if (_Function == 0)//Annealing Function
00164     {
00165         if (_Select == 0)//Select = 0
00166         {   
00167             if(_On_Time > 1)
00168             {
00169                 _On_Time_mutex.lock();
00170                 _On_Time =  _On_Time - 1;  //Decrements On time
00171                 _On_Time_mutex.unlock();
00172             }
00173         }
00174         else if (_Select == 1)//Select = 1
00175         {
00176             if(_Loop > 1)
00177             {
00178                 _Loop_mutex.lock();
00179                 _Loop = _Loop - 1;  //Decrements Repetitions in Annealing
00180                 _Loop_mutex.unlock();
00181             }
00182         }
00183         else if (_Select == 2)//Select = 2
00184         {
00185             if(_Off_Time > 1)
00186             {
00187                 _Off_Time_mutex.lock();
00188                 _Off_Time = _Off_Time - 1;  //Decrements Off time
00189                 _Off_Time_mutex.unlock();
00190             }
00191         }
00192     }
00193     else if (_Function == 1)//Test Function
00194     {  
00195         if (_Select == 1)//Select = 1
00196         {
00197             if (_Duty_Cycle > 0)
00198             {
00199                  _Duty_Cycle_mutex.lock();
00200                  _Duty_Cycle = _Duty_Cycle - 10;     //Decreases duty
00201                  _Duty_Cycle_mutex.unlock();
00202             }
00203         }
00204         else if (_Select == 0)//Select = 0
00205         {
00206             if (_Power_Time > 0)
00207             {
00208                 _Power_Time_mutex.lock();
00209                 _Power_Time = _Power_Time - 1;    //Decreases Time on
00210                 _Power_Time_mutex.unlock();
00211             }
00212         }
00213     }
00214 }
00215 void INTERFACE::Start_Stop()//Start / Stop Routine
00216 {
00217     if(_System_Running != 2)
00218     {
00219         _System_Running = !_System_Running;
00220     }
00221     //System not running cases (START)
00222     printf("Function is: %d, System is: %d\n",_Function,_System_Running);
00223     
00224     //System Running cases (Unpause)
00225     if ((_Function == 2 || _Function == 3) & _System_Running == 2)//Coil Twist selected to be unpaused
00226     {
00227         _Twist_Stop_mutex.lock();
00228         _Twist_Stop = false;
00229         _Twist_Stop_mutex.unlock();
00230         
00231         _Twist_Go_mutex.lock();
00232         _Twist_Go = true;//toggle used for coiling and twisting and Running the code
00233         _Twist_Go_mutex.unlock();
00234         
00235         STEPPER_MOTOR_1.Unpause_Code();
00236             
00237         if(_Function == 2)//Coiling
00238         {
00239             printf("Coiling Code unpaused\n");
00240         }
00241         if(_Function == 3)//Twisiting
00242         {
00243             printf("Twisting Code unpaused\n");
00244         }
00245         _System_Running = 0;
00246 
00247     }
00248     else if (_Function == 0 & _System_Running == 2) //Annealing selected to be unpaused
00249     {
00250         _Anneal_Stop_mutex.lock();
00251         _Anneal_Stop = false;
00252         _Anneal_Stop_mutex.unlock();
00253         printf("Annealing Code unpaused\n");
00254         
00255         _Anneal_Go_mutex.lock();
00256         _Anneal_Go = true;//toggle and Run the code
00257         _Anneal_Go_mutex.unlock();
00258         _System_Running = 0;
00259     }
00260     else if (_Function == 1 & _System_Running == 2) //Testing selected to be re run
00261     {
00262         _Test_Stop_mutex.lock();
00263         _Test_Stop = false;
00264         _Test_Stop_mutex.unlock();
00265         printf("Testing Code unpaused\n");
00266         _Test_Go_mutex.lock();
00267         _Test_Go = true;//toggle and run the code
00268         _Test_Go_mutex.unlock();
00269         _System_Running = 0;
00270         
00271     }
00272     //System Running cases (STOP)
00273     else if ((_Function == 2 || _Function == 3) & _System_Running == 1)//Coiling / Twisting selected to be stopped
00274     {
00275         _Twist_Stop_mutex.lock();
00276         _Twist_Stop = true;
00277         _Twist_Stop_mutex.unlock();
00278         
00279         if(_Function == 2)
00280         {
00281             printf("Coiling Code Paused\n");
00282         }
00283         if(_Function == 3)
00284         {
00285             printf("Twisting Code Paused\n");
00286         }
00287         _System_Running_mutex.lock();
00288         _System_Running = 2;
00289         _System_Running_mutex.unlock();
00290         STEPPER_MOTOR_1.Pause_Code();
00291     }
00292     else if (_Function == 0 & _System_Running == 1) //Annealing selected to be stopped
00293     {
00294         _Anneal_Stop_mutex.lock();
00295         _Anneal_Stop = true;
00296         _Anneal_Stop_mutex.unlock();
00297         printf("Annealing Code Paused\n");
00298         _System_Running_mutex.lock();
00299         _System_Running = 2;
00300         _System_Running_mutex.unlock();
00301     }
00302     else if (_Function == 1 & _System_Running == 1) //Testing selected to be stopped
00303     {
00304         _Test_Stop_mutex.lock();
00305         _Test_Stop = true;
00306         _Test_Stop_mutex.unlock();
00307         
00308         printf("Testing Code Paused\n");
00309         
00310         _Tendon_mutex.lock();
00311         Tendon_Power=0;
00312         _Tendon_mutex.unlock();
00313         
00314         _System_Running_mutex.lock();
00315         _System_Running = 2;
00316         _System_Running_mutex.unlock();
00317     }
00318     //Start Cases
00319     else if ((_Function == 2 || _Function == 3) & _System_Running == 0)//Twist selected
00320     {
00321         _Twist_Go_mutex.lock();
00322         _Twist_Go = true;//toggle used for coiling and twisting and Running the code
00323         _Twist_Go_mutex.unlock();
00324     }
00325     else if (_Function == 0 & _System_Running == 0) //Annealing selected 
00326     {
00327         _Anneal_Go_mutex.lock();
00328         _Anneal_Go = true;//toggle and Run the code
00329         _Anneal_Go_mutex.unlock();
00330     }
00331     else if (_Function == 1 & _System_Running == 0) //Testing selected 
00332     {
00333         _Test_Go_mutex.lock();
00334         _Test_Go = true;//toggle and run the code
00335         _Test_Go_mutex.unlock();
00336     }
00337 }
00338 void INTERFACE::Function()//Function Routine used to increment through the three functions
00339 {
00340     /*
00341     Function 0 = Anneal
00342     Function 1 = Test
00343     Function 2 = Coiling
00344     Function 3 = Twisting
00345     */
00346     if(Type_of_Rig == 3)
00347     {
00348         if(_Function < 3)
00349         {
00350             if(_Function == 1)//Skip coiling
00351             {
00352                 _Function_mutex.lock();
00353                 _Function = _Function + 1;
00354                 _Function_mutex.unlock();  
00355             }
00356             _Function_mutex.lock();
00357             _Function = _Function + 1;
00358             _Function_mutex.unlock();   
00359         }
00360         else if(_Function == 3)
00361         {
00362             _Function = 0;   
00363         }
00364     }
00365     if(Type_of_Rig == 2)
00366     {
00367         if(_Function < 2)
00368         {
00369             _Function_mutex.lock();
00370             _Function = _Function + 1;  
00371             _Function_mutex.unlock(); 
00372         }
00373         else if(_Function == 2)
00374         {
00375             _Function = 0;   
00376         }
00377     }
00378 }
00379 void INTERFACE::Select()//Select Routine
00380 {
00381     _Select_mutex.lock();
00382     if(_Function == 0)//Anneal
00383     {
00384         printf("Select Value is :%d\n",_Select);
00385         if(_Select == 2)
00386         {
00387             _Select = 0;
00388         }
00389         else if(_Select == 1)
00390         {
00391             _Select = 2;
00392         }
00393         else if(_Select == 0)
00394         {
00395             _Select = 1;
00396         }
00397         if(_Select == 0 || _Select == 2)
00398         {
00399             Led_Select_Left_mutex.lock();
00400             Led_Select_Left = 0;
00401             Led_Select_Left_mutex.unlock();
00402             
00403             Led_Select_Right_mutex.lock();
00404             Led_Select_Right= 1;
00405             Led_Select_Right_mutex.unlock();    
00406         }
00407         else if(_Select == 1)
00408         {
00409             Led_Select_Left_mutex.lock();
00410             Led_Select_Left = 1;
00411             Led_Select_Left_mutex.unlock();
00412             
00413             Led_Select_Right_mutex.lock();
00414             Led_Select_Right= 0;
00415             Led_Select_Right_mutex.unlock();   
00416         }
00417         printf("Select Value is :%d\n",_Select);
00418     }
00419     else if(_Function == 1)//Test
00420     {
00421         if(_Select == 0)
00422         {
00423             _Select = 1;
00424         }
00425         else if(_Select == 1)
00426         {
00427             _Select = 0;  
00428         }
00429         
00430         if(_Select == 0)
00431         {
00432             Led_Select_Left_mutex.lock();
00433             Led_Select_Left = 0;
00434             Led_Select_Left_mutex.unlock();
00435             
00436             Led_Select_Right_mutex.lock();
00437             Led_Select_Right= 1;
00438             Led_Select_Right_mutex.unlock();
00439         }
00440         else if(_Select ==1)
00441         {
00442             Led_Select_Left_mutex.lock();
00443             Led_Select_Left = 1;
00444             Led_Select_Left_mutex.unlock();
00445             
00446             Led_Select_Right_mutex.lock();
00447             Led_Select_Right= 0;
00448             Led_Select_Right_mutex.unlock();   
00449         }
00450     }
00451     else if(_Function == 2 || _Function == 3)
00452     {
00453         _Select = 0;
00454         Led_Select_Left_mutex.lock();
00455         Led_Select_Left = 0;
00456         Led_Select_Left_mutex.unlock();
00457         
00458         Led_Select_Right_mutex.lock();
00459         Led_Select_Right= 1;
00460         Led_Select_Right_mutex.unlock(); 
00461     }
00462     _Select_mutex.unlock();
00463 }
00464 void INTERFACE::Interface_main()//Main Routine to execute commands when the start button has been pressed
00465 {
00466     if (_Function == 2 || _Function == 3)       //Turning Code
00467     {
00468         if (_Twist_Go == true)
00469         {
00470             Led_Power_mutex.lock();
00471             Led_Power = 1;
00472             Led_Power_mutex.unlock();
00473             
00474             _System_Running_mutex.lock();
00475             _System_Running = 1;
00476             _System_Running_mutex.unlock();
00477             
00478             STEPPER_MOTOR_1.Rotate_Steps(_Turns_Todo,_Function);//Rotates for the specified number of steps given
00479             
00480             _System_Running_mutex.lock();
00481             _System_Running = 0;
00482             _System_Running_mutex.unlock();
00483             
00484             
00485             _Twist_Go_mutex.lock();
00486             _Twist_Go = false;
00487             _Twist_Go_mutex.unlock();
00488             
00489             Led_Power_mutex.lock();
00490             Led_Power = 0;
00491             Led_Power_mutex.unlock();
00492         }
00493     }
00494     else if (_Function == 0)  //Annealing Code
00495     {
00496         if (_Anneal_Go == true)
00497         {
00498             Led_Power_mutex.lock();
00499             Led_Power = 1;
00500             Led_Power_mutex.unlock();
00501             
00502             _System_Running_mutex.lock();
00503             _System_Running = 1;
00504             _System_Running_mutex.unlock();
00505             
00506             for ( int counter = 0; counter < _Loop; counter++)  //Loop value, check if works
00507             {
00508                 if(_System_Running == 1)
00509                 {
00510                     _Tendon_mutex.lock();
00511                     Tendon_Power.period(0.01); // set PWM period to 10 ms don't use pwm have this just to declare
00512                     Tendon_Power=1; // set duty cycle to 100%
00513                     _On_Time_mutex.lock();
00514                     wait(_On_Time);//Variable
00515                     _On_Time_mutex.unlock();
00516                     Tendon_Power=0; // set duty cycle to 0%
00517                     _Off_Time_mutex.lock();
00518                     wait(_Off_Time);//Fixed off time
00519                     _Off_Time_mutex.unlock();
00520                     _Tendon_mutex.unlock();
00521                 }
00522                 while(_System_Running == 2)//Stops the system and just spins in this while loop until system running is not paused
00523                 {
00524                     _Tendon_mutex.lock();
00525                     Tendon_Power =0;
00526                     _Tendon_mutex.unlock();
00527                 }
00528             }
00529             
00530             _System_Running_mutex.lock();
00531             _System_Running = 0;
00532             _System_Running_mutex.unlock();
00533                
00534             _Anneal_Go_mutex.lock();
00535             _Anneal_Go = false;
00536             _Anneal_Go_mutex.unlock();
00537             
00538             Led_Power_mutex.lock();
00539             Led_Power = 0;
00540             Led_Power_mutex.unlock();
00541         }
00542     }
00543     else if (_Function == 1)  //Testing Code //This code is paused / unpaused in start / stop note this code is not paused it is re run.
00544     {
00545         if (_Test_Go == true)
00546         {
00547             Led_Power_mutex.lock();
00548             Led_Power = 1;
00549             Led_Power_mutex.unlock();
00550             
00551             _System_Running_mutex.lock();
00552             _System_Running = 1;
00553             _System_Running_mutex.unlock();
00554             
00555             _Tendon_mutex.lock();
00556             Tendon_Power.period(0.01); // set PWM period to 10 ms don't use pwm have this just to declare
00557             Tendon_Power= _Duty_Cycle / 100; // set duty cycle to variable input from buttons between 0-1 (on lcd this is a percentage) also increment by values of 10
00558             wait(_Power_Time);//Variable on time for power on
00559             Tendon_Power=0; // set duty cycle to 0% and power off
00560             _Tendon_mutex.unlock();
00561             
00562             _System_Running_mutex.lock();
00563             _System_Running = 0;
00564             _System_Running_mutex.unlock();
00565             
00566             _Test_Go_mutex.lock();
00567             _Test_Go = false;
00568             _Test_Go_mutex.unlock();
00569             
00570             Led_Power_mutex.lock();
00571             Led_Power = 0;
00572             Led_Power_mutex.unlock();
00573         }
00574     }
00575 }
00576 //Setter functions
00577 void INTERFACE::Set_System_Running(int System_Running){_System_Running_mutex.lock(); _System_Running = System_Running; _System_Running_mutex.unlock();}
00578 void INTERFACE::Set_Function(int Function){_Function_mutex.lock();_Function = Function;_Function_mutex.unlock();}
00579 void INTERFACE::Set_Twist_Go(bool Twist_Go){_Twist_Go_mutex.lock();_Twist_Go = Twist_Go;_Twist_Go_mutex.unlock();}
00580 void INTERFACE::Set_Anneal_Go(bool Anneal_Go){_Anneal_Go_mutex.lock();_Anneal_Go = Anneal_Go;_Anneal_Go_mutex.unlock();}
00581 void INTERFACE::Set_Test_Go(bool Test_Go){_Test_Go_mutex.lock();_Test_Go = Test_Go;_Test_Go_mutex.unlock();}
00582 void INTERFACE::Set_Twist_Stop(bool Twist_Stop){_Twist_Stop_mutex.lock();_Twist_Stop = Twist_Stop;_Twist_Stop_mutex.unlock();}
00583 void INTERFACE::Set_Anneal_Stop(bool Anneal_Stop){_Anneal_Stop_mutex.lock();_Anneal_Stop = Anneal_Stop;_Anneal_Stop_mutex.unlock();}
00584 void INTERFACE::Set_Test_Stop(bool Test_Stop){_Test_Stop_mutex.lock();_Test_Go = Test_Stop;_Test_Stop_mutex.unlock();}
00585 void INTERFACE::Set_Select(int Select){_Select_mutex.lock();_Select = Select;_Select_mutex.unlock();}
00586 void INTERFACE::Set_Turns_Done(int Turns_Done){_Turns_Done_mutex.lock();_Turns_Done = Turns_Done;_Turns_Done_mutex.unlock();}
00587 void INTERFACE::Set_Turns_To_Do(int Turns_Todo){_Turns_Todo_mutex.lock();_Turns_Todo = Turns_Todo;_Turns_Todo_mutex.unlock();}
00588 void INTERFACE::Set_Loop(int Loop){_Loop_mutex.lock();Loop = Loop;_Loop_mutex.unlock();}
00589 void INTERFACE::Set_On_Time(int On_Time){_On_Time_mutex.lock();_On_Time = On_Time;_On_Time_mutex.unlock();}
00590 void INTERFACE::Set_Off_Time(int Off_Time){_Off_Time_mutex.lock();_Off_Time = Off_Time;_Off_Time_mutex.unlock();}
00591 void INTERFACE::Set_Duty_Cycle(int Duty_Cycle){_Duty_Cycle_mutex.lock();_Duty_Cycle = Duty_Cycle;_Duty_Cycle_mutex.unlock();}
00592 void INTERFACE::Set_Power_Time(float Power_Time){_Power_Time_mutex.lock();_Power_Time = Power_Time;_Power_Time_mutex.unlock();} 
00593 //Getter functions
00594 int INTERFACE::Get_System_Running(){return _System_Running;}
00595 int  INTERFACE::Get_Function(){return _Function;}
00596 bool INTERFACE::Get_Twist_Go(){return _Twist_Go;}
00597 bool INTERFACE::Get_Anneal_Go(){return _Anneal_Go;}
00598 bool INTERFACE::Get_Test_Go(){return _Test_Go;}
00599 bool INTERFACE::Get_Twist_Stop(){return _Twist_Stop;}
00600 bool INTERFACE::Get_Anneal_Stop(){return _Anneal_Stop;}
00601 bool INTERFACE::Get_Test_Stop(){return _Test_Stop;}
00602 int INTERFACE::Get_Select(){return _Select;}
00603 int INTERFACE::Get_Turns_Done(){_Turns_Done = STEPPER_MOTOR_1.Get_Turns(); return _Turns_Done;}
00604 int INTERFACE::Get_Turns_To_Do(){return _Turns_Todo;}
00605 int INTERFACE::Get_Loop(){return _Loop;}
00606 int INTERFACE::Get_On_Time(){return _On_Time;}
00607 int INTERFACE::Get_Off_Time(){return _Off_Time;}
00608 int INTERFACE::Get_Duty_Cycle(){return _Duty_Cycle;}
00609 int INTERFACE::Get_Power_Time(){return _Power_Time;}