Autor Tema: Procesador CNC de Codigo G  (Leído 53775 veces)

0 Usuarios y 5 Visitantes están viendo este tema.

Desconectado Judas_MzA

  • PIC10
  • *
  • Mensajes: 44
Re: Procesador CNC de Codigo G
« Respuesta #60 en: 29 de Marzo de 2009, 02:57:48 »
Buenas Noches... En estos días empiezo con mi CNC, asi que voy a intertar colaborar con este hilo. Aunque sea con alguna idea...
Con todos los cerebros que hay trabajando en esto algo bueno va a salir...
Saludos

Desconectado ADE

  • PIC10
  • *
  • Mensajes: 1
Re: Procesador CNC de Codigo G
« Respuesta #61 en: 30 de Marzo de 2009, 23:01:58 »
Boa noite desculpas por não falar espanhol mas quero colaborar a algum vizualizo este link para poder fazer um processador de código "G" aqui no brasil algumas pessoas já fizeram com TCC(trabalho de conclusão de curso).estou tentando passar a rotina abaixo para o CCS a rotina abaixo interpleta código "G" em dois eixos X Y feita para um torno CNC por favor me ajudem estou com o pi18f2550 enão cabe dentro dele gostaria de passar  a usar os cartões MMC abaixo segue o código do processador RABbit quero passar para o pic logo em seguida segue meu código no pic.....aguardo respostas
...
#define BINBUFSIZE   255
#define BOUTBUFSIZE  255
#define MAX         1000
#define TMAX          23
#define TMIN           4
#define TESPERA       10
#define NAO            0
#define SIM            1
#define LIGA           1
#define DESLIGA        0
#define ABSOLUTA       0
#define INCREMENTAL    1
#define RAIO           1
#define DIAMETRO       0
#define POLEGADAS      0
#define MILIMETROS     1

int sair, count, coordenadas, tipoX, medidas;
long t[TMAX], vEixo, bloco;
double k1, k2, k3, k4, k5, x, z;

int comentario(char linha[MAX], int i);
int G00(char linha[MAX], int i);
int G01(char linha[MAX], int i);
int inibeBloco(char linha[MAX], int i);
int N(char linha[MAX], int i);
void automatico();
void codigoG(char linha[MAX]);
void espera1(long i);
void espera2(int ti);
void manual();
void movG00(long xr, long zr);
void movG01(long xr, long zr, long ti);
void movPasso(int motor);
void recebeString(char linha[MAX]);
void timerb_isr();
void zeraString(char linha[MAX]);

main(){
   char aux[20];
     k1 = 80;     // k = 400/3,5 pulsos/mm
     k2 = 80;
     k3 = 80;
     k4 = 80;
     k5 = 7150;
     t[0] = 101;
     t[1] = 42;
    t[2] = 32;
     t[3] = 27;
     t[4] = 24;
     t[5] = 22;
     t[6] = 20;
     t[7] = 19;
     t[8] = 18;
     t[9] = 17;
     t[10] = 16;
     t[11] = 15;
     t[12] = 14;
     t[13] = 13;
     t[14] = 12;
     t[15] = 11;
     t[16] = 10;
     t[17] = 9;
     t[18] = 8;
     t[19] = 7;
     t[20] = 6;
     t[21] = 5;
     t[22] = 4;

     coordenadas = INCREMENTAL;
     medidas = MILIMETROS;
     tipoX = RAIO;

     WrPortI(SPCR, &SPCRShadow, 0x84);  //seta todas as portas PA como saida


   //Seta as Portas PD6 e PD7 como output para controlar o multiplexador
   WrPortI(PDFR, &PDFRShadow, 0x3f);
     WrPortI(PDDCR, &PDDCRShadow, 0x3f);
     WrPortI(PDDDR, &PDDDRShadow, 0xc0);
     //Fim do set

     //Zera todos os pinos, caso estejam em 1
     WrPortI(PADR, &PADRShadow, 0x00);
     BitWrPortI(PDDR, &PDDRShadow, 1, 6);
     BitWrPortI(PDDR, &PDDRShadow, 0, 6);
     WrPortI(PADR, &PADRShadow, 0x24);     //da ENABLE nos dois motores de passo
     BitWrPortI(PDDR, &PDDRShadow, 1, 7);
     BitWrPortI(PDDR, &PDDRShadow, 0, 7);
 
     count = 0;
   
     SetVectIntern(0x0B, timerb_isr);      // set up ISR

     WrPortI(TBCR, &TBCRShadow, 0x01);   // clock timer B with (perclk/2) and
                                       //     set interrupt level to 1

     WrPortI(TBL1R, NULL, 0x00);         // set initial match!
     WrPortI(TBM1R, NULL, 0x00);

     WrPortI(TBCSR, &TBCSRShadow, 0x03);   // enable timer B and B1 match interrupts

     x = z = 0;
     sair = NAO;
     serBopen(9600);
     serBparity(PARAM_EPARITY);
     serBdatabits(PARAM_8BIT);
     sair = NAO;
     while(sair == NAO){
      manual();
   }
     WrPortI(TBCSR, &TBCSRShadow, 0x00);   // disable timer B and its interrupts
     serBclose();
     WrPortI(PADR, &PADRShadow, 0x00);
     BitWrPortI(PDDR, &PDDRShadow, 1, 6);
     BitWrPortI(PDDR, &PDDRShadow, 0, 6);
     BitWrPortI(PDDR, &PDDRShadow, 1, 7);
     BitWrPortI(PDDR, &PDDRShadow, 0, 7);
}

