Category Archives: Plain

Sorting source code

I admit it sounds a bit ludicrous, but right now I miss an source code aware sort function in my IDE. If you start maintaining large files with loads of structures or functions you often end up paging up/down looking for them – to reduce this time I start sorting them in alphabetic order.

enum vm_OpCode
 Op_NOP =8,

The struct above is another example – I sorted it to easier keep things synchronized – doing so is actually an increase in quality because it means the developer uses less time looking for things and get a better overview faster – each of these also have structs, execute functions etc.

PLC Backbone Bus

Just updated the pin numbers on the PLC Backbone Bus. The idea here is that we have 4x RS-X lines (2 optional) and 5V,12V,24V,48V supplied from A and B source. The boards select what power they need. I need to connect to a PSU & battery before I am sure that this is what I want to do.

48V is mostly for higher motor effects. 24V & 12V are quite handy to have around, while 5V is an absolute requirement – thought I might actually ditch 5V and supply a separate 12V for MCU expecting the boards to regulate 5V/3.3V themselves. The idea of having a separate MCU power is to avoid the need for galvanic isolation as any effects are drawn from separate power lines.

PLC Backbone 3D

This is just an early 3D model of the minimum backbone bus just to illustrate and work on practical issues. I am going for the more minimal bus for now. The size of the bus do not decide the size of the boards, but I do like this small size. I will however adjust the actual size to practical applications.

What is missing here is a better way of interconnecting several boards and connecting to the PSU. But, I am happy with the PSU lanes – I had no problem using 2mm wide lanes on each side for GND,12V,24V,48V on both A and B. Simply said we can handle some currents in the backbone.

I added 8 slots expecting that we will use 4, but I also miss mechanical fittings of the IO boards here – as I plug IO boards in I need a mechanism to hold them in place.

It is several design considerations that is not solved yet – vibration, interconnection between boards, fixing of sub-boards, connection to RS-X networks etc. But, we need to start somewhere.

PLC Backbone 2

The previous proposal included a lot of data/address/function pins that actually is a bit obsolete design as we use multiple RS-X channels. This smaller bus uses 2×20. I can use the same width as for a Hat, but maybe a bit deeper boards.

I need the PLC system to be small and mobile to fit robots. I wanted a wider system to get more connector space, but I can add 2nd slots for those few cases. In short – I feel that increasing the size would be a mistake.

It is an alternative – and I could even do both …

PLC Backbone Bus

If I use a 10cm wide backbone I have room for a 2×30 (or 3×30) by 2.54 pitch “bus” that could contain the following Inventory:

Summarised this gives me:

  • 4 pin 5V PSU (5-10A)
  • 4 pin 12V PSU (5-10A)
  • 4 pin 24V PSU (5-10A)
  • 4 pin 48V PSU (5-10A)
  • 4 pin GND 5V
  • 4 pin GND 12,24,48
  • 8 pin data bus
  • 12 address bus
  • 10 function/extra address pins
  • 8 pin for 4 RS-X lines (RS485)
  • 2 clock pins

This is just a working draft…


PLC Standards

The more I look for any real standards within PLC the more I get the impression that they either do not exist or is not used. This is consistent with what automation engineers tell me. I was kind of getting my hopes up with “Industry 4.0”, but well… lets crack on with creating our own.

Plain VM – Instructions

The blog entries show how Plain VM/Assembly evolved, but they can be a bit confusing to read stand-alone. So, I need to clean-up the content here a bit. The exercise now is to write up Plain Assembly Reference, Plain VM Reference and make sure the two documents are synchronized and consistent. I had a working core before, but need to change it to adapt to my latest ideas. Looking at my first register only VM I admit that is much simpler, but I am really keen to get instructions like Transaction and syntax supporting distributed processing and parallelism going. Below is an updated VM Instruction table – the text is fresh from a libreOffice document that will be uploaded later.

Assign Assign an expression to a variable. Execute a pre-parsed expression tree and store the resulting value in a variable. Expression can either be written as an algebraic math expression or a list of simple operations separated with ;

Call a function. Call creates a return entry on the stack and will continue with next instruction as End of the function is reached. Call can in contrary to raise also capture event’s raised by Raise.

Decode Unpack a bit field
Encode Pack a bit Field.

End a conditional block of code by jumping to a pre-calculated ix on the stack. For (as an example) will insert it’s own ix on the stack, End will jump to that ix and remove it from the stack.

