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

📄 t.c

📁 激光加工控制系统 可以读入plt文件
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <stdio.h>
#include <process.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>

#define MINUTES 0
#define SECONDS 1

#define PI 3.1415926535898

typedef struct{
  int order,value;
}code_strt;

float count_angle(float x0, float y0, float x1, float y1);
recover_data();
set_data();

float b_x,b_y,e_x,e_y,o_x,o_y;
code_strt wait[30];
code_strt output[30];
long x_count=0,y_count=0;
int x_count1=0,y_count1=0;
int end_wait=0,end_output=0;
char go_home_str[200];     // G28
char command_str[200];     // begin code
int board_no;
float pulse_rate;          // max pulse rate means max speed
float interval;            // time to count (us)
float xsteps;              // pulses per unit(G CODE)
float ysteps;
float zsteps;
int danwei;                // Minute or Second
float xmaxv,xmaxv2,xmaxv3;               // Max Speed units(G CODE) per danwei
float ymaxv,ymaxv2,ymaxv3;
float xmaxv1;               // Max Speed units(G CODE) per danwei
float ymaxv1;
float zmaxv,zmaxv2,zmaxv3;
float xmaxa,xmaxa2,xmaxa3;               // Max Acceleration
float ymaxa,ymaxa2,ymaxa3;
float zmaxa,zmaxa2,zmaxa3;
float xydspeed;            // Default Speed
float zdspeed;
float max_accu_arc;        // circle accuracy
float xhl,xll,yhl,yll,zhl,zll;    // Max And Min Position

char filename[20];

int old_mode;
float oldx=0,oldy=0,oldi=0,oldj=0;
float x_once_max_a_pulses,x_once_max_v_pulses;
float y_once_max_a_pulses,y_once_max_v_pulses;
float x_once_max_a_pulses1,y_once_max_a_pulses1;
float cur_speed;
int begin_flag;
float x_len_to_acc,y_len_to_acc;
int x_times,y_times;
float x_adj=0,y_adj=0;
int mode=0;
int oldxx=0,oldyy=0;

get_a_line(FILE *fpe);
load_mac_para(FILE *fpe);

char buf[200];

main()
{
  FILE *fp;
  int i,times,ch;

  outportb(0x3f8,0);
  fp = fopen("setup1.fil","rb");
  if (fp == NULL){
    printf("Cann't Found File: Setup1.fil!\n");
    return;
  }
  load_mac_para(fp);
  xmaxv1 = xmaxv;               // Max Speed units(G CODE) per danwei
  ymaxv1 = ymaxv;
  x_once_max_a_pulses = xmaxa*xsteps*interval/1000.0;
  y_once_max_a_pulses = ymaxa*ysteps*interval/1000.0;
  x_once_max_a_pulses /= (1000/interval);
  y_once_max_a_pulses /= (1000/interval);
  x_once_max_v_pulses = xmaxv*xsteps*interval/1000.0;
  y_once_max_v_pulses = ymaxv*ysteps*interval/1000.0;
  if (danwei == MINUTES){
    x_once_max_a_pulses /= 60.0;
    y_once_max_a_pulses /= 60.0;
    x_once_max_v_pulses /= 60.0;
    y_once_max_v_pulses /= 60.0;
  }
  x_times = x_once_max_v_pulses/x_once_max_a_pulses;
  if (x_times <= 0)
    x_times = 1;
  x_len_to_acc = 0;
  for (i=0;i<x_times;i++){
    x_len_to_acc += (i+1)*x_once_max_a_pulses;
  }
  y_times = y_once_max_v_pulses/y_once_max_a_pulses;
  if (y_times <= 0)
    y_times = 1;
  y_len_to_acc = 0;
  for (i=0;i<y_times;i++){
    y_len_to_acc += (i+1)*y_once_max_a_pulses;
  }
  fclose(fp);
  trans_g_to_23();
  printf("If You Want To Send, Press Enter!\n");
//  ch = getch();
//  if (ch == 13)
//    send_file();
}

get_a_line(FILE *fpe)
{
  int i,ch;

  ch = fgetc(fpe);
  if (ch == EOF)
    return -1;
  i = 0;
  while ((ch != 13)&&(ch != 10)&&(ch != EOF)){
    buf[i++] = ch;
    ch = fgetc(fpe);
  }
  buf[i] = 0;
  if (ch == EOF)
    return -1;
  fgetc(fpe);

  return 0;
}

