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

📄 t2.c

📁 激光加工控制系统 可以读入plt文件
💻 C
📖 第 1 页 / 共 2 页
字号:
    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;
  }

  cur_a = count_angle(b_x,b_y,e_x,e_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){
     r_len = x_once_max_v_pulses/xsteps;
     r_times = (int)((len - 2*x_len_to_acc/xsteps)/r_len);
     len_to_acc = (len - r_times*r_len)/2;
     acc_len = x_once_max_a_pulses/xsteps;
     a1 = 0;
     a2 = 0;
     end_degrees = 0;
     while (a1 < len_to_acc){
       a2 += acc_len;
       a1 += a2;
       degree[end_degrees++] = a2;
     }
     end_degrees --;
     a1 -= a2;
     a3 = len_to_acc - a1;
     if (end_degrees == 1){
       if (a3 < degree[0]){
	 degree[1] = degree[0];
	 degree[0] = a3;
       }
       else
	 degree[1] = a3;
       end_degrees = 2;
     }
     else{
       for (kk=1;kk<end_degrees;kk++){
	 if ((degree[kk-1] <= a3)&&(degree[kk] >= a3)){
	   for (k1=end_degrees;k1>kk;k1--)
	     degree[k1] = degree[k1-1];
	   degree[kk] = a3;
	   end_degrees++;
	   break;
	 }
       }
     }
     cur_len = 0;
     for (i=0;i<2*end_degrees+r_times;i++){
       if (i<end_degrees)
	 cur_len += degree[i];
       else if (i < end_degrees+r_times)
	 cur_len += r_len;
       else
	 cur_len += degree[2*end_degrees+r_times-i-1];
       cur_x = b_x + cur_len*cos(cur_a);
       cur_y = b_y + cur_len*sin(cur_a);
       if (i == 2*end_degrees+r_times - 1){
	 cur_x = e_x;
	 cur_y = e_y;
       }
       f = (cur_x - oldx - d_x)*xsteps;
       if (f > 0)
	 x = f + 0.5;
       else
	 x = f - 0.5;
       f = (cur_y - oldy  - d_y)*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);
       if (fabs(x)+fabs(y) > 0.1){
	 put_a_line(to_file,buf1);
	 oldx += x/xsteps;
	 oldy += y/ysteps;
       }
     }

  }
  else{
    len3 = len/2;
    len2 = 0;
    speed = 0;
    i = 0;
    while (len2 < len3){
      speed += x_once_max_a_pulses;
      len2 += speed/xsteps;
      i ++;
    }
    len2 -= speed/xsteps;
    len4 = len3 - len2;
    i --;
    len1 = 0;
    speed1 = speed-x_once_max_a_pulses;
    speed = 0;
    if (i == 0){
      curx = (b_x+e_x)/2;
      cury = (b_y+e_y)/2;
      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++){
	speed += x_once_max_a_pulses;
	if (j == i-1){
	  curx = (b_x+e_x)/2;
	  cury = (b_y+e_y)/2;
	}
	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);
	put_a_line(to_file,buf1);
	oldx += x/xsteps;
	oldy += y/ysteps;
	len5 = speed/xsteps+len4/i;
	len1 += len5;
      }
    }

    speed = speed1;
    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);
	put_a_line(to_file,buf1);
	oldx += x/xsteps;
	oldy += y/ysteps;
	len5 = speed/xsteps+len4/i;
	len1 += len5;
	speed -= x_once_max_a_pulses;
      }
    }
  }
  d_x = oldx - e_x;
  d_y = oldy - e_y;
  oldx = e_x;
  oldy = e_y;
//  put_a_line(to_file,"P0 0");
}

proc_circ(int b_p,FILE *to_file,int type)
{
  double I,J,cx,cy,r,vmax,amax,curx,cury;
  double ba,ea,an,ll,len,angle,xx,yy;
  double len1,len2,len3,len4,len5,aa;
  double speed,speed1,dan,f,len_to_acc;
  int x,y;
  unsigned char buf1[20],buf2[20];
  int s,s1,s2;
  int i,j,times;
  unsigned char chbak;
  double r_da,a_da,acc_da,a1,a2,a3;
  int r_times,kk,k,k1;
  double cur_a,cur_x,cur_y;

//  strcpy(buf1,"P0 0");
  end_degrees = 0;
//  put_a_line(to_file,buf1);
  b_x = oldx;
  b_y = oldy;
  I = oldi;
  J = oldj;
  if (buf[b_p] == 'X'){
    i = b_p + 1;
    while ((buf[i] != 'Y')&&(buf[i] != 'I')&&(buf[i] != 'J')&&(buf[i] != 0))
      i ++;
    if (buf[i] != 0){
      chbak = buf[i];
      buf[i] = 0;
    }
    e_x = atof(&(buf[b_p+1]));
    e_x -= o_x;
    buf[i] = chbak;
    i = b_p + 1;
    while ((buf[i] != 'Y')&&(buf[i] != 0))
      i ++;
    if (buf[i] == 'Y'){
      j = b_p + 1;
      while ((buf[j] != 'I')&&(buf[j] != 'J')&&(buf[i] != 0))
	j ++;
      if (buf[j] != 0){
	chbak = buf[j];
	buf[j] = 0;
      }
      e_y = atof(&(buf[i+1]));
      e_y -= o_y;
      buf[j] = chbak;
    }
    else{
      e_y = b_y;
    }
  }
  else{
    e_x = b_x;
    e_y = atof(&(buf[b_p+1]));
    e_y -= o_y;
  }

  i = b_p + 1;
  while ((buf[i] != 'I')&&(buf[i] != 0))
    i ++;
  if (buf[i] == 'I'){
    j = b_p + 1;
    while ((buf[j] != 'J')&&(buf[i] != 0))
      j ++;
    if (buf[j] != 0){
      chbak = buf[j];
      buf[j] = 0;
    }
    I = atof(&(buf[i+1]));
    buf[j] = chbak;
  }
  else
    I = oldi;

  i = b_p + 1;
  while ((buf[i] != 'J')&&(buf[i] != 0))
    i ++;
  if (buf[i] == 'J'){
    J = atof(&(buf[i+1]));
  }
  else
    J = oldj;

  cx = I + b_x;
  cy = J + b_y;

  r = sqrt((b_x-cx)*(b_x-cx)+(b_y-cy)*(b_y-cy));
  vmax = x_once_max_v_pulses;
  amax = x_once_max_a_pulses;
  oldx = curx = b_x;
  oldy = cury = b_y;
  ba = count_angle(cx,cy,b_x,b_y);
  ea = count_angle(cx,cy,e_x,e_y);
  if (type == 2){
    if (ba > ea)
      aa = (ba+ea)/2;
    else{
      aa = (ba + 2*PI - ea)/2;
      if (aa < 0)
	aa += 2*PI;
    }
  }
  else{
    if (ea > ba){
      aa = (ba+ea)/2;
    }
    else{
      aa = (ea + 2*PI - ba)/2;
      if (aa < 0)
	aa += 2*PI;
    }
  }
  an = ba;
  ll = x_len_to_acc/xsteps;
  if (ba > ea){
    len = r*(ba-ea);
  }
  else{
    len = r*(ba + 2*PI - ea);
  }
  if (type == 3)
    len = r*2*PI - len;
  if ((fabs(ba-ea) < 0.0001) || (fabs(fabs(ba-ea)-2*PI) < 0.0001))
    len = r*2*PI;

//  if (len > 2*x_len_to_acc/xsteps){
    if (type == 2)
       aa = ba - ea;
     else
       aa = ea - ba;
     if (aa < 0.00001)
       aa += 2*PI;
     r_da = (x_once_max_v_pulses/xsteps)/r;
     r_times = (int)(((len - 2*x_len_to_acc/xsteps)/r)/r_da);
     if (r_times < 0){
       r_times = 0;
       len_to_acc = len/2;
     }
     else
       len_to_acc = r * (aa-r_times*r_da)/2;
     a_da = len_to_acc / r;
     acc_da = (x_once_max_a_pulses/xsteps)/r;
     a1 = 0;
     a2 = 0;
     end_degrees = 0;
     while (a1 < a_da){
       a2 += acc_da;
       a1 += a2;
       degree[end_degrees++] = a2;
     }
     end_degrees --;
     a1 -= a2;
     a3 = a_da - a1;
     if (end_degrees == 1){
       if (a3 < degree[0]){
	 degree[1] = degree[0];
	 degree[0] = a3;
       }
       else
	 degree[1] = a3;
       end_degrees = 2;
     }
     else{
       for (kk=1;kk<end_degrees;kk++){
	 if ((degree[kk-1] <= a3)&&(degree[kk] >= a3)){
	   for (k1=end_degrees;k1>kk;k1--)
	     degree[k1] = degree[k1-1];
	   degree[kk] = a3;
	   end_degrees++;
	   break;
	 }
       }
     }
     cur_a = ba;
     for (i=0;i<2*end_degrees+r_times;i++){
       if (type == 3){
	 if (i<end_degrees)
	   cur_a += degree[i];
	 else if (i < end_degrees+r_times)
	   cur_a = ba + a_da + (i-end_degrees+1)*r_da;
	 else
	   cur_a += degree[2*end_degrees+r_times-i-1];
       }
       else{
	 if (i<end_degrees)
	   cur_a -= degree[i];
	 else if (i < end_degrees+r_times)
	   cur_a -= r_da;
	 else
	   cur_a -= degree[2*end_degrees+r_times-i-1];
       }
       cur_x = cx + r*cos(cur_a);
       cur_y = cy + r*sin(cur_a);
       if (i == 2*end_degrees+r_times - 1){
	 cur_x = e_x;
	 cur_y = e_y;
       }
       f = (cur_x - oldx - d_x)*xsteps;
       if (f > 0)
	 x = f + 0.5;
       else
	 x = f - 0.5;
       f = (cur_y - oldy - d_y)*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);
       if (fabs(x)+fabs(y) > 0.1){
	 put_a_line(to_file,buf1);
	 oldx += x/xsteps;
	 oldy += y/ysteps;
       }
     }