void zeraString(char linha[MAX]){
  int i;
  for(i=0; i < MAX; i++)
    linha = 0;
}

void recebeString(char linha[MAX]){
   int c;
   int i;
   i = 0;
   c = 0;
   while (c != '?' && i < MAX) {  // Sai com o caracter ?
      c = -1;
      while ((c = serBgetc()) == -1);
      linha = c;
      i++;
   }
}

void movPasso(int motor){
  BitWrPortI(PADR, &PADRShadow, 1, motor);  //envia um pulso para o motor
  BitWrPortI(PDDR, &PDDRShadow, 1, 7);
  BitWrPortI(PDDR, &PDDRShadow, 0, 7);
  BitWrPortI(PADR, &PADRShadow, 0, motor);  //volta a zero
  BitWrPortI(PDDR, &PDDRShadow, 1, 7);
  BitWrPortI(PDDR, &PDDRShadow, 0, 7);
}

void manual(){
  char c, aux[MAX];
  long k;
  int ti;
  double v;
  ti = 100;
  while(sair == NAO){
    k=0;
    c = serBgetc();
    if(c == 'A'){
        BitWrPortI(PADR, &PADRShadow, 1, 0);
       BitWrPortI(PDDR, &PDDRShadow, 1, 7);
       BitWrPortI(PDDR, &PDDRShadow, 0, 7);
        while(c != 'P'){  // Move motor x em sentido positivo em VMAX
          movPasso(1);
          espera1(k);
           k = k+1;
          c = serBgetc();
        }
    x = x + (double)(k)/k1;
    }
    if(c == 'B'){
       BitWrPortI(PADR, &PADRShadow, 0, 0);
        BitWrPortI(PDDR, &PDDRShadow, 1, 7);
       BitWrPortI(PDDR, &PDDRShadow, 0, 7);
       while(c != 'P'){  //Move motor x em sentido negativo em VMAX
         movPasso(1);
         espera1(k);
           k = k+1;
         c = serBgetc();
        }
        x = x - (double)(k)/k1;
     }
     if(c == 'C'){
       BitWrPortI(PADR, &PADRShadow, 1, 3);
       BitWrPortI(PDDR, &PDDRShadow, 1, 7);
       BitWrPortI(PDDR, &PDDRShadow, 0, 7);
        while(c != 'P'){ //Move motor z em sentido positivo em VMAX
         movPasso(4);
          espera1(k);
          k = k+1;
        c = serBgetc();
      }
       z = z + (double)(k)/k2;
    }
    if(c == 'D'){
       BitWrPortI(PADR, &PADRShadow, 0, 3);
      BitWrPortI(PDDR, &PDDRShadow, 1, 7);
      BitWrPortI(PDDR, &PDDRShadow, 0, 7);
       while(c != 'P'){  //Move motor z em sentido negativo em VMAX
         movPasso(4);
         espera1(k);
         k = k+1;
         c = serBgetc();
       }
       z = z - (double)(k)/k2;
    }
    if(c == 'E'){
       BitWrPortI(PADR, &PADRShadow, 1, 0);
       BitWrPortI(PDDR, &PDDRShadow, 1, 7);
       BitWrPortI(PDDR, &PDDRShadow, 0, 7);
       while(c != 'P'){  // Move motor x em sentido positivo em V
         movPasso(1);
         espera2(ti);
         k = k + 1;
         c = serBgetc();
       }
       x = x + (double)(k)/k3;
    }
    if(c == 'F'){
       BitWrPortI(PADR, &PADRShadow, 0, 0);
       BitWrPortI(PDDR, &PDDRShadow, 1, 7);
       BitWrPortI(PDDR, &PDDRShadow, 0, 7);
       while(c != 'P'){  //Move motor x em sentido negativo em V
         movPasso(1);
         espera2(ti);
         k = k + 1;
         c = serBgetc();
       }
       x = x - (double)(k)/k3;
    }
    if(c == 'G'){
       BitWrPortI(PADR, &PADRShadow, 1, 3);
       BitWrPortI(PDDR, &PDDRShadow, 1, 7);
       BitWrPortI(PDDR, &PDDRShadow, 0, 7);
       while(c != 'P'){ //Move motor z em sentido positivo em V
         movPasso(4);
         espera2(ti);
         k = k + 1;
         c = serBgetc();
       }
       z = z + (double)(k)/k4;
    }
    if(c == 'H'){
       BitWrPortI(PADR, &PADRShadow, 0, 3);
       BitWrPortI(PDDR, &PDDRShadow, 1, 7);
       BitWrPortI(PDDR, &PDDRShadow, 0, 7);
       while(c != 'P'){  //Move motor z em sentido negativo em V
         movPasso(4);
         espera2(ti);
         k = k + 1;
         c = serBgetc();
       }
       z = z - (double)(k)/k4;
    }

    if(c == 'I'){
      recebeString(aux);
      v = atof(aux);
      ti = (int)(k5/(400*v));
    }
   
    if(c == 'Y')  // modo automático
      automatico();

    if(c == 'Z')
      sair = SIM;
  }
}

void espera1(long i){
  int j;
  if(i >= TMAX){
    count = 0;
    while(count < TMIN);
  }
  else{
    j = (int)(i);
    count = 0;
    while(count < t[j]);
  }
}

void espera2(int ti){
  count = 0;
  while(count < ti);
}

void movG00(long xr, long zr){
  long k;
  double e, l;
  int sentx, sentz;
 
  sentx = sentz = 1;
 
  if(xr >= 0){
    BitWrPortI(PADR, &PADRShadow, 0, 0);
    BitWrPortI(PDDR, &PDDRShadow, 1, 7);
    BitWrPortI(PDDR, &PDDRShadow, 0, 7);
  }
  else{
    sentx = -1;
    xr = -xr;
    BitWrPortI(PADR, &PADRShadow, 1, 0);
    BitWrPortI(PDDR, &PDDRShadow, 1, 7);
    BitWrPortI(PDDR, &PDDRShadow, 0, 7);
  }
  if(zr >= 0){
    BitWrPortI(PADR, &PADRShadow, 1, 3);
    BitWrPortI(PDDR, &PDDRShadow, 1, 7);
    BitWrPortI(PDDR, &PDDRShadow, 0, 7);
  }
  else{
    sentz = -1;
    zr = -zr;
    BitWrPortI(PADR, &PADRShadow, 0, 3);
    BitWrPortI(PDDR, &PDDRShadow, 1, 7);
    BitWrPortI(PDDR, &PDDRShadow, 0, 7);
  }
  if(xr >= zr){
    l = (double)(zr)/xr;
    e = l - .5;
    k = 0;
    while(k < xr){
      if(e > 0){
        e = e - 1;
        movPasso(4);
        z = z + (double)(sentz)/k2;
      }
      k++;
      e = e + l;
      movPasso(1);
      x = x + (double)(sentx)/k1;
      if(k < xr/2)
        espera1(k);
      else
        espera1(xr-k);
    }
  }
  else{
    l = (double)(xr)/zr;
    e = l - .5;
    k = 0;
    while(k < zr){
      if(e > 0){
        e = e - 1;
        movPasso(1);
        x = x + (double)(sentx)/k1;
      }
      k = k + 1;
      e = e + l;
      movPasso(4);
      z = z + (double)(sentz)/k2;
      if(k < zr/2)
        espera1(k);
      else
        espera1(zr-k);
    }
  }
}

void movG01(long xr, long zr, long ti){
  int k, tl;
  double e, l;
  int sentx, sentz;
 
  sentx = sentz = 1;
 
  if(xr >= 0){
    BitWrPortI(PADR, &PADRShadow, 0, 0);
    BitWrPortI(PDDR, &PDDRShadow, 1, 7);
    BitWrPortI(PDDR, &PDDRShadow, 0, 7);
  }
  else{
    sentx = -1;
    xr = -xr;
    BitWrPortI(PADR, &PADRShadow, 1, 0);
    BitWrPortI(PDDR, &PDDRShadow, 1, 7);
    BitWrPortI(PDDR, &PDDRShadow, 0, 7);
  }
  if(zr >= 0){
    BitWrPortI(PADR, &PADRShadow, 1, 3);
    BitWrPortI(PDDR, &PDDRShadow, 1, 7);
    BitWrPortI(PDDR, &PDDRShadow, 0, 7);
  }
  else{
    sentz = -1;
    zr = -zr;
    BitWrPortI(PADR, &PADRShadow, 0, 3);
    BitWrPortI(PDDR, &PDDRShadow, 1, 7);
    BitWrPortI(PDDR, &PDDRShadow, 0, 7);
  }

  if(xr >= zr){
    l = (double)(zr)/xr;
    tl = (int)(ti/xr);
    e = l - .5;
    k = 0;
    while(k < xr){
      if(e > 0){
        e = e - 1;
        movPasso(4);
        z = z + (double)(sentz)/k4;
      }
      k++;
      e = e + l;
      movPasso(1);
      x = x + (double)(sentx)/k3;
      espera2(tl);
    }
  }
  else{
    l = (double)(xr)/zr;
    tl = (int)(ti/zr);
    e = l - .5;
    k = 0;
    while(k < zr){
      if(e > 0){
        e = e - 1;
        movPasso(1);
        x = x + (double)(sentx)/k3;
      }
      k = k + 1;
      e = e + l;
      movPasso(4);
      z = z + (double)(sentz)/k4;
      espera2(tl);
    }
  }
}

void automatico(){
  char linha[MAX];
  while(sair == NAO){
     recebeString(linha);
       codigoG(linha);
  }     
}

#asm
timerb_isr::
   push   af                     ; save registers
   push   hl

   ld      hl, (count)
   inc   hl                     ; increment counter
   ld      (count), hl

   ld      a, 00h
   ioi   ld (TBL1R), a         ; set up next B1 match (at timer=0000h)
   ioi   ld (TBM1R), a         ; NOTE:  you _need_ to reload the match
                              ;   register after every interrupt!

done:
   ioi   ld a, (TBCSR)         ; load B1, B2 interrupt flags (clears flag)

   pop   hl                     ; restore registers
   pop   af
   
   ipres                        ; restore interrupts
   ret                        ; return
#endasm

