Shop OBEX P1 Docs P2 Docs Learn Events
New PWM Stepper Driver With Ramping — Parallax Forums

New PWM Stepper Driver With Ramping

idbruceidbruce Posts: 6,197
edited 2011-04-09 20:00 in Propeller 1
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###
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

  • idbruceidbruce Posts: 6,197
    edited 2010-12-14 14:28
    Okay here is a new version with notes and a new parameter has been added to one of the functions.

    ### The following download has a few errors within the notes section. Please download the code in the succeeding post instead, entitled G251DriverV3.spin###
  • idbruceidbruce Posts: 6,197
    edited 2010-12-15 08:21
    ### The following download had some major issues of missing steps. As of this edit, all errors should be fixed and it should work perfectly. Please download the corrected version from OBEX entitled G251DriverV4.spin and not Version 3 below###

    Bruce
  • idbruceidbruce Posts: 6,197
    edited 2010-12-16 08:35
    This object is now available in OBEX.

    Title: Trapezoidal Ramping PWM Stepper Driver
    Link: http://obex.parallax.com/objects/693/
  • idbruceidbruce Posts: 6,197
    edited 2010-12-23 20:04
    As promised, the G251DriverV4 is now available.
  • ElectricAyeElectricAye Posts: 4,561
    edited 2010-12-23 20:19
    Thanks idbruce. The more stepper stuff in the OBEX the better. Maybe somewhere in the title you might want to state that this is for a Gecko driver. Until I Googled it, I didn't know what this was for.

    http://www.geckodrive.com/product.aspx?i=14471

    Let us know how that driver works out for you. Thanks!
  • idbruceidbruce Posts: 6,197
    edited 2010-12-23 20:36
    ElectricAye

    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.
  • ElectricAyeElectricAye Posts: 4,561
    edited 2011-03-24 13:20
    idbruce wrote: »
    As promised, the G251DriverV4 is now available.

    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, :-)
  • idbruceidbruce Posts: 6,197
    edited 2011-03-24 15:19
    ElectricAye

    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.
    PUB Pulser(StepPin, TotalSteps, Offset) | Counter
      CTRA[30..26] := %00100 ' Configure Counter A to NCO
      CTRA[5..0] := StepPin
      FRQA := 1
      DIRA[StepPin]~~
      Counter := CNT
     
      repeat TotalSteps
        PHSA := -HIGH_PULSE_WIDTH
        WAITCNT(Counter += Offset)
    

    Bruce
  • ElectricAyeElectricAye Posts: 4,561
    edited 2011-03-24 15:24
    idbruce wrote: »
    ...Try the attachmentment below, that will get you going. ...

    Thanks! :-)
  • idbruceidbruce Posts: 6,197
    edited 2011-03-25 05:34
    ElectricAye

    How did that work for you?

    Bruce
  • ElectricAyeElectricAye Posts: 4,561
    edited 2011-03-25 06:03
    idbruce wrote: »
    ElectricAye

    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! :-)
  • idbruceidbruce Posts: 6,197
    edited 2011-03-25 06:59
    ElectricAye

    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
  • ElectricAyeElectricAye Posts: 4,561
    edited 2011-03-29 10:00
    idbruce wrote: »
    ElectricAye

    How did that work for you?

    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! :-)
  • idbruceidbruce Posts: 6,197
    edited 2011-03-29 10:13
    ElectricAye
    After spending too much time making my own stepper drives

    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. :)
    PUB Pulser(StepPin, TotalSteps, Offset) | Counter
      CTRA[30..26] := %00100 ' Configure Counter A to NCO
      CTRA[5..0] := StepPin
      FRQA := 1
      DIRA[StepPin]~~
      Counter := CNT
      
      repeat TotalSteps
        PHSA := -HIGH_PULSE_WIDTH
        WAITCNT(Counter += Offset)
    

    But anyhow, I am glad I was able to help.

    Bruce
  • JTCJTC Posts: 60
    edited 2011-04-09 15:20
    IDBruce,
    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.
  • idbruceidbruce Posts: 6,197
    edited 2011-04-09 19:10
    JTC

    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
  • JTCJTC Posts: 60
    edited 2011-04-09 20:00
    Thank you for your response. Yes I have designed interfaces for CNC router tables, mills , plasma cutters so am familiar with the difference.
    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.
Sign In or Register to comment.