//  }
//  else{

//  }

  oldi = I;
  oldj = J;
  d_x = oldx - e_x;
  d_y = oldy - e_y;
  oldx = e_x;
  oldy = e_y;
//  strcpy(buf1,"P0 0");
//  put_a_line(to_file,buf1);
}

double count_angle(double x0, double y0, double x1, double y1)
{
  double a;

  if ((x1-x0)*(x1-x0)+(y1-y0)*(y1-y0) < 1e-8)
    return 0;
  a = acos((x1-x0)/sqrt((x1-x0)*(x1-x0)+(y1-y0)*(y1-y0)));
  if (y1 < y0)
    a = 2*PI - a;
  return a;
}

recover_data()
{
  int i;

  xmaxv = xmaxv3;
  ymaxv = ymaxv3;
//  test_dog();
  zmaxv = zmaxv3;
  xmaxa = xmaxa3;
  ymaxa = ymaxa3;
  zmaxa = zmaxa3;
  xmaxv1 = xmaxv;               // Max Speed units(G CODE) per danwei
  ymaxv1 = ymaxv;
  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_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;
  }
}

set_data()
{
  int i;

  xmaxv3 = xmaxv;
  ymaxv3 = ymaxv;
  zmaxv3 = zmaxv;
  xmaxa3 = xmaxa;
  ymaxa3 = ymaxa;
  zmaxa3 = zmaxa;
  xmaxv = xmaxv2;
  ymaxv = ymaxv2;
  zmaxv = zmaxv2;
  xmaxa = xmaxa2;
  ymaxa = ymaxa2;
  zmaxa = zmaxa2;
  xmaxv1 = xmaxv;               // Max Speed units(G CODE) per danwei
  ymaxv1 = ymaxv;
  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_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;
  }
}

⌨️ 快捷键说明

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