Big thread code



// big lathe using Probotix controllers and motors. LOW = forward & clockwise
//still trying to figure out whether I need to invert the step signal. Both ways seem to work
//*************************************
//these are the variables to change for the part.
float spiralLen = 30;  //length of the spiral on part being cut
float pitch = 2.5;     //pitch -- revs per inch on part being cut
int rpm = 15; //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 = 13;  //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);

}

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;

  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, LOW);
      digitalWrite(6, HIGH);

      if(y>0) {  // if feed  steps > 1, loop through until next feed step
        for(int x = 1; x <= y; x++){
          digitalWrite(4, LOW);
          digitalWrite(4, HIGH);

          feedLoc = feedLoc + 1;
          if(x == y) {
            break;
          }
          delayMicroseconds(uSDelay);
        }
      }
      spindleLoc = spindleLoc + 1;
      if(spindleLoc >= spindleRev) {
        spindleLoc = spindleLoc - spindleRev;
      }
      delayMicroseconds(uSDelay);
      if (Serial.available() > 0) {
        quit = Serial.read();
      }
      if(quit == 48 + 2) {
        break;
      }

      if(lastDisplay + 250 < millis()) {
        spindleAngle = getSpindleAngle(spindleLoc);
        feedInches = getFeedInches(feedLoc);
        Serial.println(spindleAngle);
        lastDisplay = millis();   
      }
    }
    start = 0;
  }

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


  if(start == 48 + 4) {
    spindleLoc = 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);
  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;
  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;
}

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


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

No comments:

Post a Comment

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