Pythonic control system programming - Page 1
Forum section: electronics / general

author: Delta
author_rank: Super Contributor
timestamp: November 16, 2018, 07:44:26 am
content: I am looking for some general guidance as to the correct / best / pythonic approach to writing basic control system programmes in Python. In short, I am looking to use a RasPi to perform basic PLC-style control of hardware.I have worked with PLCs a lot professionally (Siemens SIMATIC S7 in particular - a bit of programming but mainly debugging during commissioning and digging through the code to aid with hardware fault finding), the RasPi stuff is just a hobby, but I am struggling to stop thinking of programming in a PLC-type way; as inRead inputsprocess / logicWrite outputsrepeatFor a basic example. say I want to use the RasPi to read pushbuttons, and when pressed set outputs high for X seconds.My immediate instinct is just to run everything in one main loop (with a sleep delay to prevent 100% CPU usage) such as in the following pseudopython (ignoring debouncing, edge detection etc)Code:while True:read_GPIO_inputs()if PB1_pressed:output1_timer = time_now() + output1_on_timeif output1_timer < time_now():output1 = Trueelse:output1 = Falsewrite_GPIO_outputs()sleep DELAY_TIMEThis approach does of course work, but I feel it is not the "correct" way, especially as the system and programme grows. I'm sure there are much better approaches, using threads, flags, callbacks, interrupts etc, but I'm even struggling to find the correct Google search terms to help me start!Any advice would be much appreciated, and I hope you can understand what I am trying to ask!

author: IconicPCB
author_rank: Super Contributor
timestamp: November 16, 2018, 08:43:53 am
content: Forget al of that and look at LinuxCNC software.Part of LinuxCNC is Classic Ladder ( i think thats what it's called.This is a stand alone PLC program already implemented in either Raspbian or Ubuntu or RaspberyPi.

author: JVR
author_rank: Regular Contributor
timestamp: November 16, 2018, 09:21:45 am
content: Don't force the system to conform to your knowledge/experience. Build the knowledge to use the system to its best abilities. A RPi is a SBC, not a PLC, so you should use it to program a PLC, not be a PLC. If you like python, then start by just following tutorials, I've been doing C,C++,C$ for the last 15 years, and did my first python a few months ago. There are really good mature tutorials out there, use them.

author: Delta
author_rank: Super Contributor
timestamp: November 16, 2018, 10:26:53 am
content: Quote from: IconicPCB on November 16, 2018, 08:43:53 amErm, thanks, and that's interesting; but the whole point of doing this is to learn something new - not just use an SBC to emulate what I already know.Quote from: JVR on November 16, 2018, 09:21:45 amYes, that is what I am trying to do. I have done a bit of python already, and have one project successfully running for months (TCP sockets and data processing stuff), and I have read many good guides and tutorials. But what I am asking here is specifically how to structure a programme for this kind of thing. (I have no formal education in programming / computer science etc, so what might seem obvious philosophies / structures / paradigms / etc may not be obvious to me.

author: JVR
author_rank: Regular Contributor
timestamp: November 16, 2018, 10:50:56 am
content: Quote from: Delta on November 16, 2018, 10:26:53 amAh, here you enter "here be dragons" territory. Google things like Design Patterns, OOAD, etc, that will put you into a rabbit hole of the internet where dick swinging is a large part of the argument. As a beginner in software, don't stress the paradigms too much, just build stuff that interests you.You will reach a point in the project where you will think that there must be a better way to do this, and there generally is. This is the time that you learn about that. I don't know enough of Python to help you there directly. I'll much rather write code in C-like languages, where I am in control, not the VM or the compiler.A good start, is to read a few books on software architecture, not the high level stuff, but the down and dirty, drawing data flow diagrams, sequence diagrams etc, with those at hand, writing the code becomes trivial, as the design is done. It also makes for mostly less shitty code.