load_mac_para(FILE *fpe)
{
  int ret,order,value,i;
  int step,len;

  ret = 0;
  step = 0;
  while (ret == 0){
    ret = get_a_line(fpe);
    if ((buf[0] == 'I')&&(step == 0)){
      get_a_line(fpe);
      board_no = atol(&(buf[0]));
      step ++;
    }
    else if ((buf[0] == 'P')&&(step == 1)){
//      test_dog();
      get_a_line(fpe);
      pulse_rate = atol(&(buf[0]));
      step ++;
    }
    else if ((buf[0] == 'U')&&(step == 2)){
      get_a_line(fpe);
      interval = atol(&(buf[0]));
      step ++;
    }
    else if ((buf[0] == 'X')&&(buf[1] == 'S')&&(step == 3)){
      get_a_line(fpe);
      xsteps = atol(&(buf[0]));
      step ++;
    }
    else if ((buf[0] == 'Y')&&(buf[1] == 'S')&&(step == 4)){
      get_a_line(fpe);
      ysteps = atol(&(buf[0]));
      step ++;
    }
    else if ((buf[0] == 'Z')&&(buf[1] == 'S')&&(step == 5)){
      get_a_line(fpe);
      zsteps = atol(&(buf[0]));
      step ++;
    }
    else if ((buf[0] == 'm')||(buf[0] == 's')&&(step == 6)){
      if (buf[0] == 'm')
	danwei = MINUTES;
      else
	danwei = SECONDS;
      step ++;
    }
    else if ((buf[0] == 'X')&&(buf[1] == 'M')&&(step == 7)){
      get_a_line(fpe);
      xmaxv2 = xmaxv = atol(&(buf[0]))/2;
      step ++;
    }
    else if ((buf[0] == 'Y')&&(buf[1] == 'M')&&(step == 8)){
      get_a_line(fpe);
      ymaxv2 = ymaxv = atol(&(buf[0]))/2;
      step ++;
    }
    else if ((buf[0] == 'Z')&&(buf[1] == 'M')&&(step == 9)){
      get_a_line(fpe);
      zmaxv2 = zmaxv = atol(&(buf[0]));
      step ++;
    }
    else if ((buf[0] == 'X')&&(buf[1] == 'M')&&(step == 10)){
      get_a_line(fpe);
      xmaxa2 = xmaxa = atol(&(buf[0]));
      step ++;
    }
    else if ((buf[0] == 'Y')&&(buf[1] == 'M')&&(step == 11)){
      get_a_line(fpe);
      ymaxa2 = ymaxa = atol(&(buf[0]));
      step ++;
    }
    else if ((buf[0] == 'Z')&&(buf[1] == 'M')&&(step == 12)){
      get_a_line(fpe);
      zmaxa2 = zmaxa = atol(&(buf[0]));
      step ++;
    }
    else if ((buf[0] == 'X')&&(step == 13)){
      get_a_line(fpe);
      xydspeed = atol(&(buf[0]));
      step ++;
    }
    else if ((buf[0] == 'Z')&&(step == 14)){
      get_a_line(fpe);
      zdspeed = atol(&(buf[0]));
      step ++;
    }
    else if ((buf[0] == 'M')&&(step == 15)){
      get_a_line(fpe);
      max_accu_arc = atof(&(buf[0]));
    }
    else if ((buf[0] == 'W')&&(step == 15)){
      get_a_line(fpe);
      order = atol(&(buf[0]));
      get_a_line(fpe);
      for (i=0;i<strlen(buf);i++)
	if (buf[i] == 'X')
	  buf[i] = '0';
      for (i=0;i<strlen(buf);i++)
	buf[i] -= '0';
      value = buf[0] * 32 + buf[1] * 16 + buf[2] * 8 + buf[3] * 4 +
	      buf[4] * 2 + buf[5];
      wait[end_wait].order = order;
      wait[end_wait++].value = value;
    }
    else if ((buf[0] == 'S')&&(step == 15)){
      get_a_line(fpe);
      order = atol(&(buf[0]));
      get_a_line(fpe);
      for (i=0;i<strlen(buf);i++)
	if (buf[i] == 'X')
	  buf[i] = '0';
      len = strlen(buf);
      for (i=0;i<len;i++)
	buf[i] -= '0';
      value = buf[0] * 32 + buf[1] * 16 + buf[2] * 8 + buf[3] * 4 +
	      buf[4] * 2 + buf[5];
      output[end_output].order = order;
      output[end_output++].value = value;
    }
    else if ((buf[0] == 'X')&&(buf[1] == 'H')&&(step == 15)){
      get_a_line(fpe);
      xhl = atol(&(buf[0]));
      step ++;
    }
    else if ((buf[0] == 'X')&&(buf[1] == 'L')&&(step == 16)){
      get_a_line(fpe);
      xll = atol(&(buf[0]));
      step ++;
    }
    else if ((buf[0] == 'Y')&&(buf[1] == 'H')&&(step == 17)){
      get_a_line(fpe);
      yhl = atol(&(buf[0]));
      step ++;
    }
    else if ((buf[0] == 'Y')&&(buf[1] == 'L')&&(step == 18)){
      get_a_line(fpe);
      yll = atol(&(buf[0]));
      step ++;
    }
    else if ((buf[0] == 'Z')&&(buf[1] == 'H')&&(step == 19)){
      get_a_line(fpe);
      zhl = atol(&(buf[0]));
      step ++;
    }
    else if ((buf[0] == 'Z')&&(buf[1] == 'L')&&(step == 20)){
      get_a_line(fpe);
      zll = atol(&(buf[0]));
      step ++;
    }
    else if ((buf[0] == 'G')&&(buf[1] == 'o')&&(step == 21)){
      get_a_line(fpe);
      strcpy(go_home_str,buf);
      step ++;
      strcpy(command_str,"");
    }
    else if ((buf[0] == 'I')&&(buf[1] == 'n')&&(step == 22)){
      get_a_line(fpe);
      strcat(command_str," ");
      strcat(command_str,buf);
    }
  }
}

