I've an electric car that I need it to travel specific distance. In order to do that I've an 2 phase rotary encoder attached to one of the rear wheels.
I am currently driving all things using a Raspberry Pi, USB Joystick and a couple of logic level converters. Raspberry Pi's reading events created by the joystick in a infinite loop.
I've written the code using Python. If I count pulses in the main scope, I'm getting no problems but car may move after counting ends. Therefore I need to continiously count pulses in another thread. So I can know how much car literally moved. I couldn't get my program working that way. I've used threading, multiprocessing. However, I couldn't get these 2 threads working properly. Sometimes, pulse counting thread miss upcoming pulses and sometimes, joystick reading thread becomes unresponsive.
How can I count up encoder pulses while reading joystick at the same time?
Ideas that I came up with:
Using a microcontroller that doesn't consist Linux (like ARM M
series, Arduino) and count encoder pulses using that. Then
communicate it with Raspberry Pi over UART, I2C or SPI.
Counting pulses over hardware instead of software.