Go Down

Topic: Controlling NEMA 34 motor, for audio indexing system  (Read 602 times) previous topic - next topic

flytyer232

Hello,

I am still working on an audio indexing system, for a punching machine that i am making for school. i am using AccelStepper to ramp up/down the stepper motor. I have done alot of work with AccelStepper, in getting this bit of code to move the motor forward and back

Code: [Select]

int moveMobileCylinderForward( float distanceInInches )
{
  long stepsToMove = distanceInInches*stepsPerInch;
  long deccel_step = stepsToMove-1000;

  //move forward
   stepper.moveTo(stepsToMove);
   while (stepper.currentPosition() != deccel_step) // Full speed up to 300
   {
     stepper.run();
   }
   stepper.stop(); // Stop as fast as possible: sets new target
   stepper.runToPosition();
  // Now stopped after quickstop

}

int moveMobileCylinderBackward( float distanceInInches )
{
  long stepsToMove = distanceInInches*stepsPerInch;
  long deccel_step = stepsToMove-1000;
 

  //move forward
   stepper.moveTo(-stepsToMove); // negative because moving backwards
   while (stepper.currentPosition() != 0) // Full speed back to 0
   {
     stepper.run();
   }
   stepper.stop(); // Stop as fast as possible: sets new target
   stepper.runToPosition();
}




Next, in order for the operator to interface with the system, and the system to move the motor back and forth discrete distances, i need to come up with an interface/control system. I decided to use Sanwa buttons, as they have been used quite a bit, and was able to figure that bit of code out. The problem being, when I incorporate the two bits of code, the control system works, however the Arduino isn't giving a the required output signal for the motor. The code is below. Any help would be very, very much appreciated.


flytyer232

Here is the full code, forgot to put on post:

Code: [Select]

// "Select" button allows user to select which profile to punch.
// "Start" button starts punching the profile (or pauses the machine if it is already punching).
// "Reset" button lets user tell machine that the workpiece has been removed.

#include "AccelStepper.h"  //use "" and no <>, b/c accelstepper is a librabry that i defined

class pneumaticClamp
{
  int myPin;
  
  // Constructor
  public:
  pneumaticClamp( int pin )
  {
  myPin = pin;
  }
  void clamp()
  {
    digitalWrite( myPin, HIGH );
  }
  void unClamp()
  {
    digitalWrite( myPin, LOW );
  }
};


class punch
{
  int myPin;
  
  // Constructor
  public:
  punch( int pin )
  {
  myPin = pin;
  }
  void punchHole()
  {
    digitalWrite( myPin, HIGH );
    delay( 1000 ); // Give it time to punch through the material
    digitalWrite( myPin, LOW );
  }
};
//board stuff
const int serialBaudRate = 9600; // [bps]
const int oneTimeStep = 33;  // Smallest timestep in milliseconds

//pin assignments
const int selectButtonPin = 2; // Number of pin connected to "select" button
const int startButtonPin = 3; // Number of pin connected to "start" button
const int resetButtonPin = 4; // Number of pin connected to "start" button
const int mechStopPins[] = { 10, 11, 12, 13 };
const int mobileClampPin = 8;
const int staticClampPin = 9;
const int alertPin = 6;
const int punchPin = 7;

//constants for motor movement
float stepsPerInch = 2272.727;

//profile checking
bool punchingAProfile  = false;
bool workpieceNeedsToBeRemoved = false;

bool selectIsDown      = false;
bool selectWasDown     = false;
bool selectFreshlyUp   = false;

bool startIsDown       = false;
bool startWasDown      = false;
bool startFreshlyUp    = false;

bool resetIsDown       = false;
bool resetWasDown      = false;
bool resetFreshlyUp    = false;

const int numDistancesPerProfile = 21;
//hole patterns, in inches
float distanceBetweenConsecutiveHoles[ 4 ][ numDistancesPerProfile ] = {
                                                                       { 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 0, 0, 0,0 },  //IS-8-bottom rail
                                                                       { 4.000, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 0, 0, 0, 0, 0, 0, 0,0 },                  //IS-6-bottom rail
                                                                       { 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364}, //IL-8-botoom rail
                                                                       { 3.273, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 0, 0, 0, 0, 0}       //IL-6-bottom rail
                                                                     }; // a list of the intervals between consecutive holes for each profile.  If 0, that means there is no next hole.
int chosenProfile = 0; // 0: 6S, 1: 6L, 2: 8S, 3: 8L