trans_g_to_23()
{
  FILE *fr_file,*to_file;

  printf("Please Input The File Name To Trans: ");
  scanf("%s",filename);
  if (filename[strlen(filename)-2] != '.')
    strcat(filename,".G");

  fr_file = fopen(filename,"rb");
  if (fr_file == NULL){
    printf("Can not Find File!\n");
    return -1;
  }
  filename[strlen(filename)-2] = 0;
  strcat(filename,".23");
  to_file = fopen(filename,"w");
  if (to_file == NULL){
    fclose(fr_file);
    return -1;
  }

  put_a_line(to_file,"V1");
  put_a_line(to_file,"I768");
  put_a_line(to_file,"U20");
  put_a_line(to_file,"N2");
  strcpy(buf,"=");
  strcat(buf,command_str);
  put_a_line(to_file,buf);

  trans_file(fr_file,to_file);

  fclose(fr_file);
  fclose(to_file);
}

int order = 0;
put_a_line(FILE *fpe,char *buf)
{
  int i,xx,yy;

  order ++;
  for (i=0;i<strlen(buf);i++)
    fputc(buf[i],fpe);
  if (buf[0] == 'P'){
    xx = atoi(&(buf[1]));
    i = 0;
    while (buf[i] != ' ')
      i ++;
    yy = atoi(&(buf[i]));
    x_count1 += xx;
    y_count1 += yy;
  }
  if (fabs(yy) > 100)
    i = 0;
  fputc('\n',fpe);
}

