measuring microseconds
kenken
Posts: 13
I am trying to make a precise and synchronous stepper motor control for 2 motors. But I just can't figure out, how my ON an OFF pulse can be precise over a long time.
A cog handles the pulse routine (simplified):
so I tried to constantly measure the time of the loop and adjust (shorten) the period in which the OFF phase is actually waiting ... (simplified)
But how can I reliably measure and compare these short periods?
* First I tried it with saving the clockcounter:
and compare it later with the new value of cnt
But it didn't manage to handle the fact that the counter is 32-bit unsigned and also cycling. So measuring continuously was not possible.
* Then I tried to use a seperate cog as counter but I only managed to get a resolution of 40 microseconds. below that the counter just refused..
For this second approach I would need a reolution of 5 microseconds.
Can anybody please help me?
A cog handles the pulse routine (simplified):
repeat ON wait for e.g. 200 microseconds OFF wait for e.g. 200 microseconds the routine does something else and loses important microseconds
so I tried to constantly measure the time of the loop and adjust (shorten) the period in which the OFF phase is actually waiting ... (simplified)
repeat ON wait for e.g. 200 microseconds OFF wait for e.g. 200 microseconds - correction measure the loop and calculate correction
But how can I reliably measure and compare these short periods?
* First I tried it with saving the clockcounter:
time:=cnt
and compare it later with the new value of cnt
realspeed := cnt - time
But it didn't manage to handle the fact that the counter is 32-bit unsigned and also cycling. So measuring continuously was not possible.
* Then I tried to use a seperate cog as counter but I only managed to get a resolution of 40 microseconds. below that the counter just refused..
PUB Main musecond~ result := cnt repeat waitcnt( result += 3200 ) 'every 40 mus musecond += 40 if musecond == 2_000_000_000 musecond := 0 Pub getMusecond return musecond
For this second approach I would need a reolution of 5 microseconds.
Can anybody please help me?
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Links to other interesting threads:
· Home of the MultiBladeProps: TriBlade,·RamBlade, RetroBlade,·TwinBlade,·SixBlade, website
· Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
· Prop Tools under Development or Completed (Index)
· Emulators: Micros eg Altair, and Terminals eg VT100 (Index) ZiCog (Z80) , MoCog (6809)
· Search the Propeller forums·(uses advanced Google search)
My cruising website is: ·www.bluemagic.biz·· MultiBladeProp is: www.bluemagic.biz/cluso.htm
Yu can use a second COG with a couple of waitpeq/waitpne for the output pins and report the difference (reading CNT before and after) and write this value back in a HUB variable.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Visit some of my articles at Propeller Wiki:
MATH on the propeller propeller.wikispaces.com/MATH
pPropQL: propeller.wikispaces.com/pPropQL
pPropQL020: propeller.wikispaces.com/pPropQL020
OMU for the pPropQL/020 propeller.wikispaces.com/OMU
You will need to work through the examples in the manual to get some ideas for starters.
Your pasm code will be something like... (lots missing and probably not the simplest but will give you an idea)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Links to other interesting threads:
· Home of the MultiBladeProps: TriBlade,·RamBlade, RetroBlade,·TwinBlade,·SixBlade, website
· Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
· Prop Tools under Development or Completed (Index)
· Emulators: Micros eg Altair, and Terminals eg VT100 (Index) ZiCog (Z80) , MoCog (6809)
· Search the Propeller forums·(uses advanced Google search)
My cruising website is: ·www.bluemagic.biz·· MultiBladeProp is: www.bluemagic.biz/cluso.htm
Hopefully I will understand it [noparse];)[/noparse]
The above code should cycle pin zero with a period of exactly 400 micro seconds, and a duty cycle of almost exactly 50%. This should be accurate down to one clock cycle independent of clock frequency. (with too slow a clock this code will hang) This code has exactly the same code after the WAITCNT to set or clear pin zero which effectively hides the overhead of spin. Also, computing the timing targets outside of a WAITCNT allows any overhead in the WAITCNT itself to be hidden.
Lawson
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Lunch cures all problems! have you had lunch?
I've used that exact code and it works just fine. The rollover of CNT is handled because all math in spin is 32-bit and based on two's compliment so sign can be ignored much of the time on real hardware with limited precision.
Lawson
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Lunch cures all problems! have you had lunch?
vary in its execution-time and the waitcnt catches up the differences so that you get a rock-solid
frequency-output.
If you want to drive two steppermotors at different speeds but still absolutly synchron
you can use the bresenham-algorithm. This algorythm is fast because this algorithm just
needs addition and substraction to do all the math for calculate pulse-times
see attached file
best regards
Stefan