4. Use an encoder-reader IC. Does this thing even exists? I think I've seen it somewhere, but I can't really remember. Its an IC that is made specially for reading encoders.
It exists but I don't remember name....
Anyway, encoding is a matter of code.... You really need only one MCU for a quadrature encoder or maybe too....
How to do it....
Let's say you have a global clock that ticks it's own way on your system... It's 2am in the MCU 2am at the encoder (realtime system)...
In order to do parallel processing you need to set time as small small small fragments...
The MCU will read the ports and do all the math in that fragment.
You set that fragments with Timer interrupts, and let's say that our fragment is 1ms for example.
Take a look at the code below
' Timer interrupt is set to 1ms
Do
IF a = 1 THEN
a = 0
CODE
CODE
CODE
CODE
ELSE
LOOP
TimerInterrupt:
a = 1
RETURN
' Note that this code is executed in less time than the 1ms clock circle.
But if you want to count pulses then what???
Well..... in that case....
We need some more theory...
A signal from an encoder is typically a wave, to detect fronts of this wave you need the transition from low voltage to high
or reverse...To do that you simply need to read the port state. Once every tick of the interrupt.
Measure some ticks for some time then change the speed to the motors respectively.
So the code will look something like this
' Timer interrupt every 1ms
P0 = Pin(from one encoder)
Do
IF a = 1 THEN
a = 0
IF b < 1000 THEN
P1 = Pin(from encoder)
IF P1 <> P0 THEN
C = C + 1
ELSE
ENDIF
ELSE
b = 0 ' 1 second has passed
ENDIF
P0 = P1
GOTO/GOSUB Motor_corrections
ENDIF
LOOP
And... well I hope you get the rest of the concept :-P