Generic Step Motor WebInterface - control a step motor using a Pololu A4983 driver from a webinterface (EXPERIMENTAL PROTOTYPE - just to be used as a proof-of-concept for a IoT talk, will not be updating this code so often)

Dependencies:   EthernetNetIf RPCInterface mbed HTTPServer

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers A4983.c Source File

A4983.c

00001 //---------------------------------------------------------------------------------------------
00002 #include "mbed.h"
00003 #include "A4983.h"
00004 //---------------------------------------------------------------------------------------------
00005 // Pins configuration
00006 //---------------------------------------------------------------------------------------------
00007 //LEDs port definition on mbed
00008 DigitalOut myled1(LED1);
00009 DigitalOut myled2(LED2);
00010 DigitalOut myled3(LED3);
00011 DigitalOut myled4(LED4);
00012 
00013 //ports connected to pololu driver board = straightforward connection of one side of mbed
00014 DigitalOut Dir(p23); //direction of movement
00015 DigitalOut Step(p24); //make one step at rising edge
00016 DigitalOut nSleep(p25); // be HIGH to make it work
00017 DigitalOut nReset(p26); // be HIGH to make it work
00018 DigitalOut MS3(p19); //microstep mode selectors
00019 DigitalOut MS2(p18);
00020 DigitalOut MS1(p17);
00021 DigitalOut nEnable(p27);// be LOW to make it work
00022 
00023 #define HIGH 1
00024 #define LOW 0
00025 #define ON 1
00026 #define OFF 0
00027 #define Clockwise LOW
00028 #define CounterClockwise HIGH
00029 
00030 //which mode of driver to be used. This is now 1/16 stepping.
00031 #define Factor  16
00032 //---------------------------------------------------------------------------------------------
00033 // USB serial port setup - debug messages
00034 //---------------------------------------------------------------------------------------------
00035 Serial pc(USBTX, USBRX); // tx, rx  
00036 //---------------------------------------------------------------------------------------------  
00037 
00038 //---------------------------------------------------------------------------------------------  
00039 // constructor
00040 //---------------------------------------------------------------------------------------------
00041 A4983::A4983()
00042 {
00043   //set communication speed
00044   pc.baud(115200);
00045   //test to see if comm is working
00046   printf("\r\nInitialize MBED Serial Port!\r\n");
00047   
00048   myled1 = 0;
00049   myled2 = 0;
00050   myled3 = 0;
00051   myled4 = 0;
00052 
00053   //-------------------------------------------------------------------------------------------
00054   // adjust microstepping mode
00055   adjust_microstepping_mode(16);
00056 
00057   // initialisation of power stage
00058   Dir = CounterClockwise;
00059   Step = LOW;
00060   nSleep = HIGH;
00061   nEnable = HIGH;
00062   nReset = LOW;
00063   myled1 = 1;
00064   myled2 = 0;
00065   myled3 = 0;
00066   myled4 = 0;
00067 
00068   wait(0.5);
00069 
00070   nReset = HIGH;
00071   myled1 = 0;
00072   myled2 = 1;
00073   myled3 = 0;
00074   myled4 = 0;
00075 
00076   wait(0.5);
00077 
00078   nEnable = LOW;
00079   myled1 = 1;
00080   myled2 = 1;
00081   myled3 = 0;
00082   myled4 = 0;
00083 
00084   wait(0.5);
00085 
00086   Step = HIGH;
00087   myled1 = 0;
00088   myled2 = 0;
00089   myled3 = 1;
00090   myled4 = 0;
00091 
00092   wait(0.5);
00093 
00094   Step = LOW;
00095   myled1 = 1;
00096   myled2 = 0;
00097   myled3 = 1;
00098   myled4 = 0;
00099 
00100   wait(0.5);
00101   //-------------------------------------------------------------------------------------------
00102  
00103   f_motor_enable = 0;    // flag to Enable/Disable Step Motor, 0 => Disable, 1 => Enable
00104   f_motor_direction = 0; // flag for the Step Motor direction, 0 => Normal, 1 => Inverted
00105   k_delay = 0.001;       // delay constant in seconds (Step Motor Speed)
00106 }
00107 //---------------------------------------------------------------------------------------------
00108 void A4983::adjust_microstepping_mode(uint8_t factor)
00109 {
00110   if(factor==1)
00111   {
00112     MS1 = LOW;
00113     MS2 = LOW;
00114     MS3 = LOW;  // 1/1
00115   }
00116   else if(factor==2)
00117   {
00118     MS1 = HIGH;
00119     MS2 = LOW;
00120     MS3 = LOW;  // 1/2
00121   }    
00122   else if(factor==4)
00123   {
00124     MS1 = LOW;
00125     MS2 = HIGH;
00126     MS3 = LOW;  // 1/4
00127   }
00128   else if(factor==8)
00129   { 
00130     MS1 = HIGH;
00131     MS2 = HIGH;
00132     MS3 = LOW;  // 1/8
00133   }
00134   else if(factor==16)
00135   {
00136     MS1 = HIGH;
00137     MS2 = HIGH;
00138     MS3 = HIGH; // 1/16
00139   }
00140 }
00141 //---------------------------------------------------------------------------------------------
00142 // Step function
00143 //---------------------------------------------------------------------------------------------
00144 void A4983::singlestep()
00145 {
00146   if(Dir != f_motor_direction)
00147     Dir = f_motor_direction;
00148        
00149   Step = HIGH;
00150   wait(k_delay/2);
00151 
00152   Step = LOW;
00153   wait(k_delay/2);
00154 }
00155 //---------------------------------------------------------------------------------------------
00156 // Release Motor Command
00157 //---------------------------------------------------------------------------------------------
00158 void A4983::sleep(bool value)
00159 {
00160   if(value == true)
00161   {
00162     //nEnable = LOW;
00163     nSleep = LOW;
00164   }
00165   else
00166   {
00167     //nEnable = HIGH;
00168     nSleep = HIGH; 
00169   }
00170 }
00171 //---------------------------------------------------------------------------------------------
00172 // check if f_motor_enable is true and call singlestep() - loop mode
00173 //---------------------------------------------------------------------------------------------
00174 void A4983::looprun()
00175 {
00176   if(f_motor_enable == 1)  
00177   {    
00178     singlestep();
00179   }  
00180 }
00181 //---------------------------------------------------------------------------------------------
00182 // enable loop mode
00183 //---------------------------------------------------------------------------------------------
00184 void A4983::loopstart()
00185 {
00186   f_motor_enable = 1;
00187 }
00188 //---------------------------------------------------------------------------------------------
00189 // disable loop mode
00190 //---------------------------------------------------------------------------------------------
00191 void A4983::loopstop()
00192 {
00193   f_motor_enable = 0;
00194 }
00195 //---------------------------------------------------------------------------------------------