pneumaticClamp mobileClamp( mobileClampPin );
pneumaticClamp staticClamp( staticClampPin );
punch unicyl( punchPin );

//stepper motor intilization, for accel stepper
AccelStepper stepper(1,0,1); //pin 9 =pluse, pin 8=direction


void setup()
{
  pinMode( mechStopPins[0], OUTPUT );
  pinMode( mechStopPins[1], OUTPUT );
  pinMode( mechStopPins[2], OUTPUT );
  pinMode( mechStopPins[3], OUTPUT );
  pinMode( mobileClampPin, OUTPUT );
  pinMode( punchPin, OUTPUT);
  pinMode( staticClampPin, OUTPUT );
  pinMode( alertPin, OUTPUT );
  pinMode( selectButtonPin, INPUT );
  pinMode( startButtonPin,  INPUT );
  pinMode( resetButtonPin,  INPUT );
  
  Serial.begin( serialBaudRate );



  //for stepper motor control
  stepper.setMaxSpeed(2000);    //in steps per second
  stepper.setAcceleration(1400); //in steps per second
}

void loop()
{
  selectIsDown = digitalRead( selectButtonPin );
  startIsDown  = digitalRead( startButtonPin  );
  resetIsDown  = digitalRead( resetButtonPin  );
  
  if ( selectWasDown && !selectIsDown ){ selectFreshlyUp = true; }
  if ( startWasDown  && !startIsDown  ){ startFreshlyUp  = true; }
  if ( resetWasDown  && !resetIsDown  ){ resetFreshlyUp  = true; }
 
  digitalWrite( mechStopPins[0], LOW );
  digitalWrite( mechStopPins[1], LOW );
  digitalWrite( mechStopPins[2], LOW );
  digitalWrite( mechStopPins[3], LOW );
  
  if ( punchingAProfile ){
    // if punching a profile
    
    punchProfile();
    punchingAProfile = false;
    workpieceNeedsToBeRemoved = true;
    digitalWrite( alertPin, HIGH );
  }
  else
  {
    // if not punching a profile
    if ( workpieceNeedsToBeRemoved )
    {
      //see if reset button was freshly pressed and if so, set this variable to false.
      if ( resetFreshlyUp ){
        workpieceNeedsToBeRemoved = false;
        digitalWrite( alertPin, LOW );
      }
    }
    else
    {
      if ( selectFreshlyUp ){
        if ( chosenProfile == 3 ){
         chosenProfile = 0;
        }
        else{ chosenProfile++;
        }
      }
  
      if ( startFreshlyUp ){
      
        // Switch into "punching a profile" mode
        punchingAProfile = true;
      
      }
      digitalWrite( mechStopPins[ chosenProfile ], HIGH );
    }
  }
    
  selectWasDown = selectIsDown;
  startWasDown  = startIsDown;
  resetWasDown  = resetIsDown;
  startFreshlyUp  = false;
  selectFreshlyUp = false;
  resetFreshlyUp  = false;
  delay( oneTimeStep );
}


void punchProfile()
{
  mobileClamp.clamp();
  staticClamp.clamp();
  delay( 2000 ); // Give clamps time to clamp securely
  unicyl.punchHole();
  for (int i=0; i < numDistancesPerProfile; i++){
    int distanceToMove = distanceBetweenConsecutiveHoles[ chosenProfile ][ i ];
    if ( distanceToMove != 0 )
    {
      moveWorkpieceForward( distanceToMove );
      unicyl.punchHole();  
    }
  }
  mobileClamp.unClamp();
  staticClamp.unClamp();
}

void moveWorkpieceForward( int distanceToMove )
{
  //Serial.print( "Moving workpiece " );
  //Serial.print( distanceToMove );
  staticClamp.clamp();
  mobileClamp.unClamp();
  
  Serial.print( "mClamp -> " );
  Serial.print( distanceToMove );
  moveMobileCylinderForward(distanceToMove); //moving forward
  Serial.print( ". " );
  delay( 1000*distanceToMove ); // REMOVE
  
  mobileClamp.clamp();
  staticClamp.unClamp();
  
  Serial.print( "mClamp <- " );
  Serial.print( distanceToMove );
  moveMobileCylinderBackward(distanceToMove );  //moving backward
  Serial.print( ". " );
  
  delay( 1000*distanceToMove ); //REMOVE
  staticClamp.clamp();
  delay(2000); //REMOVE
  
}