trans_file(FILE *fr_file,FILE *to_file)
{
  int ret,b_pos,b_p,i,flag,order,order1;
  char buf1[100],buf2[20];
  float ff;

  ret = 0;
  begin_flag = 1;
  cur_speed = xydspeed;
  order1 = 1;
  while (ret == 0){
    if ((order1 % 10) == 0)
      printf("Line: %d\n",order1);
    order1 ++;
    ret = get_a_line(fr_file);
    if (fabs(x_count1-e_x*100) > 2)
      ret = ret;
    if (ret == -1)
      strcpy(buf,"");
    b_pos = 0;
    if (buf[0] == 'N'){
      i = 1;
      while (((buf[i] >= '0')&&(buf[i] <= '9'))||(buf[i] == ' '))
	i ++;
      b_pos = i;
    }
    if (buf[b_pos] == 'F'){
      cur_speed = atof(&(buf[b_pos+1]));
      cur_speed /= 60;
      if (cur_speed < xmaxv1){
	xmaxv = cur_speed*60/2;
	ymaxv = cur_speed*60/2;
  x_once_max_v_pulses = xmaxv*xsteps*interval/(1000.0);
  y_once_max_v_pulses = ymaxv*ysteps*interval/(1000.0);
  x_once_max_v_pulses /= 60;
  y_once_max_v_pulses /= 60;

  x_times = x_once_max_v_pulses/x_once_max_a_pulses;
  if (x_times <= 0)
    x_times = 1;
  x_len_to_acc = 0;
  for (i=0;i<x_times;i++){
    x_len_to_acc += (i+1)*x_once_max_a_pulses;
  }
  y_times = y_once_max_v_pulses/y_once_max_a_pulses;
  if (y_times <= 0)
    y_times = 1;
  y_len_to_acc = 0;
  for (i=0;i<y_times;i++){
    y_len_to_acc += (i+1)*y_once_max_a_pulses;
  }
      }
    }
    else if (buf[b_pos] == 'M'){
      order = atoi(&(buf[b_pos+1]));
    for (i=0;i<end_output;i++)
	if (order == output[i].order){
	  if (mode == 1){
//	    put_a_line(to_file,"P0 0");
	    put_a_line(to_file,"A2");
	    put_a_line(to_file,"=2Q0 1Q0");
	  }
	  strcpy(buf1,"O ");
	  itoa(output[i].value,buf2,10);
	  strcat(buf1,buf2);
	  itoa(63,buf2,10);
	  strcat(buf1," ");
	  strcat(buf1,buf2);
	  put_a_line(to_file,buf1);
	  begin_flag = 1;
	}
      if (order == 2){
	put_a_line(to_file,"A2");
	put_a_line(to_file,"S2");
	put_a_line(to_file,"E");
      }
    }
    else if ((buf[b_pos] == 'G')&&(buf[b_pos+1] == '9')&&(buf[b_pos+2] == '2')){
      b_p = b_pos;
      while ((buf[b_p] != 'X')&&(buf[b_p] != 'x'))
	b_p ++;
      o_x = atof(&(buf[b_p+1]));
      while ((buf[b_p] != 'Y')&&(buf[b_p] != 'y'))
	b_p ++;
      o_y = atof(&(buf[b_p+1]));
    }
    else if (buf[b_pos] == 'G'){
      i = b_pos + 1;
      while (((buf[i] >= '0')&&(buf[i] <= '9'))||(buf[i] == ' '))
	i ++;
      b_p = i;
      flag = atoi(&(buf[b_pos+1]));
      if ((flag >= 0)&&(flag <= 3)){
	if (begin_flag == 1){
	  strcpy(buf1,"=1Q2 2Q2 1TD");
	  itoa(interval,buf2,10);
	  strcat(buf1,buf2);
	  strcat(buf1," 2TD");
	  strcat(buf1,buf2);
	  strcat(buf1," MSL11X MSS");
	  put_a_line(to_file,buf1);
	  begin_flag = 0;
	  mode = 1;
	}
      }
      switch (flag){
	case 0:
	  set_data();
	  proc_line(b_p,to_file);
	  recover_data();
	  old_mode = 1;
	  break;
	case 1:
	  proc_line(b_p,to_file);
	  old_mode = 1;
	  break;
	case 2:
	  proc_circ(b_p,to_file,2);
	  old_mode = 2;
	  break;
	case 3:
	  proc_circ(b_p,to_file,3);
	  old_mode = 3;
	  break;
	case 4:
	  strcpy(buf1,"W");
	  i = 0;
	  while (buf[i] != 'P')
	    i ++;
	  strcat(buf1,&(buf[i+1]));
	  put_a_line(to_file,buf1);
	  break;
      }
    }
    else if ((buf[b_pos] == 'X')||(buf[b_pos] == 'Y')){
      flag = old_mode;
      if ((flag >= 0)&&(flag <= 3)){
	if (begin_flag == 1){
	  strcpy(buf1,"=1Q2 2Q2 1TD");
	  itoa(interval,buf2,10);
	  strcat(buf1,buf2);
	  strcat(buf1," 2TD");
	  strcat(buf1,buf2);
	  strcat(buf1," MSL11X MSS");
	  put_a_line(to_file,buf1);
	  begin_flag = 0;
	  mode = 1;
	}
      }
      b_p = b_pos;
      switch (flag){
	case 0:
	  set_data();
	  proc_line(b_p,to_file);
	  recover_data();
	  old_mode = 1;
	  break;
	case 1:
	  proc_line(b_p,to_file);
	  old_mode = 1;
	  break;
	case 2:
	  proc_circ(b_p,to_file,2);
	  old_mode = 2;
	  break;
	case 3:
	  proc_circ(b_p,to_file,3);
	  old_mode = 3;
	  break;
      }
    }
  }
}

