Libary for Strpper motor controller, eg: Rep-Rap smart stick Catering for both, Phisical 'PIN' endstops, and PORT Expander end stops . ** BOTH IN TEST ** ** Phisical PIN tested (minimal) **** PORT PIN NOT TESTED ****
Stepper.cpp
00001 00002 00003 00004 #include "mbed.h" 00005 #include "Stepper.h" 00006 00007 00008 /* ....................................... 00009 00010 ? Pars Expanded Byte(s) 00011 00012 Assuming Port expander is used; 00013 00014 then on ISR, read chip registors .. 00015 -> code -> 00016 ........................................ */ 00017 00018 00019 00020 Stepper::Stepper(PinName Step, PinName Dir, PinName En, 00021 PinName Endstop_Left, 00022 PinName Endstop_Right, 00023 00024 bool Invert_Dir, 00025 bool Invert_ESL, 00026 bool Invert_ESR) 00027 00028 00029 : _Step (Step), _Dir(Dir), _En(En), _Endstop_Left(Endstop_Left), _Endstop_Right(Endstop_Right) 00030 { 00031 00032 // Assume End Stop Left is MIN, 00033 // and End Stop Right is MAX endstop. 00034 00035 _Invert_Dir = Invert_Dir, 00036 _Invert_ESL = Invert_ESL, 00037 _Invert_ESR = Invert_ESR; 00038 00039 { 00040 // bool read_1(); 00041 if (Endstop_Left != NC) 00042 { 00043 ESL = true; 00044 _Endstop_Left.mode (PullUp); 00045 } 00046 else 00047 { 00048 ESL = false; 00049 } 00050 if (Endstop_Right != NC) 00051 { 00052 ESR = true; 00053 _Endstop_Right.mode (PullUp); 00054 } 00055 else 00056 { 00057 ESR = false; 00058 } 00059 00060 // Set default val;ues .. no potr Endstops 00061 //Port_MASK_L = 0; 00062 //Port_INV_MASK_L = 0; 00063 00064 } 00065 } 00066 00067 // ------------------------------------------------------------------------------------ 00068 00069 00070 bool Stepper::Pulse() 00071 { 00072 // if (Step == NC) 00073 // { 00074 // return false; 00075 // } 00076 // else 00077 // Enable motor .. if not enabled .. short delay befor moveing .. 00078 00079 if (_En) 00080 { 00081 _En = 0; 00082 wait (0.005); // 5 mSec. 00083 } 00084 00085 _En = 0; 00086 00087 00088 { 00089 _Step = 1; 00090 wait_us (2); 00091 _Step = 0; 00092 return true; 00093 } 00094 } 00095 00096 // ------------------------------------------------------------------------------------ 00097 00098 00099 void Stepper::Enable (bool OnOff) 00100 { 00101 _En = !OnOff; 00102 } 00103 00104 // ------------------------------------------------------------------------------------ 00105 00106 bool Stepper::Set_Dir (bool Dir2Set) 00107 { 00108 // if (Dir == NC) 00109 // { 00110 // return false; 00111 // } 00112 // else 00113 { 00114 if (Dir2Set) 00115 { 00116 _Invert_Dir ? _Dir = 0 : _Dir = 1; 00117 } 00118 else 00119 { 00120 _Invert_Dir ? _Dir = 1 : _Dir = 0; 00121 } 00122 00123 return true; 00124 } 00125 } 00126 00127 00128 // ------------------------------------------------------------------------------------ 00129 00130 // test if ENDSTOP is enabled .... _Endstop_Left 00131 00132 bool Stepper::ESL_Fitted() 00133 { 00134 // (ESL) ? return true : return false; 00135 if (ESL) 00136 return true; 00137 else 00138 return false; 00139 } 00140 00141 // 00142 00143 bool Stepper::ESR_Fitted() 00144 { 00145 //ESR ? return true : return false; 00146 if (ESR) 00147 return true; 00148 else 00149 return false; 00150 } 00151 00152 // ............................................................................... 00153 00154 bool Stepper::ESL_Activeated() 00155 { 00156 bool retval; 00157 00158 if (_Invert_ESL) 00159 { 00160 if (_Endstop_Left) retval = true; else retval = false; 00161 } 00162 else 00163 { 00164 if (_Endstop_Left) retval = false; else retval = true; // 00165 } 00166 00167 if (retval) 00168 { 00169 //StepCount = 0; // might be very bad for H-BOT 00170 } 00171 00172 return retval; 00173 } 00174 00175 // 00176 00177 bool Stepper::ESR_Activeated() 00178 { 00179 if (_Invert_ESR) 00180 { 00181 if(_Endstop_Right) return true; else return false; 00182 } 00183 else 00184 { 00185 if(_Endstop_Right) return false; else return true; 00186 } 00187 } 00188 // ------------------------------------------------------------------------------------ 00189 00190 // test if endstop is triggerd. 00191 // ** will return false if not triggerd AND false if pin is NC 00192 // ** user should test if connected befor in a nested routeen from MAIN !! 00193 00194 00195 00196 bool Stepper::ESL_Port(int PORT_In) 00197 { 00198 bool Ret = false; 00199 00200 if (!Port_MASK_L) 00201 { 00202 Ret = false; 00203 } 00204 else 00205 { 00206 if (PORT_In && Port_MASK_L) 00207 { 00208 (Port_INV_MASK_L) ? Ret = true : Ret = false; 00209 } 00210 else 00211 { 00212 (Port_INV_MASK_L) ? Ret = false : Ret = true; 00213 } 00214 } 00215 00216 return (Ret); 00217 } 00218 00219 00220 // ............................. 00221 00222 void Stepper::ESL_Set_MASK (int Port_MASK_L_in) 00223 { 00224 Port_MASK_L = Port_MASK_L_in; 00225 } 00226 00227 void Stepper::ESL_Set_INV_MASK (int Port_INV_MASK_L_in) 00228 { 00229 Port_INV_MASK_L = Port_INV_MASK_L_in; 00230 } 00231 00232 00233 00234 00235 00236 00237 00238 00239 00240 00241 00242 00243 /* ********************************************************************** 00244 00245 ** 00246 * ` want an entity for a SINGLE Stepper motor, and assosiated things .. 00247 00248 PINS: 00249 step, 00250 Direction, 00251 Enable, 00252 00253 endstop_left 00254 endstop_right 00255 00256 Direction_Invert (T/F) 00257 00258 calls: 00259 step 00260 Endstop_Detected 00261 ** 00262 * 00263 * 00264 Vars: 00265 Number of steps to take 00266 * 00267 * 00268 ** 00269 * also need to be able to tell if endstops are defined 00270 * 00271 * 00272 00273 if (ESR != NC) ESR_En = true; else ESR_En = false; // 44; 00274 00275 // got 340 with g1 defined .. 00276 // got 44 with NO define :) 00277 00278 *************************** 00279 00280 if (Endstop_Right_Enabled) 00281 { 00282 00283 } 00284 else 00285 { 00286 00287 } 00288 00289 * ********************************************************************* */
Generated on Sun Jul 17 2022 00:47:36 by 1.7.2