​ ​ ​ ​ stepper-control-with-acceleration
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 17

Thread: stepper-control-with-acceleration

  1. #1

    Default stepper-control-with-acceleration

    Hippy & Alistairsam



    I am using the folowing pogram from post #56 on page

    http://www.picaxeforum.co.uk/showthr...e-output/page6

    I tryed to figure out how i could set the max speed of the steppermotor? But did not succeeded.
    The program is working fine but I would like to change the max rpm. could one of you please help me here?
    I would allso like to know how i can change the accelleration.

    Alistairsam here is your code unchanged.
    Code:
    '28x2
    ' picaxe pin assignment for stepper driver inputs
    ' Stepper Driver - Allegro A4983 driver 
    ' High speed slew below uses 1/16 micro stepping
    ' enablepin = pinC.0
    ' Dirpin = pinC.1  -> High - CCW, Low - CW
    ' Clckpin = pinC.2
    ' MS0pin = pinC.3
    ' MS1pin = pinC.4
    ' MS2pin = pinC.5
    
    ' Full stepping - MS0,MS1,MS2 low 
    ' 1/16th stepping - MS0, MS1, MS2 high - for tracking speed, hi and low speeds
    ' 1/8th stepping - MS0 high, MS1 high, MS2 low
    
    'pwmdiv ranges @ 8Mhz
    'pwmdiv 16 -> 500 - 1940 Hz
    'pwmdiv 4 -> 1950 - 7800 Hz 
    
    setfreq m8
    
    Symbol BB = w0
    Symbol percal2 = w2
    Symbol perd = w3
    Symbol Fmin = w4
    Symbol Fmax = w5
    Symbol F = w6
    Symbol Dutyreq =w7
    Symbol Duty = w8 
    Symbol dutycal1 = w9
    Symbol dutycal2 = w10
    Symbol F2 = w11
    Symbol Fdiff = w12
    
    
    'Enter max freq as max freq / 10 kHz
    'Enter duty as duty % * 10
    
    let dirsB = %00000000
    let Dutyreq = 300
    let Fmin = 50
    let Fmax = 2400
    
    
    Setpinslow:
    pwmout C.2,off
    let dirsC = %10111111
    let pinsC = %10000001 'Turn LED on C.7 on, Enable off
    goto keyscan:
    
    
    keyscan:
    Do
    for BB = 1 to 10000 step 1
    pause 100
    if pinB.3 = 1 then highCW
    if pinB.4 = 1 then HighCCW
    if pinB.5 = 1 then LowCW
    if pinB.6 = 1 then LowCCW
    if pinB.7 = 1 then TrackCW
    if BB = 10000 then goto slp
    next BB
    Loop
    
    
    
    HighCW:
    pauseus 5000
    if pinB.3 = 0 then Setpinslow:
    let pinsC = %10111000 'set enable low and DIR low, 1/16 step
    gosub pwroutine
    Do
    if pinB.3 = 0 then Decel:
    pwmout C.2, perd, duty
    Loop
    
    
    HighCCW:
    pauseus 5000
    if pinB.4 = 0 then Setpinslow:
    let pinsC = %10111010 'set enable low and DIR high, 1/16 step
    gosub pwroutine
    Do
    if pinB.4 = 0 then Decel:
    pwmout C.2, perd, duty
    Loop
    
    
    
    LowCW:
    pauseus 5000
    let pinsC = %10111000 'set enable low and DIR low, 1/16th step
    Do
      if pinB.5 = 0 then Setpinslow:
      Toggle C.2
      PauseUs 10
     Loop
    
    LowCCW:
    pauseus 5000
    let pinsC = %10111010 'set enable low and DIR high, 1/16th step
    Do
    if pinB.6 = 0 then Setpinslow:
    Toggle C.2
     PauseUs 10
    Loop
    
    
    TrackCW:
    pauseus 5000
    let pinsC = %10111000 'set enable low and DIR low, 1/16th step
    Do
    if pinB.7 = 0 then Setpinslow:
     Toggle C.2
     Pauseus 500
    Loop
    
    
    
    pwroutine:
    
     sertxd ("Div16")
     F = Fmin
    Do
     Select Case F
     Fdiff = F >> 2
     Case > 186  goto Div4
     Case < 187 : F = F + Fdiff
     End Select 
    percal2 = 12504 / F
    perd = percal2 - 1
    dutycal1 = Dutyreq / 25
    dutycal2 = perd + 1
    duty = dutycal1 * dutycal2 /10
    sertxd (#w6,",",#w3,",",#w8)
    pwmout pwmdiv16, C.2,perd,duty
    Loop
    
    
    Div4:
    sertxd ("Div4")
    Do
     Select Case F
     Fdiff = F >> 3
     Case > Fmax  Return
     Case > 758 goto NoDiv
     Case < 759 : F = F + Fdiff
     End Select 
    percal2 = 50000 / F
    perd = percal2 - 1
    dutycal1 = Dutyreq / 25
    dutycal2 = perd + 1
    duty = dutycal1 * dutycal2 / 10
    sertxd (#w6,",",#w3,",",#w8)
    pwmout pwmdiv4, C.2,perd,duty
    Loop
    
    
    NoDiv:
    sertxd ("NoDiv")
    Do
     if pinB.3 = 0 and pinB.4 = 0 then Decel
     Select Case F
     Fdiff = F >> 5
     Case > Fmax  Return
     Case < Fmax : F = F + Fdiff
    End Select 
    percal2 = 20000 / F 
    perd = percal2 * 10
    perd = perd - 1
    dutycal1 = Dutyreq / 25
    dutycal2 = perd + 1
    duty = dutycal1 * dutycal2 / 10
    sertxd (#w6,",",#w3,",",#w8)
    pwmout C.2,perd,duty
    
    Loop
    
    return
    
    
    Decel:
    sertxd ("decel")
    Do
     Select Case F
     Fdiff = F >> 4
     Case < 790 goto Setpinslow
     Case > 790 : F = F - Fdiff
    End Select 
    percal2 = 20000 / F 
    perd = percal2 * 10
    perd = perd - 1
    dutycal1 = Dutyreq / 25
    dutycal2 = perd + 1
    duty = dutycal1 * dutycal2 / 10
    sertxd (#w6,",",#w3,",",#w8)
    pwmout C.2,perd,duty
    Loop
    
    
         
    slp:
    
    hInt0Flag = 0
    HIntSetup %00010001 ' Wake on B.0 (HINT0) going high
    let pinsC = %00000001
    disablebod
    Sleep 0
    'Do : Loop Until hint0Flag = 1
    enablebod
    goto setpinsl

    Thank you
    Willy
    Last edited by Satchid; 20-05-2017 at 10:47. Reason: added code an extra question

  2. #2
    Senior Member
    Join Date
    Jan 1970
    Location
    Colorado USA
    Posts
    2,966

    Default

    Hi - perhaps use MAX function? I'm not familiar with the program...

  3. #3
    Senior Member
    Join Date
    Feb 2012
    Location
    London
    Posts
    2,414

    Default

    Hi Willy,

    The speed of the stepper motor seems to be set by the period, pulse-width and division ratio in the PWM settings. To set a maximum speed you will need to set MINIMUM values for those parameters (perhaps using the MIN function). But those three parameters are inter-related and need to be carefully calculated; depending on the range and resolution of speeds required, it might be preferable to select from a "list" of pre-calculated speed parameters, for example using SELECT...CASE or LOOKUP tables.

    Cheers, Alan.

  4. #4
    Technical Support
    Join Date
    Jan 1970
    Location
    UK
    Posts
    23,234

    Default

    I am not familiar with the code but assuming PWMOUT sets the rate of stepping, it should just be a case of limiting the PWMOUT period as suggested and adjusting duty to match that. For 50% duty it would "duty = 2 * period".

  5. #5
    Senior Member
    Join Date
    Feb 2013
    Location
    UK
    Posts
    359

    Default

    I find that the easiest method to control stepper motors is by using TOGGLE within a DO:LOOP and then using a time delay variable to set the speed; incrementing or decrementing as needed to change acceleration/deceleration. On a 20x2 running at 64MHz, a bare DO:LOOP can drive a square wave with TOGGLE at 7.47 KHz. I have not yet found a need for anything like such a speed when driving steppers even when using microstepping. It is much simpler than using PWM. What sort of speed are you looking for?
    Last edited by Circuit; 21-05-2017 at 11:28. Reason: correction

  6. #6
    Senior Member
    Join Date
    Aug 2011
    Location
    US
    Posts
    445

    Default

    Quote Originally Posted by Circuit View Post
    I find that the easiest method to control stepper motors is by using TOGGLE within a DO:LOOP and then using a time delay variable to set the speed; incrementing or decrementing as needed to change acceleration/deceleration. On a 20M2 running at 64MHz, a bare DO:LOOP can drive a square wave with TOGGLE at 7.47 KHz. I have not yet found a need for anything like such a speed when driving steppers even when using microstepping. It is much simpler than using PWM. What sort of speed are you looking for?
    A 20M2 can run at 64 MHz? What am I missing?

  7. #7
    Senior Member
    Join Date
    Jan 1970
    Location
    Perth, Western Australia
    Posts
    4,270

    Default

    Quote Originally Posted by rq3 View Post
    A 20M2 can run at 64 MHz? What am I missing?
    Perhaps it was a 20M2 with a 20X2 under the bonnet/hood .

  8. #8
    Senior Member
    Join Date
    Feb 2013
    Location
    UK
    Posts
    359

    Default

    Quote Originally Posted by inglewoodpete View Post
    Perhaps it was a 20M2 with a 20X2 under the bonnet/hood .
    Sorry folks, yes, a typo of course. I meant an X2 - a PIC18F14K22 on the label!

  9. #9

    Default

    hi Circuit,

    Quote Originally Posted by Circuit View Post
    I find that the easiest method to control stepper motors is by using TOGGLE within a DO:LOOP and then using a time delay variable to set the speed; incrementing or decrementing as needed to change acceleration/deceleration. On a 20x2 running at 64MHz, a bare DO:LOOP can drive a square wave with TOGGLE at 7.47 KHz. I have not yet found a need for anything like such a speed when driving steppers even when using microstepping. It is much simpler than using PWM. What sort of speed are you looking for?
    I forsee about 2000HZ, I am using a 28x2

    I can not start the stepper motor at full speed at once, I need the speed to slope from 0 to 2000 in 1" and from 2000 to 0 in 1". Can I do that with your approache?
    Last edited by Satchid; 21-05-2017 at 13:33.

  10. #10
    Technical Support
    Join Date
    Jan 1970
    Location
    UK
    Posts
    23,234

    Default

    Quote Originally Posted by Satchid View Post
    I can not start the stepper motor at full speed at once, I need the speed to slope from 0 to 2000 in 1" and from 2000 to 0 in 1". Can I do that with your approache?
    Probably not the right execution speed but something like this should work ...

    Code:
    For w0 = 0 To 2000
      w1 = 2000 - w0
      High STEPPER : PauseUs w0
      Low  STEPPER : PauseUs w1
    Next

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •