ShIoTiny: Nodes, Links, and Events or Features of Drawing Programs

ShIoTiny: Nodes, Links, and Events or Features of Drawing Programs

Key points or what this article is about

The topic of the article is visual programming of the PLC ShIoTiny for a smart home, described here: ShIoTiny: small automation, Internet of things or “six months before vacation”.

Very briefly concepts such as nodes, connection, events, as well as the features of loading and executing a visual program on ESP8266, which is the basis of the PLC ShIoTiny.

Introduction or a couple of organizational questions

In a previous article about my development, I made a brief overview of the controller's capabilities. ShIoTiny.

Oddly enough, the public showed quite a strong interest and asked me quite a few questions. Some comrades even immediately offered to buy a controller from me. No, I'm not against making some money, but my conscience does not allow me to sell something that is still very raw in terms of software.

Therefore, I posted the firmware binaries and the device diagram on GitHub: firmware + instruction shortest + diagram + examples.

Now everyone can flash the ESP-07 and play around with the firmware themselves. If anyone really wants just such a board as in the photo, then I have several of them. Write to mail [email protected]. But, as the unforgettable Ogurtsov used to say: “I am not responsible for anything!”.

So, let's get to the point: what is "node" (node) and "event"? How is the program executed?

As usual, let's start in order: by downloading the program.

How the program is loaded

Let's start with what happens when we press a button Upload in the editor ElDraw and our scheme-program, consisting of beautiful squares, flies into the device.

First, on the basis of the scheme drawn by us, its description is built in text form.
Secondly, it is checked whether all node inputs are connected to outputs. "Hanging" inputs should not be. If such an input is found, the schema will not load in ShIoTiny, and the editor will display a warning.

If everything went well, then the editor sends a text description of the circuit to ShIoTiny one node at a time. Of course, the existing schema is previously removed from ShIoTiny. The resulting text description is stored in FLASH-memory.

By the way, if you want to remove a schema from a device, simply load an empty schema (which does not contain any node elements) into it.

As soon as the entire scheme-program is loaded into the ShIoTiny PLC, it begins to "run". What does it mean?

Note that the processes of loading a circuit from FLASH-memory at power-up and when receiving a circuit from the editor are identical.

First, the object-nodes are created based on their description.
Then the links between the nodes are arranged. That is, links of outputs to inputs and inputs to outputs are generated.

And only after all this, the main program execution loop starts.

I wrote for a long time, but the whole process - from "loading" the circuit from FLASH memory to starting the main loop - takes a fraction of a second for a circuit of 60-80 nodes.

How does the main loop work? Very simple. First, he waits for the emergence events at some node, then handles that event. And so without end. Well, or until a new scheme is loaded into ShIoTiny.

Already several times I have mentioned things like events, nodes и connection. But what is it from a software point of view? This is what we'll talk about today.

Nodes, links and events

Just look at the examples of program schemes for ShIoTinyto understand that the scheme consists of only two entities - nodes (or elements) and links between them.

Node, but yes or circuit element is a virtual representation of some activity over the data. It can be an arithmetic operation, a logical operation, or any other operation that comes to mind. The main thing is that the node has an input and an output.

Sign In - this is the place where the node receives data. The input images are points that are always on the left side of the node.

Log out - this is the place where the result of the node's work is retrieved from. Output images are points that are always on the right side of the node.

Some nodes do not have inputs. Such nodes generate a result within themselves. For example, a constant node or a sensor node: they do not need data from other nodes to report the result.

Other nodes, on the other hand, have no outputs. These are nodes that display, for example, actuators (relays or some other similar ones). They receive data, but do not generate a calculation result that is available to other nodes.

In addition, there is also a unique comment node. It does nothing, has no inputs or outputs. Its purpose is to be an explanation on the diagram.

What's happened "event"? Event is the occurrence of new data in any node. For example, events include: a change in the input state (node Input), receiving data from another device (nodes MQTT и UDP), expiration of a given time interval (nodes timer и Delay) and so on.

What are events for? Yes, in order to determine in which node new data has appeared and the states of which nodes need to be changed in connection with the receipt of new data. The event, as it were, "passes" through the chain of nodes until it bypasses all the nodes whose state needs to be checked and changed.

All nodes can be divided into two categories.
Let's call the nodes that can generate events "active nodes».
The nodes that cannot generate events will be called "passive nodes».

When a node generates an event (that is, new data appears at its output), then in the general case the state of the entire chain of nodes connected to the output of the event generator node changes.

To make it clear, consider the example in the figure.

ShIoTiny: Nodes, Links, and Events or Features of Drawing Programs

The active nodes here are Input1, Input2 and Input3. The rest of the nodes are passive. Consider what happens when one or another input closes. The results are summarized in a table for convenience.

ShIoTiny: Nodes, Links, and Events or Features of Drawing Programs

As you can see, when an event occurs, a chain is built from the source node of the event to the end node. The state of those nodes that do not fall into the chain does not change.

A legitimate question arises, what will happen if two or even several events occur simultaneously?

As a lover of the work of Gleb Anfilov, I am drawn to send a curious questioner to his book "Escape from Surprise". This is such a “relativity theory for the smallest”, which tells well what “simultaneously” is and how to live with it.

But purely practically everything is much simpler: when two or even several events occur, all chains from each event source are sequentially built and processed in turn and no miracles.

The next quite legitimate question of a curious reader is what will happen if the nodes are connected in a ring? Or, as it is customary to say among these smart people of yours, to introduce feedback. That is, connect the output of one of the nodes with the input of the previous node so that the state of the output of this node affects the state of its own input. The editor will not allow you to directly connect the output of a node with its input. ElDraw. But indirectly, as in the figure below - this can be done.

So what will happen in this case? The answer will be very "definite": depending on which nodes. Consider an example in the figure.

ShIoTiny: Nodes, Links, and Events or Features of Drawing Programs

When the Input1 input contacts are open at the top input of node A - 0. At the output of node A, also 0. At the output of node B - 1. And, finally, at the bottom input of node A - 1. Everything is clear. And to whom it is not clear - see below the description of how the "AND" and "NOT" nodes work.

Now we will close the contacts of the Input1 input, that is, we will supply a unit to the upper input of node A. Those who are familiar with electronics know that in fact we will get a classic oscillator circuit based on logic elements. And in theory, such a circuit should endlessly output sequences 1-0-1-0-1-0 ... at the output of elements A and B. and 0-1-0-1-0-1-…. After all, the event should constantly change the state of nodes A and B, running in a circle 2-3-2-3-…!

But in reality this does not happen. The circuit will fall into a random state - or the relay will remain on or off, or it may slightly buzz on and off several times in a row. It all depends on the weather at the south pole of Mars. And here's why it happens.

An event from the Input1 node changes the state of node A, then node B, and so on in a circle several times. The program detects the "looping" of the event and forcibly stops this carnival. After that, the state change of nodes A and B is blocked until a new event occurs. The moment at which the program decides - "stop spinning in circles!" - in the general case depends on many factors and can be considered random.

Be careful when connecting nodes in a ring - the effects will not always be obvious! Have a good idea of ​​what and why you are doing!

Is it still possible to build a generator on the nodes available to us? Yes, you can! But this requires a node that itself can generate events. And there is such a node - this is the "delay line". Let's see how the generator works with a period of 6 seconds in the figure below.

ShIoTiny: Nodes, Links, and Events or Features of Drawing Programs

The key element of the generator is node A - the delay line. If you change the input state of the delay line from 0 to 1, then 1 will not appear at the output immediately, but only after a specified time. In our case it is 3 seconds. Similarly, if you change the input state of the delay line from 1 to 0, then 0 will appear at the output after the same 3 seconds. The delay time is specified in tenths of a second. That is, the value 30 means - 3 seconds.

A feature of the delay line is that it generates an event after the delay time has elapsed.

Suppose that initially the output of the delay line was 0. After passing through node B - the inverter - this 0 turns into 1 and enters the input of the delay line. Nothing happens right away. At the output of the delay line, as it was 0, it will remain, but the countdown of the delay time is turned on. 3 seconds pass. And then the delay line generates an event. At the output, she has 1. This unit, after passing node B - the inverter - turns into 0 and enters the input of the delay line. Another 3 seconds pass... and the process repeats. That is, every 3 seconds the delay line output status changes from 0 to 1 and then from 1 to 0. The relay clicks. The generator is running. The pulse period is 6 seconds (3 seconds at the output is zero and 3 seconds is one).

But, in real circuits, it is usually not necessary to use this example either. There are special timer nodes that perfectly and without outside help generate a sequence of pulses with a given period. The duration of "zero" and "one" in these pulses is equal to half the period.

To set periodic actions, use timer nodes.

I note that such digital signals, where the duration of "zero" and "one" are equal, are called "meander".

I hope I have clarified the question a little about how events are propagated between nodes and what should not be done?

Conclusion and links

The article turned out to be short, but this article is the answer to the questions that have arisen on nodes and events.

As firmware develops and new examples appear, I will write about how to program ShIoTiny small articles, as long as it will be interesting to people.

As before, the diagram, firmware, examples, description of nodes and all the rest is here.

Questions, wishes, criticism - this is here: [email protected]

Source: habr.com

Add a comment