Kenny
Joined: 07 Sep 2003 Posts: 173 Location: Australia
|
|
Posted: Sat Mar 07, 2009 11:13 pm |
|
|
The counter will work in the range of 0 to 0xFFFF (65535). The range can be extended by sampling the count frequently, and adding the difference in sampled values between successive samples to a signed int32 variable. This difference may be negative, so signed arithmetic is necessary.
Code: |
signed int32 position = 0;
signed int16 new;
signed int16 old = 0;
signed int16 difference;
|
The following needs to be in a loop. The count has to be sampled faster than half the shortest time it would take for the count to change by the width of the counter. eg. if it takes the qei counter 10 seconds to go from 0 to 65535 at the fastest expected speed in one direction then the count has to be taken faster than every 5 seconds. Sampling faster than needed would be OK.
Code: |
new = qei_read();
difference = new - old;
position += difference;
old = new;
|
Obviously, if the direction of rotation is one way for a long period then even the signed int32 value will also overflow. However, for a situation where there is a finite number of rotations, for example in a machine with a lead screw, this works well. |
|