stepper motor time a bit off
Bits
Posts: 414
Below is the code that runs a stepper motor. When I compare the RPMs with a meter I am off slightly. What can I do to improve the time?
RPMs is calculated as
60_000 = msec to minute conversion
20 steps = 1 revolution whole stepping
40 steps = 1 revolution half stepping
60_000 / 40 = 1.5K 'half stepping here
For better resolution I take 1.5K * 100 but then make adjustment in motor object.
= 150_000 / rpms
main object calling the motor object
The Motor object
I think that the IF and Else statements are creating the lag in time. Is there a way to cancel this out?
RPMs is calculated as
60_000 = msec to minute conversion
20 steps = 1 revolution whole stepping
40 steps = 1 revolution half stepping
60_000 / 40 = 1.5K 'half stepping here
For better resolution I take 1.5K * 100 but then make adjustment in motor object.
= 150_000 / rpms
main object calling the motor object
mot.Control(150_000 / StirSpeed,power)
The Motor object
Pub Running(_time, _activate) Dira[pin1..pin4]~~ Time := _Time activate := _activate Repeat IF Activate outa[pin1..pin4] := halfSteps[coil := ++coil // 8] waitcnt((clkfreq / 100_000) * Time + cnt) Else outa[pin1..pin4] := $0000
I think that the IF and Else statements are creating the lag in time. Is there a way to cancel this out?
Comments
You may ultimately find yourself with something like this:
I would tend to want to pass a pointer to the timing value (calculated outside) and a pointer to a flag that allows the cog to unload itself. Of course, you have to start this with cognew.
I used this appnote to solve the problem. Once I found out how many clock cycles it took I recalculated the constant with this
mot.Control(145_325 / StirSpeed,power)
Looking over your code I cant help but ask why you and others clear variables when they are cleared to began with.
You called
Why clear "coil" when this will always be %0 on a new boot. Is it not a local variable?
Thanks none the less for the help
you are wrong there. The only 'local' variable initialised is RESULT.
all other local variables are NOT initialised by spin but contain whatever is at that stack location from earlier use of that adress.
Enjoy!
Mike