Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

emctask.cc

Go to the documentation of this file.
00001 /*
00002   emctask.cc
00003 
00004   Mode and state management for EMC_TASK class
00005 
00006   Modification history:
00007 
00008   14-Nov-2000 WPS modified emcTaskExecute so that hopefully after an abort,estop or fault the current line would still be displayed. 
00009   2-Jun-2000  FMP added emcLubeOn,Off() to emcTaskSetState(), to turn lube
00010   on when machine goes on, off otherwise
00011   10-Jun-1999  FMP added emcTaskPlanCommand(); took setting of task->command
00012   out of emcTaskUpdate() since it's in emctaskmain.cc during read-exec phase
00013   22-Feb-1999  FMP took calls to turn spindle, coolant off when going into
00014   estop since they interfered with estop command
00015   19-Feb-1999  FMP changed order for estopping, doing aux estop, then
00016   motion, then spindle, coolant, etc.
00017   23-Dec-1998  FMP added emcTaskPlanSet,Is,ClearWait(); took tool length
00018   offset update out of here and moved into emctaskmain.cc so that it won't
00019   be read-ahead; ditto with program origin
00020   3-Aug-1998  FMP added toolOffset.z = GET_TOOL_LENGTH_OFFSET()
00021   30-Jul-1998  FMP added active G,M codes
00022   2-Jul-1998  FMP added emcTaskPlanSynch()
00023   26-Jun-1998  FMP added calls to turn spindle and coolant off when
00024   going into estop
00025   20-May-1998  FMP added call to rs274ngc_synch() when entering MDI
00026   or AUTO mode
00027   24-Feb-1998  FMP made abort order axis, traj, io, in emcTaskAbort()
00028   7-Jan-1998  FMP changed call from emcIoHalt() (which deletes NML
00029   buffer) to emcIoAbort(), in emcTaskAbort()
00030   15-Dec-1997  FMP eliminated checks on state in emcTaskSetState, just
00031   forcing the function calls regardless
00032   17-Jul-1997  FMP added subsystem checks for mode and state changes
00033   3-Jul-1997  FMP created from original HME wmsa.cc
00034   */
00035 
00036 #include <stdlib.h>
00037 #include <string.h>             // strncpy()
00038 
00039 #include "emc.hh"               // EMC NML
00040 #include "emcglb.h"             // EMC_INIFILE
00041 #include "interpl.hh"           // NML_INTERP_LIST, interp_list
00042 #include "canon.hh"             // CANON_VECTOR, GET_PROGRAM_ORIGIN()
00043 #include "rs274ngc.hh"          // the interpreter
00044 
00045 
00046 #ifdef NEW_INTERPRETER
00047 #include "rs274ngc_return.hh"   // NCE_FILE_NOT_OPEN
00048 #endif
00049 
00050 // ident tag
00051 #ifndef __GNUC__
00052 #ifndef __attribute__
00053 #define __attribute__(x)
00054 #endif
00055 #endif
00056 
00057 static char __attribute__((unused)) ident[] = "$Id: emctask.cc,v 1.15 2001/11/14 19:07:12 wshackle Exp $";
00058 
00059 // flag for how we want to interpret traj coord mode, as mdi or auto
00060 static int mdiOrAuto = EMC_TASK_MODE_AUTO;
00061 
00062 // EMC_TASK interface
00063 
00064 int emcTaskInit()
00065 {
00066   return 0;
00067 }
00068 
00069 int emcTaskHalt()
00070 {
00071   return 0;
00072 }
00073 
00074 int emcTaskAbort()
00075 {
00076   emcMotionAbort();
00077   emcIoAbort();
00078 
00079   return 0;
00080 }
00081 
00082 int emcTaskSetMode(int mode)
00083 {
00084   int retval = 0;
00085 
00086   switch (mode) {
00087   case EMC_TASK_MODE_MANUAL:
00088     // go to manual mode
00089     emcTrajSetMode(EMC_TRAJ_MODE_FREE);
00090     mdiOrAuto = EMC_TASK_MODE_AUTO; // we'll default back to here
00091     break;
00092 
00093   case EMC_TASK_MODE_MDI:
00094     // go to mdi mode
00095     emcTrajSetMode(EMC_TRAJ_MODE_COORD);
00096     emcTaskPlanSynch();
00097     mdiOrAuto = EMC_TASK_MODE_MDI;
00098     break;
00099 
00100   case EMC_TASK_MODE_AUTO:
00101     // go to auto mode
00102     emcTrajSetMode(EMC_TRAJ_MODE_COORD);
00103     emcTaskPlanSynch();
00104     mdiOrAuto = EMC_TASK_MODE_AUTO;
00105     break;
00106 
00107   default:
00108     retval = -1;
00109     break;
00110   }
00111 
00112   return retval;
00113 }
00114 
00115 int emcTaskSetState(int state)
00116 {
00117   int t;
00118   int retval = 0;
00119 
00120   switch (state) {
00121   case EMC_TASK_STATE_OFF:
00122     // turn the machine servos off-- go into ESTOP_RESET state
00123     for (t = 0; t < emcStatus->motion.traj.axes; t++) {
00124       emcAxisDisable(t);
00125     }
00126     emcTrajDisable();
00127     emcLubeOff();
00128     break;
00129 
00130   case EMC_TASK_STATE_ON:
00131     // turn the machine servos on
00132     emcTrajEnable();
00133     for (t = 0; t < emcStatus->motion.traj.axes; t++) {
00134       emcAxisEnable(t);
00135     }
00136     emcLubeOn();
00137     break;
00138 
00139   case EMC_TASK_STATE_ESTOP_RESET:
00140     // reset the estop
00141     if(emcStatus->io.aux.estopIn)
00142       {
00143         rcs_print("Can't come out of estop while the estop button is in.");
00144       }
00145     emcAuxEstopOff();
00146     emcLubeOff();
00147     break;
00148 
00149   case EMC_TASK_STATE_ESTOP:
00150     // go into estop-- do both IO estop and machine servos off
00151     emcAuxEstopOn();
00152     for (t = 0; t < emcStatus->motion.traj.axes; t++) {
00153       emcAxisDisable(t);
00154     }
00155     emcTrajDisable();
00156     emcLubeOff();
00157     break;
00158 
00159   default:
00160     retval = -1;
00161     break;
00162   }
00163 
00164   return retval;
00165 }
00166 
00167 // WM access functions
00168 
00169 /*
00170   determineMode()
00171 
00172   Looks at mode of subsystems, and returns associated mode
00173 
00174   Depends on traj mode, and mdiOrAuto flag
00175 
00176   traj mode   mdiOrAuto     mode
00177   ---------   ---------     ----
00178   FREE        XXX           MANUAL
00179   COORD       MDI           MDI
00180   COORD       AUTO          AUTO
00181   */
00182 static int determineMode()
00183 {
00184   // if traj is in free mode, then we're in manual mode
00185   if (emcStatus->motion.traj.mode == EMC_TRAJ_MODE_FREE ||
00186       emcStatus->motion.traj.mode == EMC_TRAJ_MODE_TELEOP ) {
00187     return EMC_TASK_MODE_MANUAL;
00188   }
00189 
00190   // else traj is in coord mode-- we can be in either mdi or auto
00191   return mdiOrAuto;
00192 }
00193 
00194 /*
00195   determineState()
00196 
00197   Looks at state of subsystems, and returns associated state
00198 
00199   Depends on traj enabled, io estop, and desired task state
00200 
00201   traj enabled   io estop      state
00202   ------------   --------      -----
00203   DISABLED       ESTOP         ESTOP
00204   ENABLED        ESTOP         ESTOP
00205   DISABLED       OUT OF ESTOP  ESTOP_RESET
00206   ENABLED        OUT OF ESTOP  ON
00207   */
00208 static int determineState()
00209 {
00210   if (emcStatus->io.aux.estop) {
00211     return EMC_TASK_STATE_ESTOP;
00212   }
00213 
00214   if (! emcStatus->motion.traj.enabled) {
00215     return EMC_TASK_STATE_ESTOP_RESET;
00216   }
00217 
00218   return EMC_TASK_STATE_ON;
00219 }
00220 
00221 static int waitFlag = 0;
00222 
00223 
00224 #ifdef NEW_INTERPRETER
00225 static char rs274ngc_error_text_buf[256];
00226 static char rs274ngc_stack_buf[256];
00227 
00228 static void print_rs274ngc_error(int retval)
00229 {
00230   int index = 0;
00231   if(retval == 0)
00232     {
00233       return;
00234     }
00235 
00236   if( 0 != emcStatus)
00237     {
00238       emcStatus->task.interpreter_errcode = retval;
00239     }
00240 
00241   rs274ngc_error_text_buf[0]=0;
00242   rs274ngc_error_text(retval, rs274ngc_error_text_buf,256);
00243   if(0 != rs274ngc_error_text_buf[0])
00244     {
00245       rcs_print_error("rs274ngc_error: %s\n",rs274ngc_error_text_buf);
00246     }
00247   emcOperatorError(0,rs274ngc_error_text_buf);
00248   index=0;
00249   if (EMC_DEBUG & EMC_DEBUG_INTERP)
00250     {
00251       rcs_print("rs274ngc_stack: \t");
00252       while(index < 5)
00253         {
00254           rs274ngc_stack_buf[0]=0;
00255           rs274ngc_stack_name(index,rs274ngc_stack_buf,256);
00256           if(0 == rs274ngc_stack_buf[0])
00257             {
00258               break;
00259             }
00260           rcs_print(" - %s ",rs274ngc_stack_buf);
00261           index++;
00262         }
00263       rcs_print("\n");
00264     }
00265 }
00266 
00267 #endif // NEW_INTERPRETER
00268 
00269 
00270 int emcTaskPlanInit()
00271 {
00272   rs274ngc_ini_load(EMC_INIFILE);
00273   waitFlag = 0;
00274 
00275   int retval = rs274ngc_init();
00276 #ifdef NEW_INTERPRETER
00277   if(retval > RS274NGC_MIN_ERROR)
00278     {
00279       print_rs274ngc_error(retval);
00280     }
00281   else 
00282     {
00283       if( 0 != RS274NGC_STARTUP_CODE[0])
00284         {
00285           retval = rs274ngc_execute(RS274NGC_STARTUP_CODE);
00286           if(retval > RS274NGC_MIN_ERROR)
00287             {
00288               print_rs274ngc_error(retval);
00289             }
00290         }
00291     }
00292 #else
00293   if( 0 != RS274NGC_STARTUP_CODE[0])
00294     {
00295       rs274ngc_execute(RS274NGC_STARTUP_CODE);
00296     }
00297 #endif
00298   return retval;
00299 }
00300 
00301 int emcTaskPlanSetWait()
00302 {
00303   waitFlag = 1;
00304 
00305   return 0;
00306 }
00307 
00308 int emcTaskPlanIsWait()
00309 {
00310   return waitFlag;
00311 }
00312 
00313 int emcTaskPlanClearWait()
00314 {
00315   waitFlag = 0;
00316 
00317   return 0;
00318 }
00319 
00320 int emcTaskPlanSynch()
00321 {
00322   return rs274ngc_synch();
00323 }
00324 
00325 int emcTaskPlanExit()
00326 {
00327   return rs274ngc_exit();
00328 }
00329 
00330 int emcTaskPlanOpen(const char *file)
00331 {
00332   if(emcStatus != 0)
00333     {
00334       emcStatus->task.motionLine = 0;
00335       emcStatus->task.currentLine = 0;
00336       emcStatus->task.readLine = 0;
00337     }
00338 
00339   int retval = rs274ngc_open(file);
00340 #ifdef NEW_INTERPRETER
00341   if(retval > RS274NGC_MIN_ERROR)
00342     {
00343       print_rs274ngc_error(retval);
00344       return retval;
00345     }
00346 #endif
00347   taskplanopen=1; 
00348   return retval;
00349 }
00350 
00351 
00352 int emcTaskPlanRead()
00353 {
00354   int retval = rs274ngc_read();
00355 #ifdef NEW_INTERPRETER
00356   if(retval == NCE_FILE_NOT_OPEN)
00357     {
00358       if(emcStatus->task.file[0] != 0)
00359         {
00360           retval = rs274ngc_open(emcStatus->task.file);
00361           if(retval > RS274NGC_MIN_ERROR)
00362             {
00363           print_rs274ngc_error(retval);
00364             }
00365           retval = rs274ngc_read();
00366         }
00367     }
00368   if(retval > RS274NGC_MIN_ERROR)
00369     {
00370       print_rs274ngc_error(retval);
00371     }
00372 #endif
00373   return retval;
00374 }
00375 
00376 int emcTaskPlanExecute(const char *command)
00377 {
00378   if(command != 0)
00379     {
00380       if(*command != 0)
00381         {
00382           rs274ngc_synch();
00383         }
00384     }
00385   int retval = rs274ngc_execute(command);
00386 #ifdef NEW_INTERPRETER
00387   if(retval > RS274NGC_MIN_ERROR)
00388     {
00389       print_rs274ngc_error(retval);
00390     }
00391 #endif
00392   return retval;
00393 }
00394 
00395 int emcTaskPlanClose()
00396 {
00397   int retval = rs274ngc_close();
00398 #ifdef NEW_INTERPRETER
00399   if(retval > RS274NGC_MIN_ERROR)
00400     {
00401       print_rs274ngc_error(retval);
00402     }
00403 #endif
00404 
00405   taskplanopen=0; 
00406   return retval;
00407 }
00408 
00409 int emcTaskPlanLine()
00410 {
00411   return rs274ngc_line();
00412 }
00413 
00414 int emcTaskPlanCommand(char *cmd)
00415 {
00416   strcpy(cmd, rs274ngc_command());
00417   return 0;
00418 }
00419 
00420 int emcTaskUpdate(EMC_TASK_STAT *stat)
00421 {
00422   stat->mode = determineMode();
00423   stat->state = determineState();
00424 
00425   // execState set in main
00426   // interpState set in main
00427   if(emcStatus->motion.traj.id > 0)
00428     {
00429       stat->motionLine = emcStatus->motion.traj.id;
00430     }
00431   // currentLine set in main
00432   // readLine set in main
00433 
00434   strcpy(stat->file, rs274ngc_file());
00435   // command set in main
00436 
00437   // update active G and M codes
00438   rs274ngc_active_g_codes(&stat->activeGCodes[0]);
00439   rs274ngc_active_m_codes(&stat->activeMCodes[0]);
00440   rs274ngc_active_settings(&stat->activeSettings[0]);
00441 
00442   stat->heartbeat++;
00443 
00444   return 0;
00445 }
00446 
00447 
00448 
00449 
00450 
00451 
00452 
00453 
00454 
00455 
00456 
00457 
00458 
00459 

Generated on Sun Dec 2 15:27:40 2001 for EMC by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001