⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kkem.c

📁 非常重要的嵌入式单片机开发语言 是美国德州仪器的MSP430系列的系统语言4
💻 C
📖 第 1 页 / 共 4 页
字号:

#include "cnc.h"

#define USER_macro_process(num) SendMessage(hwndUSER,WM_USER,num,0L)

static CNCpolar_t CNC_polar;         /* polar  parameter */
static CNCpolar_t CNC_rotate;        /* rotate parameter */
static CNCaxis_t  CNC_scale0;        /* scaling center   */
static CNCaxis_t  CNC_scalef;        /* scaling factor   */
static CNCaxis_t  CNC_unit0;         /* unit zero        */
static CNCaxis_t  CNC_unitf;         /* unit factor      */

static int        nDot;              /* decimal index    */
static int        CNC_hfile;         /* handle of file   */
static long       CNC_pfile;         /* pointer of file  */
static char       CNC_code=4;        /* last command     */
static char       CNC_stop=1;        /* stop index       */
static char       CNC_lead=0;        /* leading area     */

static CNCaxis_t  aGoal,aGoalX;
static CNCaxis_t  aCenter,aCenterX;

void CNC_initial_process(void);
void CNC_end_process(void);
void CNC_reset_process(void);
void CNC_control_process(void);

static void AUTO_mode_control(void);
static BOOL open_CNC_file(int);
static void close_CNC_file(void);
static void clear_CNC_file(void);
static void absolute_to_local(LPAXIS,LPAXIS);
static void local_to_absolute(LPAXIS,LPAXIS);
static void local_to_machine(LPAXIS,LPAXIS);
static void machine_to_local(LPAXIS,LPAXIS);
static void absolute_to_machine(LPAXIS,LPAXIS);
static void machine_to_absolute(LPAXIS,LPAXIS);
static void get_axis_absolute(LPAXIS);
static void get_center_absolute(LPAXIS);
static void get_axis_input(LPAXIS);
static void get_center_input(LPAXIS);
static double get_value_input(char,int);
static int get_axis_number(char);
static BOOL is_axis_exist(int);
static BOOL is_any_axis_exist(void);
static BOOL is_all_axis_exist(void);
static BOOL is_center_exist(int);
static BOOL is_any_center_exist(void);
static BOOL is_all_center_exist(void);
static BOOL is_input_exist(char);

static void CNC_command_decoding(void);
static BOOL collect_CNC_command(void);

static BOOL calling_command(void);
static BOOL MST_command(void);
static BOOL special_command(void);
static BOOL motion_command(void);
static BOOL plane_command(void);
static BOOL absolute_command(void);
static BOOL limit_command(void);
static BOOL feed_command(void);
static BOOL unit_command(void);
static BOOL cutter_command(void);
static BOOL tool_command(void);
static BOOL cycle_command(void);
static BOOL return_command(void);
static BOOL scaling_command(void);
static BOOL macro_command(void);
static BOOL speed_command(void);
static BOOL working_command(void);
static BOOL stop_command(void);
static BOOL rotate_command(void);
static BOOL polar_command(void);
static BOOL offset_command(void);
static BOOL data_command(void);
static BOOL skip_command(void);
static void PATH_calculation(int);
static void preset_calculation(void);
static void scale_calculation(void);
static void offset_calculation(void);
static void stop_calculation(void);

/*=========================================================
  entry points of CNC program

  CNC_program_initial() : initial CNC program
  CNC_program_end()     : end of CNC program
  CNC_program_process() : timer process CNC program loop
=========================================================*/
                          /***** initial CNC program *****/
void CNC_initial_process()
{    int i;
     for (i=0; i<=6; i++) CNC_working[i]=aNull;
     for (i=0; i<6;  i++) CNC_unitf.v[i]=INCH;
     CNC_unit0=aNull;
     PATH_initial_process();
     CNC_reset_process();
}
                               /***** end of process *****/
void CNC_end_process()
{    PATH_end_process();
     clear_define_table();
     clear_stack();
     close_G10_table();
}
                                /***** reset process *****/
void CNC_reset_process()
{    int i;
                                       /* USER interface */
     *(USER_path->command)=0;
     USER_path->level=0; USER_path->pfile=0L;
                                       /* real-time area */
     PLC_path->code=99;
     lstrcpy(PLC_path->string, "N0000");
     lstrcpy(PLC_path->command,"Main Program");
                                         /* CNC decoding */
     nOther=nMove=nPlane=nInc=nLimit=0;
     nFeed=nUnit=nCut=nTool=nCycle=0;
     nReturn=nScale=nMacro=nSpeed=0;
     nStop=nRotate=nPolar=nOffset=nData=0;
     CNC_last=aNull; CNC_lastX=aNull; CNC_local=aNull;
     nDot=3; nWork=1;
     CNC_code=4; CNC_stop=1;
     CNC_nx=0; CNC_ny=1; CNC_nz=2;
     CNC_level=-1; CNC_error=0; CNC_lead=0;
     for (i=0; i<25; i++) CNC_calling->v[i]=0.;
     CNC_Dcode=CNC_Hcode=CNC_Tcode=CNC_Mcode=0;
     CNC_Ocode=CNC_Ncode=0;
     CNC_Fcode=CNC_G01max;
     CNC_Scode=CNC_RPMmax;
     clear_stack();
                                         /* PATH process */
     PATH_reset_process();
}

/*=========================================================
  CNC_control_process()
      execute CNC program every timer interrupt
=========================================================*/
void CNC_control_process()
{    int hfile,i,k,n; LPSTR s,d; char buf[81];
                                         /* AUTO-mode ?? */
     if (PLC_mode!=1) return;
     for (k=0; k<100; k++)
     {    lpCmd=&lpPath[nIn];
                                      /* check interface */
          AUTO_mode_control();
          if (PLC_flag>2) break;
                                 /* path control process */
          PATH_control_process();
                                       /* buffer full ?? */
          lpCmd=&lpPath[nIn];
          if (PLC_count>1)                      break;
          if (!(PLC_status&bPlotOnly))
          {    if (CNC_stop || PLC_path->stop || PLC_path->start)
               {    if (PLC_count)              break;
                    if ((!nCut) && (nIn!=nOut)) break;
               }
          }
                                      /* single block ?? */
          if (PLC_status&bSingleBlock)
          {    if (!(PLC_status&bCycleStart))   break;
               PLC_status&=(~bCycleStart); CNC_stop=1;
          }
                            /***** get next block !! *****/
          if (*(USER_path->command))      /* MDI mode ?? */
          {    lpCmd->level=CNC_level;
               lpCmd->pfile=CNC_Ncode;
               lstrncpy((LPSTR)buf,USER_path->command,79);
               set_fact_from_string(buf);
               lstrncpy(lpCmd->string,(LPSTR)buf,50);
               *(USER_path->command)=0;
          } else if (PLC_flag==2)        /* USER mode ?? */
          {    USER_path->level=CNC_Ocode;
               USER_path->pfile=CNC_Ncode;
               if (!USER_macro_process(10000))  break;
               *lpCmd=*USER_path; CNC_Ncode++;
               lstrcpy(lpCmd->command,CNC_stack[CNC_level].string);
               n=USER_path->pfile; sprintf(buf,"N%04u ",(unsigned)n);
               lstrncat((LPSTR)buf,USER_path->string,50);
               lstrncpy(lpCmd->string,(LPSTR)buf,50);
               CNC_command_decoding();
               continue;
          } else                         /* FILE mode !! */
          {    if (PLC_flag==0 || CNC_level<0)  break;
               hfile = CNC_stack[CNC_level].hfile;
               lpCmd->level = CNC_level;
               lpCmd->pfile = get_next_line_from_file(hfile);
               if (lpCmd->pfile<0L)     { close_CNC_file(); continue; }
               CNC_stack[CNC_level].pfile = _llseek(hfile,0,SEEK_CUR);
          }
                   /***** string-command decoding !! *****/
          CNC_Ncode++;
          if (DEFINE_command())     continue;
          if (PUSH_command())       continue;
          if (DIRECT_command())     continue;
          if (TABLE_command())      continue;
          if (CALLC_command())      continue;
          if (open_G10_table())     continue;
                                  /* collect CNC command */
          if (!collect_CNC_command()) { CNC_error=eDecode; break; }
                                       /* preset message */
          if (fN) CNC_Ncode=vN;
          else    vN=CNC_Ncode;
          if (CNC_level<0) lstrcpy(lpCmd->command,(LPSTR)"MDI Input");
          else    lstrcpy(lpCmd->command,CNC_stack[CNC_level].string);
          n=CNC_Ncode; sprintf(buf,"N%04u ",(unsigned)n);
          lstrncat((LPSTR)buf,lpCmd->string,50);
          lstrncpy(lpCmd->string,(LPSTR)buf,50);
                                             /* decoding */
          CNC_command_decoding();
     }
}