int moveMobileCylinderForward( float distanceInInches )
{
  long stepsToMove = distanceInInches*stepsPerInch;
  long deccel_step = stepsToMove-1000;

  //move forward
   stepper.moveTo(stepsToMove);
   while (stepper.currentPosition() != deccel_step) // Full speed up to 300
   {
     stepper.run();
   }
   stepper.stop(); // Stop as fast as possible: sets new target
   stepper.runToPosition();
  // Now stopped after quickstop

}

int moveMobileCylinderBackward( float distanceInInches )
{
  long stepsToMove = distanceInInches*stepsPerInch;
  long deccel_step = stepsToMove-1000;
  

  //move forward
   stepper.moveTo(-stepsToMove); // negative because moving backwards
   while (stepper.currentPosition() != 0) // Full speed back to 0
   {
     stepper.run();
   }
   stepper.stop(); // Stop as fast as possible: sets new target
   stepper.runToPosition();
}




Robin2

You can't use delay() in a program that uses accelstepper. And your calls to stepper.run() should be in loop(), not inside a function. They need to be called all the time and as often as possible - hundreds or thousands of times per second..

Have a look at how millis() is used to manage timing without blocking in Several things at a time.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

flytyer232

On the first part, i took the delays out, and found a couple errors that i have since took out, and it works! The table moves forward and back, according to the profile, as seen in the code.

I am still really struggling to grasp what you are recommending to do with the millis(). According to the examples, i can pretty much use it like a delay? What would the structure of that look like?

Code: [Select]

// "Select" button allows user to select which profile to punch.
// "Start" button starts punching the profile (or pauses the machine if it is already punching).
// "Reset" button lets user tell machine that the workpiece has been removed.

#include "AccelStepper.h"  //use "" and no <>, b/c accelstepper is a librabry that i defined

class pneumaticClamp
{
  int myPin;
 
  // Constructor
  public:
  pneumaticClamp( int pin )
  {
  myPin = pin;
  }
  void clamp()
  {
    digitalWrite( myPin, HIGH );
  }
  void unClamp()
  {
    digitalWrite( myPin, LOW );
  }
};


class punch
{
  int myPin;
 
  // Constructor
  public:
  punch( int pin )
  {
  myPin = pin;
  }
  void punchHole()
  {
    digitalWrite( myPin, HIGH );
    delay( 1000 ); // Give it time to punch through the material
    digitalWrite( myPin, LOW );
  }
};
//board stuff
const int serialBaudRate = 9600; // [bps]
const int oneTimeStep = 33;  // Smallest timestep in milliseconds

//pin assignments
const int selectButtonPin = 4; // Number of pin connected to "select" button
const int startButtonPin = 5; // Number of pin connected to "start" button
const int resetButtonPin = 6; // Number of pin connected to "start" button
const int mechStopPins[] = { 10, 11, 12, 13 };
const int mobileClampPin = 8;
const int staticClampPin = 9;
const int alertPin = 0;
const int punchPin = 7;



//constants for motor movement
float stepsPerInch = 2272.727;

//profile checking
bool punchingAProfile  = false;
bool workpieceNeedsToBeRemoved = false;

bool selectIsDown      = false;
bool selectWasDown     = false;
bool selectFreshlyUp   = false;

bool startIsDown       = false;
bool startWasDown      = false;
bool startFreshlyUp    = false;

bool resetIsDown       = false;
bool resetWasDown      = false;
bool resetFreshlyUp    = false;

const int numDistancesPerProfile = 21;
//hole patterns, in inches
float distanceBetweenConsecutiveHoles[ 4 ][ numDistancesPerProfile ] = {
                                                                       { 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 0, 0, 0,0 },  //IS-8-bottom rail
                                                                       { 4.000, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 0, 0, 0, 0, 0, 0, 0,0 },                  //IS-6-bottom rail
                                                                       { 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364}, //IL-8-botoom rail
                                                                       { 3.273, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 0, 0, 0, 0, 0}       //IL-6-bottom rail
                                                                     }; // a list of the intervals between consecutive holes for each profile.  If 0, that means there is no next hole.
int chosenProfile = 0; // 0: 6S, 1: 6L, 2: 8S, 3: 8L



pneumaticClamp mobileClamp( mobileClampPin );
pneumaticClamp staticClamp( staticClampPin );
punch unicyl( punchPin );

//stepper motor intilization, for accel stepper
AccelStepper stepper(1,3,2); //pin 3 =pluse, pin 2=direction


void setup()
{
  pinMode( mechStopPins[0], OUTPUT );
  pinMode( mechStopPins[1], OUTPUT );
  pinMode( mechStopPins[2], OUTPUT );
  pinMode( mechStopPins[3], OUTPUT );
  pinMode( mobileClampPin, OUTPUT );
  pinMode( punchPin, OUTPUT);
  pinMode( staticClampPin, OUTPUT );
  pinMode( alertPin, OUTPUT );
  pinMode( selectButtonPin, INPUT );
  pinMode( startButtonPin,  INPUT );
  pinMode( resetButtonPin,  INPUT );
 
  Serial.begin( serialBaudRate );



  //for stepper motor control
  stepper.setMaxSpeed(2000);    //in steps per second
  stepper.setAcceleration(1400); //in steps per second
}

void loop()
{
  selectIsDown = digitalRead( selectButtonPin );
  startIsDown  = digitalRead( startButtonPin  );
  resetIsDown  = digitalRead( resetButtonPin  );
 
  if ( selectWasDown && !selectIsDown ){ selectFreshlyUp = true; }
  if ( startWasDown  && !startIsDown  ){ startFreshlyUp  = true; }
  if ( resetWasDown  && !resetIsDown  ){ resetFreshlyUp  = true; }
 
  digitalWrite( mechStopPins[0], LOW );
  digitalWrite( mechStopPins[1], LOW );
  digitalWrite( mechStopPins[2], LOW );
  digitalWrite( mechStopPins[3], LOW );
 
  if ( punchingAProfile ){
    // if punching a profile
   
    punchProfile();
    punchingAProfile = false;
    workpieceNeedsToBeRemoved = true;
    digitalWrite( alertPin, HIGH );
  }
  else
  {
    // if not punching a profile
    if ( workpieceNeedsToBeRemoved )
    {
      //see if reset button was freshly pressed and if so, set this variable to false.
      if ( resetFreshlyUp ){
        workpieceNeedsToBeRemoved = false;
        digitalWrite( alertPin, LOW );
      }
    }
    else
    {
      if ( selectFreshlyUp ){
        if ( chosenProfile == 3 ){
         chosenProfile = 0;
        }
        else{ chosenProfile++;
        }
      }
 
      if ( startFreshlyUp ){
     
        // Switch into "punching a profile" mode
        punchingAProfile = true;
     
      }
      digitalWrite( mechStopPins[ chosenProfile ], HIGH );
    }
  }
   
  selectWasDown = selectIsDown;
  startWasDown  = startIsDown;
  resetWasDown  = resetIsDown;
  startFreshlyUp  = false;
  selectFreshlyUp = false;
  resetFreshlyUp  = false;
  delay( oneTimeStep );
}


void punchProfile()
{
  mobileClamp.clamp();
  staticClamp.clamp();
  delay( 2000 ); // Give clamps time to clamp securely
  unicyl.punchHole();
  for (int i=0; i < numDistancesPerProfile; i++){
    int distanceToMove = distanceBetweenConsecutiveHoles[ chosenProfile ][ i ];
    if ( distanceToMove != 0 )
    {
      moveWorkpieceForward( distanceToMove );
      unicyl.punchHole(); 
    }
  }
  mobileClamp.unClamp();
  staticClamp.unClamp();
}

void moveWorkpieceForward( int distanceToMove )
{
  int xa=0;
  int xb=0;
  //Serial.print( "Moving workpiece " );
  //Serial.print( distanceToMove );
  staticClamp.clamp();
  mobileClamp.unClamp();
 
  Serial.print( "mClamp -> " );
  Serial.print( distanceToMove );
  xa = moveMobileCylinderForward(distanceToMove); //moving forward
  Serial.print( ". " );
 // delay( 1000*distanceToMove ); // REMOVE
 
  mobileClamp.clamp();
  staticClamp.unClamp();
 
  Serial.print( "mClamp <- " );
  Serial.print( distanceToMove );
  xb = moveMobileCylinderBackward(distanceToMove );  //moving backward
  Serial.print( ". " );
 
  //delay( 1000*distanceToMove ); //REMOVE
  staticClamp.clamp();
  //delay(2000); //REMOVE
 
}

int moveMobileCylinderForward( float distanceInInches )
{
  long stepsToMove = distanceInInches*stepsPerInch;
  long deccel_step = stepsToMove-1000;

  //move forward
   stepper.moveTo(stepsToMove);
   while (stepper.currentPosition() != deccel_step) // Full speed up to 300
   {
     stepper.run();
   }
   stepper.stop(); // Stop as fast as possible: sets new target
   stepper.runToPosition();
  // Now stopped after quickstop

}

int moveMobileCylinderBackward( float distanceInInches )
{
  long stepsToMove = -1*distanceInInches*stepsPerInch; //want to make this negative, so
  long deccel_step = stepsToMove-1000;
 

  //move forward
   stepper.moveTo(stepsToMove); // has to be negative because moving backwards
   while (stepper.currentPosition() != 0) // Full speed back to 0
   {
     stepper.run();
   }
   stepper.stop(); // Stop as fast as possible: sets new target
   stepper.runToPosition();
}


flytyer232

Also,
I do have stepper.run in a loop, a while loop to be exact, which is within the function. It seams to work, but how would i put this in a different loop, as you recommend?


Code: [Select]


void moveWorkpieceForward( int distanceToMove )
{
  int xa=0;
  int xb=0;
  //Serial.print( "Moving workpiece " );
  //Serial.print( distanceToMove );
  staticClamp.clamp();
  mobileClamp.unClamp();
  
  Serial.print( "mClamp -> " );
  Serial.print( distanceToMove );
  xa = moveMobileCylinderForward(distanceToMove); //moving forward
  Serial.print( ". " );
 // delay( 1000*distanceToMove ); // REMOVE
  
  mobileClamp.clamp();
  staticClamp.unClamp();
  
  Serial.print( "mClamp <- " );
  Serial.print( distanceToMove );
  xb = moveMobileCylinderBackward(distanceToMove );  //moving backward
  Serial.print( ". " );
  
  //delay( 1000*distanceToMove ); //REMOVE
  staticClamp.clamp();
  //delay(2000); //REMOVE
  
}

int moveMobileCylinderForward( float distanceInInches )
{
  long stepsToMove = distanceInInches*stepsPerInch;
  long deccel_step = stepsToMove-1000;

  //move forward
   stepper.moveTo(stepsToMove);
   while (stepper.currentPosition() != deccel_step) // Full speed up to 300
   {
     stepper.run();
   }
   stepper.stop(); // Stop as fast as possible: sets new target
   stepper.runToPosition();
  // Now stopped after quickstop

}

int moveMobileCylinderBackward( float distanceInInches )
{
  long stepsToMove = -1*distanceInInches*stepsPerInch; //want to make this negative, so
  long deccel_step = stepsToMove-1000;
  

  //move forward
   stepper.moveTo(stepsToMove); // has to be negative because moving backwards
   while (stepper.currentPosition() != 0) // Full speed back to 0
   {
     stepper.run();
   }
   stepper.stop(); // Stop as fast as possible: sets new target
   stepper.runToPosition();
}




jremington

#5
Dec 06, 2017, 07:49 pm Last Edit: Dec 06, 2017, 07:55 pm by jremington
Quote
According to the examples, i can pretty much use it like a delay? What would the structure of that look like?
Study the simple "blink without delay" example that comes with the Arduino software package.

The following code is blocking and is a bad idea. Nothing else can run until it finishes. You need to learn new program strategies (exemplified by "blink without delay" and more sophisticated examples like "how to do several things at the same time") for controlling a process.

Code: [Select]
  while (stepper.currentPosition() != deccel_step) // Full speed up to 300
   {
     stepper.run();
   }
   stepper.stop(); // Stop as fast as possible: sets new target
   stepper.runToPosition();

Robin2

Also,
I do have stepper.run in a loop, a while loop to be exact, which is within the function. It seams to work, but how would i put this in a different loop, as you recommend?
I did not mean any old different loop. I mean in the function that is called loop(). Something as simple as

Code: [Select]
void loop() {
  // all your other code
  myStepper.run()
}


Quote
I am still really struggling to grasp what you are recommending to do with the millis(). According to the examples, i can pretty much use it like a delay? What would the structure of that look like?
You can use millis() to obtain a similar effect to a delay() but the way it is done is different. The structure is in my examples.

Using millis() is much the same as using your kitchen wall clock to time the cooking of a chicken. Using delay() is the equivalent of sitting watching the oven for 90 minutes - which you would not dream of doing. Instead you note the time when the chicken goes in the oven and periodically check the clock to see if the 90 minutes has elapsed.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

flytyer232

Okay, guys, i spent quite a bit of time trying to understand and use millis(). I did implement a version of that code, however ran into some issues. Looking back, these issue persisted in the code without using millis() as well.

Basically, the stepper motor is going forward and back, but is coming back further than it is going forward.and the weird thing is this only happens for the first hole of the profile? i.e. the second profile return to the same point, as with the remaining 20 profiles.

I took all my "control" code out, hooked it up to the stepper motor and driver, and had the same issue?