Exit Exit the VM. Will terminate the current module.

For Loop. Will create a stack return entry causing the next End instruction to jump back to the loop start.

If If statement to allow if..elsif..else with expressions.

No Operation. Basically not needed except usefully for testing basic performance.


On capture event’s raised by a Call or State. It contains parameters defined by the Event definition in a Func.


Call an event with parameters, but without creating a return entry. Raise remove the stack, but will leave the stack entry point. The next end will return to the last “call”.

Switch Switch to a selected value.

Will wait until a listed event is raised before it process that event and continue.

While Loop while an expression is true.

Lock access to listed “interface” variables. Transaction will wait until access is granted or we time out. Transaction can also “spawn” in which case the transaction block is treated as a function in a different, local module that execute in parallel. See instruction Spawn.


Update complete a Transaction by either Commit or Rollback changes.


Can be called separate inside a Transaction block to commit part of the changes made.


Can be called separate inside a Transaction block to reset changes made. Changes are reset to the last commit.


Spawn call a function located in a different module. This function is executed in parallel and the returning event is inserted into our event queue.

PLC Modular System

I have worked far to long with 19″ cabinets and things you mount from front, so what I am thinking is a micro-version of a rack system. We box each electronic module with a backbone plug and custom front connectors. We then plug them in, wire using standard wiring (that we probably have to create) in front – no wiring in the back.

These boxes are simple and can easily be printed on a 3D printer. They will also allow us to mount more electronics tighter to address the total size.

A classic PLC uses 2 wires for a 24V pulse signal – we can typically standardize these so that we apply standard, plug & play cables and avoid as much custom wiring as possible. The top front is after all for wiring to equipment, not for internal wiring that is already done in the back-plane. I think this can work, but I need to talk it through with professional automation engineers – luckily I have access to them in numbers.

One drawback I can see straight away is vibration. I was planning to make this so small that it could fit mobile Equipment, but mobile Equipment vibrate a lot. We will need an outer box and holding mechanism that tolerate very high vibration – or more correctly reduce vibration.

Open PLC System

PLC (Programmable Logic Controllers) have been around for years. It usually is a modular system of electronic boxes with IO capabilities that can be assembled into a system for automation purposes. PLC have 5 “standard” languages that are not really that much standard. The standards tend to serve as minimum list of features, but as the vendors add much proprietary stuff the PLC applications end up being very proprietary. Plain have a much higher possibility of actually being portable.

A PLC system cost a lot of money, but as making custom electronics and writing code in C/C++ cost more the vendors get away with high pricing on electronics, tools and HMI. Also – the hard fact is that it is not much I can do with a professional PLC that I can’t do better, faster and with less cost using Arduino or Raspberry PI these days.

I have briefly mentioned HMI – I will return to that later. But, my Raspberry PI Hat’s and RS-X modules powered by Plain is an excellent PLC substitute. I do however fancy a more dedicated, modular system that can compete with classic PLC in a smaller space. My Hat’s are great, but it’s limited what I can create on the size of a Hat + the Raspberry PI is often not needed. I want to keep the option to add a RPI module with a Hat, but I want a new system.

Using Ethernet as backbone I would like to create a motherboard that control 4 x IO modules. Using RS-X as bus allows me to use the modules stand-alone or plugged them into a assembled system. Adding more motherboards I can scale up with 4+4+4+ modules as much as I have space. I believe I can do this on very small Space.

The major restriction I have on the Hat’s is connector space. I want to use standard connectors with standard cabling to avoid the need for custom cabling as much as possible. My “PLC” language will obviously be Plain.

What needs to be different from what I have done so far is Galvanic Isolation on all communication to limit the chain reaction if something backfires – because it will!

The added work-load on coding does not worry me because we can re-use things that I am in the process of creating anyway + I think this will be fun. 10 x 10 cm comes to mind as a size factor. It is easy to create modules that are 5 x 10,  10 x 5 or 10 x 10 to be mounted as part of the same system so it gives us freedom. It is also easy to create a 10×10 carrier module for Raspberry PI so a RPI+ Hat’s can become a module. But, I will look for standard, low cost project boxes before I decide on size.

It will be fun if nothing else and thats why I am here in the first place, so lets crack on with an open source, home made, low cost PLC system. My home is an excellent testing ground – assuming it don’t burn to the ground in the process 🙂