Motor shield code

This code calculates a ratio between feed and spindle, then for each step on the spindle, if a step is required on the feed, it's sent. Basically it takes spindle / feed and subtracts the sum of the last value from the sum of the current value. If it's an integer, feed motor will step. This assumes there are more spindle steps than feed steps. Later code allows for more feed than spindle.

Also, once I used this code on the big machine, I had to change most of the int variables to longs.


//
#include <AFMotor.h>
//*************************************
//these are the variables to change.
float spiralLen = .1;  //length of the spiral
float pitch = 10;     //pitch -- revs per inch
//**************************************
float  factor;   
int spindleRev = 200; //steps on the spindle motor
int feedRev = 48;     //steps on the feed motor
int screwPitch = 20;  //pitch on the feedscrew. don't change without changing hardware



AF_Stepper motor1(spindleRev, 1);
AF_Stepper motor2(feedRev, 2);

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps


  motor1.setSpeed(150);
  motor2.setSpeed(150);

}

void loop() {
  int start = 0;
  int i = 0;
  int y = 0;
  int z;
  int h = 1;
  int spindleLoc = 0;
  int feedLoc = 0;
  int quit = 0;
  factor = screwPitch/pitch;

  z = calcLen(); // how many steps total
  h = 1;
  if (Serial.available() > 0) {
    start = Serial.read();
  }

  delay(500);
  if(start == 49) { 
    while (h <= z) {

      y = calcY(i); //figure out if a feed step is required
      h = h + y;
      //Serial.println(h);
      i = i + 1;

      motor1.step(1, FORWARD, SINGLE); //one spindle step
      spindleLoc = spindleLoc + 1; //keeping track of spindle location
      if(spindleLoc == 201) { //wrap
        spindleLoc = 1;
      }
      motor2.step(y, FORWARD, DOUBLE); //send a feed step y (y may be zero, no step)
      feedLoc = feedLoc + y; //keep track of feed location
      if (Serial.available() > 0) {
        quit = Serial.read();
      }
      if(quit == 50) {
        h = z;
      }
      Serial.print(h); //output location to screen -- this REALLY slows things down.
      Serial.print(" ");
      Serial.print(z);
      Serial.print(" -- rotation: ");
      Serial.print(spindleLoc);
      Serial.print("  feed: ");
      Serial.println(feedLoc);
    }
    start = 0;
  }
}

int calcRot(int step){
  if(step == 200){
    return 0;
  }
}
int calcLen(){
  int x;
  x = spiralLen * screwPitch * feedRev;
  return x;
}

int calcY(int v){ //here's the heart of the code. Calculate whether a feed step is required for a spindle step.
  float fr = float(feedRev);
  float sr = float(spindleRev);
  int m;
  int j;
  float k;
  k = fr/sr*factor;

  m = v * k;
  j = (v-1) * k;
  j = m- j;
  return j;
}

No comments:

Post a Comment

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