Jeroen Lodder / Mbed 2 deprecated StepperMotor_Bipolar

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001  /**    
00002  * @file    main.cpp
00003  * @brief   Bipolar Steppr Motor Control with dual H Bridge
00004  * @details Software plays stepper motor lookup tables for dual h bridge control.
00005  *                  
00006  *
00007  * @Author  Jeroen Lodder
00008  * @Date    September 2013
00009  * @version 1
00010  *
00011  */
00012 
00013 #include "mbed.h"
00014 
00015 /* IO declarations */
00016     /* LED coil indicators */
00017     BusOut leds(LED1, LED2, LED3, LED4);
00018     /* H Bridge output IO */
00019     BusOut motor(p21, p22, p23, p24, p25, p26, p27, p28);
00020     /* MOSFET io 
00021      *  FET         L1  L2  H1  H2      H3  H4  L3  L4
00022      *  Motor       MA                  MB          
00023      *  mbed        p21 p22 p23 p24     p25 p26 p27 p28
00024      * ------------------------ V+
00025      * |      |       |      |       
00026      * \H1    \H2     \H3    \H4 
00027      * |--MA--|       |--MB--|       
00028      * \L1    \L2     \L3    \L4 
00029      * |      |       |      |       
00030      * ------------------------ V- 
00031      */
00032 
00033 /* Single phase control */
00034 const uint8_t motortable_singlephase[4]     = { 
00035     0x09    ,
00036     0x90    ,
00037     0x06    ,
00038     0x60    
00039 };
00040 
00041 /* Dual phase motor control */
00042 const uint8_t motortable_dualphase[4]       = { 
00043     0x99 ,
00044     0x96 ,
00045     0x66 ,
00046     0x69
00047 };
00048 
00049 /* Half step motor control */
00050 const uint8_t motortable_half_step[8]           =    {  
00051     0x09 ,
00052     0x99 ,
00053     0x90 ,
00054     0x96 ,
00055     0x06 ,
00056     0x66 ,
00057     0x60 ,
00058     0x69
00059 };
00060 
00061 /* LED coil indicators tables */
00062 const uint8_t ledtable_singlephase[4]   =   {   0x4,0x2,0x8,0x1 };
00063 const uint8_t ledtable_dualphase[4]     =   {   0x6,0xA,0x9,0x5 };
00064 const uint8_t ledtable_half_step[8]     = { 0x4,0x6,0x2,0xA,0x8,0x9,0x1,0x5 };
00065 
00066 /* Fet driver definitions */
00067     /* Turn off time fet driver in micro seconds (uS) */
00068     #define TURN_ON_TIME_FET_DRIVER 750
00069 
00070 /* Control definitions */
00071     /* Mutliplier of speed wait time setting */ 
00072     #define SPEED_SCALING   0.05
00073 
00074 int main(void) {
00075 /* Control declarations */
00076     /* Direction LUT are played */
00077     DigitalIn direction( p20 ); 
00078     /* Step pulse input if step mode */
00079     DigitalIn step( p18 );
00080     /* Speed potmeter input */
00081     AnalogIn    speed( p19 );
00082 
00083 /* config variables */
00084     /* Autostep or stepmode with step input */
00085     uint8_t autostep = 1;
00086     /* Halfstep or Dual phase mode */
00087     uint8_t halfstep = 0;
00088  
00089     
00090 /* Stepper motor control loop */    
00091     while(1) {
00092         if( halfstep ){
00093             if(direction){
00094                 for(int i=0; i<8; i++) {
00095                     leds    = ledtable_half_step[i];
00096                     motor =  0;
00097                     wait_us(TURN_ON_TIME_FET_DRIVER);
00098                     motor =  motortable_half_step[i];
00099                     /* Delay */
00100                     if(!autostep){                      
00101                         while(!step);
00102                         wait(0.001);
00103                         while(step);
00104                     }else{
00105                         wait((speed*SPEED_SCALING)+0.001);
00106                     }
00107                 }
00108             }else{
00109                 for(int i=7; i>-1; i--) {
00110                     leds    = ledtable_half_step[i];
00111                     motor =  0;
00112                     wait_us(TURN_ON_TIME_FET_DRIVER);
00113                     motor =  motortable_half_step[i];
00114                     /* Delay */
00115                     if(!autostep){                      
00116                         while(!step);
00117                         wait(0.001);
00118                         while(step);
00119                     }else{
00120                         wait((speed*SPEED_SCALING)+0.001);
00121                     }
00122                 }
00123             }
00124         }else{ /* Half step */
00125             if(direction){
00126                 for(int i=0; i<4; i++) {
00127                     leds    = ledtable_dualphase[i];
00128                     motor =  0;
00129                     wait_us(TURN_ON_TIME_FET_DRIVER);
00130                     motor =  motortable_dualphase[i];
00131                     /* Delay */
00132                     if(!autostep){                      
00133                         while(!step);
00134                         wait(0.001);
00135                         while(step);
00136                     }else{
00137                         wait((speed*SPEED_SCALING)+0.001);
00138                     }
00139                 }
00140             }else{
00141                 for(int i=3; i>-1; i--) {
00142                     leds    = ledtable_dualphase[i];
00143                     motor = 0;
00144                     wait_us(TURN_ON_TIME_FET_DRIVER);
00145                     motor =  motortable_dualphase[i];
00146                     /* Delay */
00147                     if(!autostep){                      
00148                         while(!step);
00149                         wait(0.001);
00150                         while(step);
00151                     }else{
00152                         wait((speed*SPEED_SCALING)+0.001);
00153                     }
00154                 }
00155             }
00156         }
00157     }
00158 }