-1

Having used Python for a simple robotics platform for experimentation with machine learning I've found Python lacks in supporting hardware in anywhere near real time. Polling the GPIO using C is noticeably faster than in Python, as the motors run much more smoothly, and the likes of ultrasonic sensors are more accurate as the interval between polling is reduced.

The ideal would not to have to poll the GPIO at all, but to rely on an interrupt driver. Python is not suitable for writing such code, since it has no support for real-time processing. I don't doubt C would do the job as would Ada or perhaps Rust, but I'm lacking documentation on how to write the IRQ handler in either language.

9
  • I don't understand what you are asking. If you are using Linux then it handles interrupts and there are hundreds of tutorials showing how to get GPIO interrupts. If you are using some other operating system you need to say what it is in your question. – joan Aug 7 '20 at 11:20
  • 1
    @joan pardon me if I’m telling you how to suck eggs but the tutorials you refer to rely on a thread running in a loop testing the pin to see if it’s state has changed. If the loop is not quick enough then the pin could change state twice in one cycle so you wouldn’t catch the change. An IRQ handler is tied directly to the hardware. As soon as the pin goes high the CPU is interrupted immediately, dumps any necessary state, runs the IRQ handler code, restores the state and continues. There is of course a danger that the handler is called while a previous interrupt is being handled. – Nick Aug 7 '20 at 12:08
  • No they don't busy spin, they wait for interrupts. Have you actually searched for Linux interrupts? – joan Aug 7 '20 at 12:19
  • 1
    @joan yes, there’s not enough space in a comment :-( I’m perfectly aware the Linux kernel handles interrupts, though it does not ‘wait’ for them exactly, as I explained. I’m looking to add my own IRQ handlers direct to the GPIO pins. – Nick Aug 7 '20 at 12:25
  • Are you looking for something userspace or are you ready to dabble into kernel-space? – MaxDZ8 Aug 7 '20 at 12:36
1

You should probably go for an Arduino if performance is really important to you. There are many ways you can get a Raspberry Pi to communicate with an Arduino if you need a Pi to be in your project.

If you have decided on using a Raspberry Pi only, you can use WiringPi if you want to control the GPIO pins with C/C++. You can read the documentation for it here. For more info on programming the GPIO Pins with C/C++, you should read the "Programming with GPIO" section on the Official GPIO Docs. I also found a blog post with instructions on how to go about using C/C++ to work with the GPIO pins.

0

You may want to have a look at the Raspberry Pi Python package RPi.GPIO. It monitors the GPIO pins and allows you to specify callback functions that are called when the pin state changes.

3
  • The first line of that package documentation says "Note that this module is unsuitable for real-time or timing critical applications. This is because you can not predict when Python will be busy garbage collecting." Python is not suitable for my task, as I said. – Nick Aug 7 '20 at 10:49
  • My impression was that you didn't want to use Python for the interrupt driver but would otherwise use Python for the application. – Ole Wolf Aug 7 '20 at 12:33
  • Once I drop down to the IRQ handler level only the code in the handler must run, which excludes the Python interpreter. It may also require the use of semaphores or similar to ensure mutual exclusion. Python outside of the IRQ handler is fine so long as any interaction between the two is safe. – Nick Aug 7 '20 at 12:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.