proc_line(int b_p,FILE *to_file)
{
  float len,xx,yy,oldxx1,oldyy1,oldxx2,oldyy2,len1;
  float speed,speed1,len2,len3,len4,len5;
  int i,x,y,times,j,s,s1,s2;
  float vmax,ll,curx,cury,xx1,yy1,f;
  char buf1[100],buf2[20];

//  put_a_line(to_file,"P0 0");
  vmax = x_once_max_v_pulses;
  b_x = oldx;
  b_y = oldy;
  if (buf[b_p] == 'X'){
    e_x = atof(&(buf[b_p+1]));
    e_x -= o_x;
    i = b_p + 1;
    while ((buf[i] != 'Y')&&(buf[i] != 0))
      i ++;
    if (buf[i] == 'Y'){
      e_y = atof(&(buf[i+1]));
      e_y -= o_y;
    }
    else{
      e_y = b_y;
    }
  }
  else{
    e_x = b_x;
    e_y = atof(&(buf[b_p+1]));
    e_y -= o_y;
  }

  if (fabs(e_x-b_x) + fabs(e_y-b_y) < 0.0001)
    return -1;
  oldx = curx = b_x;
  oldy = cury = b_y;
  len = sqrt((e_x-b_x)*(e_x-b_x)+(e_y-b_y)*(e_y-b_y));
  if (len > 2*x_len_to_acc/xsteps){
    speed = 0;
    oldx = curx = b_x;
    oldy = cury = b_y;
    len1 = 0;
    while (sqrt((e_x-curx)*(e_x-curx)+(e_y-cury)*(e_y-cury)) > x_len_to_acc/xsteps){
      if (speed < vmax)
	speed += x_once_max_a_pulses;
      else
	speed = vmax;
      curx = b_x + (e_x-b_x)*(len1+speed/xsteps)/len;
      cury = b_y + (e_y-b_y)*(len1+speed/xsteps)/len;
      f = (curx - oldx)*xsteps;
      if (f > 0)
	x = f + 0.5;
      else
	x = f - 0.5;
      f = (cury - oldy)*ysteps;
      if (f > 0)
	y = f + 0.5;
      else
	y = f - 0.5;
      strcpy(buf1,"P");
      itoa(x,buf2,10);
      strcat(buf1,buf2);
      itoa(y,buf2,10);
      strcat(buf1," ");
      strcat(buf1,buf2);
      put_a_line(to_file,buf1);
      oldx += x/xsteps;
      oldy += y/ysteps;
      len5 = speed/xsteps;
      len1 += len5;
    }
    len3 = len - len1;
    i = 0;
    len2 = 0;
    speed = 0;
    while (len2 < len3){
      speed += x_once_max_a_pulses;
      len2 += speed/xsteps;
      i ++;
    }
    len2 -= speed/xsteps;
    len4 = len3 - len2;
    speed -= x_once_max_a_pulses;
    i --;
    if (i == 0){
      curx = e_x;
      cury = e_y;
      f = (curx - oldx)*xsteps;
      if (f > 0)
	x = f + 0.5;
      else
	x = f - 0.5;
      f = (cury - oldy)*ysteps;
      if (f > 0)
	y = f + 0.5;
      else
	y = f - 0.5;
      strcpy(buf1,"P");
      itoa(x,buf2,10);
      strcat(buf1,buf2);
      itoa(y,buf2,10);
      strcat(buf1," ");
      strcat(buf1,buf2);
      put_a_line(to_file,buf1);
      oldx += x/xsteps;
      oldy += y/ysteps;
    }
    else{
      for (j=0;j<i;j++){
	if (j == i-1){
	  curx = e_x;
	  cury = e_y;
	}
	else{
	  curx = b_x + (e_x-b_x)*(len1+speed/xsteps+len4/i)/len;
	  cury = b_y + (e_y-b_y)*(len1+speed/xsteps+len4/i)/len;
	}
      f = (curx - oldx)*xsteps;
      if (f > 0)
	x = f + 0.5;
      else
	x = f - 0.5;
      f = (cury - oldy)*ysteps;
      if (f > 0)
	y = f + 0.5;
      else
	y = f - 0.5;
	strcpy(buf1,"P");
	itoa(x,buf2,10);
	strcat(buf1,buf2);
	itoa(y,buf2,10);
	strcat(buf1," ");
	strcat(buf1,buf2);

⌨️ 快捷键说明

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