Jean Mercier / Mbed 2 deprecated jmStepperAxis

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers jmStepper.c Source File

jmStepper.c

00001 /*************************************************************************
00002  * @file    jmStepper.c
00003  * @brief   Command Line Interface Stepper Module
00004  *               
00005  * @version 1.0
00006  * @date    Feb 12, 2011
00007  */
00008 
00009 #include "jmMessages.h"
00010 #include "jmStepper.h"
00011 #include "LPC17xx.h"
00012 #include "main.h"
00013 #include "jmCommands.h"
00014 #include "jmMessages.h"
00015 #include "jmRingBuffer.h"
00016 #include "stdio.h"
00017 
00018 #ifndef nonStop
00019    #define nonStop 65535
00020 #endif
00021 
00022 #define stNotInit 0
00023 #define stRunningCW 1
00024 #define stRunningCCW 2
00025 #define stStopped 3
00026 
00027 // static structures creation
00028 struct StructStepper sStepper[stepperQty];
00029 
00030 /** Module Data Structure Initialization
00031  * @brief   All State Machines put to sleep.
00032  * @param[in]   none
00033  * @returns none
00034  */
00035 void StepperInit(void){
00036    int i;
00037    for(i=0;i<stepperQty;i++){
00038      sStepper[i].active = stNotInit;
00039    }
00040 }
00041 
00042 /**  @brief Step Function
00043  * Change Coil drives according to stepper number and direction.
00044  * For 2 bit hardware controllers (full steps, full torque)
00045  * @param[in]   number     (stepper number)0..3  
00046  * @param[in]   CW         (direction)0..1
00047  * @returns none
00048  */
00049 void Step(int number, uint8_t CW){
00050   uint8_t seq = sStepper[number].seq;
00051    if(CW){
00052       if(seq)sStepper[number].portCoilA->FIOPIN ^=sStepper[number].CoilA_bitValue;
00053       else sStepper[number].portCoilB->FIOPIN ^=sStepper[number].CoilB_bitValue;
00054    }
00055    else {
00056       if(seq)sStepper[number].portCoilB->FIOPIN ^=sStepper[number].CoilB_bitValue;
00057       else sStepper[number].portCoilA->FIOPIN ^=sStepper[number].CoilA_bitValue;
00058    }
00059    sStepper[number].seq = !seq;
00060 }
00061 
00062 
00063 /**  
00064  * @brief stepperStop Command Line Interface.
00065  * Format: command name (arg info)min..max values 
00066  * @param[in]   number   Extracted from command line (stepper ORed ids)1..15  
00067  * @returns none
00068  */
00069 void cli_StepperStop(void){
00070    unsigned int value, i;
00071    if(ExtractUInteger(pLine,&value,1,15)){ 
00072       for(i=0;i<stepperQty;i++){
00073          if(value & 1<<i)sStepper[i].nSteps=0;
00074          rGPPST(i);  // GPPST id pinA pinB delay status
00075       }
00076 
00077       if(Feedback)printf("StepperStop %d\n",value);
00078       return;
00079    }
00080    if(Help)printf("stepperStop (Value)1..15\n");
00081    // Ignore pending command line
00082    NextCommand(nl,pLine);
00083 }
00084 
00085 /** Speed control for step motors (1 or up to 4)
00086  * @brief stepSpeed Command Line Interface.
00087  * Format: command name (arg info)min..max values 
00088  * @param[in] Extracted from command line (stepper ORed ids)1..15  
00089  * @returns Message: stepSpeed ids value
00090  */
00091 void cli_StepSpeed(void){
00092    unsigned int id, i, delay;
00093    if(ExtractUInteger(pLine,&id,1,15)){ 
00094       if(ExtractUInteger(pLine,&delay,1,255)){ 
00095           for(i=0;i<stepperQty;i++){
00096              // change delay for one stepper
00097              if(id & 1<<i)sStepper[i].delay=(uint8_t)delay;
00098              // report to gui
00099              printf("stepSpeed %d %d\n",id,delay);
00100           }
00101       }
00102 
00103       if(Feedback)printf("StepperSpeed ID %d Delay %d\n",id,delay);
00104       return;
00105    }
00106    if(Help)printf("stepSpeed (Stepper IDs ORed)1..15 (Delay Value)1..255\n");
00107    // Ignore pending command line
00108    NextCommand(nl,pLine);
00109 }
00110 
00111 /**  @brief Module State Machine.
00112  * @param[in]   none 
00113  * @returns    On Ending steps, Send Message: GPPST id pinA pinB delay status
00114  */
00115 void StepperSM(void){
00116    int i;
00117 
00118    for(i=0;i<stepperQty;i++){                      // for each stepper
00119       if(sStepper[i].active){                      // SM initialized ?  
00120          if(sStepper[i].nSteps!=0){                // step to do ?
00121             if(sStepper[i].eggTimer==0){           // time to step ?
00122                Step(i,sStepper[i].cw);             // step
00123                sStepper[i].eggTimer=sStepper[i].delay;  // set delay timer for next step
00124                if(sStepper[i].nSteps<nonStop){     // continuous mode ?
00125                   if(--sStepper[i].nSteps == 0)    // decrease step qty if not continuous mode
00126                      rGPPST(i);   // Message GPPST id pinA pinB delay status
00127                }
00128             }     
00129          }
00130       }
00131    }
00132 }
00133 
00134 
00135 /**  @brief stepper Command Line Interface.
00136  * This function controls the stepper 
00137  * Command Line Format: stepper (value)0..3 (direction)0..1 (PinA)0..432 (PinB)0..432 (delay)1..255 (steps)1..65535  
00138  * @param[in]   Extracted from command line (value)0..3 (PinA)0..432 (PinB)0..432 (direction)0..1 (delay)1..255 (steps)1..65535 
00139  * @returns    Message: GPPST id pinA pinB delay status
00140  */
00141 void cli_Stepper(void){
00142 unsigned int id,cw, delay, steps, pinA, pinB, port, bitValue;
00143 
00144    if(ExtractUInteger(pLine,&id,0,stepperQty-1)){       // extract stepper id
00145      if(ExtractUInteger(pLine,&pinA,0,432)){            // extract pinA
00146          if(ExtractUInteger(pLine,&pinB,0,432)){        // extract pinB
00147               if(ExtractUInteger(pLine,&cw,0,1)){               // extract direction
00148                  if(ExtractUInteger(pLine,&delay,1,255)){       // extract step delay
00149                     if(!ExtractUInteger(pLine,&steps,1,65535)){ // check for optionnal steps arg
00150                        steps = nonStop;  // default to nonStop
00151                     }
00152                                   
00153                   // Get ports, bit and bit values
00154                   sStepper[id].pinA=(uint8_t)pinA;
00155                   sStepper[id].pinB=(uint8_t)pinB;
00156                   port = pinA/100;
00157                   bitValue = 1<<(pinA%100);
00158                   sStepper[id].portCoilA = jmGPIO[port];    // Coil A port
00159                   sStepper[id].CoilA_bitValue = bitValue; // Coil A bit
00160     
00161                   port = pinB/100;
00162                   bitValue = 1<<(pinB%100);
00163                   sStepper[id].portCoilB = jmGPIO[port];    // Coil B port
00164                   sStepper[id].CoilB_bitValue = bitValue; // Coil B bit
00165     
00166                   sStepper[id].active = 1;            // initialized true
00167     
00168                   // Port.Pin set to outputs
00169                   sStepper[id].portCoilA->FIODIR |= sStepper[id].CoilA_bitValue;
00170                   sStepper[id].portCoilB->FIODIR |= sStepper[id].CoilB_bitValue;
00171                     
00172                   sStepper[id].cw =cw;           
00173                   sStepper[id].delay = (uint8_t)delay;
00174                   sStepper[id].nSteps = (uint16_t)steps;
00175                     
00176                   // chip report to GUI
00177                   rGPPST(id); //Message: GPPST id pinA pinB delay status
00178 
00179                   if(Feedback)printf("Stepper:%d PinA:%d PinB:%d CW:%d Delay:%d Steps:%d\n",id,pinA,pinB,cw,delay,steps);              
00180                   return;
00181                 }// delay
00182               }// direction
00183             }// pinB
00184         }// pinA
00185     }// stepper number
00186     if(Help)printf("stepper (Stepper id)0..3 (PinA)0..432 (PinB)0..432 (Direction cw/ccw)0..1 (Delay)1..255 [(Steps)1..65535]\n"); 
00187     // Ignore pending command line
00188     NextCommand(nl,pLine);
00189 }
00190 
00191 /** Module Get Module Process Properties Command Line Interface
00192  * @brief       Command Line Interface to Get Module Public Process Properties
00193  * @param[in]   id Extracted from command line id Process identification
00194  * @returns    Message: GPPST id pinA pinB delay status
00195  */
00196 void cli_GPPST(void)
00197 {   unsigned int id;
00198    if(ExtractUInteger(pLine,&id,0,stepperQty-1)){  // extract id Number
00199        rGPPST(id);   // Message: GPPST id pinA pinB delay status
00200        return;
00201     }
00202 
00203     if(Help)printf("GPPST (Stepper id)0..%d\n",stepperQty-1);  
00204     // Ignore pending command line
00205     NextCommand(nl,pLine);
00206 }
00207 
00208 /** Public Properties Message
00209  * @brief   Send Process Properties to update GUI
00210  * @param[in] id Process identification
00211  * @returns   Message: GPPST id pinA pinB ddelay status
00212  */
00213 void rGPPST(unsigned int id ){
00214     int status;
00215     if( sStepper[id].active )
00216     {   if(sStepper[id].nSteps == 0)status = stStopped;
00217         else 
00218         {
00219          if( sStepper[id].cw) status = stRunningCW;
00220          else status = stRunningCCW;
00221         }
00222     }
00223     else  status = stNotInit;                    
00224     printf("GPPST %d %d %d %d %d\n",id,sStepper[id].pinA,sStepper[id].pinB,sStepper[id].delay,status);
00225 }