Controlling NEMA 34 motor, for audio indexing system


hello,

i still working on audio indexing system, punching machine making school. using accelstepper ramp up/down stepper motor. have done alot of work accelstepper, in getting bit of code move motor forward , 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 300
   {
     stepper.run();
   }
   stepper.stop(); // stop fast possible: sets new target
   stepper.runtoposition();
  // 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 0
   {
     stepper.run();
   }
   stepper.stop(); // stop fast possible: sets new target
   stepper.runtoposition();
}




next, in order operator interface system, , system move motor , forth discrete distances, need come interface/control system. decided use sanwa buttons, have been used quite bit, , able figure bit of code out. problem being, when incorporate 2 bits of code, control system works, arduino isn't giving required output signal motor. code below. very, appreciated.

here full code, forgot put on post:

code: [select]

// "select" button allows user select profile punch.
// "start" button starts punching profile (or pauses machine if punching).
// "reset" button lets user tell machine workpiece has been removed.

#include "accelstepper.h"  //use "" , no <>, b/c accelstepper librabry 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 time punch through 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 "select" button
const int startbuttonpin = 3; // number of pin connected "start" button
const int resetbuttonpin = 4; // number of pin connected "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 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
                                                                     }; // list of intervals between consecutive holes each profile.  if 0, means there 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, 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 profile
    
    punchprofile();
    punchingaprofile = false;
    workpieceneedstoberemoved = true;
    digitalwrite( alertpin, high );
  }
  else
  {
    // if not punching profile
    if ( workpieceneedstoberemoved )
    {
      //see if reset button freshly pressed , if so, set variable false.
      if ( resetfreshlyup ){
        workpieceneedstoberemoved = false;
        digitalwrite( alertpin, low );
      }
    }
    else
    {
      if ( selectfreshlyup ){
        if ( chosenprofile == 3 ){
         chosenprofile = 0;
        }
        else{ chosenprofile++;
        }
      }
  
      if ( startfreshlyup ){
      
        // switch "punching 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 clamp securely
  unicyl.punchhole();
  for (int i=0; < numdistancesperprofile; i++){
    int distancetomove = distancebetweenconsecutiveholes[ chosenprofile ][ ];
    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 300
   {
     stepper.run();
   }
   stepper.stop(); // stop fast possible: sets new target
   stepper.runtoposition();
  // 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 0
   {
     stepper.run();
   }
   stepper.stop(); // stop fast possible: sets new target
   stepper.runtoposition();
}





Arduino Forum > Using Arduino > Motors, Mechanics, and Power (Moderator: fabioc84) > Controlling NEMA 34 motor, for audio indexing system


arduino

Comments

Popular posts from this blog

Error compiling for board Arduino/Genuino Uno.

Installation database is corrupt

esp8266 (nodemcu 0.9) client.write très lent ???