I know i am supposed to have the stepper.run() function in the main loop of the program, however i am not quite there with my understanding of program. what do you guys think is causing this issue?

Here is the motor movement code:
Code: [Select]

void moveWorkpieceForward( float distanceToMove )
{
  int xa=0;
  int xb=0;
  //Serial.print( "Moving workpiece " );
  //Serial.print( distanceToMove );
  staticClamp.clamp();
  mobileClamp.unClamp();
 
  Serial.print( "mClamp -> " );
  Serial.print( distanceToMove );
  xa = moveMobileCylinderForward(distanceToMove); //moving forward
  Serial.print( ". " );
  delay( 1000 );
  //delay( 1000*distanceToMove ); // REMOVE
 
  mobileClamp.clamp();
  staticClamp.unClamp();
 
  Serial.print( "mClamp <- " );
  Serial.print( distanceToMove );
  xb = moveMobileCylinderBackward(distanceToMove );  //moving backward
  Serial.print( ". " );
  delay( 1000 );
  //delay( 1000*distanceToMove ); //REMOVE
  staticClamp.clamp();
  //delay(2000); //REMOVE
 
}

int moveMobileCylinderForward( float distanceInInches )
{
  long stepsToMove = distanceInInches*stepsPerInch;
  long deccel_step = stepsToMove-1000;

  //move forward
   stepper.moveTo(stepsToMove);
   while (stepper.currentPosition() != stepsToMove) // Full speed up to 300
   {
     stepper.run();
   }
   stepper.stop(); // Stop as fast as possible: sets new target
   stepper.runToPosition();
  // Now stopped after quickstop

}

int moveMobileCylinderBackward( float distanceInInches )
{
  long stepsToMove = -1*distanceInInches*stepsPerInch;
  long deccel_step = stepsToMove-1000;
 

  //move forward
   stepper.moveTo(stepsToMove); // negative because moving backwards
   while (stepper.currentPosition() != 0) // Full speed back to 0
   {
     stepper.run();
   }
   stepper.stop(); // Stop as fast as possible: sets new target
   stepper.runToPosition();
}void moveWorkpieceForward( float distanceToMove )
{
  int xa=0;
  int xb=0;
  //Serial.print( "Moving workpiece " );
  //Serial.print( distanceToMove );
  staticClamp.clamp();
  mobileClamp.unClamp();
 
  Serial.print( "mClamp -> " );
  Serial.print( distanceToMove );
  xa = moveMobileCylinderForward(distanceToMove); //moving forward
  Serial.print( ". " );
  delay( 1000 );
  //delay( 1000*distanceToMove ); // REMOVE
 
  mobileClamp.clamp();
  staticClamp.unClamp();
 
  Serial.print( "mClamp <- " );
  Serial.print( distanceToMove );
  xb = moveMobileCylinderBackward(distanceToMove );  //moving backward
  Serial.print( ". " );
  delay( 1000 );
  //delay( 1000*distanceToMove ); //REMOVE
  staticClamp.clamp();
  //delay(2000); //REMOVE
 
}

int moveMobileCylinderForward( float distanceInInches )
{
  long stepsToMove = distanceInInches*stepsPerInch;
  long deccel_step = stepsToMove-1000;

  //move forward
   stepper.moveTo(stepsToMove);
   while (stepper.currentPosition() != stepsToMove) // Full speed up to 300
   {
     stepper.run();
   }
   stepper.stop(); // Stop as fast as possible: sets new target
   stepper.runToPosition();
  // Now stopped after quickstop

}

int moveMobileCylinderBackward( float distanceInInches )
{
  long stepsToMove = -1*distanceInInches*stepsPerInch;
  long deccel_step = stepsToMove-1000;
 

  //move forward
   stepper.moveTo(stepsToMove); // negative because moving backwards
   while (stepper.currentPosition() != 0) // Full speed back to 0
   {
     stepper.run();
   }
   stepper.stop(); // Stop as fast as possible: sets new target
   stepper.runToPosition();
}



 

flytyer232

Also,
Here is my code as a whole, without using millis(). I figure i should try to debig this one first, as it is the simpler of the two codes. Additionally, i really appreciate it the responses and help from you guys.
Code: [Select]

// "Select" button allows user to select which profile to punch.
// "Start" button starts punching the profile (or pauses the machine if it is already punching).
// "Reset" button lets user tell machine that the workpiece has been removed.

#include "AccelStepper.h"  //use "" and no <>, b/c accelstepper is a librabry that i defined

class pneumaticClamp
{
  int myPin;
  // Constructor
  public:
  pneumaticClamp( int pin )
  {
  myPin = pin;
  }
  void clamp()
  {
    digitalWrite( myPin, HIGH );
  }
  void unClamp()
  {
    digitalWrite( myPin, LOW );
  }
};
class punch
{
  int myPin;
  // Constructor
  public:
  punch( int pin )
  {
  myPin = pin;
  }
  void punchHole()
  {
    digitalWrite( myPin, HIGH );
    delay( 1000 ); // Give it time to punch through the material
    digitalWrite( myPin, LOW );
  }
};
//board stuff
const int serialBaudRate = 9600; // [bps]
const int oneTimeStep = 33;  // Smallest timestep in milliseconds

//pin assignments
const int selectButtonPin = 4; // Number of pin connected to "select" button
const int startButtonPin = 5; // Number of pin connected to "start" button
const int resetButtonPin = 6; // Number of pin connected to "start" button
const int mechStopPins[] = { 10, 11, 12, 13 };
const int mobileClampPin = 8;
const int staticClampPin = 9;
const int alertPin = 23;
const int punchPin = 7;
const int profileSwitchPin = 27;

//constants for motor movement
//float stepsPerInch = 2272.727;
float stepsPerInch = 1408.45;

//profile checking
bool punchingAProfile  = false;
bool workpieceNeedsToBeRemoved = false;

bool selectIsDown      = false;
bool selectWasDown     = false;
bool selectFreshlyUp   = false;

bool startIsDown       = false;
bool startWasDown      = false;
bool startFreshlyUp    = false;

bool resetIsDown       = false;
bool resetWasDown      = false;
bool resetFreshlyUp    = false;

const int profileSetALength = 21;
const int profileSetBLength = 29;
//hole patterns, in inches
float profileSetA[ 4 ][ profileSetALength ] = {

  { 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 0, 0, 0,0 },  //IS-8-bottom rail
 { 4.000, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 0, 0, 0, 0, 0, 0, 0,0 },                  //IS-6-bottom rail
 { 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364}, //IL-8-botoom rail
 { 3.273, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 0, 0, 0, 0, 0}       //IL-6-bottom rail
};

// a list of the intervals between consecutive holes for each profile.  If 0, that means there is no next hole.
float profileSetB[ 4 ] [ profileSetBLength ] = {
  { 5.333, 5.333, 2.67, 5.333, 5.333, 2.67, 5.333, 5.333, 2.67, 5.333, 5.333, 2.67, 5.333, 5.333, 2.67, 5.333, 5.333, 2.67, 5.333, 5.333, 2.67, 5.333, 5.333,2.67,  5.333, 0, 0, 0,0 },  //IS-8-bottom rail
 { 4.000, 5.333, 2.67, 5.333, 5.333, 2.67, 5.333, 5.333, 2.67, 5.333, 5.333, 2.67, 5.333, 5.333, 2.67, 5.333, 5.333, 2.67, 5.333, 0, 0, 0, 0, 0, 0, 0,0 },                  //IS-6-bottom rail
 { 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364}, //IL-8-botoom rail
 { 3.273, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 0, 0, 0, 0, 0}       //IL-6-bottom rail
};
int chosenProfile = 0; // 0: 6S, 1: 6L, 2: 8S, 3: 8L


pneumaticClamp mobileClamp( mobileClampPin );
pneumaticClamp staticClamp( staticClampPin );
punch unicyl( punchPin );

//stepper motor intilization, for accel stepper
AccelStepper stepper(1,3,2); //pin 3 =pluse, pin 2=direction


void setup()
{
  pinMode( mechStopPins[0], OUTPUT );
  pinMode( mechStopPins[1], OUTPUT );
  pinMode( mechStopPins[2], OUTPUT );
  pinMode( mechStopPins[3], OUTPUT );
  pinMode( mobileClampPin, OUTPUT );
  pinMode( punchPin, OUTPUT);
  pinMode( staticClampPin, OUTPUT );
  pinMode( alertPin, OUTPUT );
  pinMode( selectButtonPin, INPUT );
  pinMode( startButtonPin,  INPUT );
  pinMode( resetButtonPin,  INPUT );
  pinMode( profileSwitchPin,  INPUT );

  Serial.begin( serialBaudRate );

  //for stepper motor control
  stepper.setMaxSpeed(1500);    //in steps per second
  stepper.setAcceleration(800); //in steps per second
}

