Large FIFO queue

Thread Starter

imalearnin

Joined Oct 7, 2022
13
I have an optical rotary encoder that has 2 channels with 2400 pulses per revolution. I am reading it just fine with my Arduino Uno using interrupts but I want to run some experiments with it so it "appears" I am turning the shaft slower than I actually am. There are multiple pieces of hardware connected so I can't just turn it slower without using some mechanical gearing or belts.

My thoughts were to read the pulses via interrupts and store them in a FIFO array (queue). The array would store the actual time in microseconds multiplied by some factor between 1 and 10 or so for each interrupt. I would then output the signal to a different pin when the actual time equaled the time in the array thus effectively making it look like I turned the encoder slower.

The issue I have is that this would require a relatively large array with each element storing the time in microseconds (probably as a long int). As the data is output, the array size would decrease and eventually going to zero if I stopped turning the shaft.

I would like to turn the shaft for at least 10 revolutions for my experiments, so the number of time stamps would approach 2400*10 or 24,000 per channel. (it would actually be somewhat smaller depending on how many data points were dequeued in that time-frame).
Is there a microprocessor that could handle this? This will not be a permanent thing but just for some experiments I want to run.
I suppose a Raspberry Pi would be able to handle this but is there something smaller/cheaper that would work?
Or is there a better/alternative approach to accomplish this that I am not thinking of?

Thanks
 

WBahn

Joined Mar 31, 2012
32,706
Why not just respond to every nth pulse.

I'm assuming your two channels are because the encoder is quadrature encoded, so you know which direction the encoder is going for each pulse. Thus you can maintain a counter for each channel. Output a pulse for each channel on your output pin whenever the counter reaches n (and reset the counter back to zero) or goes below 0 (and reset the counter back to n-1).
 

Beau Schwabe

Joined Nov 7, 2019
186
The Arduino may not be fast enough unless you write the code directly in assembly. Some micro's have a CLC (Configurable Logic Cell) that can atomically (<-run by itself without CPU clock) decode the quadrature as far as pulse and direction, then it's just a matter of directing that signal to a counter. I am not sure if the Arduino has that capability. ... alternatively the decoding section may need to be in external hardware to the Arduino if the quadrature signal is super fast.
 

joeyd999

Joined Jun 6, 2011
6,204
My thoughts were to read the pulses via interrupts...
If it's a quadrature encoder, keep in mind that an encoder has two output channels, and there is useful information on both the rising and falling edges of each channel.

It is far easier to implement a driver on an MCU with an interrupt-on-change (IOC) feature than one with just external interrupts.

I've posted an example of some encoder code for Microchip PIC 18F here, and it could be modified to do exactly what you want.
 
Top