Shop OBEX P1 Docs P2 Docs Learn Events
Interpolating Dual Stepper Motor Driver in FemtoBasic — Parallax Forums

Interpolating Dual Stepper Motor Driver in FemtoBasic

Duane C. JohnsonDuane C. Johnson Posts: 955
edited 2012-02-28 09:12 in Propeller 1
I just updated one of my Stepper Motor driers that I wrote in FemtoBasic.
So I decided to post it in a presentable form.

It works quite well.
Of course this is not a Whiz-Bang do all program that runs at fast speed.
Most of what I do doesn't have to run fast.

This example can be configured to drive either:
1. 2 Bit Binary motor driver.
2. Gray Code 2 Phase (BiPolar) stepper motors.
3. 4 Phase (UniPolar) stepper motors.

This code controls the steppers using an interpolation routine so they both start and end motions simultaneously, even though they travel different distances. The motions both move linearly at different speeds. This allows strait line motions on an X-Y table or any other deice.
I only present a 2 motor example but 3 or more motors could be done using this technique.
NEW
1   PRINT "Interpolating Stepper Motor Driver"
10  A =  0 : B = 0 : C =   0 : D = 0     :REM X Stepper Start, Intermediate, Stop, Phase
20  E =  0 : F = 0 : G =   0 : H = 0     :REM Y Stepper Start, Intermediate, Stop, Phase
30  M =  0                               :REM   Initial Step Speed
40  I =C-A : IF I   < 0   THEN I = 0 - I :REM X Absolute value of (C-A)
50  J =G-E : IF J   < 0   THEN J = 0 - J :REM Y Absolute value of (G-E)
60  K =  J : IF J   < I   THEN K = I     :REM Greater of I or J Step (Number of Step Cycles)
70  L = -1 : IF A+E < C+G THEN L = 1     :REM Step Direction
80  PRINT "K","I","J","L"
90  PRINT  K , I , J , L
100 PRINT "T","D B","H F"
110 FOR    T = 0 TO K*L STEP L
120        B = T*(C-A)/K*L+A   :REM Calculate X Intermediate Step Position
130    REM D = B&3                            :REM Calculate X Step Code 2 Bit Binary
140    REM D = B&3:IF B&2=2 THEN D=B&2+(B+1)&1:REM Calculate X Gray Code 2 Phase (BiPolar)
150        D = 1 SHL (B & 3)                  :REM Calculate X Step Code 4 Phase (UniPolar)
160        OUTA [15..12] = D   :REM Do The X Output
170        F = T*(G-E)/K*L+E   :REM Calculate Y Intermediate Step Position
180    REM H = F&3                            :REM Calculate Y Step Code 2 Bit Binary
190    REM H = F&3:IF F&2=2 THEN H=F&2+(F+1)&1:REM Calculate Y Gray Code 2 Phase (BiPolar)
200        H = 1 SHL (F & 3)                  :REM Calculate Y Step Code 4 Phase (UniPolar)
210        OUTA [21..18] = H   :REM Do The Y Output
220 PRINT T,D;" ";B,H;" ";F
230 PAUSE  M                   :REM Control the Speed
240 NEXT   T
250 A = C : E = G              :REM Reset Starting Positions
260 PRINT "                              "; A;",  ";E;",  ";M;"mS"
270 INPUT "Destination & Speed C, G, M ? "; C, G, M
280 GOTO 40                    :REM Start Over For Next Movement
RUN
Have fun with it.
Duane J
Sign In or Register to comment.