This is the first version of this code, pre threading

Dependencies:   mbed 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::Rotate_Steps(int Steps)
00014 {
00015          #define STIME    1000
00016          
00017          Steps = Steps*50;
00018          //int correctionfactor;
00019          //int timeofturn=correctinfactor*speed;
00020          
00021          int mystep=0;
00022          printf("START!!! step value is=%d\n\r",mystep);
00023          for(int x =0 ; x <= Steps; x++)
00024          {
00025              printf("FOR LOOP! step value is=%d  %d\n\r",mystep,x); // debugging feedback
00026              // phase pulse order
00027                    this->pin1 = 0;
00028                    this->pin2 = 1;
00029                    this->pin3 = 0;
00030                    this->pin4 = 1;
00031                    mystep=1;
00032                    wait_us(1000);
00033                    
00034               printf("FOR LOOP! step value is=%d  %d\n\r",mystep,x);
00035                    this->pin1 = 0;
00036                    this->pin2 = 1;
00037                    this->pin3 = 1;
00038                    this->pin4 = 0; 
00039                    mystep=2;
00040                    wait_us(1000);
00041                    
00042               printf("FOR LOOP! step value is=%d  %d\n\r",mystep,x);
00043                    this->pin1 = 1;
00044                    this->pin2 = 0;
00045                    this->pin3 = 1;
00046                    this->pin4 = 0;
00047                    mystep=3;
00048                    wait_us(1000);
00049                    
00050               printf("FOR LOOP! step value is=%d  %d\n\r",mystep,x);
00051                    this->pin1 = 1;
00052                    this->pin2 = 0;
00053                    this->pin3 = 0;
00054                    this->pin4 = 1;
00055                    mystep=4;
00056                    wait_us(1000);;
00057                    
00058  /*             printf("FOR LOOP! step value is=%d  %d\n\r",mystep,x);
00059                    this->pin1 = 0;
00060                    this->pin2 = 1;
00061                    this->pin3 = 0;
00062                    this->pin4 = 0; 
00063                    mystep=5;
00064                    wait_ms(0.6);
00065               printf("FOR LOOP! step value is=%d  %d\n\r",mystep,x);
00066                    this->pin1 = 1;
00067                    this->pin2 = 1;
00068                    this->pin3 = 0;
00069                    this->pin4 = 0;
00070                    mystep=6;
00071                    wait_ms(0.6);
00072               printf("FOR LOOP! step value is=%d  %d\n\r",mystep,x);
00073                    this->pin1 = 1;
00074                    this->pin2 = 0;
00075                    this->pin3 = 0;
00076                    this->pin4 = 0;
00077                    mystep=7;
00078                    wait_ms(0.6);
00079              
00080                    this->pin1 = 1;
00081                    this->pin2 = 0;
00082                    this->pin3 = 0;
00083                    this->pin4 = 1;
00084                    mystep=0;
00085                    wait_ms(0.6);
00086 
00087                    this->pin1 = 0;
00088                    this->pin2 = 0;
00089                    this->pin3 = 0;
00090                    this->pin4 = 0;
00091                    mystep=0;
00092                    wait_ms(0.6);*/
00093             }
00094 }
00095                    
00096                    /*
00097              switch(mystep){ 
00098                case 0:{
00099                   
00100                 break;  
00101                case 1:{
00102                   
00103                break;  
00104                case 2: 
00105                    
00106                break;  
00107                case 3: 
00108                    
00109                break;  
00110                case 4: 
00111                  
00112                break;  
00113                case 5: 
00114                    
00115                break;  
00116                  case 6: 
00117                  
00118                break;  
00119                case 7: 
00120                   
00121                break;  
00122                default: 
00123                
00124                break;  
00125              } 
00126              
00127              if(_dir){ 
00128                _step++; 
00129              }else{ 
00130                _step--; 
00131              } 
00132              if(_step>7){ 
00133                _step=0; 
00134              } 
00135              if(_step<0){ 
00136                _step=7; 
00137              } 
00138              */
00139              //wait_ms(1);
00140     
00141 
00142 void STEPPER_MOTOR::Permanent_Rotate_clock_wise()
00143 {
00144     switch(_step){ 
00145        case 0:
00146            pin1 = 0;
00147            pin2 = 0;
00148            pin3 = 0;
00149            pin4 = 1;
00150        break;  
00151        case 1:
00152            pin1 = 0;
00153            pin2 = 0;
00154            pin3 = 1;
00155            pin4 = 1; 
00156        break;  
00157        case 2: 
00158            pin1 = 0;
00159            pin2 = 0;
00160            pin3 = 1;
00161            pin4 = 0;
00162        break;  
00163        case 3: 
00164            pin1 = 0;
00165            pin2 = 1;
00166            pin3 = 1;
00167            pin4 = 0;
00168        break;  
00169        case 4: 
00170            pin1 = 0;
00171            pin2 = 1;
00172            pin3 = 0;
00173            pin4 = 0; 
00174        break;  
00175        case 5: 
00176            pin1 = 1;
00177            pin2 = 1;
00178            pin3 = 0;
00179            pin4 = 0;
00180        break;  
00181          case 6: 
00182            pin1 = 1;
00183            pin2 = 0;
00184            pin3 = 0;
00185            pin4 = 0;
00186        break;  
00187        case 7: 
00188            pin1 = 1;
00189            pin2 = 0;
00190            pin3 = 0;
00191            pin4 = 1;
00192        break;  
00193        default: 
00194            pin1 = 0;
00195            pin2 = 0;
00196            pin3 = 0;
00197            pin4 = 0;
00198        break;  
00199      } 
00200      if(_dir){ 
00201        _step++; 
00202      }else{ 
00203        _step--; 
00204      } 
00205      if(_step>7){ 
00206        _step=0; 
00207      } 
00208      if(_step<0){ 
00209        _step=7; 
00210      } 
00211 } 
00212 
00213 void STEPPER_MOTOR::Permanent_Rotate_anti_clock_wise()
00214 {
00215 
00216         //Rotate
00217     switch(_step){ 
00218        case 0:
00219            pin1 = 1;
00220            pin2 = 0;
00221            pin3 = 0;
00222            pin4 = 1;
00223        break;  
00224        case 1:
00225            pin1 = 1;
00226            pin2 = 0;
00227            pin3 = 0;
00228            pin4 = 0; 
00229        break;  
00230        case 2: 
00231            pin1 = 1;
00232            pin2 = 1;
00233            pin3 = 0;
00234            pin4 = 0;
00235        break;  
00236        case 3: 
00237            pin1 = 0;
00238            pin2 = 1;
00239            pin3 = 0;
00240            pin4 = 0;
00241        break;  
00242        case 4: 
00243            pin1 = 0;
00244            pin2 = 1;
00245            pin3 = 1;
00246            pin4 = 0; 
00247        break;  
00248        case 5: 
00249            pin1 = 0;
00250            pin2 = 0;
00251            pin3 = 1;
00252            pin4 = 0;
00253        break;  
00254          case 6: 
00255            pin1 = 0;
00256            pin2 = 0;
00257            pin3 = 1;
00258            pin4 = 1;
00259        break;  
00260        case 7: 
00261            pin1 = 0;
00262            pin2 = 0;
00263            pin3 = 0;
00264            pin4 = 1;
00265        break;  
00266        default: 
00267            pin1 = 0;
00268            pin2 = 0;
00269            pin3 = 0;
00270            pin4 = 0;
00271        break;  
00272      } 
00273      if(_dir){ 
00274        _step++; 
00275      }else{ 
00276        _step--; 
00277      } 
00278      if(_step>7){ 
00279        _step=0; 
00280      } 
00281      if(_step<0){ 
00282        _step=7; 
00283      } 
00284      //wait_ms(1); 
00285 } 
00286