32bit counter to 64bits
Mickster
Posts: 2,698
Now that we have 64bit integers I would like to extend the range of the 32bit quadrature count. I can think of several ways to accomplish this but they will all be clunky
Anyone have a bit of slick-trickery to accomplish this, please?
Craig
Comments
I'd use the delta readings you get when X register is set. Upon each period, just accumulate the resulting delta into a 64-bit variable. Here's a C example:
So each _rdpin returns the delta from the previous _rdpin?
So rollover never occurs?
Edit: If the _pinread function = false, there is no delta to be read?
Craig
Technically -1 is an unsigned rollover. On that note, it might work better to cast the delta to signed and make the 64-bit variable signed too. Not sure, I haven't actually tested anything.
Correct, you'll just get the pre-existing delta until a new one is ready. The X timer can be used as the servo loop timer - can also generate a metronomic event or interrupt.
This is pretty exciting stuff
Yeah the aim is to end-up with signed 64-bit.
Many thanks.
Craig
I think it's working okay as is. Signed doesn't seem to matter. However, FlexC's printf() is doing weird emits with 64-bit negative values. If I use %d instead of %lld then I get what appears to be a pair of 32-bit signed numbers printed that would be correct as a combined 64-bit integer. Eg:
-1, -1
for negative one. But with %lld it prints a huge negative number when it should be negative one. EDIT:-18446744069414584321
If you're programming a continuous rotary machine that flows in one direction, then there's no need to go 64-bit. The 32-bit rollover is clean. You don't get overflows or accumulated errors as long as you're using true integers.
What I did was do all the profile calculations with velocity and curve fitting using floats but always converted back to integers for the final positional mapping. Everything neatly packed into exact lossless full turns.
Partially resembles unsigned 64-bit:
18446744073709551615
%lld is a 64-bit signed integer emit. It ignores the actual data type of the variable, although will be bound to the variable's memory space.
Not so much need 64-bit but if it can be had, it would be nice to have a position range of -9223372036854775808 to 9223372036854775807 quad-counts.
Craig
There was a bug with casts from 32 to 64 bit values. It should be fixed in github now.
Fixed indeed.