void codigoG(char linha[MAX]){
   int i, n;
   n = strlen(linha);
   i = 0;
   while(i < n){
      if(linha == 'G' || linha == 'g'){
         i++;
         if(linha == ' '){ //G00
            i++;
            i = G00(linha, i);
         }
         else{
            if(linha == '0'){
               i++;
               if(linha == ' '){ //G00
                  i++;
                  i = G00(linha, i);
               }
               else{
                  if(linha == '0'){ //G00
                     i++;
                     i = G00(linha, i);
                  }
                  else{
                     if(linha == '1'){ //G01
                        i++;
                        i = G01(linha, i);
                     }
                     else
                        i++;
                  }
               }
            }
            else{
               if(linha == '1'){ //G01
                  i++;
                  i = G01(linha, i);
               }
               else{
                  if(linha == '2'){
                     i++;
                     if(linha == '0'){ //G20
                        i++;
                        tipoX = DIAMETRO;
                     }
                     else{
                        if(linha == '1'){ //G21
                           i++;
                           tipoX = RAIO;
                        }
                        else
                           i++;
                     }
                  }
                  else{
                     if(linha == '7'){
                        i++;
                        if(linha == '0'){ //G70
                           i++;
                           medidas = POLEGADAS;
                        }
                        else{
                           if(linha == '1'){ //G71
                              i++;
                              medidas = MILIMETROS;
                           }
                           else
                              i++;
                        }
                     }
                     else{
                        if(linha == '9'){
                           i++;
                           if(linha == '0'){ //G90
                              i++;
                              coordenadas = ABSOLUTA;
                           }
                           else{
                              if(linha == '1'){ //G91
                                 i++;
                                 coordenadas = INCREMENTAL;
                              }
                              else
                                 i++;
                           }
                        }
                        else
                           i++;
                     }
                  }
               }
            }
         }
      } //Fim do G
      else{
         if(linha == 'N' || linha == 'n'){
            i++;
            i = N(linha, i);
         } //Fim do N
         else{
            if(linha == '/'){
               i++;
               i = inibeBloco(linha, i);
            }
            else{
               if(linha == ';'){
                  i++;
                  i = comentario(linha, i);
               }
               else{
                  if(linha == 'M' || linha == 'm'){
                     i++;
                     if(linha == '0'){
                        i++;
                        if(linha == '2'){ //M02
                           i++;
                           sair = SIM;
                        }
                        else
                           i++;
                     }
                  }
                  else
                     i++;
               }
            }
         }
      }
   }
}

