0

I have seen seen in many places that the Raspberry Pi does not really work well with realtime control, but I am wondering if reading from input pins can be interrupted by the operating system.

In other words, if I have the following code:

GPIO.add_event_detect(IR_PIN, GPIO.BOTH, callback=cb)

and I run that from a Python program in a while loop - is there any way that the operating system would cause Python to miss an edge detection?

Would the raspberry pi ever pause execution of the executing program for some reason?

Startec
  • 297
  • 1
  • 3
  • 8
  • The modulated frequency messages. – Startec Apr 22 '20 at 01:28
  • @jsotola updated. – Startec Apr 22 '20 at 02:20
  • this article indicates that RPi GPIO interrupts are emulated in software ... that means that Linux system interrupts take precedence ... https://roboticsbackend.com/raspberry-pi-gpio-interrupts-tutorial/#How_Raspberry_Pi_GPIO_interrupts_work – jsotola Apr 22 '20 at 02:52

1 Answers1

1

Linux as shipped with the Pi is not a real time operating system but version do exist called RTOS or RT Linux and some kernel mods for the Pi have been seen in the wild to give some added ability.

Add onto this, Python is an interpreted language and can suffer from the Global Interpreter locking threads let alone from the OS wanting the CPU for its own tasks.

Python brings its own multi-threading and multi-process libraries to help getting more than one thing done.

You are always limited by the OS, devices attached to the CPU and native programming so ‘real-time’ is not something a multitasking computer is very good at.

In your case, it is always possible to lose data (see Tom Scott on YouTube for why no message can be 100% guaranteed) and this is why check routines and hardware signalling has been part of computing since day one.

Remember some communications subsystems on the Pi are handled by the OS (disk / serial / USB) and they operate at speeds in excess of normal GPIO based data transfers by building in checks and correct hardware design.

If you are looking for robust communication, you need to look at:

  • Interrupt driven capture so the link disrupts the normal program flow not the other way around.
  • Limiting what the machine is doing outside of data communications
  • Have a fast enough processor and memory and disk etc etc to handle all the demands
  • Building control signalling (either hardware or software) into the link
  • Building checks into the data so you know it’s correct or not
  • Look at using buffers (Python Queues being one way) where processing is separate from capture
  • Look at using extra hardware to provide a buffer between the high speed data and the slow speed data.

I think you will be surprised as to the amount of data and speed the Pi can handle BUT it depends on the application and connections.

  • See https://raspberrypi.stackexchange.com/questions/108896/what-is-the-max-i2c-speed-of-the-raspberry-pi-4 for getting data over I2C and the speeds it can handle in Python. –  Apr 22 '20 at 03:12