Jean Mercier / Mbed 2 deprecated jmBridge
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers jmBridge.c Source File

jmBridge.c

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