/*=========================================================
  AUTO_mode_control()
       AUTO-mode control process

  PLC_mode : CNC control mode
               =1:AUTO
  PLC_work : CNC working condition
               =0: off-process, can be switch to another mode
               =1: in-process
  PLC_flag : AUTO-mode internal status
               =0: MDI-mode  process
               =1: FILE-mode process
               =2: USER-mode process
               =9: error exist
               >9: emergency stop
  CNC_error: AUTO-mode error indication
               =0: no error
               >0: error exists
=========================================================*/
static void AUTO_mode_control()
{    switch (PLC_flag)
     {
     case 0 :                     /* check I/O interface */
          if (PLC_status&bEmergency) { PLC_flag+=10;             break; }
          if (PLC_status&bFeedHold)  { PLC_work=0; PLC_enable=0; break; }
          if (CNC_error)             { PLC_flag=9;               break; }
          PLC_enable=(PLC_status&bPlotOnly)?0:1;
          PLC_work  = PLC_count||(nIn!=nOut);
          if (PLC_work)            { PLC_status&=(~bCycleStart); break; }
                                 /* wait for cycle-start */
          if (!(PLC_status&bCycleStart)) break;
          PLC_status&=(~bCycleStart);
                                             /* re-start */
          CNC_reset_process();
          lstrcpy(lpCmd->string,"Main Program");
          open_CNC_file(PLC_mainfile);
          DSP_initial=1; CNC_lead=1;
          break;
     case 1 : case 2 :              /* normal process !! */
          if (PLC_status&bEmergency) { PLC_flag+=10;             break; }
          if (PLC_status&bFeedHold)  { PLC_work=0; PLC_enable=0; break; }
          if (CNC_error)             { PLC_flag=9;               break; }
          PLC_enable=(PLC_status&bPlotOnly)?0:1;
          PLC_work  =1;
          break;
     case 8 :                           /* error release */
          if   (PLC_status&bEmergency) break;
          PLC_status&=(~bCycleStart);
          clear_CNC_file(); CNC_reset_process();
          PLC_flag=0;
          break;
     case 9 :                          /* error exist !! */
          PLC_work=0; PLC_enable=0; PLC_status|=bEmergency;
          PLC_flag=8;
          lstrcpy(PLC_message,(LPSTR)"=>");
          lstrncpy(PLC_message+2,lpCmd->string,40);
          PostMessage(hwndFPLC,WM_USER,0,0L);
          break;
     case 10 : case 11 : case 12 :    /* emergency ON !! */
          PLC_work=0; PLC_enable=0;
          PLC_status&=(~bCycleStart);
          if (CNC_error) { PLC_flag=9; break; }
          if (PLC_status&bEmergency)   break;
          PLC_flag+=10;
          break;
     case 20 : case 21 : case 22 :   /* emergency OFF !! */
          PLC_work=0; PLC_enable=0;
          if (PLC_status&bEmergency) { PLC_flag-=10; break; }
/*          if (!(PLC_status&bCycleStart))             break;
*/
          PLC_status&=(~bCycleStart);
          PLC_flag-=20;
          break;
     }
}

/*=========================================================
  interface functions about CNC file operation

  open_CNC_file()  : open new CNC file
  close_CNC_file() : close old CNC file, and open next file
  clear_CNC_file() : close all CNC file
=========================================================*/
static BOOL open_CNC_file(n)
int n;        /* program no. */
{    static OFSTRUCT of; char buf1[81],buf2[21]; int hfile;
     int i; LPSTR s,d;
                                       /* buffer full ?? */
     if ((++CNC_level)>19) { CNC_level--; CNC_error=eFile; return(FALSE); }
                             /* preset calling condition */
     CNC_stack[CNC_level].name  =CNC_Ocode;
     CNC_stack[CNC_level].hfile =0;
     CNC_stack[CNC_level].pfile =0L;
     CNC_stack[CNC_level].nloop =1;
     CNC_stack[CNC_level].nflag =PLC_flag;
     CNC_stack[CNC_level].ncode =CNC_Ncode;
     CNC_stack[CNC_level].ncycle=nCycle;
     CNC_stack[CNC_level].nmacro=nMacro;
     CNC_stack[CNC_level].ndata =nData;
     lstrncpy(CNC_stack[CNC_level].string,lpCmd->string,80);
     nCycle=nMacro=nData=0; CNC_Ocode=n; CNC_Ncode=0;
     push_stack(40400,150);
     for (i=0,s=(LPSTR)lpCmd,d=(LPSTR)CNC_calling; i<150; i++)
          *d++ = *s++;
                                    /* open USER process */
     if (n>=9000 && hwndUSER)
     {    if (USER_macro_process(n)) { PLC_flag=2; return(TRUE); }
     }
                                        /* open CNC file */
     lstrncpy(buf1,PLC_directory,80);
     sprintf(buf2, "O%04d.CNC", n);
     lstrncat(buf1,buf2,80);
     hfile = OpenFile((LPSTR)buf1, &of, OF_READ);
     if (hfile<=0) { CNC_level--; CNC_error=eFile; return(FALSE); }
     CNC_stack[CNC_level].hfile=hfile;
     PLC_flag=1;
     return(TRUE);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -