We are using the EQEP module to monitor QEP inputs to track the position of our motor. The code we're using is largely based on the motorware lab 12 a example. We're using quadrature count mode, using the A and B inputs to determine encoder count and direction, and resetting on max position. Frequently we run the encoder through thousands to hundreds of thousands of revolutions in one direction, then turn around and run it a comparable number of total revolutions in the other. We are not using the I input. We see a rare but devastating problem in calculating the total position or total encoder tick count in a particular direction, which is # of times we have rolled over the max position*#ticks in a revolution + # ticks since a rollover.
Our problem is that occasionally, near a rollover, we read the overflow/underflow flags, then read the encoder count, and calculate our total # ticks from those separate reads. The motor is still moving while we are making this calculation. Unfortunately, occasionally the encoder rolls past the rollver point between the time we read the flags and the time we read the encoder count. We then of course get a position that is off by approximately one encoder revolution.
None of this is interrupt driven. We calculate the position as needed by our system. So, some questions for the forum:
1. What is an appropriate way to perform an atomic read of both over/underflow flags and encoder count or otherwise use an appropriate latched position with flags?
2. Can you provide some guidance on the sizing constraints for QPOSCNT? Specifically, one temporary improvement I was hoping to employ for our present problem is simply to make the QPOSMAX very large, so that we actually roll over only very rarely. (Not a solution, obviously, just an improvement.) With a 32-bit int or uint, there's plenty of room for this. But the sample code from lab 12 and our code uses the Enc_getRawEncoderCount function that casts the QPOSCNT as a 16-bit uint. I see this number is multiplied by several other 32-bit ints and ultimately saved to a 32-bit int. And from experimentation I see that I can't even use the whole range within a 16-bit uint wihtout causing errors that are presumably related to generating overly large numbers by multiplying several 16 or 32 bit numbers together and saving as a 32 bit int. Can you share your thinking about the approach presented in the sample code?
Thanks!
Regards,
Janet