Control up to 4 steppers axis with limit switch logic from serial port

Dependencies:   mbed

Committer:
jm
Date:
Sat Feb 12 16:49:03 2011 +0000
Revision:
0:3e676fcd9c71
jmStepperAxis Command Line Interface Module

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jm 0:3e676fcd9c71 1 /*************************************************************************
jm 0:3e676fcd9c71 2 * @file jmStepperAxis.c
jm 0:3e676fcd9c71 3 * @brief Command Line Interface StepperAxis Module
jm 0:3e676fcd9c71 4 *
jm 0:3e676fcd9c71 5 * @version 1.0
jm 0:3e676fcd9c71 6 * @date Feb 12, 2011
jm 0:3e676fcd9c71 7 */
jm 0:3e676fcd9c71 8
jm 0:3e676fcd9c71 9 #include "jmMessages.h"
jm 0:3e676fcd9c71 10 #include "jmStepperAxis.h"
jm 0:3e676fcd9c71 11 #include "LPC17xx.h"
jm 0:3e676fcd9c71 12 #include "main.h"
jm 0:3e676fcd9c71 13 #include "jmCommands.h"
jm 0:3e676fcd9c71 14 #include "jmMessages.h"
jm 0:3e676fcd9c71 15 #include "jmRingBuffer.h"
jm 0:3e676fcd9c71 16 #include "stdio.h"
jm 0:3e676fcd9c71 17 #include "jmSwitch.h"
jm 0:3e676fcd9c71 18 #include "jmStepper.h"
jm 0:3e676fcd9c71 19
jm 0:3e676fcd9c71 20 #ifndef nonStop
jm 0:3e676fcd9c71 21 #define nonStop 65535
jm 0:3e676fcd9c71 22 #endif
jm 0:3e676fcd9c71 23
jm 0:3e676fcd9c71 24 #define NotInit 0
jm 0:3e676fcd9c71 25 #define Initialized 1
jm 0:3e676fcd9c71 26 #define CW 1
jm 0:3e676fcd9c71 27 #define CCW 0
jm 0:3e676fcd9c71 28 #define Open 1
jm 0:3e676fcd9c71 29 #define Close 0
jm 0:3e676fcd9c71 30 #define bothLimitSwitchActivated 4
jm 0:3e676fcd9c71 31 #define LimitCWReached 3
jm 0:3e676fcd9c71 32 #define LimitCCWReached 2
jm 0:3e676fcd9c71 33 #define LogicOK 1
jm 0:3e676fcd9c71 34 #define true 1
jm 0:3e676fcd9c71 35 #define false 0
jm 0:3e676fcd9c71 36
jm 0:3e676fcd9c71 37 // static structures creation
jm 0:3e676fcd9c71 38 struct StructStepperAxis sStepperAxis[stepperAxisQty];
jm 0:3e676fcd9c71 39
jm 0:3e676fcd9c71 40 /** Module Data Structure Initialization
jm 0:3e676fcd9c71 41 * @brief All State Machines put to sleep.
jm 0:3e676fcd9c71 42 * @param[in] none
jm 0:3e676fcd9c71 43 * @returns none
jm 0:3e676fcd9c71 44 */
jm 0:3e676fcd9c71 45 void StepperAxisInit(void){
jm 0:3e676fcd9c71 46 int i;
jm 0:3e676fcd9c71 47 for(i=0;i<stepperAxisQty;i++){
jm 0:3e676fcd9c71 48 sStepperAxis[i].active = false;
jm 0:3e676fcd9c71 49 }
jm 0:3e676fcd9c71 50 }
jm 0:3e676fcd9c71 51
jm 0:3e676fcd9c71 52 /** @brief stepperAxis Command Line Interface.
jm 0:3e676fcd9c71 53 * Set up a stepper axis from one stepper and two limit switch modules
jm 0:3e676fcd9c71 54 * Command Line Format: stepperAxis id stepperID limitCWid limitCCWid
jm 0:3e676fcd9c71 55 * @param[in] Extracted from command line id stepperID limitCWid limitCCWid
jm 0:3e676fcd9c71 56 * @returns Message: GPPSTA id stepperID limitCWid limitCCWid status
jm 0:3e676fcd9c71 57 */
jm 0:3e676fcd9c71 58 void cli_StepperAxis(void){
jm 0:3e676fcd9c71 59 unsigned int id, stepperID, limitCWid, limitCCWid;
jm 0:3e676fcd9c71 60
jm 0:3e676fcd9c71 61 if(ExtractUInteger(pLine,&id,0,stepperAxisQty-1)){ // extract stepper axis id
jm 0:3e676fcd9c71 62 if(ExtractUInteger(pLine,&stepperID,0,stepperQty-1)){ // extract stepper id
jm 0:3e676fcd9c71 63 if(ExtractUInteger(pLine,&limitCWid,0,switchQty-1)){ // extract Limit Switch CW id
jm 0:3e676fcd9c71 64 if(ExtractUInteger(pLine,&limitCCWid,0,switchQty-1)){ // extract Limit Switch CCW id
jm 0:3e676fcd9c71 65
jm 0:3e676fcd9c71 66 sStepperAxis[id].active=true;
jm 0:3e676fcd9c71 67 sStepperAxis[id].stepperID=stepperID;
jm 0:3e676fcd9c71 68 sStepperAxis[id].limitCW=limitCWid;
jm 0:3e676fcd9c71 69 sStepperAxis[id].limitCCW=limitCCWid;
jm 0:3e676fcd9c71 70
jm 0:3e676fcd9c71 71 // chip report to GUI
jm 0:3e676fcd9c71 72 rGPPSTA(id);
jm 0:3e676fcd9c71 73 if(Feedback)printf("StepperAxis %d StepperID %d limitCCWid %d limitCWid %d\n",id,stepperID,limitCCWid,limitCWid);
jm 0:3e676fcd9c71 74 return;
jm 0:3e676fcd9c71 75 }// CCW
jm 0:3e676fcd9c71 76 }// CW
jm 0:3e676fcd9c71 77 }// stepper
jm 0:3e676fcd9c71 78 }// axis
jm 0:3e676fcd9c71 79 if(Help)printf("stepperAxis (Stepper id)0..3 (limit switch CCW id)0..7 (limit switch CW id)0..7\n");
jm 0:3e676fcd9c71 80 // Ignore pending command line
jm 0:3e676fcd9c71 81 NextCommand(nl,pLine);
jm 0:3e676fcd9c71 82 }
jm 0:3e676fcd9c71 83
jm 0:3e676fcd9c71 84 /** @brief
jm 0:3e676fcd9c71 85 * @param[in] none
jm 0:3e676fcd9c71 86 * @returns
jm 0:3e676fcd9c71 87 */
jm 0:3e676fcd9c71 88 void StepperAxisSM(void)
jm 0:3e676fcd9c71 89 {
jm 0:3e676fcd9c71 90 int i;
jm 0:3e676fcd9c71 91 for(i=0;i<stepperAxisQty;i++)
jm 0:3e676fcd9c71 92 {
jm 0:3e676fcd9c71 93 if(sStepperAxis[i].active==true)
jm 0:3e676fcd9c71 94 {
jm 0:3e676fcd9c71 95 uint8_t oldStatus = sStepperAxis[i].status;
jm 0:3e676fcd9c71 96
jm 0:3e676fcd9c71 97 // Limits Reached
jm 0:3e676fcd9c71 98 // two limits switches activated ?
jm 0:3e676fcd9c71 99 if(sSwitch[sStepperAxis[i].limitCW].state == Close && sSwitch[sStepperAxis[i].limitCCW].state==Close){
jm 0:3e676fcd9c71 100 sStepper[sStepperAxis[i].stepperID].nSteps=0; // stop stepper
jm 0:3e676fcd9c71 101 sStepperAxis[i].status = bothLimitSwitchActivated;
jm 0:3e676fcd9c71 102 if(oldStatus != bothLimitSwitchActivated)rGPPSTA(i); // report change
jm 0:3e676fcd9c71 103 continue;
jm 0:3e676fcd9c71 104 }
jm 0:3e676fcd9c71 105 // Limit Switch CW activated and step direction CW
jm 0:3e676fcd9c71 106 if(sSwitch[sStepperAxis[i].limitCW].state == Close && sStepper[sStepperAxis[i].stepperID].cw ==CW){
jm 0:3e676fcd9c71 107 sStepper[sStepperAxis[i].stepperID].nSteps=0; // stop stepper
jm 0:3e676fcd9c71 108 sStepperAxis[i].status = LimitCWReached;
jm 0:3e676fcd9c71 109 if(oldStatus != LimitCWReached)rGPPSTA(i); // report change
jm 0:3e676fcd9c71 110 continue;
jm 0:3e676fcd9c71 111 }
jm 0:3e676fcd9c71 112
jm 0:3e676fcd9c71 113 // Limit Switch CCW activated and step direction CCW
jm 0:3e676fcd9c71 114 if(sSwitch[sStepperAxis[i].limitCCW].state == Close && sStepper[sStepperAxis[i].stepperID].cw ==CCW){
jm 0:3e676fcd9c71 115 sStepper[sStepperAxis[i].stepperID].nSteps=0; // stop stepper
jm 0:3e676fcd9c71 116 sStepperAxis[i].status = LimitCCWReached;
jm 0:3e676fcd9c71 117 if(oldStatus != LimitCCWReached)rGPPSTA(i); // report change
jm 0:3e676fcd9c71 118 continue;
jm 0:3e676fcd9c71 119 }
jm 0:3e676fcd9c71 120
jm 0:3e676fcd9c71 121 // safe operating area
jm 0:3e676fcd9c71 122 // no limit switch activated
jm 0:3e676fcd9c71 123 if(sSwitch[sStepperAxis[i].limitCW].state == Open && sSwitch[sStepperAxis[i].limitCCW].state == Open){
jm 0:3e676fcd9c71 124 sStepperAxis[i].status = LogicOK;
jm 0:3e676fcd9c71 125 if(oldStatus != LogicOK)rGPPSTA(i); // report change
jm 0:3e676fcd9c71 126 continue;
jm 0:3e676fcd9c71 127 }
jm 0:3e676fcd9c71 128
jm 0:3e676fcd9c71 129 // Limit Switch CW activated and step direction CCW
jm 0:3e676fcd9c71 130 if(sSwitch[sStepperAxis[i].limitCW].state == Close && sStepper[sStepperAxis[i].stepperID].cw ==CCW){
jm 0:3e676fcd9c71 131 sStepperAxis[i].status = LogicOK;
jm 0:3e676fcd9c71 132 if(oldStatus != LogicOK)rGPPSTA(i); // report change
jm 0:3e676fcd9c71 133 continue;
jm 0:3e676fcd9c71 134 }
jm 0:3e676fcd9c71 135
jm 0:3e676fcd9c71 136 // Limit Switch CCW activated and step direction CW
jm 0:3e676fcd9c71 137 if(sSwitch[sStepperAxis[i].limitCCW].state == Close && sStepper[sStepperAxis[i].stepperID].cw ==CW){
jm 0:3e676fcd9c71 138 sStepperAxis[i].status = LogicOK;
jm 0:3e676fcd9c71 139 if(oldStatus != LogicOK)rGPPSTA(i); // report change
jm 0:3e676fcd9c71 140 continue;
jm 0:3e676fcd9c71 141 }
jm 0:3e676fcd9c71 142 }
jm 0:3e676fcd9c71 143 }
jm 0:3e676fcd9c71 144 }
jm 0:3e676fcd9c71 145
jm 0:3e676fcd9c71 146 /** Module Get Module Process Properties Command Line Interface
jm 0:3e676fcd9c71 147 * @brief Command Line Interface to Get Module Public Process Properties
jm 0:3e676fcd9c71 148 * @param[in] id Extracted from command line id Process identification
jm 0:3e676fcd9c71 149 * @returns none
jm 0:3e676fcd9c71 150 */
jm 0:3e676fcd9c71 151 void cli_GPPSTA(void)
jm 0:3e676fcd9c71 152 { unsigned int id;
jm 0:3e676fcd9c71 153 if(ExtractUInteger(pLine,&id,0,stepperAxisQty-1)){ // extract id
jm 0:3e676fcd9c71 154 rGPPSTA(id);
jm 0:3e676fcd9c71 155 return;
jm 0:3e676fcd9c71 156 }
jm 0:3e676fcd9c71 157
jm 0:3e676fcd9c71 158 if(Help)printf("GPPSTA (StepperAxis id)0..%d\n",stepperAxisQty-1);
jm 0:3e676fcd9c71 159 // Ignore pending command line
jm 0:3e676fcd9c71 160 NextCommand(nl,pLine);
jm 0:3e676fcd9c71 161 }
jm 0:3e676fcd9c71 162
jm 0:3e676fcd9c71 163 /** Public Properties Message
jm 0:3e676fcd9c71 164 * @brief Send Process Properties to update GUI
jm 0:3e676fcd9c71 165 * @param[in] id Process identification
jm 0:3e676fcd9c71 166 * @returns Message: GPPSTA id stepperID limitCWid limitCCWid status
jm 0:3e676fcd9c71 167 */
jm 0:3e676fcd9c71 168 void rGPPSTA(unsigned int id ){
jm 0:3e676fcd9c71 169 int status;
jm 0:3e676fcd9c71 170 if(sStepperAxis[id].active==false)status=0;
jm 0:3e676fcd9c71 171 else status = sStepperAxis[id].status;
jm 0:3e676fcd9c71 172 printf("GPPSTA %d %d %d %d %d\n",id,sStepperAxis[id].stepperID,sStepperAxis[id].limitCCW,sStepperAxis[id].limitCW ,status);
jm 0:3e676fcd9c71 173 }