PROJ515 / Mbed 2 deprecated CoilingRigV13

Dependencies:   mbed mbed-rtos TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers STEPPER_MOTOR.cpp Source File

STEPPER_MOTOR.cpp

00001 #include "mbed.h"           //Include the mbed libraries
00002 #include "STEPPER_MOTOR.hpp"          //Include the header file, this acts like a series of forward declarations
00003 
00004 //Constructor
00005 STEPPER_MOTOR::STEPPER_MOTOR(PinName N1, PinName N2, PinName N3, PinName N4) : pin1(N1),pin2(N2),pin3(N3),pin4(N4)
00006 {
00007      _dir = true;
00008     _step = 0;    
00009 }
00010 
00011 STEPPER_MOTOR::~STEPPER_MOTOR(){}   //Destructor
00012 
00013 void STEPPER_MOTOR::Pause_Code()
00014 {
00015     _Pause_Code = 1;   
00016 }
00017 void STEPPER_MOTOR::Unpause_Code()
00018 {
00019     _Pause_Code = 0;   
00020 }
00021 float STEPPER_MOTOR::Get_Turns()
00022 {
00023     return (_Steps_Done /50);
00024 }
00025 void STEPPER_MOTOR::Rotate_Steps(int Steps,int Function)
00026 {   
00027     if(Function == 2)
00028     {
00029       loop_wait_time = 1;   
00030     }
00031     else if(Function == 3)
00032     {
00033         loop_wait_time = 2;
00034     } 
00035     Steps = Steps*50;
00036     //int correctionfactor;
00037     //int timeofturn=correctinfactor*speed;
00038     _Steps_Done = 0;
00039     //int mystep=0;
00040     //printf("START!!! step value is=%d\n\r",mystep);
00041     for(int x =0 ; x <= Steps; x++)
00042     {
00043         if(_Pause_Code == 0)
00044         {
00045             this->pin1 = 0;
00046             this->pin2 = 1;
00047             this->pin3 = 0;
00048             this->pin4 = 1;
00049             Thread::wait(loop_wait_time);
00050             this->pin1 = 0;
00051             this->pin2 = 1;
00052             this->pin3 = 1;
00053             this->pin4 = 0; 
00054             Thread::wait(loop_wait_time);
00055             this->pin1 = 1;
00056             this->pin2 = 0;
00057             this->pin3 = 1;
00058             this->pin4 = 0;
00059             Thread::wait(loop_wait_time);
00060             this->pin1 = 1;
00061             this->pin2 = 0;
00062             this->pin3 = 0;
00063             this->pin4 = 1;     
00064             Thread::wait(loop_wait_time);  
00065             _Steps_Done = _Steps_Done +1;
00066         }
00067         else if (_Pause_Code == 1)
00068         {
00069             printf("Stepper motor code paused\n");
00070             while(_Pause_Code == 1)
00071             {
00072                 Thread::wait(1);
00073             }
00074         }
00075     }//End of for loop
00076     this->pin1 = 0;
00077     this->pin2 = 0;
00078     this->pin3 = 0;
00079     this->pin4 = 0;
00080 }
00081 void STEPPER_MOTOR::Permanent_Rotate_clock_wise()
00082 {
00083     switch(_step){ 
00084        case 0:
00085            pin1 = 0;
00086            pin2 = 0;
00087            pin3 = 0;
00088            pin4 = 1;
00089        break;  
00090        case 1:
00091            pin1 = 0;
00092            pin2 = 0;
00093            pin3 = 1;
00094            pin4 = 1; 
00095        break;  
00096        case 2: 
00097            pin1 = 0;
00098            pin2 = 0;
00099            pin3 = 1;
00100            pin4 = 0;
00101        break;  
00102        case 3: 
00103            pin1 = 0;
00104            pin2 = 1;
00105            pin3 = 1;
00106            pin4 = 0;
00107        break;  
00108        case 4: 
00109            pin1 = 0;
00110            pin2 = 1;
00111            pin3 = 0;
00112            pin4 = 0; 
00113        break;  
00114        case 5: 
00115            pin1 = 1;
00116            pin2 = 1;
00117            pin3 = 0;
00118            pin4 = 0;
00119        break;  
00120          case 6: 
00121            pin1 = 1;
00122            pin2 = 0;
00123            pin3 = 0;
00124            pin4 = 0;
00125        break;  
00126        case 7: 
00127            pin1 = 1;
00128            pin2 = 0;
00129            pin3 = 0;
00130            pin4 = 1;
00131        break;  
00132        default: 
00133            pin1 = 0;
00134            pin2 = 0;
00135            pin3 = 0;
00136            pin4 = 0;
00137        break;  
00138      } 
00139      if(_dir){ 
00140        _step++; 
00141      }else{ 
00142        _step--; 
00143      } 
00144      if(_step>7){ 
00145        _step=0; 
00146      } 
00147      if(_step<0){ 
00148        _step=7; 
00149      } 
00150 } 
00151 
00152 void STEPPER_MOTOR::Permanent_Rotate_anti_clock_wise()
00153 {
00154 
00155         //Rotate
00156     switch(_step){ 
00157        case 0:
00158            pin1 = 1;
00159            pin2 = 0;
00160            pin3 = 0;
00161            pin4 = 1;
00162        break;  
00163        case 1:
00164            pin1 = 1;
00165            pin2 = 0;
00166            pin3 = 0;
00167            pin4 = 0; 
00168        break;  
00169        case 2: 
00170            pin1 = 1;
00171            pin2 = 1;
00172            pin3 = 0;
00173            pin4 = 0;
00174        break;  
00175        case 3: 
00176            pin1 = 0;
00177            pin2 = 1;
00178            pin3 = 0;
00179            pin4 = 0;
00180        break;  
00181        case 4: 
00182            pin1 = 0;
00183            pin2 = 1;
00184            pin3 = 1;
00185            pin4 = 0; 
00186        break;  
00187        case 5: 
00188            pin1 = 0;
00189            pin2 = 0;
00190            pin3 = 1;
00191            pin4 = 0;
00192        break;  
00193          case 6: 
00194            pin1 = 0;
00195            pin2 = 0;
00196            pin3 = 1;
00197            pin4 = 1;
00198        break;  
00199        case 7: 
00200            pin1 = 0;
00201            pin2 = 0;
00202            pin3 = 0;
00203            pin4 = 1;
00204        break;  
00205        default: 
00206            pin1 = 0;
00207            pin2 = 0;
00208            pin3 = 0;
00209            pin4 = 0;
00210        break;  
00211      } 
00212      if(_dir){ 
00213        _step++; 
00214      }else{ 
00215        _step--; 
00216      } 
00217      if(_step>7){ 
00218        _step=0; 
00219      } 
00220      if(_step<0){ 
00221        _step=7; 
00222      } 
00223      //wait_ms(1); 
00224 } 
00225