void loop()
{
  selectIsDown = digitalRead( selectButtonPin );
  startIsDown  = digitalRead( startButtonPin  );
  resetIsDown  = digitalRead( resetButtonPin  );
  bool profileSwitch = digitalRead( profileSwitchPin );
  
  if ( selectWasDown && !selectIsDown ){ selectFreshlyUp = true; }
  if ( startWasDown  && !startIsDown  ){ startFreshlyUp  = true; }
  if ( resetWasDown  && !resetIsDown  ){ resetFreshlyUp  = true; }
 
  digitalWrite( mechStopPins[0], LOW );
  digitalWrite( mechStopPins[1], LOW );
  digitalWrite( mechStopPins[2], LOW );
  digitalWrite( mechStopPins[3], LOW );
  
  if ( punchingAProfile ){
    // if punching a profile
    punchingAProfile = false;

    if (profileSwitch) {
      punchProfile(profileSetA[chosenProfile], profileSetALength);
    } else {
      punchProfile(profileSetB[chosenProfile], profileSetBLength);
    }
    workpieceNeedsToBeRemoved = true;
    digitalWrite( alertPin, HIGH );
  } else {
    // if not punching a profile
    if ( workpieceNeedsToBeRemoved ) {
      //see if reset button was freshly pressed and if so, set this variable to false.
      if ( resetFreshlyUp ) {
        workpieceNeedsToBeRemoved = false;
        digitalWrite( alertPin, LOW );
      }
    } else {
      if ( selectFreshlyUp ) {
        if ( chosenProfile == 3 ) {
         chosenProfile = 0;
        } else {
          chosenProfile++;
        }
      }
  
      if ( startFreshlyUp ) {
        // Switch into "punching a profile" mode
        punchingAProfile = true;
      }
      digitalWrite( mechStopPins[ chosenProfile ], HIGH );
    }
  }
    
  selectWasDown = selectIsDown;
  startWasDown  = startIsDown;
  resetWasDown  = resetIsDown;
  startFreshlyUp  = false;
  selectFreshlyUp = false;
  resetFreshlyUp  = false;
  delay( oneTimeStep );
}

void punchProfile(float profile[], int profileLength)
{
  mobileClamp.clamp();
  staticClamp.clamp();
  delay( 2000 ); // Give clamps time to clamp securely
  for (int i=0; i < profileLength; i++){
    float distanceToMove = profile[ i ];
    if ( distanceToMove != 0 )
    {
      unicyl.punchHole();
      moveWorkpieceForward( distanceToMove );
    }
  }
  unicyl.punchHole();
  mobileClamp.unClamp();
  staticClamp.unClamp();
  delay( 2000 );
}

void moveWorkpieceForward( float distanceToMove )
{
  int xa=0;
  int xb=0;
  //Serial.print( "Moving workpiece " );
  //Serial.print( distanceToMove );
  staticClamp.clamp();
  mobileClamp.unClamp();
  
  Serial.print( "mClamp -> " );
  Serial.print( distanceToMove );
  xa = moveMobileCylinderForward(distanceToMove); //moving forward
  Serial.print( ". " );
  delay( 1000 );
  //delay( 1000*distanceToMove ); // REMOVE
  
  mobileClamp.clamp();
  staticClamp.unClamp();
  
  Serial.print( "mClamp <- " );
  Serial.print( distanceToMove );
  xb = moveMobileCylinderBackward(distanceToMove );  //moving backward
  Serial.print( ". " );
  delay( 1000 );
  //delay( 1000*distanceToMove ); //REMOVE
  staticClamp.clamp();
  //delay(2000); //REMOVE
  
}

int moveMobileCylinderForward( float distanceInInches )
{
  long stepsToMove = distanceInInches*stepsPerInch;
  long deccel_step = stepsToMove-1000;

  //move forward
   stepper.moveTo(stepsToMove);
   while (stepper.currentPosition() != stepsToMove) // Full speed up to 300
   {
     stepper.run();
   }
   stepper.stop(); // Stop as fast as possible: sets new target
   stepper.runToPosition();
  // Now stopped after quickstop

}

int moveMobileCylinderBackward( float distanceInInches )
{
  long stepsToMove = -1*distanceInInches*stepsPerInch;
  long deccel_step = stepsToMove-1000;
  

  //move forward
   stepper.moveTo(stepsToMove); // negative because moving backwards
   while (stepper.currentPosition() != 0) // Full speed back to 0
   {
     stepper.run();
   }
   stepper.stop(); // Stop as fast as possible: sets new target
   stepper.runToPosition();
}


Go Up