Jean Mercier / Mbed 2 deprecated jmMotor

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers jmMotor.c Source File

jmMotor.c

00001 /*************************************************************************
00002  * @file    jmMotor.c                 
00003  * @version 1.0
00004  * @date    Feb 12, 2011
00005  */
00006 
00007 #include "jmMotor.h"
00008 #include "LPC17xx.h"
00009 #include "main.h"
00010 #include "jmCommands.h"
00011 #include "jmMessages.h"
00012 #include "jmRingBuffer.h"
00013 #include "stdio.h"
00014 
00015 #ifndef nonStop
00016   #define nonStop 65535  // for continuous mode operation
00017 #endif
00018 
00019 #define mtNotInit 0
00020 #define mtRunningCW 1
00021 #define mtRunningCCW 2
00022 #define mtStopped 3
00023 
00024 extern LPC_GPIO_TypeDef *jmGPIO[5];
00025 
00026 // Module Data Structure
00027 struct StructMotor sMotor[motorQty]; // static creation
00028 
00029 /** Module Data Structure Initialization
00030  * @brief   All State Machines non initialized
00031  * @param[in]    none
00032  * @returns none
00033  */
00034 void MotorInit(void){
00035    int i;
00036    for(i=0;i<motorQty;i++){
00037      sMotor[i].active = mtNotInit;
00038    }
00039 }
00040 
00041 /** Speed control for motors (1 or up to 4)
00042  * @brief motorSpeed Command Line Interface.
00043  * Format: command name (arg info)min..max values 
00044  * motorSpeed (Motor IDs ORed)1..15 (tOn Value)0..255 (tOff Value)0..255
00045  * @param[in] Extracted from command line (Motor IDs ORed)1..15 (tOn Value)0..255 (tOff Value)0..255 
00046  * @returns Message: motorSpeed ids tOn tOff
00047  */
00048 void cli_MotorSpeed(void){
00049    unsigned int id, i, tOn,tOff;
00050    if(ExtractUInteger(pLine,&id,1,15)){          //extract id
00051       if(ExtractUInteger(pLine,&tOn,0,255)){      //extract tOn
00052          if(ExtractUInteger(pLine,&tOff,0,255)){ //extract tOff
00053               for(i=0;i<motorQty;i++){              // check each motor
00054                   if(id & 1<<i){                 // motor included ?     
00055                       if(sMotor[i].active){         // motor initialized ?
00056                            sMotor[i].tOn=(uint8_t)tOn;
00057                            sMotor[i].tOff=(uint8_t)tOff;
00058 
00059                          
00060                          if(tOn==0){             // 0% ?
00061                              sMotor[i].cycles=0;            
00062                             sMotor[i].drivePort->FIOPIN &= ~sMotor[i].driveBitValue;    // output pin low 
00063                           }
00064     
00065                           if(tOff==0){              // 100% ?            
00066                             sMotor[i].drivePort->FIOPIN |= sMotor[i].driveBitValue;    // output pin high
00067                           }
00068     
00069                           if(tOn!=0 && tOff!=0){   // PWM ?
00070                              // change motor tOn and tOff values
00071                              sMotor[i].cycles=nonStop;
00072                           }
00073                           // report to gui
00074                           rGPPMT(i);
00075                       }    
00076                   }             
00077               }
00078           }
00079       }
00080 
00081       if(Feedback)printf("MotorSpeed ID %d tOn %d tOff %d\n",id,tOn, tOff);
00082       return;
00083    }
00084    if(Help)printf("motorSpeed (Motor IDs ORed)1..15 (tOn Value)1..255 (tOff Value)1..255\n");
00085    // Ignore pending command line
00086    NextCommand(nl,pLine);
00087 }
00088 
00089 
00090 /***********************************************************************
00091  * @brief    Module State Machine.  
00092  * Enable different motor control concurrently.
00093  * @param none
00094  * @returns Message at end of cycles: GPPMT id dirPin drivePinB tOn tOff status
00095  */ 
00096 void MotorSM(void){
00097   int i;
00098    for(i=0;i<motorQty;i++){ // for each SM define by pins
00099      // SM active ?
00100      if(!sMotor[i].active) continue;
00101    
00102      if(sMotor[i].cycles!=0){  // Cycles to DO ?
00103        switch(sMotor[i].state){       
00104 
00105          // pulse is low
00106          case  0: if(sMotor[i].eggTimer==0){             // time to change ?     
00107                      sMotor[i].drivePort->FIOPIN |= sMotor[i].driveBitValue;  // set pin High         
00108                      sMotor[i].eggTimer=sMotor[i].tOn;   // load timer with tOn   
00109                      sMotor[i].state=1;                  // next state 
00110                   }
00111                   break;
00112 
00113          // pulse is High
00114          case  1: if(sMotor[i].eggTimer==0){                    // time to change ?
00115                      sMotor[i].drivePort->FIOPIN &= ~sMotor[i].driveBitValue;    // reset pin low      
00116                      sMotor[i].eggTimer=sMotor[i].tOff;  // load timer with tOff  
00117                      if(sMotor[i].cycles != nonStop){    // continuous mode ?
00118                         if(--sMotor[i].cycles == 0)      // decrease qty if not continuous mode
00119                            rGPPMT(i);   // Message: GPPMT id dirPin drivePinB tOn tOff status
00120                      }
00121                      sMotor[i].state=0;                  // state 0
00122                   }
00123                   break;
00124          }//switch
00125        }//if
00126   }//for
00127 }//MotorSM
00128 
00129 
00130 /** @brief motor Command Line Interface
00131  * Command Line Interface to control Motor on mbed pins DIP5 to 30.
00132  * Format: command name (arg info)min..max values 
00133  * Command Line Format: motor (Motor ID)0..3 (Direction Pin)0..432 (Drive Pin)0..432 (Direction)0..1 (tOn)0..255 (tOff)0..255 (Cycles)[1..65535]
00134  * @param[in]    Extracted from command line ((Motor ID)0..3 (Direction Pin)0..432 (Drive Pin)0..432 (Direction)0..1 (tOn)1..255 (tOff)1..255 (Cycles)[1..65535]
00135  * @returns    Message: GPPMT id dirPin drivePinB tOn tOff status
00136  */
00137 void cli_Motor(void){
00138    unsigned int id,dirPin,drivePin, dir, tOn,tOff,cycles;
00139 
00140    if(ExtractUInteger(pLine,&id,0,motorQty-1)){         // extract motor id
00141       if(ExtractUInteger(pLine,&dirPin,0,432)){            // extract direction    pin
00142          if(ExtractUInteger(pLine,&drivePin,0,432)){       // extract drive pin
00143             if(ExtractUInteger(pLine,&dir,0,1)){            // extract direction
00144                 if(ExtractUInteger(pLine,&tOn,0,255)){       // extract tOn
00145                    if(ExtractUInteger(pLine,&tOff,0,255)){   // extract tOff
00146                        // extract cycles and test for nonStop mode
00147                        if(!ExtractUInteger(pLine,&cycles,1,nonStop))cycles = nonStop;
00148     
00149                        sMotor[id].dirBitValue = 1<<(dirPin%100);
00150                        sMotor[id].driveBitValue = 1<<(drivePin%100);;
00151                        sMotor[id].drivePort = jmGPIO[drivePin/100];
00152                        sMotor[id].dirPort = jmGPIO[dirPin/100];
00153                        sMotor[id].active = 1;
00154             
00155                        // Port.Pin set to outputs
00156                        sMotor[id].dirPort->FIODIR |= sMotor[id].dirBitValue;
00157                        sMotor[id].drivePort->FIODIR |= sMotor[id].driveBitValue;
00158         
00159                        sMotor[id].direction = dir;
00160                        sMotor[id].tOn = (uint8_t)tOn;
00161                        sMotor[id].tOff = (uint8_t)tOff;
00162                        sMotor[id].cycles = (uint16_t)cycles;
00163                        sMotor[id].state = 0;
00164                        sMotor[id].eggTimer = 0;
00165                        sMotor[id].dirPin = dirPin;
00166                        sMotor[id].drivePin = drivePin;
00167     
00168                       // set output direction bit according to given direction
00169                       if(dir) sMotor[id].dirPort->FIOPIN |=    sMotor[id].dirBitValue;
00170                       else sMotor[id].dirPort->FIOPIN &=    ~sMotor[id].dirBitValue;
00171     
00172                       // 100%
00173                       if(tOff==0){
00174                          sMotor[id].drivePort->FIOPIN |= sMotor[id].driveBitValue;
00175                          // SM off
00176                          sMotor[id].cycles = 0;
00177                       }
00178     
00179                       // 0%
00180                       if(tOn==0){
00181                          sMotor[id].drivePort->FIOPIN &= ~sMotor[id].driveBitValue;
00182                          // SM off
00183                          sMotor[id].cycles = 0;
00184                       }
00185                       
00186                       rGPPMT(id);   // Message: GPPMT id dirPin drivePinB tOn tOff status
00187         
00188                       if(Feedback)printf("Motor %d DirPin %d DrivePin %d Dir %d tOn %d tOff %d Cycles %d\n",id,
00189                                          dirPin, drivePin, dir,tOn,tOff,cycles); 
00190                        return;
00191                 }
00192              }
00193           }
00194        }
00195        }
00196     // Ignore pending command line
00197        NextCommand(nl,pLine);
00198     return;
00199    }
00200 
00201    if(Help)printf("motor (Motor ID)0..%d (Direction Pin)0..432 (Drive Pin)0..432 (Direction)0..1 (tOn)0..255 (tOff)0..255 (Cycles)[1..65535]\n",motorQty-1);  
00202    // Ignore pending command line
00203    NextCommand(nl,pLine);
00204 }
00205 
00206 /** Module Get Module Process Properties Command Line Interface
00207  * @brief        Command Line Interface to Get Module Public Process Properties
00208  * @param[in]    id Extracted from command line
00209  * @returns    Message: GPPMT id dirPin drivePinB tOn tOff status
00210  */
00211 void cli_GPPMT(void)
00212 {    unsigned int id;
00213    if(ExtractUInteger(pLine,&id,0,motorQty-1)){  // extract id
00214        rGPPMT(id);   // Message: GPPMT id dirPin drivePinB tOn tOff status
00215        return;
00216     }
00217 
00218     if(Help)printf("GPPST (Motor id)0..%d\n",motorQty-1);  
00219     // Ignore pending command line
00220     NextCommand(nl,pLine);
00221 }
00222 
00223 /** Public Properties Message
00224  * @brief    Send Process Properties to update GUI
00225  * @param[in] id Process identification
00226  * @returns   Message: GPPMT id dirPin drivePinB tOn tOff status
00227  */
00228 void rGPPMT(unsigned int id ){
00229     int status;
00230     if( sMotor[id].active )
00231     {   if(sMotor[id].cycles == 0)status = mtStopped;
00232         else 
00233         {
00234          if( sMotor[id].direction) status = mtRunningCW;
00235          else status = mtRunningCCW;
00236         }
00237     }
00238     else  status = mtNotInit;                     
00239     printf("GPPMT %d %d %d %d %d %d\n",id,sMotor[id].dirPin,sMotor[id].drivePin,sMotor[id].tOn,sMotor[id].tOff,status);
00240 }