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.

migrated from electronics.stackexchange.com Nov 9 '18 at 16:33

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

  • 2
    I don't think the Raspberry Pi is a real time system, and that is probably the cause of your problem. The ideas your came up with are good. Either use a hardware timer/counter that won't be affected if your software is busy with other tasks, or move the timing critical part of your design into an external MCU. – Peter Karlsen Nov 9 '18 at 8:53
  • Posting your code would allow people to help you better. You have not explained or shown why your code needs to be multithreaded, or what you want it to do vs what it is currently doing. RPI is not a real time system, but it can still be very efficient with proper code. Doing it with a micro controller is probably the BEST approach, but it does introduce a whole new level of complexity dealing with two code bases. – Chad G Nov 9 '18 at 17:06
  • Running Linux on the RPi means other processes will interrupt your code and you might miss pulses. Making it worse python uses a GIL (global interpreter lock) for it's threads so only one thread might run python code at any time. So your pulse counting thread will easily be blocked too long. At a minimum you should have a separate process count pulses and send the count to the main program through e.g. a pipe. – Goswin von Brederlow Nov 19 '18 at 15:35
  • A simple solution in hardware might be to use a 4 bit binary counter (e.g. 74169) and connect the 4 output pins to the RPi. That buys you more time to read pulses, you only loose count when you skip 16 pulses. If that's not enough then use a micro-controller and connect the pulses to an interrupt pin. – Goswin von Brederlow Nov 19 '18 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy