New PWM Stepper Driver With Ramping
idbruce
Posts: 6,197
I am sure it is not perfect, but it works. Any suggestions on refinement will be greatly appreciated. I will add comments as the questions arise, and it will soon be posted in OBEX Happy Steppin'
### For updated driver, please download G251DriverV3.spin###
### For updated driver, please download G251DriverV3.spin###
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 VAR Long nTotalStepsDivByTwo Long nStepsRemaining Long nCurrentFrequency Long nCounterTime Long nStepPulseWidth PUB Main G251DriveStepper(300_000, 10_000, 1_000, 3, 200_000) PUB G251DriveStepper(nMaxFreqDurHZ, nMinFreqDurHZ, nRampingFreq, nStepPin, nTotalSteps) nCounterTime := cnt ' Mark counter time nStepPulseWidth := clkfreq/1_000_000 '1 ms minimum count for step duration nCurrentFrequency := nMaxFreqDurHZ nTotalStepsDivByTwo := nTotalSteps / 2 nStepsRemaining := nTotalSteps ctra[30..26] := %00100 ' Configure Counter A to NCO ctra[5..0] := nStepPin frqa := 1 dira[nStepPin]~~ repeat while nCurrentFrequency > nMinFreqDurHZ and nStepsRemaining > nTotalStepsDivByTwo phsa := -nStepPulseWidth nCounterTime += nCurrentFrequency ' Calculate next cycle repeat waitcnt(nCounterTime) ' Wait for next cycle nCurrentFrequency := nCurrentFrequency - nRampingFreq 'ramp up the speed nStepsRemaining-- if nCurrentFrequency =< nMinFreqDurHZ or nStepsRemaining == nTotalStepsDivByTwo repeat until nTotalStepsDivByTwo - nStepsRemaining == 0 nCounterTime += nCurrentFrequency phsa := -nStepPulseWidth waitcnt(nCounterTime) nStepsRemaining-- repeat until nStepsRemaining == 0 or nCurrentFrequency == nMaxFreqDurHZ nCurrentFrequency := nCurrentFrequency + nRampingFreq 'ramp down the speed phsa := -nStepPulseWidth nCounterTime += nCurrentFrequency waitcnt(nCounterTime) nStepsRemaining--
Comments
### The following download has a few errors within the notes section. Please download the code in the succeeding post instead, entitled G251DriverV3.spin###
Bruce
Title: Trapezoidal Ramping PWM Stepper Driver
Link: http://obex.parallax.com/objects/693/
http://www.geckodrive.com/product.aspx?i=14471
Let us know how that driver works out for you. Thanks!
I just got it working perfectly today. This driver should work for any stepper driver that can accept PWM onto a step pulse pin, not just the G250 and G251 from Gecko. Just modify the parameters to suit your needs. It works great! Thanks for the reply.
Bruce,
did you remove this object from the OBEX? or am I just really lousy at finding things there?
I just got a Gecko drive and thought I'd give your code a try.
thanks, :-)
I gave up on that driver, because it was not as fast as I thought it was. Try the attachment below, that will get you going. If you have any problems with the CONSTANTS, just ask.
I have found certain circumstances where the ramping would not work for me because of too few steps required. In those instances, I used the following function. Additionally, if you need help with the Offset parameter below, just ask.
Bruce
Thanks! :-)
How did that work for you?
Bruce
Bruce,
I haven't had a chance to try it out, yet. Maybe today or maybe next week. I still need to get the proper set resistor.
How are your new CNCs working out?
thanks again! :-)
Thanks for asking.
The wire bending CNC was done quite a while ago, and I almost have the packaging machine complete, however, I put the packaging machine on hold and decided to make a new attachment for the bending machine for manned packaging. I wanted the entire setup to be fully automated, but the patent office is finally getting around to my patent application, and I just don't have time for unknown or unforeseen problems.
Hopefully I will have a final solution by the end of today.
Bruce
Bruce,
just to answer: I'm running my stepper motor extremely slowly, so I ended up rewriting the code quite a bit to accommodate my particular needs, but I used your same NCO configuration technique for everything, and it works great. Even at 1 step every ~53 seconds or so, the NCO configuration seems to work without glitching.
After spending too much time making my own stepper drives with L6208 chips, I've found this G251x Gecko Drive to be well worth the money (when making a few devices at a time).
thanks again for the code sample and for suggesting this drive! :-)
LOL I am so glad that I also abandoned that effort. The G251's make it sooooo much easier.
However, you shouldn't have had to rewrite anything. In the following code, you could have set HIGH_PULSE_WIDTH to your desired pulse width (minimum 1 us), and you could have set your offset to 53 seconds.
But anyhow, I am glad I was able to help.
Bruce
How do you ceate the code to control the motors? Is it Gcode if so is there a program like SheetCam to convert a DXF file to use with your code. Is it sent via serial? I have not ran the demo yet, but will soon. I do have a 4 drive Xylotex board and a router table to test it on.
This is just a stepper driver that tells the stepper motors how to operate, such as stepping sequence and pps (pulses per second). G-Code would tell the stepper driver how many turns to make. They are two completely different things.
To my knowledge, there is no G-Code interpreter for the propeller, however, I wish someone would write one. I did create a G-Code parser for EagleCad drill files, you may want to download this and hang on to it, because it may be beneficial to you in the future.
Additionally, I will be rewriting this stepper driver in the near future.
Bruce
You answered my question on the G code. I did download the parser.
I have a board designed a board with 4 axis step and dir with 4 limit sw in puts and 4 mosfet outputs to drive a relay. I want to give your code a go on it as soon as I get some time. I have the board working on my bench
I agree a prop interpreter would be great. If someone wrote one I would buy it at reasonable price. I can program but that is not my expertize.