Jean Mercier
/
jmAll
Command all jmCLIG modules from serial port
jmStepper.c@0:9112e09912db, 2011-02-12 (annotated)
- Committer:
- jm
- Date:
- Sat Feb 12 16:50:25 2011 +0000
- Revision:
- 0:9112e09912db
jmAll Command Line Interface Module
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jm | 0:9112e09912db | 1 | /************************************************************************* |
jm | 0:9112e09912db | 2 | * @file jmStepper.c |
jm | 0:9112e09912db | 3 | * @brief Command Line Interface Stepper Module |
jm | 0:9112e09912db | 4 | * |
jm | 0:9112e09912db | 5 | * @version 1.0 |
jm | 0:9112e09912db | 6 | * @date Feb 12, 2011 |
jm | 0:9112e09912db | 7 | */ |
jm | 0:9112e09912db | 8 | |
jm | 0:9112e09912db | 9 | #include "jmMessages.h" |
jm | 0:9112e09912db | 10 | #include "jmStepper.h" |
jm | 0:9112e09912db | 11 | #include "LPC17xx.h" |
jm | 0:9112e09912db | 12 | #include "main.h" |
jm | 0:9112e09912db | 13 | #include "jmCommands.h" |
jm | 0:9112e09912db | 14 | #include "jmMessages.h" |
jm | 0:9112e09912db | 15 | #include "jmRingBuffer.h" |
jm | 0:9112e09912db | 16 | #include "stdio.h" |
jm | 0:9112e09912db | 17 | |
jm | 0:9112e09912db | 18 | #ifndef nonStop |
jm | 0:9112e09912db | 19 | #define nonStop 65535 |
jm | 0:9112e09912db | 20 | #endif |
jm | 0:9112e09912db | 21 | |
jm | 0:9112e09912db | 22 | #define stNotInit 0 |
jm | 0:9112e09912db | 23 | #define stRunningCW 1 |
jm | 0:9112e09912db | 24 | #define stRunningCCW 2 |
jm | 0:9112e09912db | 25 | #define stStopped 3 |
jm | 0:9112e09912db | 26 | |
jm | 0:9112e09912db | 27 | // static structures creation |
jm | 0:9112e09912db | 28 | struct StructStepper sStepper[stepperQty]; |
jm | 0:9112e09912db | 29 | |
jm | 0:9112e09912db | 30 | /** Module Data Structure Initialization |
jm | 0:9112e09912db | 31 | * @brief All State Machines put to sleep. |
jm | 0:9112e09912db | 32 | * @param[in] none |
jm | 0:9112e09912db | 33 | * @returns none |
jm | 0:9112e09912db | 34 | */ |
jm | 0:9112e09912db | 35 | void StepperInit(void){ |
jm | 0:9112e09912db | 36 | int i; |
jm | 0:9112e09912db | 37 | for(i=0;i<stepperQty;i++){ |
jm | 0:9112e09912db | 38 | sStepper[i].active = stNotInit; |
jm | 0:9112e09912db | 39 | } |
jm | 0:9112e09912db | 40 | } |
jm | 0:9112e09912db | 41 | |
jm | 0:9112e09912db | 42 | /** @brief Step Function |
jm | 0:9112e09912db | 43 | * Change Coil drives according to stepper number and direction. |
jm | 0:9112e09912db | 44 | * For 2 bit hardware controllers (full steps, full torque) |
jm | 0:9112e09912db | 45 | * @param[in] number (stepper number)0..3 |
jm | 0:9112e09912db | 46 | * @param[in] CW (direction)0..1 |
jm | 0:9112e09912db | 47 | * @returns none |
jm | 0:9112e09912db | 48 | */ |
jm | 0:9112e09912db | 49 | void Step(int number, uint8_t CW){ |
jm | 0:9112e09912db | 50 | uint8_t seq = sStepper[number].seq; |
jm | 0:9112e09912db | 51 | if(CW){ |
jm | 0:9112e09912db | 52 | if(seq)sStepper[number].portCoilA->FIOPIN ^=sStepper[number].CoilA_bitValue; |
jm | 0:9112e09912db | 53 | else sStepper[number].portCoilB->FIOPIN ^=sStepper[number].CoilB_bitValue; |
jm | 0:9112e09912db | 54 | } |
jm | 0:9112e09912db | 55 | else { |
jm | 0:9112e09912db | 56 | if(seq)sStepper[number].portCoilB->FIOPIN ^=sStepper[number].CoilB_bitValue; |
jm | 0:9112e09912db | 57 | else sStepper[number].portCoilA->FIOPIN ^=sStepper[number].CoilA_bitValue; |
jm | 0:9112e09912db | 58 | } |
jm | 0:9112e09912db | 59 | sStepper[number].seq = !seq; |
jm | 0:9112e09912db | 60 | } |
jm | 0:9112e09912db | 61 | |
jm | 0:9112e09912db | 62 | |
jm | 0:9112e09912db | 63 | /** |
jm | 0:9112e09912db | 64 | * @brief stepperStop Command Line Interface. |
jm | 0:9112e09912db | 65 | * Format: command name (arg info)min..max values |
jm | 0:9112e09912db | 66 | * @param[in] number Extracted from command line (stepper ORed ids)1..15 |
jm | 0:9112e09912db | 67 | * @returns none |
jm | 0:9112e09912db | 68 | */ |
jm | 0:9112e09912db | 69 | void cli_StepperStop(void){ |
jm | 0:9112e09912db | 70 | unsigned int value, i; |
jm | 0:9112e09912db | 71 | if(ExtractUInteger(pLine,&value,1,15)){ |
jm | 0:9112e09912db | 72 | for(i=0;i<stepperQty;i++){ |
jm | 0:9112e09912db | 73 | if(value & 1<<i)sStepper[i].nSteps=0; |
jm | 0:9112e09912db | 74 | rGPPST(i); // GPPST id pinA pinB delay status |
jm | 0:9112e09912db | 75 | } |
jm | 0:9112e09912db | 76 | |
jm | 0:9112e09912db | 77 | if(Feedback)printf("StepperStop %d\n",value); |
jm | 0:9112e09912db | 78 | return; |
jm | 0:9112e09912db | 79 | } |
jm | 0:9112e09912db | 80 | if(Help)printf("stepperStop (Value)1..15\n"); |
jm | 0:9112e09912db | 81 | // Ignore pending command line |
jm | 0:9112e09912db | 82 | NextCommand(nl,pLine); |
jm | 0:9112e09912db | 83 | } |
jm | 0:9112e09912db | 84 | |
jm | 0:9112e09912db | 85 | /** Speed control for step motors (1 or up to 4) |
jm | 0:9112e09912db | 86 | * @brief stepSpeed Command Line Interface. |
jm | 0:9112e09912db | 87 | * Format: command name (arg info)min..max values |
jm | 0:9112e09912db | 88 | * @param[in] Extracted from command line (stepper ORed ids)1..15 |
jm | 0:9112e09912db | 89 | * @returns Message: stepSpeed ids value |
jm | 0:9112e09912db | 90 | */ |
jm | 0:9112e09912db | 91 | void cli_StepSpeed(void){ |
jm | 0:9112e09912db | 92 | unsigned int id, i, delay; |
jm | 0:9112e09912db | 93 | if(ExtractUInteger(pLine,&id,1,15)){ |
jm | 0:9112e09912db | 94 | if(ExtractUInteger(pLine,&delay,1,255)){ |
jm | 0:9112e09912db | 95 | for(i=0;i<stepperQty;i++){ |
jm | 0:9112e09912db | 96 | // change delay for one stepper |
jm | 0:9112e09912db | 97 | if(id & 1<<i)sStepper[i].delay=(uint8_t)delay; |
jm | 0:9112e09912db | 98 | // report to gui |
jm | 0:9112e09912db | 99 | printf("stepSpeed %d %d\n",id,delay); |
jm | 0:9112e09912db | 100 | } |
jm | 0:9112e09912db | 101 | } |
jm | 0:9112e09912db | 102 | |
jm | 0:9112e09912db | 103 | if(Feedback)printf("StepperSpeed ID %d Delay %d\n",id,delay); |
jm | 0:9112e09912db | 104 | return; |
jm | 0:9112e09912db | 105 | } |
jm | 0:9112e09912db | 106 | if(Help)printf("stepSpeed (Stepper IDs ORed)1..15 (Delay Value)1..255\n"); |
jm | 0:9112e09912db | 107 | // Ignore pending command line |
jm | 0:9112e09912db | 108 | NextCommand(nl,pLine); |
jm | 0:9112e09912db | 109 | } |
jm | 0:9112e09912db | 110 | |
jm | 0:9112e09912db | 111 | /** @brief Module State Machine. |
jm | 0:9112e09912db | 112 | * @param[in] none |
jm | 0:9112e09912db | 113 | * @returns On Ending steps, Send Message: GPPST id pinA pinB delay status |
jm | 0:9112e09912db | 114 | */ |
jm | 0:9112e09912db | 115 | void StepperSM(void){ |
jm | 0:9112e09912db | 116 | int i; |
jm | 0:9112e09912db | 117 | |
jm | 0:9112e09912db | 118 | for(i=0;i<stepperQty;i++){ // for each stepper |
jm | 0:9112e09912db | 119 | if(sStepper[i].active){ // SM initialized ? |
jm | 0:9112e09912db | 120 | if(sStepper[i].nSteps!=0){ // step to do ? |
jm | 0:9112e09912db | 121 | if(sStepper[i].eggTimer==0){ // time to step ? |
jm | 0:9112e09912db | 122 | Step(i,sStepper[i].cw); // step |
jm | 0:9112e09912db | 123 | sStepper[i].eggTimer=sStepper[i].delay; // set delay timer for next step |
jm | 0:9112e09912db | 124 | if(sStepper[i].nSteps<nonStop){ // continuous mode ? |
jm | 0:9112e09912db | 125 | if(--sStepper[i].nSteps == 0) // decrease step qty if not continuous mode |
jm | 0:9112e09912db | 126 | rGPPST(i); // Message GPPST id pinA pinB delay status |
jm | 0:9112e09912db | 127 | } |
jm | 0:9112e09912db | 128 | } |
jm | 0:9112e09912db | 129 | } |
jm | 0:9112e09912db | 130 | } |
jm | 0:9112e09912db | 131 | } |
jm | 0:9112e09912db | 132 | } |
jm | 0:9112e09912db | 133 | |
jm | 0:9112e09912db | 134 | |
jm | 0:9112e09912db | 135 | /** @brief stepper Command Line Interface. |
jm | 0:9112e09912db | 136 | * This function controls the stepper |
jm | 0:9112e09912db | 137 | * Command Line Format: stepper (value)0..3 (direction)0..1 (PinA)0..432 (PinB)0..432 (delay)1..255 (steps)1..65535 |
jm | 0:9112e09912db | 138 | * @param[in] Extracted from command line (value)0..3 (PinA)0..432 (PinB)0..432 (direction)0..1 (delay)1..255 (steps)1..65535 |
jm | 0:9112e09912db | 139 | * @returns Message: GPPST id pinA pinB delay status |
jm | 0:9112e09912db | 140 | */ |
jm | 0:9112e09912db | 141 | void cli_Stepper(void){ |
jm | 0:9112e09912db | 142 | unsigned int id,cw, delay, steps, pinA, pinB, port, bitValue; |
jm | 0:9112e09912db | 143 | |
jm | 0:9112e09912db | 144 | if(ExtractUInteger(pLine,&id,0,stepperQty-1)){ // extract stepper id |
jm | 0:9112e09912db | 145 | if(ExtractUInteger(pLine,&pinA,0,432)){ // extract pinA |
jm | 0:9112e09912db | 146 | if(ExtractUInteger(pLine,&pinB,0,432)){ // extract pinB |
jm | 0:9112e09912db | 147 | if(ExtractUInteger(pLine,&cw,0,1)){ // extract direction |
jm | 0:9112e09912db | 148 | if(ExtractUInteger(pLine,&delay,1,255)){ // extract step delay |
jm | 0:9112e09912db | 149 | if(!ExtractUInteger(pLine,&steps,1,65535)){ // check for optionnal steps arg |
jm | 0:9112e09912db | 150 | steps = nonStop; // default to nonStop |
jm | 0:9112e09912db | 151 | } |
jm | 0:9112e09912db | 152 | |
jm | 0:9112e09912db | 153 | // Get ports, bit and bit values |
jm | 0:9112e09912db | 154 | sStepper[id].pinA=(uint8_t)pinA; |
jm | 0:9112e09912db | 155 | sStepper[id].pinB=(uint8_t)pinB; |
jm | 0:9112e09912db | 156 | port = pinA/100; |
jm | 0:9112e09912db | 157 | bitValue = 1<<(pinA%100); |
jm | 0:9112e09912db | 158 | sStepper[id].portCoilA = jmGPIO[port]; // Coil A port |
jm | 0:9112e09912db | 159 | sStepper[id].CoilA_bitValue = bitValue; // Coil A bit |
jm | 0:9112e09912db | 160 | |
jm | 0:9112e09912db | 161 | port = pinB/100; |
jm | 0:9112e09912db | 162 | bitValue = 1<<(pinB%100); |
jm | 0:9112e09912db | 163 | sStepper[id].portCoilB = jmGPIO[port]; // Coil B port |
jm | 0:9112e09912db | 164 | sStepper[id].CoilB_bitValue = bitValue; // Coil B bit |
jm | 0:9112e09912db | 165 | |
jm | 0:9112e09912db | 166 | sStepper[id].active = 1; // initialized true |
jm | 0:9112e09912db | 167 | |
jm | 0:9112e09912db | 168 | // Port.Pin set to outputs |
jm | 0:9112e09912db | 169 | sStepper[id].portCoilA->FIODIR |= sStepper[id].CoilA_bitValue; |
jm | 0:9112e09912db | 170 | sStepper[id].portCoilB->FIODIR |= sStepper[id].CoilB_bitValue; |
jm | 0:9112e09912db | 171 | |
jm | 0:9112e09912db | 172 | sStepper[id].cw =cw; |
jm | 0:9112e09912db | 173 | sStepper[id].delay = (uint8_t)delay; |
jm | 0:9112e09912db | 174 | sStepper[id].nSteps = (uint16_t)steps; |
jm | 0:9112e09912db | 175 | |
jm | 0:9112e09912db | 176 | // chip report to GUI |
jm | 0:9112e09912db | 177 | rGPPST(id); //Message: GPPST id pinA pinB delay status |
jm | 0:9112e09912db | 178 | |
jm | 0:9112e09912db | 179 | if(Feedback)printf("Stepper:%d PinA:%d PinB:%d CW:%d Delay:%d Steps:%d\n",id,pinA,pinB,cw,delay,steps); |
jm | 0:9112e09912db | 180 | return; |
jm | 0:9112e09912db | 181 | }// delay |
jm | 0:9112e09912db | 182 | }// direction |
jm | 0:9112e09912db | 183 | }// pinB |
jm | 0:9112e09912db | 184 | }// pinA |
jm | 0:9112e09912db | 185 | }// stepper number |
jm | 0:9112e09912db | 186 | 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"); |
jm | 0:9112e09912db | 187 | // Ignore pending command line |
jm | 0:9112e09912db | 188 | NextCommand(nl,pLine); |
jm | 0:9112e09912db | 189 | } |
jm | 0:9112e09912db | 190 | |
jm | 0:9112e09912db | 191 | /** Module Get Module Process Properties Command Line Interface |
jm | 0:9112e09912db | 192 | * @brief Command Line Interface to Get Module Public Process Properties |
jm | 0:9112e09912db | 193 | * @param[in] id Extracted from command line id Process identification |
jm | 0:9112e09912db | 194 | * @returns Message: GPPST id pinA pinB delay status |
jm | 0:9112e09912db | 195 | */ |
jm | 0:9112e09912db | 196 | void cli_GPPST(void) |
jm | 0:9112e09912db | 197 | { unsigned int id; |
jm | 0:9112e09912db | 198 | if(ExtractUInteger(pLine,&id,0,stepperQty-1)){ // extract id Number |
jm | 0:9112e09912db | 199 | rGPPST(id); // Message: GPPST id pinA pinB delay status |
jm | 0:9112e09912db | 200 | return; |
jm | 0:9112e09912db | 201 | } |
jm | 0:9112e09912db | 202 | |
jm | 0:9112e09912db | 203 | if(Help)printf("GPPST (Stepper id)0..%d\n",stepperQty-1); |
jm | 0:9112e09912db | 204 | // Ignore pending command line |
jm | 0:9112e09912db | 205 | NextCommand(nl,pLine); |
jm | 0:9112e09912db | 206 | } |
jm | 0:9112e09912db | 207 | |
jm | 0:9112e09912db | 208 | /** Public Properties Message |
jm | 0:9112e09912db | 209 | * @brief Send Process Properties to update GUI |
jm | 0:9112e09912db | 210 | * @param[in] id Process identification |
jm | 0:9112e09912db | 211 | * @returns Message: GPPST id pinA pinB ddelay status |
jm | 0:9112e09912db | 212 | */ |
jm | 0:9112e09912db | 213 | void rGPPST(unsigned int id ){ |
jm | 0:9112e09912db | 214 | int status; |
jm | 0:9112e09912db | 215 | if( sStepper[id].active ) |
jm | 0:9112e09912db | 216 | { if(sStepper[id].nSteps == 0)status = stStopped; |
jm | 0:9112e09912db | 217 | else |
jm | 0:9112e09912db | 218 | { |
jm | 0:9112e09912db | 219 | if( sStepper[id].cw) status = stRunningCW; |
jm | 0:9112e09912db | 220 | else status = stRunningCCW; |
jm | 0:9112e09912db | 221 | } |
jm | 0:9112e09912db | 222 | } |
jm | 0:9112e09912db | 223 | else status = stNotInit; |
jm | 0:9112e09912db | 224 | printf("GPPST %d %d %d %d %d\n",id,sStepper[id].pinA,sStepper[id].pinB,sStepper[id].delay,status); |
jm | 0:9112e09912db | 225 | } |