Author Topic: Pythonic control system programming  (Read 2095 times)

0 Members and 1 Guest are viewing this topic.

Offline DeltaTopic starter

  • Super Contributor
  • ***
  • Posts: 1221
  • Country: gb
Pythonic control system programming
« on: November 16, 2018, 07:44:26 am »
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 in
Read inputs
process / logic
Write outputs
repeat

For 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: [Select]
while True:
read_GPIO_inputs()

if PB1_pressed:
output1_timer = time_now() + output1_on_time

if output1_timer < time_now():
output1 = True
else:
output1 = False

write_GPIO_outputs()

sleep DELAY_TIME


This 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!
 

Offline IconicPCB

  • Super Contributor
  • ***
  • Posts: 1631
  • Country: au
Re: Pythonic control system programming
« Reply #1 on: November 16, 2018, 08:43:53 am »
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.
 

Offline JVR

  • Regular Contributor
  • *
  • Posts: 201
  • Country: be
Re: Pythonic control system programming
« Reply #2 on: November 16, 2018, 09:21:45 am »
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.
 
The following users thanked this post: kony

Offline DeltaTopic starter

  • Super Contributor
  • ***
  • Posts: 1221
  • Country: gb
Re: Pythonic control system programming
« Reply #3 on: November 16, 2018, 10:26:53 am »
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.

Erm, 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.


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.

Yes, 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.

 

Offline JVR

  • Regular Contributor
  • *
  • Posts: 201
  • Country: be
Re: Pythonic control system programming
« Reply #4 on: November 16, 2018, 10:50:56 am »
Yes, 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.

Ah, 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.
 
The following users thanked this post: nugglix


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->