本文非原创,转载自:http://www.schmalzhaus.com/EasyDriver/Examples/EasyDriverExamples.html
Easy DriverExamples
Sample code and projects to get your stepperrunning!Description:
Lotsof folks buyEasyDriversorBigEasyDriversandthen get them to work just fine in their project. But some don't,and so I thought it would be a good idea to write down some simpleinstructions for getting your Easy Driver working as quickly andeasily as possible.Allof these examples are going to be done with my Easy Driver and BigEasy Driver stepper motor driver boards driving several differentrandom stepper motors I have lying around the lab. I will begenerating the step and direction pulses withanArduinoUNOandachipKITUNO32,although all of these examples should work with any Arduino orArduino clone or Arduino compatible (like all chipKITboards).
Anddon't forget to read Dan Thompson'sexcellentEasyDriver tutorial blog postifyou want to read more up on this stuff. Some great questionsanswered in the comments on that blog post.
Note1:All examples will work equally well with Easy Drivers or Big EasyDrivers.
Note2:All examples will work on Arduino as well as chipKIT boards (andsome will run much better on chipKIT because of the PIC32speed)
Note3:All examples show a barrel jack for power input - you need tosupply power to the EasyDrivers somehow, but it doesn't need to bea barrel jack. You should have a power supply that can output somevoltage between 5V and 30V, at 1 Amp or more.
Example 1:Basic Arduino setup
Thisis the most basic example you can have with an Arduino, an EasyDriver, and a stepper motor. Connect the motor's four wires to theEasy Driver (note the proper coil connections), connect a powersupply of 12V is to the Power In pins, and connect the Arduino'sGND, pin 8 and pin 9 to the Easy Driver.Thenload this sketch and run it on your Arduino orchipKIT:
void setup() {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}
void loop() {
digitalWrite(9, HIGH);
delay(1);
digitalWrite(9, LOW);
delay(1);
}Itdoesn't get much simpler than that. What is the code doing? It setsup pin 8 and 9 as outputs. It sets them both low to begin with.Then in the main loop, it simply toggles pin 9 high and low,wai ting 1ms between toggles. We use pin 9 as the STEP control andpin 8 as the DIRECTION control to the Easy Driver.
Sincewe are not pulling either MS1 or MS2 low on the Easy Driver low,the Easy Driver will default to 1/8th microstep mode. That meansthat each time the "digitalWrite(9, HIGH);" call is executed, thestepper motor will move 1/8th of a full step. So if your motor is1.8 degrees per step, there will be 200 full steps per revolution,or 1600 microsteps perrevolution.
Sohow fast is this code going to run the stepper? Well, with the STEPsignal 1ms high and 1ms low, each complete pulse will take 2ms oftime. Since there are 1000ms in 1 second, then 1000/2 = 500microsteps/second.
Whatif we wanted the motor to go slower? We change the delay(); linesto have longer delays. If you use delay(10); for both, the you'llmove at 50 microsteps/second.
Whatif you wanted the motor to go faster? We can't really delay forless than 1 ms, can we? Yes, of course we can! We can change thedelay() calls to delayMicroseconds(100); calls and then each delaywould be 100 microseconds (or us), so the motor would be driven at5000 microsteps/second.
Now,one thing you should play with is the current adjustment pot onyour Easy Driver. You need a tiny little screw driver to turn it,and be sure not to force it too far one way or the other (they'redelicate). Also, some Easy Drivers were built with pots that haveno physical stops on them, so they spin around and around. As yourun the above code, slowly turn the pot one way or the other.Depending upon the type of motor you have (and its coil resistance)you may hear/feel no difference as you spin the pot, or you maynotice quite a big difference.
Example 2:Moving back and forth
Ifwe take Example 1, and simply change the sketch a little bit, wecan move a certain number of steps forward or backward. Likeso:int Distance = 0; // Record the number of steps we've taken
void setup() {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}
void loop() {
digitalWrite(9, HIGH);
delayMicroseconds(100);
digitalWrite(9, LOW);
delayMicroseconds(100);
Distance = Distance + 1; // record this step
// Check to see if we are at the end of our move
if (Distance == 3600)
{
// We are! Reverse direction (invert DIR signal)
if (digitalRead(8) == LOW)
{
digitalWrite(8, HIGH);
}
else
{
digitalWrite(8, LOW);
}
// Reset our distance back to zero since we're
// starting a new move
Distance = 0;
// Now pause for half a second
delay(500);
}
}Nowusing this sketch, we move for 3600 steps in one direction, pausefor a bit, and move 3600 steps in the other direction. I'm sure youcan figure out how to make many different lengths of moves now. Andyou can change the delay between steps for each move to occur atseparate speeds.
Example 3:Using a pre-built library - AccelStepper
Onething the above examples can't do well is handle multiple steppersfrom the same Arduino or chipKIT. Also, acceleration anddeceleration are difficult as well. Other people have run into thisproblem, and so now we have libraries that we can download andinstall into the Arduino IDE or MPIDE to fix theseproblems.Downloadthe zip file for the AccelStepper libraryfromthispage.Unzip the downloaded file, and place the AccelStepper in to thelibraries folder in your Arduino install directory. Note that forMPIDE (chipKIT) users, you need to copy the AccelStepper folderinto both the libraries folder at the top level as well ashardwarepic32libraries so that both the AVR and PIC32 sides canuse it.
Usingthe same hardware from Example 1, restart the IDE, and enter thefollowing sketch:
#include
// Define a stepper and the pins it will use
AccelStepper stepper(1, 9, 8);
int pos = 3600;
void setup()
{
stepper.setMaxSpeed(3000);
stepper.setAcceleration(1000);
}
void loop()
{
if (stepper.distanceToGo() == 0)
{
delay(500);
pos = -pos;
stepper.moveTo(pos);
}
stepper.run();
}Thiscode does basically the same thing as Example 2, but usingacceleration/deceleration via the AccelStepper library, and runningfor twice as many steps. (Thanks Mr. Duffy for pointing out thisimportant fact!) The reason it runs twice as many steps is becausewe do "pos = -pos" to keep things short and simple. This means thatit will run from 0 to 3600, then from 3600 to -3600 (which is 7200steps).
Example 4:Running multiple stepper motors
Oneof the great things about the AccelStepper library is that you canrun as many stepper motors as you want, at the same time, just bymaking more AccelStepper objects. Now, if you try to run them toofast, the steps won't be smooth, so you have to be careful not toload down the Arduino too much. The chipKIT does not have thisproblem because it is so much faster than theArduino.Inthis diagram, we now have two Easy Drivers and two stepper motors.We just need 2 more pins from the Arduino to add this secondmotor.
The code for this example is shownbelow:
#include
// Define two steppers and the pins they will use
AccelStepper stepper1(1, 9, 8);
AccelStepper stepper2(1, 7, 6);
int pos1 = 3600;
int pos2 = 5678;
void setup()
{
stepper1.setMaxSpeed(3000);
stepper1.setAcceleration(1000);
stepper2.setMaxSpeed(2000);
stepper2.setAcceleration(800);
}
void loop()
{
if (stepper1.distanceToGo() == 0)
{pos1 =-pos1;
stepper1.moveTo(pos1);
}
if (stepper2.distanceToGo() == 0)
{
pos2 = -pos2;
stepper2.moveTo(pos2);
}
stepper1.run();
stepper2.run();
}
Ifyou run this code, you may find that the acceleration anddeceleration are not quite as smooth as with a single motor (on anArduino - again, this problem doesn't occur on chipKIT) - that isbecause our two maximum speeds (3000 and 1000) are pretty high forthe ability of the processor to handle them. One solution is tomake your max speeds lower, then switch from 1/8th microstepping to1/4, half, or full step mode. If done right, you'll see the sameshaft rotation speeds, but with less CPU load (because you aren'tgenerating as many steps per second.)
Youcan see that for this example, I just copied and pasted the codefrom Example 3 and made two positions and two steppers. Thisexample code is very simple and not all that useful, but you canstudy the existing examples from the AccelStepper library, and readthe help pages on the different functions, and get good ideas aboutwhat else you can do with your stepper control.