Armand Coetzer / Stepper
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Stepper.cpp Source File

Stepper.cpp

00001 #include "Stepper.h"
00002 #include "mbed.h"
00003   
00004 Stepper::Stepper(PinName Coil1, PinName Coil2, PinName Coil3, PinName Coil4) : _Coils(Coil1, Coil2, Coil3, Coil4)
00005 {
00006     _Coils = 0;
00007     stepcount = 0;
00008     Prevtime = 0;
00009     secinter = 0.5;
00010     lastsecinter = 0;
00011     StepDir = 1;
00012     stepping = 0;
00013     stepincr = 0;
00014     stepper_steps[0] = 0x09;
00015     stepper_steps[1] = 0x0A;
00016     stepper_steps[2] = 0x06;
00017     stepper_steps[3] = 0x05;
00018     
00019 }
00020  
00021 void Stepper::Run() 
00022 {   
00023    
00024     run = 1; 
00025     stepping = 0;
00026     
00027     if(secinter != lastsecinter) 
00028     {  
00029         ticker.attach(this, &Stepper::stepper_isr,secinter);
00030         lastsecinter = secinter;
00031     }
00032 }
00033 
00034 void Stepper::Stop() 
00035 {
00036     run = 0;
00037     stepping = 0;
00038 
00039     
00040 }
00041 
00042 void Stepper::Direction(bool dir) 
00043 {   
00044     if(dir == true)
00045     {
00046         StepDir = 1;
00047     }
00048     else if(dir == false)
00049     {
00050         StepDir = -1;  
00051     }
00052 }
00053 
00054 void Stepper::Interval(float sec) 
00055 {
00056     secinter = sec;
00057     if(secinter != lastsecinter) 
00058     {  
00059         ticker.attach(this, &Stepper::stepper_isr,secinter);
00060         lastsecinter = secinter;
00061     }
00062 }
00063 
00064 void Stepper::Step(int steps)
00065 {
00066     Steps = steps;
00067     stepincr = 0;
00068     run = 0;
00069     stepping = 1;
00070     
00071     if(secinter != lastsecinter) 
00072     {  
00073         ticker.attach(this, &Stepper::stepper_isr,secinter);
00074         lastsecinter = secinter;
00075     }
00076     
00077     
00078 }
00079 
00080 void Stepper::stepper_isr()
00081 {
00082     if(run == 1)
00083     { 
00084         _Coils = stepper_steps[stepcount];
00085              
00086         stepcount = stepcount + StepDir;
00087         
00088         if(stepcount > 3)
00089         {
00090             stepcount = 0;    
00091         }
00092         if(stepcount < 0)
00093         {
00094             stepcount = 3;    
00095         }
00096          
00097     }
00098     
00099     if(stepping == 1 && stepincr < Steps)
00100     { 
00101         _Coils = stepper_steps[stepcount];
00102              
00103         stepcount = stepcount + StepDir;
00104         stepincr++;
00105         
00106         if(stepcount > 3)
00107         {
00108             stepcount = 0;    
00109         }
00110         if(stepcount < 0)
00111         {
00112             stepcount = 3;    
00113         }
00114          
00115     }
00116 
00117     
00118     
00119 }
00120 
00121