Stepper motor code - why code like this?
JimG
Posts: 84
Below is a piece of sample code that is available from Parallax to work with their stepper motor.· I am not criticising their code but wonder why they go through all the gyrations with modulus to get the values 0, 1, 2, or 3 to determine which phase of the motor to run.· I seems to me that you could set constants for each phase, i.e. Phase0 CON 0, and be done with it.· Why do they go through all of the other work...other than maybe just to show a way of doing it demonstrating features of PBasic?· Can anyone help?
Note: I am just strating to fiddle (technical term) with steppers and need to make things a simple as possible my poor small brain!
' {$STAMP BS2}
' {$PBASIC 2.5}
Phase VAR OUTB ' phase control outputs
StpsPerRev CON 48 ' whole steps per rev
idx VAR Byte ' loop counter
stpIdx VAR Nib ' step pointer
stpDelay VAR Byte ' delay for speed control
Steps DATA %0011, %0110, %1100, %1001
Setup:
DIRB = %1111 ' make P4..P7 outputs
stpDelay = 15 ' set step delay
Main:
· FOR idx = 1 TO StpsPerRev ' one revolution
··· GOSUB Step_Fwd ' rotate clockwise
· NEXT
· PAUSE 500 ' wait 1/2 second
· FOR idx = 1 TO StpsPerRev ' one revolution
··· GOSUB Step_Rev ' rotate counter-clockwise
· NEXT
· PAUSE 500 ' wait 1/2 second
· GOTO Main
END
Step_Fwd:
· stpIdx = stpIdx + 1 // 4 ' point to next step
· DEBUG "Stepping forward..."
· DEBUG ? stpidx
· GOTO Do_Step
Step_Rev:
· stpIdx = stpIdx + 3 // 4 ' point to previous step
· GOTO Do_Step
Do_Step:
· READ (Steps + stpIdx), Phase ' output new phase data
· PAUSE stpDelay ' pause between steps
· RETURN
Jim
Note: I am just strating to fiddle (technical term) with steppers and need to make things a simple as possible my poor small brain!
' {$STAMP BS2}
' {$PBASIC 2.5}
Phase VAR OUTB ' phase control outputs
StpsPerRev CON 48 ' whole steps per rev
idx VAR Byte ' loop counter
stpIdx VAR Nib ' step pointer
stpDelay VAR Byte ' delay for speed control
Steps DATA %0011, %0110, %1100, %1001
Setup:
DIRB = %1111 ' make P4..P7 outputs
stpDelay = 15 ' set step delay
Main:
· FOR idx = 1 TO StpsPerRev ' one revolution
··· GOSUB Step_Fwd ' rotate clockwise
· NEXT
· PAUSE 500 ' wait 1/2 second
· FOR idx = 1 TO StpsPerRev ' one revolution
··· GOSUB Step_Rev ' rotate counter-clockwise
· NEXT
· PAUSE 500 ' wait 1/2 second
· GOTO Main
END
Step_Fwd:
· stpIdx = stpIdx + 1 // 4 ' point to next step
· DEBUG "Stepping forward..."
· DEBUG ? stpidx
· GOTO Do_Step
Step_Rev:
· stpIdx = stpIdx + 3 // 4 ' point to previous step
· GOTO Do_Step
Do_Step:
· READ (Steps + stpIdx), Phase ' output new phase data
· PAUSE stpDelay ' pause between steps
· RETURN
Jim
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
I see how it outputs but don't understand why you couln't drop in 4 lines of code with the phases addressed and be done with it?
Jim
take care
Jeff T.
The program supplied by Parallax is quite a universal program, and not just written for one particular stepper motor. The key to this, expressed by Jeff, is repeated here for emphasis:
" ... with one simple adjustment to the StpsPerRev you can alter the degree of movement quite simply."
Not every stepper has the same number of steps per revolution. Thus, this becomes a rather universal program in that respect. Could it be simpler - sure, but it might well lose some of its universality and flexability.
Regards,
Bruce Bates
Post Edited (Bruce Bates) : 11/5/2006 9:31:18 AM GMT
I completely agree on the us of that variable. I code that way myself. It is the whole modulus activity that I am having trouble understanding. Well, not exactly understanding, I know how it works, it just seems like a lot of steps to get where we need to be. I haven't had time to see if I can come up with a simpler way of doing it so I need to shut up until I either have a simler way or accept what it being done as the best way.
Thanks,
Jim
That's the way I do it in assembler for another chip I use.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think outside the BOX!