int comentario(char linha[MAX], int i){
   int k;
   for(k = 0; linha[i+k] != '#'; k++);
   return i+k;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int G00(char linha[MAX], int i){
   int k, j;
   double xr, zr, xr2, zr2;
   char aux[MAX];
   int xi, zi;
   long xr3, zr3;
   
   k = 0;
   xi = zi = NAO;
   while(linha[i+k] != 'G' && linha[i+k] != 'g' && linha[i+k] != 'M' && linha[i+k] != 'm' && sair == NAO && linha[i+k] != '?'){
      if(linha[i+k] == 'N'){
         k++;
         k = N(linha, i+k) - i;
      }
      else{
         if(linha[i+k] == 'X' || linha[i+k] == 'x'){
            k++;
            j = 0;
            zeraString(aux);
            while(linha[i+k+j] != '.' && linha[i+k+j] != ' ' && linha[i+k+j] != '#'){
               aux[j] = linha[i+k+j];
               j++;
            }
            k = k+j;
            if(linha[i+k] == '.')
               xr = atof(aux);
            else
               xr = atof(aux)/10000;
            if(medidas == POLEGADAS)
               xr = 2.54*xr;
            if(tipoX == DIAMETRO)
               xr = xr/2;
            xi = SIM;
         }
         else{
            if(linha[i+k] == 'Z' || linha[i+k] == 'z'){
               k++;
               j = 0;
               zeraString(aux);
               while(linha[i+k+j] != '.' && linha[i+k+j] != ' ' && linha[i+k+j] != '#'){
                  aux[j] = linha[i+k+j];
                  j++;
               }
               k = k+j;
               if(linha[i+k] == '.')
                  zr = atof(aux);
               else
                  zr = atof(aux)/10000;
               if(medidas == POLEGADAS)
                  zr = 2.54*zr;
               zi = SIM;
            }
            else{
               if(linha[i+k] == '#'){
                  if(xi == SIM)
                     xr2 = xr;
                  else
                     xr2 = 0;
                  if(zi == SIM)
                     zr2 = zr;
                  else
                     zr2 = 0;
                  if(coordenadas == ABSOLUTA){
                        xr2 = xr2 - x;
                        zr2 = zr2 - z;
                  }
                  xr3 = (long)(k3*xr2);
                  zr3 = (long)(k4*zr2);
                  movG00(xr3, zr3);
                  xr = zr = 0;
                  k++;
                  xi = zi = NAO;
               }
               else
                  k++;
            }
         }
      }
   }
   return i+k;
}

int G01(char linha[MAX], int i){
   int k, j;
   long fr3;
   double xr, zr, fr, xr2, zr2, fr2;
   char aux[MAX];
   int xi, zi, fi;
   long xr3, zr3;
   
   k = 0;
   fr = 0;
   xi = zi = fi = NAO;
   while(linha[i+k] != 'G' && linha[i+k] != 'g' && linha[i+k] != 'M' && linha[i+k] != 'm' && sair == NAO && linha[i+k] != '?'){
      if(linha[i+k] == 'N'){
         k++;
         k = N(linha, i+k) - i;
      }
      else{
         if(linha[i+k] == 'X' || linha[i+k] == 'x'){
            k++;
            j = 0;
            zeraString(aux);
            while(linha[i+k+j] != '.' && linha[i+k+j] != ' ' && linha[i+k+j] != '#'){
               aux[j] = linha[i+k+j];
               j++;
            }
            k = k+j;
            if(linha[i+k] == '.')
               xr = atof(aux);
            else
               xr = atof(aux)/10000;
            if(medidas == POLEGADAS)
               xr = 2.54*xr;
            if(tipoX == DIAMETRO)
               xr = xr/2;
            xi = SIM;
         }
         else{
            if(linha[i+k] == 'Z' || linha[i+k] == 'z'){
               k++;
               j = 0;
               zeraString(aux);
               while(linha[i+k+j] != '.' && linha[i+k+j] != ' ' && linha[i+k+j] != '#'){
                  aux[j] = linha[i+k+j];
                  j++;
               }
               k = k+j;
               if(linha[i+k] == '.')
                  zr = atof(aux);
               else
                  zr = atof(aux)/10000;
               if(medidas == POLEGADAS)
                  zr = 2.54*zr;
               zi = SIM;
            }
            else{
               if(linha[i+k] == 'F' || linha[i+k] == 'f'){
                  k++;
                  j = 0;
                  zeraString(aux);
                  while(linha[i+k+j] != '.' && linha[i+k+j] != ' ' && linha[i+k+j] != '#'){
                     aux[j] = linha[i+k+j];
                     j++;
                  }
                  k = k+j;
                  if(linha[i+k] == '.')
                     fr = atof(aux);
                  else
                     fr = atof(aux)/10000;
                  if(medidas == POLEGADAS)
                     fr = 2.54*fr;
                  fi = SIM;
               }
               else{
                  if(linha[i+k] == '#'){
                     if(xi == SIM)
                        xr2 = xr;
                     else
                        xr2 = 0;
                     if(zi == SIM)
                        zr2 = zr;
                     else
                        zr2 = 0;
                     if(coordenadas == ABSOLUTA){
                           xr2 = xr2 - x;
                           zr2 = zr2 - z;
                     }
                     if(fi == SIM)
                        fr2 = sqrt(xr2*xr2+zr2*zr2)/fr;
                     xr3 = (int)(k3*xr2);
                     zr3 = (int)(k4*zr2);
                     fr3 = (long)(k5*fr2);
                     movG01(xr3, zr3, fr3);
                     xr = zr = 0;
                     k++;
                     xi = zi = NAO;
                  }
                  else
                     k++;
               }
            }
         }
      }
   }
   return i+k;
}

int inibeBloco(char linha[MAX], int i){
   int k;
   for(k = 0; linha[i+k] != '#'; k++)
   return i+k;
}

int N(char linha[MAX], int i){
   int k;
   char aux[MAX];

   for(k = 0; linha[i+k] != ' ' && linha[i+k] != '#'; k++)
      aux[k] = linha;

   bloco = atol(linha);
   return i+k;
}
...

meu código.....
...
#include<18F2550.h >
#include<usart.c>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#use delay(clock=2000000)
#fuses XT,PUT,NOWDT,NOBROWNOUT,NOLVP
//#include <lcd1.c>
#define BINBUFSIZE   255
#define BOUTBUFSIZE  255
#define MAX         1000
#define TMAX          23
#define TMIN           4
#define TESPERA       10
#define NAO            0
#define SIM            1
#define LIGA           1
#define DESLIGA        0
#define ABSOLUTA       0
#define INCREMENTAL    1
#define RAIO           1
#define DIAMETRO       0
#define POLEGADAS      0
#define MILIMETROS     1

int sair, count, coordenadas, tipoX, medidas;//counter
int t[TMAX], vEixo, bloco;
int k1, k2, k3, k4, k5, x, z;
/////////////////////////////////////////////////////////////////////////////////////////////////
#int_TIMER0
void TIMER0(void)
{

count++;
}

/////////////////////////////////////////////////////////////////////////////////////////////////

void zeraString(char linha[MAX]){
  char i;
  for(i=0;i<MAX;i++){
    linha = 0;
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////
void espera1(int i){
  int j;
  if(i >= TMAX){
    count = 0;
    while(count < TMIN);
  }
  else{
    j = (int)(i);
    count = 0;
    while(count < t[j]);
  }
}
////////////////////////////////////////////////////////////////////////////////////////////////
void espera2(int ti){
  count = 0;
  while(count < ti);
}
//////////////////////////////////////////////////////////////////////
void recebeString(char linha[MAX]){
   char c;
   int i;
   i = 0;
   c = 0;
   while (c != '?' && i < MAX) {  // Sai com o caracter ?
      c = 1;
      while ((c = usart_recebe()) == 1);//RECEBE COMUNICAÇAÕ DA USART
      linha = c;
      i++;
   }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////
void movPasso(char motor){
if(motor==1){
  output_high(PIN_B0);//RODA MOTOR "X"
  output_low(PIN_B0);                                                   
  }
if(motor==4){
  output_high(PIN_B1);//RODA MOTOR "Y"
  output_low(PIN_B1);                                                     
  }
}                     
////////////////////////////////////////////////////////////////////////////////////////////////////// /

/////////////////////////////////////////////////////////////////
int inibeBloco(char linha[MAX], int i){
   int k;
   for(k = 0; linha[i+k] != '#'; k++)
   return i+k;
}
////////////////////////////////////////////////////////
int comentario(char linha[MAX], int i){
   int k;
   for(k = 0; linha[i+k] != '#'; k++);
   return i+k;
}
////////////////////////////////////////////////////////
void movG00(long xr, long zr){
  signed int k;
  signed int e, l;
  signed int sentx, sentz;
 
  sentx = sentz = 1;
 
  if(xr >= 0){
       output_low(PIN_B1);
   
  }
  else{
    sentx = -1;
    xr = -xr;
       output_high(PIN_B1);
   
  }
  if(zr >= 0){
      output_high(PIN_B2);
   
  }
  else{
    sentz = -1;
    zr = -zr;
   output_low(PIN_B2);
 
  }
  if(xr >= zr){
    l = (int)(zr)/xr;
    e = l - .5;
    k = 0;
    while(k < xr){
      if(e > 0){
        e = e - 1;
        movPasso(4);
        z =z +(int)(sentz)/k2;
      }
      k++;
      e = e + l;
      movPasso(1);
      x = x +(int)sentx/k1;
      if(k < xr/2)
        espera1(k);
      else
        espera1(xr-k);
    }
  }
  else{
     l = (int)(xr)/zr;
    e = l - .5;
    k = 0;
    while(k < zr){
      if(e > 0){
        e = e - 1;
        movPasso(1);
        x = x +(int) (sentx)/k1;
      }
      k = k + 1;
      e = e + l;
      movPasso(4);
      z = z + (int)(sentz)/k2;
      if(k < zr/2)
        espera1(k);
      else
        espera1(zr-k);
    }
  }
}
//////////////////////////////////////////////////////////
void movG01(long xr, long zr, long ti){
  int k, tl;
  long e, l;
  char sentx, sentz;
 
  sentx = sentz = 1;
 
  if(xr >= 0){
   output_low(PIN_B1);
   
  }
  else{
    sentx = 2;//substituido -1 por 2
    xr = -xr;
   output_high(PIN_B1);
   
  }
  if(zr >= 0){
   output_high(PIN_B2);
   
  }
  else{
    sentz = 2;//substituido -1 por 2
    zr = -zr;
   output_low(PIN_B2);
   
  }

  if(xr >= zr){
    l = (int)(zr)/xr;
    tl = (int)(ti/xr);
    e = l - .5;
    k = 0;
    while(k < xr){
      if(e > 0){
        e = e - 1;
        movPasso(4);
        z =z +(int)(sentz)/k4;
      }
      k++;
      e = e + l;
      movPasso(1);
      x = x +(int) (sentx)/k3;
      espera2(tl);
    }
  }
  else{
    l = (int)(xr)/zr;
    tl = (int)(ti/zr);
    e = l - .5;
    k = 0;
    while(k < zr){
      if(e > 0){
        e = e - 1;
        movPasso(1);
        x = x +(int) (sentx)/k3;
      }
      k = k + 1;
      e = e + l;
      movPasso(4);
      z =z +(int)(sentz)/k4;
      espera2(tl);
    }
  }
}
/////////////////////////////////////////////////////////////////////////////////////////////
int N(char linha[MAX], int i){
   int k;
   char aux[MAX];

   for(k = 0; linha[i+k] != ' ' && linha[i+k] != '#'; k++)
      aux[k] = linha;

   bloco = atol(linha);
   return i+k;
}
////////////////////////////////////////////////////////////////////////////////////////////////
int G01(char linha[MAX], int i){
   int32 k, j;
   int32 fr3;
   long xr, zr, fr, xr2, zr2, fr2;
   char aux[MAX];
   int xi, zi, fi;
   int32 xr3, zr3;
   
   k = 0;
   fr = 0;
   xi = zi = fi = NAO;
   while(linha[i+k] != 'G' && linha[i+k] != 'g' && linha[i+k] != 'M' && linha[i+k] != 'm' && sair == NAO && linha[i+k] != '?'){
      if(linha[i+k] == 'N'){
         k++;
         k = N(linha, i+k) - i;
      }
      else{
         if(linha[i+k] == 'X' || linha[i+k] == 'x'){
            k++;
            j = 0;
            zeraString(aux);
            while(linha[i+k+j] != '.' && linha[i+k+j] != ' ' && linha[i+k+j] != '#'){
               aux[j] = linha[i+k+j];
               j++;
            }
            k = k+j;
            if(linha[i+k] == '.')
               xr = atof(aux);
            else
               xr = atof(aux)/10000;
            if(medidas == POLEGADAS)
               xr = 2.54*xr;
            if(tipoX == DIAMETRO)
               xr = xr/2;
            xi = SIM;
         }
         else{
            if(linha[i+k] == 'Z' || linha[i+k] == 'z'){
               k++;
               j = 0;
               zeraString(aux);
               while(linha[i+k+j] != '.' && linha[i+k+j] != ' ' && linha[i+k+j] != '#'){
                  aux[j] = linha[i+k+j];
                  j++;
               }
               k = k+j;
               if(linha[i+k] == '.')
                  zr = atof(aux);
               else
                  zr = atof(aux)/10000;
               if(medidas == POLEGADAS)
                  zr = 2.54*zr;
               zi = SIM;
            }
            else{
               if(linha[i+k] == 'F' || linha[i+k] == 'f'){
                  k++;
                  j = 0;
                  zeraString(aux);
                  while(linha[i+k+j] != '.' && linha[i+k+j] != ' ' && linha[i+k+j] != '#'){
                     aux[j] = linha[i+k+j];
                     j++;
                  }
                  k = k+j;
                  if(linha[i+k] == '.')
                     fr = atof(aux);
                  else
                     fr = atof(aux)/10000;
                  if(medidas == POLEGADAS)
                     fr = 2.54*fr;
                  fi = SIM;
               }
               else{
                  if(linha[i+k] == '#'){
                     if(xi == SIM)
                        xr2 = xr;
                     else
                        xr2 = 0;
                     if(zi == SIM)
                        zr2 = zr;
                     else
                        zr2 = 0;
                     if(coordenadas == ABSOLUTA){
                           xr2 = xr2 - x;
                           zr2 = zr2 - z;
                     }
                     if(fi == SIM)//////////////////////////PARTE PRINCIPAL DA INTERPOLAÇÃO///
                        fr2 = sqrt(xr2*xr2+zr2*zr2)/fr;
                     xr3 = (k3*xr2);
                     zr3 = (k4*zr2);
                     fr3 = (k5*fr2);
                     movG01(xr3, zr3, fr3);
                     xr = zr = 0;
                     k++;
                     xi = zi = NAO;
                  }
                  else
                     k++;
               }
            }
         }
      }
   }
   return i+k;
}
/////////////////////////////////////////////////////////
int G00(char linha[MAX], int i){
   int k, j;
   long xr, zr, xr2, zr2;
   char aux[MAX];
   int xi, zi;
   long xr3, zr3;
   
   k = 0;
   xi = zi = NAO;
   while(linha[i+k] != 'G' && linha[i+k] != 'g' && linha[i+k] != 'M' && linha[i+k] != 'm' && sair == NAO && linha[i+k] != '?'){
      if(linha[i+k] == 'N'){
         k++;
         k = N(linha, i+k) - i;
      }
      else{
         if(linha[i+k] == 'X' || linha[i+k] == 'x'){
            k++;
            j = 0;
            zeraString(aux);
            while(linha[i+k+j] != '.' && linha[i+k+j] != ' ' && linha[i+k+j] != '#'){
               aux[j] = linha[i+k+j];
               j++;
            }
            k = k+j;
            if(linha[i+k] == '.')
               xr = atof(aux);
            else
               xr = atof(aux)/10000;
            if(medidas == POLEGADAS)
               xr =2.54*xr;
            if(tipoX == DIAMETRO)
               xr = xr/2;
            xi = SIM;
         }
         else{
            if(linha[i+k] == 'Z' || linha[i+k] == 'z'){
               k++;
               j = 0;
               zeraString(aux);
               while(linha[i+k+j] != '.' && linha[i+k+j] != ' ' && linha[i+k+j] != '#'){
                  aux[j] = linha[i+k+j];
                  j++;
               }
               k = k+j;
               if(linha[i+k] == '.')
                  zr = atof(aux);
               else
                  zr = atof(aux)/10000;
               if(medidas == POLEGADAS)
                  zr = 2.54*zr;
               zi = SIM;
            }
            else{
               if(linha[i+k] == '#'){
                  if(xi == SIM)
                     xr2 = xr;
                  else
                     xr2 = 0;
                  if(zi == SIM)
                     zr2 = zr;
                  else
                     zr2 = 0;
                  if(coordenadas == ABSOLUTA){
                        xr2 = xr2 - x;
                        zr2 = zr2 - z;
                  }
                  xr3 =(k3*xr2);
                  zr3 = (k4*zr2);
                  movG00(xr3, zr3);
                  xr = zr = 0;
                  k++;
                  xi = zi = NAO;
               }
               else
                  k++;
            }
         }
      }
   }
   return i+k;
}
//////////////////////////////////////////////////////////

void codigoG(char linha[MAX]){
   int i, n;
   n = strlen(linha);
   i = 0;
   while(i < n){
      if(linha == 'G' || linha == 'g'){
         i++;
         if(linha == ' '){ //G00
            i++;
            i = G00(linha, i);
         }
         else{
            if(linha == '0'){
               i++;
               if(linha == ' '){ //G00
                  i++;
                  i = G00(linha, i);
               }
               else{
                  if(linha == '0'){ //G00
                     i++;
                     i = G00(linha, i);
                  }
                  else{
                     if(linha == '1'){ //G01
                        i++;
                        i = G01(linha, i);
                     }
                     else
                        i++;
                  }
               }
            }
            else{
               if(linha == '1'){ //G01
                  i++;
                  i = G01(linha, i);
               }
               else{
                  if(linha == '2'){
                     i++;
                     if(linha == '0'){ //G20
                        i++;
                        tipoX = DIAMETRO;
                     }
                     else{
                        if(linha == '1'){ //G21
                           i++;
                           tipoX = RAIO;
                        }
                        else
                           i++;
                     }
                  }
                  else{
                     if(linha == '7'){
                        i++;
                        if(linha == '0'){ //G70
                           i++;
                           medidas = POLEGADAS;
                        }
                        else{
                           if(linha == '1'){ //G71
                              i++;
                              medidas = MILIMETROS;
                           }
                           else
                              i++;
                        }
                     }
                     else{
                        if(linha == '9'){
                           i++;
                           if(linha == '0'){ //G90
                              i++;
                              coordenadas = ABSOLUTA;
                           }
                           else{
                              if(linha == '1'){ //G91
                                 i++;
                                 coordenadas = INCREMENTAL;
                              }
                              else
                                 i++;
                           }
                        }
                        else
                           i++;
                     }
                  }
               }
            }
         }
      } //Fim do G
      else{
         if(linha == 'N' || linha == 'p'){
            i++;
            i = N(linha,i);
         } //Fim do N


         else{
            if(linha == '/'){
               i++;
               i = inibeBloco(linha, i);
            }
            else{
               if(linha == ';'){
                  i++;
                  i = comentario(linha, i);
               }
               else{
                  if(linha == 'M' || linha == 'm'){
                     i++;
                     if(linha == '0'){
                        i++;
                        if(linha == '2'){ //M02
                           i++;
                           sair = SIM;
                        }
                        else
                           i++;
                     }
                  }
                  else
                     i++;
               }
            }
         }
      }
   }
}

////////////////////////////////////////////////////////////////

void automatico(){
  char linha[MAX];
  while(sair == NAO){
     recebeString(linha);
       codigoG(linha);
  }     
}

///////////////////////////////////////////////////////////////////
void manual(){
  char c, aux[MAX];
  int k;
  int ti;
  int v;
  ti = 100;
     while(sair == NAO){
     k=0;
    c = usart_recebe();
    if(c == 'A'){
         output_high(PIN_B1);
      
        while(c != 'P'){  // Move motor x em sentido positivo em VMAX
          movPasso(1);
          espera1(k);
           k = k+1;
           c = usart_recebe();
        }
    x = x +(int) k/k1;
    }
    if(c == 'B'){
         output_low(PIN_B1);
       
       while(c != 'p'){  //Move motor x em sentido negativo em VMAX
         movPasso(1);
          k = k+1;
            espera1(k);
           
          c = usart_recebe();
        }
        x = x-(int) k/k1;//48
     }
     if(c == 'C'){
        output_high(PIN_B2);
       
        while(c != 'P'){ //Move motor z em sentido positivo em VMAX
         movPasso(4);
          espera1(k);
          k = k+1;
         c = usart_recebe();
      }
       z = z +(int) k/k2;
    }
    if(c == 'D'){
       output_low(PIN_B2);
      
       while(c != 'P'){  //Move motor z em sentido negativo em VMAX
         movPasso(4);
         espera1(k);
         k = k+1;
          c = usart_recebe();
       }
       z =z -(int) k/k2;
    }
    if(c == 'E'){
    output_high(PIN_B1);
   
       while(c != 'P'){  // Move motor x em sentido positivo em V
         movPasso(1);
         espera2(ti);
         k = k + 1;
          c = usart_recebe();
       }
       x = x +(int) k/k3;
    }
    if(c == 'F'){
       output_low(PIN_B1);
       
       while(c != 'P'){  //Move motor x em sentido negativo em V
         movPasso(1);
         espera2(ti);
         k = k + 1;
         c = usart_recebe();
       }
       x =x -(long) k/k3;
    }
    if(c == 'G'){
       output_high(PIN_B2);
   
       while(c != 'P'){ //Move motor z em sentido positivo em V
         movPasso(4);
         espera2(ti);
         k = k + 1;
       c = usart_recebe();
       }
       z = z +(long) k/k4;
    }
    if(c == 'H'){
       output_low(PIN_B1);
       
       while(c != 'P'){  //Move motor z em sentido negativo em V
         movPasso(4);
         espera2(ti);
         k = k + 1;
         c = usart_recebe();
       }
       z =z -(long) k/k4;
    }

    if(c == 'I'){
      recebeString(aux);
      v = atof(aux);
      ti = k5/(400*v);
    }
   
    if(c == 'Y')  // modo automático
      automatico();

    if(c == 'Z')
      sair = SIM;
  }
}
/////////////////////////////////////////////////////////////////////////////////////////////////
void main(void){
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256|RTCC_8_BIT);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);
set_tris_b(0x00);//define a direçao do portB
usart_inicializa(12,1);//INICIA USART
//char aux[20];
     k1 = 80;     // k = 400/3,5 pulsos/mm
     k2 = 80;
     k3 = 80;
     k4 = 80;
     k5 = 7150;
     t[0] = 101;
     t[1] = 42;
    t[2] = 32;
     t[3] = 27;
     t[4] = 24;
     t[5] = 22;
     t[6] = 20;
     t[7] = 19;
     t[8] = 18;
     t[9] = 17;
     t[10] = 16;
     t[11] = 15;
     t[12] = 14;
     t[13] = 13;
     t[14] = 12;
     t[15] = 11;
     t[16] = 10;
     t[17] = 9;
     t[18] = 8;
     t[19] = 7;
     t[20] = 6;
     t[21] = 5;
     t[22] = 4;

     coordenadas = INCREMENTAL;
     medidas = MILIMETROS;
     tipoX = RAIO;
    count = 0;
     x = z = 0;
     sair = NAO;
    while(sair == NAO){
      manual();
   }
 }

...
ADEMILSON ANTUNES
ademilsonantunes@uol.com.br

Desconectado ema

  • Colaborador
  • PIC24H
  • *****
  • Mensajes: 1078
Re: Procesador CNC de Codigo G
« Respuesta #62 en: 30 de Marzo de 2009, 23:20:30 »
Bienvenido al foro ADE, te comento que en este foro vas a encontrar mucha ayuda, pero no creo que alguien se lea todo este codigo y te diga en que estas fallando o que haga el trabajo por ti.
Te recomiendo que lo separes de acuerdo a la funcionalidad de cada parte del código y ademas comentes cada algunas lineas.

Saludos


Desconectado NANO1985

  • Colaborador
  • PIC24H
  • *****
  • Mensajes: 1698
    • Desarrollos Tecnologicos - Tucuman - Argentina
Re: Procesador CNC de Codigo G
« Respuesta #63 en: 11 de Mayo de 2010, 14:28:45 »
coindido con ema jajajaja  :D ....
qu lindo proycto ést.... lastima que se haya estancado...  :(
voy a ver si puerdo remontar con algo!
"La inquebrantable voluntad de vencer"
"hay dos cosas infinitas... El universo y la Estupidez humana" Albert Einstein
 "El sabio actua sin anhelos, permanece sosegado,... así no es afectado por el resultado de sus acciones sean éstas el triunfo o el fracaso"
- UNIVERSIDAD TECNOLOGICA NACIONAL - FACULTAD REGIONAL TUCUMAN -

Desconectado REL

  • PIC10
  • *
  • Mensajes: 2
Re: Procesador CNC de Codigo G
« Respuesta #64 en: 31 de Mayo de 2010, 23:22:44 »
Aiiii, estoy llegando muy tarde a este foro :( porque yo estaba tratando por mis medios de hacer algo asi... ahora mismo estoy en proyecto de hacerme una fresadora de 4 ejes, pero en cuanto pueda voy a intentar dar mi aporte para hacer un interprete de G.


 

anything