Read here how to use the GRAFCET Engine on a Raspberry Pi and then transfer a GRAFCET plan into the device.
You need GRAFCET-Studio version 188.8.131.52 or newer!
Users of the Raspberry Pi usually use the Python programming language because it is very easy to learn and easy to use. For this reason we provide the Grafcet-Engine as a Python (V3) library.
The Raspberry Pi runs a Python script (V3) and communicates via MQTT with a Windows PC running the Grafcet Studio software. With Grafcet-Studio, Grafcet charts can be created and downloaded to the Raspberry Pi at the push of a button. The Grafcet logic then runs independently on the Raspberry Pi.
The combination of Raspberry Pi and Grafcet Studio offers the following possibilities:
Below is a diagram of MQTT communication between PC and Raspberry Pi (with and without the Internet).
Here a public MQTT broker is used and the communication is handled over the Internet.
It is also possible without Internet: In the local network the PC or the Raspberry Pi can provide an MQTT broker.
We recommend to use the operating system Raspian. After the installation Python3 is immediately available.
The image can be downloaded here: https://www.raspberrypi.org/downloads/
The Grafcet Engine communicates with Grafcet-Studio via MQTT. Therefore the library "paho-mqtt" must be installed. Start the terminal and enter the command:
sudo pip3 install paho-mqtt
When the PC and Raspberry Pi are connected to the Internet, a public MQTT broker can be used. In this case, the steps in the next section are not necessary.
To ensure that communication works without the Internet, install an MQTT Broker on the PC or on the Raspberry Pi. Here you can download e.g. a MQTT Broker for the PC free of charge:
MQTT Broker for the PC: https://mosquitto.org/download/
After starting the MQTT Broker a command window (console) opens and afterwards you can communicate via it in the local network. In Grafcet-Studio in this case you have to enter the local host address in the connection settings under "IP address": 127.0.0.1
Download the following ZIP file:
In the ZIP file you will find these files:
The Python script grafcet_Raspberry_gpio.py:
In this example 4 outputs of the R-Pi are used.
|1||Pin 14, 15,18,23 are defined as outputs (BCM numbering).|
|2||An instance of the Grafcet engine is created.|
|3||Communication is configured.|
|4||In the loop: Here the first 4 digital inputs are described with 1-0-1-0 for test purposes. Hardware inputs are not used in this example.|
|5||In the loop: The Grafcet engine is called.|
|6||In the loop: The first 4 dig. Outputs are read and stored in Q0-Q3.|
|7||In the loop: The states of Q0-Q3 are written to the hardware outputs.|
The following Grafcet chart could be used to run a test:
For the communication between the R-Pi and the PC to take place correctly, these three parameters must be identical:
Topic 1 and Topic 2 are used to identify the device. You should choose it like a password.
How to set the MQTT parameters in Grafcet-Studio can be read below.
In this example we use the Pibrella I/O Shield: http://pibrella.com/
Properties of the shield:
With the following command the necessary library for the shield can be installed:
sudo pip3 install pibrella
Via the Grafcet logic the green and yellow LED flashes. The red LED and the speaker can be switched on via the red button. In the following picture, the red button is pressed:
The picture shows the Grafcet logic:
Here you can download the Grafcet plan in Grafcet-Studio format: Test-Pibrella-Shield.zip
|1||Grafcet logic for LED green and yellow.|
|2||Grafcet logic for LED red and speaker. These are switched on via the red button.|
|3||I/O-Panel: Display of inputs and outputs and their states.|
|5||The outputs start at address 25.|
Here the corresponding Python script grafcet_pibrella.py:
|1||The Grafcet engine is imported.|
|2||The library for the Pibrella shield is imported.|
|3||An instance of the Grafcet engine is created.|
|4||The communication is set up (the parameters must match the parameters in Grafcet Studio)|
|5||In the loop: the state of the button is written to the Grafcet input "I0" (address 0.0).|
|6||In the loop: The Grafcet outputs are read.|
|7||In the loop: The graphcet outputs are written to the shield.|
In order to address the hardware of the Raspberry Pi via the Grafcet logic, the Grafcet inputs must be written and the Grafcet outputs must be read out.
Explanation: Symbols for inputs and outputs are defined in Grafcet Studio. The Grafcet logic works with these symbols. A connection must now be established between the hardware of the Raspberry Pi and these symbols so that the Grafcet logic can use the hardware of the Raspberry Pi. Read and write functions are provided for this purpose. (The read_bit and write_bit functions have already been used in the example Python scripts.)
The Grafcet input symbols must therefore be written with the current values of the hardware inputs. And the states of the Grafcet outputs are written to the hardware outputs of the R-Pi.
The following read functions are available.
|read_bit(addr, bit)||Returns true or false (1 bit)||as function result|
|read_uint8(addr)||Returns a byte value (8 bit) as the function result|
|read_uint16(addr)||Returns an unsigned 16-bit integer value as a function result|
|read_uint32(addr)||Returns an unsigned 32-bit integer value as a function result|
|read_int16(addr)||Returns a signed 16-bit integer value as function result|
|read_int32(addr)||Returns a signed 32-bit integer value as a function result|
|read_float(addr)||Returns a float value (32 bit) as function result|
The following write functions are available.
|write_bit(addr, bit, bitvalue)||The given value is written at the given address|
|write_uint8(addr, bytevalue)||The given value is written at the given address|
|write_uint16(addr, uint16value)||The specified value is written at the given address|
|write_uint32(addr, uint32value)||The specified value is written at the given address|
|write_int16(addr, int16value)||The specified value is written at the given address|
|write_int32(addr, int32value)||The specified value is written at the given address|
|write_float(addr, floatvalue)||The specified value is written at the given address|
Summary: The following three things must be done in a loop: the hardware input states must be written to the Grafcet inputs, the Grafcet cycle 'GrafcetCyclic' must be called and the output states of the Grafcet outputs must be written to the hardware outputs. A programmable logic controller (PLC) also works in a similar way.
Add an MQTT device in Grafcet-Studio:
|1||Display the 'Grafcet Engine' window and press the Plus button. If the simulator is still connected, press the disconnect button first.|
|2||The 'Add Device' dialog appears. Select from the list: 'Grafcet-Engine MQTT'|
|3||Enter a short description. This will be available later as a selection in the list.|
|4||Press the 'Ok'||button.|
|5||Enter the connection parameters: Address of the Mqtt broker, Topic1 and Topic2. The two topics are arbitrary, but at least 3 characters long. The device is identified via these topics. These topics must match the topics in the Python code. Otherwise no connection is established.|
|6||Press the 'Ok' button.|
|7||Select the Device you just created|
|8||Press the Connect button. (The Python script should already be started.)|
In the Python script the connection settings must be identical. See point 5. Otherwise no communication can take place. Here again the corresponding picture: