Pages

Monday, May 19, 2025

Cutting Slots

 I mentioned the need to cut slots.  First I wrote a bash script to produce the needed gCode.  Then, because one little enhancement took me into direct conflict with bash's unobvious math comparisons, I rewrote in C.  The code is much clearer.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>

int main (int argc, char* argv[])
{
  int inDebugger = 0;
  if (argc < 12+inDebugger || argc > 12+inDebugger)
  {
    printf("wrong number of arguments\n");
    displayValidArgs();
    exit(2);
  }
  float xStart=atof(argv[1+inDebugger]);
  float yStart=atof(argv[2+inDebugger]); 
  float xEnd=atof(argv[3+inDebugger]); 
  float yEnd=atof(argv[4+inDebugger]);
  float zStart=atof(argv[5+inDebugger]);
  float zStep=atof(argv[6+inDebugger]);
  float zEnd=atof(argv[7+inDebugger]);
  float xyFeed=atof(argv[8+inDebugger]);
  float zFeed=atof(argv[9+inDebugger]);
  float millDiameter=atof(argv[10+inDebugger]);
  char* outputFileName=argv[11+inDebugger];
  FILE* outputFile=fopen(outputFileName, "w");
  if (outputFile == NULL)
  {
    printf("unable to create file %s\n", outputFileName);
    exit(2);
  }
  /* is slot wider than mill? */
  float slotWidth=yEnd-yStart;
  if (millDiameter > slotWidth)
  {
    printf("mill too wide for reequested slot\n");
    exit(2);
  }
  showArgs(argc, argv);
  float millRadius = millDiameter/2.0f;
  float xCutStart = xStart+millRadius;
  float yCutStart=yStart+millRadius;
  float xCutEnd=xEnd-millRadius;
  float yCutEnd=yEnd-millRadius;
  /* copy prolog */
  copyFile("/home/clayton/subs/prolog.nc", outputFile);
  /* raise tool */
  fprintf(outputFile, "G1 Z1 F25\n");
  fprintf(outputFile, "G1 X%0.4f Y%0.4f F25\n", xCutStart, yCutStart);
  /* lower to just above contact */
  fprintf(outputFile, "G1 Z0.05 F25\n");
  /* to contact */
  fprintf(outputFile, "G1 Z0 F25\n");
  float z;
  for (z=zStart; z > zEnd; z += zStep)
    {
      fprintf(outputFile, "G1 Z%0.4f F%0.4f\n", z, zFeed);
      float y;
      for (y = yCutStart; y < (yCutEnd - millDiameter); y += millDiameter)
    {
      fprintf(outputFile, "G1 X%0.4f Y%0.4f F%0.4f\n",
          xCutStart, y, xyFeed);
      fprintf(outputFile, "G1 X%0.4f F%0.4f\n", xCutEnd, xyFeed);
    }
      /* out of loop.  Did we complete section between y and yCutEnd? */
      if (y < yCutEnd)
    {
      y = yCutEnd - millRadius;
      fprintf(outputFile, "G1 X%0.4f Y%0.4f F%0.4f\n",
          xCutStart, yCutStart, xyFeed);
      fprintf(outputFile, "G1 X%0.4f F%0.4f\n", xCutEnd, xyFeed);
    }
    }     
  fprintf(outputFile, "G1 Z2 F25\n");
  fprintf(outputFile, "G1 x0 Y0 F25\n");
  fprintf(outputFile, "m2\n%\n");
}


displayValidArgs(void)
{
  printf("xstart ystart xend yend zstart zstep zend xyfeed zfeed milldiameter outputFilename\n");
}

showArgs(int argc, char* argv[])
{
  int i;

  for (i=0; i < argc; i++)
    {
      printf("%s ", argv[i]);
    }
  putchar('\n');
}
 
copyFile(char *srcFileName, FILE* outputFile)
{
  char copyBuffer[80];
  FILE* srcFile = fopen(srcFileName, "r");
  if (srcFile == NULL)
  {
    printf("unable to open file %s\n", srcFileName);
    exit (4);
  }
  while(NULL != fgets(copyBuffer, sizeof(copyBuffer), srcFile))
  {
    fputs(copyBuffer, outputFile);
  }
}


The prolog.nc file:

%
g17 g20 g54

Yes, I should just fprintf for those two lines.


No comments:

Post a Comment