Negar
Hello Friends,

As far as I know OpenPLC runs on top of an Operating System like Raspbian (for Raspberry  pi).
Lets say we know that Raspbian is not a Real Time OS by Default. Then how can we alter OpenPlc with real PLC. (As we know one of the most important characteristic of PC is having Hard Real time OS).

That would be great if anyone can give me mode information🙂
Quote 0 0
thiagoralves
This would require a major change in the runtime architecture. Some people tried in the past to shrink the runtime to make it fit on an atmega microcontroller. You can search the forum for more information about their attempt.
Quote 0 0
kg5tfj
Negar,

Short version: you can use the CONFIG_PREEMPT_RT kernel option

There has long been a Linux side-project which develops and maintains realtime extensions to the Linux kernel.  Though Linus Torvalds said long ago something like "I will not accept real-time patches, but I will accept good patches", much of the important work of the real-time project has been incorporated into the kernel as "good patches" which are generally available if the kernel is compiled with the CONFIG_PREEMPT option.  I believe the Raspian distro (and most) kernel is compiled with the CONFIG_PREEMPT option.

However, there are more, hard realtime kernel options available as a patchset from the Linux RT project.  At least one person has incorporated that patchset into the Raspian kernel, compiled it with the CONFIG_PREEMPT_RT option (which does not exist without the RT patch) and provided information here.  Note that to take advantage of the realtime features, you must select a realtime scheduler (at runtime) and use any necessary realtime kernel functions/methods in your code at compile time.  Some self-education is required.

Using CONFIG_PREEMPT_RT properly can reduce the maximum latency to double digit (XX) microseconds on most modern processors.  There are test tools for the RT patch that can help you determine the max latency on your (RaspPi) platform.  The test tools source code can also be used as examples to help understand how to do realtime with the RT patch.

Hope that helps!
Quote 0 0
kg5tfj
Now that I've thought about it a bit - don't hold your breath - but I may do some tests with both CONFIG_PREEMPT and CONFIG_PREEMPT_RT.  There may be ways, just with existing schedulers and priority schemes, to achieve some reasonable determinism without too much effort.  It's been a while since I worked to hard realtime deadlines in Linux and then I used the RT patch.  I'll have to study up again on what's possible with the mainline kernel.

Negar, do you want FAST and deterministic or would just a hard, max latency be good enough for you?

Oh, and though I'm new to OpenPLC, I'll guess that any hard realtime that I will consider must be limited to processes and IO handled directly by the host executing the OpenPLC runtime.  I.e. on the Raspberry Pi, not some slave device.
Quote 0 0
thiagoralves
kg5tfj, it would be great if you could contribute to improve OpenPLC's real time behavior. I had pretty good results with isolcpu on the bootloader parameters. Check  https://openplc.discussion.community/post/problem-with-simple-project-10310920?pid=1309893961&random=21082
Quote 0 0
kg5tfj
Your using the FIFO scheduler is an excellent start, but how well it works depends on the kernel config.  I don't have a Raspberry Pi to play with right now, but my Orange Pi's Armbian kernel is NOT compiled with any PREEMPT option.  CPU isolation certainly helps.  It would be lovely to have a real-time configuration that's achievable within someone's stock OS distribution.

I'll have a proper Raspberry Pi 3 B this by weekend and I'll keep an eye out for real-time performance in general as I find my way around in here.
Quote 0 0
kg5tfj
Got my new RPi 3 B today and installed latest Rasbian Desktop.  It's kernel is not compiled with any PREEMPT options, but it turns out there's a semi-official -rt patched kernel project (check different branches for different kernel versions) that seems to be kept up to date so far, making it a fair bit easier for the regular folks to have a real-time kernel for their Pi.

I'll keep this RT topic in mind as I continue to learn about OpenPLC and its peripheral packages.
Quote 0 0
thiagoralves
Nice finding. Let me know if OpenPLC (without changes) behaves better on the PREEMPT_RT patch kernel.
Quote 0 0
Robindev
Nice finding. Let me know if OpenPLC (without changes) behaves better on the PREEMPT_RT patch kernel.


Hello,

I did some work in the past to create a real-time ethernet packet generator using linux on a MPC8313e powerpc board for my thesis(unfortunately it's in dutch) and came to the conclusion that PREEMPT_RT at thewill not allow hard realtime guarantees, as there are still some kernel drivers that are allowed to take priority over the process. (raw_spinlock critical regions, not sure how many times those regions will be hit in most common usecases).

At the time I saw adeos-ipipe as the most reasonable alternative. This is an interrupt pipeline where some interrupts can be given priority over linux no matter what happens, thus guaranteeing hard real-time behaviour, while allowing the linux kernel to persist next to it. A fully real-time linux extention taking advantage of this is RTAI (https://www.rtai.org) or xenomai(https://gitlab.denx.de/Xenomai/xenomai/wikis/home) and might be a better fit. So far I have not tried the raspberry pi support that xenomai provides, and it might require  some work to get openplc and its dependencies to play nice, but I think it would be the best candidate if hard realtime is desired.

UPDATE:
re-reading some articles about PREEMPT_RT seem to indicate my previous notion is/was invalid, and PREEMPT_RT should be able to perfectly fit your needs, as long as the process gets enough priority. That being said, I think ADEOS-IPIPE/Xenomai can still be a valid alternative, but might be the more labour intesive route. Sorry for any confusion caused.
Quote 0 0