Final Arduino code -- big nut

Here's the last Arduino code I used just prior to pulling the Arduino out and converting the machine to Mach3. This was used to cut the big bolt and nut.

The hilarious thing is all this Arduino code can be replaced by this single line of gcode in Mach3:
 "G1 X20 Z-.25 A1800"

// big lathe using Probotix controllers and motors. LOW = forward & clockwise
//*************************************
//these are the variables to change for the part.
float spiralLen = 17;  //length of the spiral on part being cut
float pitch = 2.5;     //pitch -- revs per inch on part being cut
int rpm = 40; //tweak as needed for performance
//**************************************

//motor setup
int spindleMotorRevs = 200; //steps on the spindle motor
int feedMotorRevs = 200;     //steps on the feed motor
int spindleMSteps = 4; //microstepping for the spindle motor
int feedMSteps = 8; //microstepping for the feed motor
int spindleRev; //actual movement on the machine1
int feedRev;
float spindleGearRatio = 4; //gear ratio for spindle == 4:1 reduction
float feedGearRatio = 1; //gear ratio for feed
float feedStepsPerRev; // feed steps per spindle rev
long spindleLoc = 0;
long feedLoc = 0;

//thread setup
int leadPitch = 5;  //pitch on the feedscrew. don't change without changing hardware
float  pitchRatio;  //ration of the hardware pitch to the part pitch
float motorRatio;
int uSDelay;

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  pinMode(3, OUTPUT);  //feed dir
  pinMode(4, OUTPUT);  //feed step
  pinMode(5, OUTPUT);  //spindle dir
  pinMode(6, OUTPUT);  //spindle step
  spindleRev = spindleMotorRevs * spindleMSteps * spindleGearRatio; // calc true spindleRev
  feedRev = feedMotorRevs * feedMSteps * feedGearRatio;  //calc true feedrev
  pitchRatio = leadPitch / pitch;
  motorRatio = float(feedRev) / float(spindleRev);
  feedStepsPerRev = motorRatio * pitchRatio * spindleRev;
 
  //Serial.println(feedStepsPerRev);
 
  uSDelay = getDelay(rpm, feedRev);
  //int start = 0; //start command
  //long i = 0;
  //long y = 0;
 // long z;//
  //long h = 1;
  //long spindleLoc = 0;
  //long feedLoc = 0;
  //int quit = 0;


 //z = calcLen();
}

void loop() {



 int start = 0; //start command
 long i = 0;
 long y = 0;
 long z;
 long h = 1;

 int quit = 0;
 long lastDisplay;
 float spindleAngle;
 float feedInches;
 float spindleAngleRatio;
 float feedInchesRatio;


//Serial.print("uSDelay  ");
//Serial.println(uSDelay);
 z = calcLen();

 h = 1;
if (Serial.available() > 0) {
  start = Serial.read();
}
digitalWrite(3, LOW); //LOW IS away from headstock (forward)
digitalWrite(5, HIGH); //HIGH IS clockwise


delay(100);
if(start == 48 + 1) {
   while (h <= z) {

     y = calcY(i);
     h = h + y;
     i = i + 1;
   
     digitalWrite(6, HIGH);
     digitalWrite(6, LOW);
    //Serial.println(y);
     if(y>0) {  // if feed  steps > 1, loop through until next feed step
       for(int x = 1; x <= y; x++){
       digitalWrite(4, HIGH);
       digitalWrite(4, LOW);
       //Serial.println(y);
       feedLoc = feedLoc + 1;
       if(x == y) { break;}
       delayMicroseconds(uSDelay);}
     }
     spindleLoc = spindleLoc + 1;
     if(spindleLoc >= spindleRev) {spindleLoc = spindleLoc - spindleRev;}
     //Serial.println(spindleLoc);
     //Serial.println(spindleRev);
     //spindleAngle = getSpindleAngle(spindleLoc);
     delayMicroseconds(uSDelay);
     if (Serial.available() > 0) {quit = Serial.read();}
     if(quit == 48 + 2) {
        break;
      }
   
     if(lastDisplay + 250 < millis()) {
       spindleAngle = getSpindleAngle(spindleLoc);
       //Serial.println(feedLoc);
       feedInches = getFeedInches(feedLoc);
       //Serial.print("Feed: ");
       //Serial.print(feedInches);
       //Serial.print(" in. -- Spindle: ");
       Serial.println(spindleAngle);
       lastDisplay = millis();  
     }
   }
 start = 0;
 }

 if(start == 48 + 3) {
   //zeroFeed(634400);
   zeroFeed(feedLoc);
 }


if(start == 48 + 4) {
  zeroSpindle(spindleLoc);
}
}


 
long calcLen(){
  long x;
  x = spiralLen * leadPitch * feedRev;
  return x;
}
 


int calcY(long v){ // calculate feed step
  float r;
  long m;
  long j;
  float k;
  r = floor(v * feedStepsPerRev / spindleRev);
  //Serial.println(spindleRev);
  k = floor((v-1) * feedStepsPerRev / spindleRev);
  m = floor(r - k);
  return m;
}
 
 
 
  int getDelay(float RPM, float motorSteps) {
    float uS = 0;
    uS = 1000000/((RPM/60) * motorSteps);
    return floor(uS);
  }
 
  float getFeedInches(long loc) {
    float m;
    float j;
    m = feedRev * leadPitch;
    //Serial.println(loc);
    j = float(loc) / m;
    return j;
  }

 float getSpindleAngle(long loc) {
    float m;
    float j;
    if(loc == 0) {return 0;}
    m = 360 / float(spindleRev);
    j = float(loc) * m;
    return j;
 }

 void zeroFeed(long loc) {
   long i;
   if(loc ==0) {return;}
   if(loc > 0) {
     digitalWrite(3, HIGH);
     for(i = loc; i >0; i--){ 
        digitalWrite(4, HIGH);
        digitalWrite(4, LOW);
        delayMicroseconds(175);
     }
   }
    else if(loc < 0) {
      digitalWrite(3, LOW);
      for(i = loc; i < 0; i++){ 
        digitalWrite(4, HIGH);
        digitalWrite(4, LOW);
        delayMicroseconds(175);
     }
    }
 }

void zeroSpindle(int loc) {
  long i;
  if(loc == 0) {return;}
  digitalWrite(5, LOW);
  for(i = loc; i > 0; i--){
      digitalWrite(6, HIGH);
      digitalWrite(6, LOW);
      delay(10);
}  
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.