Industrial IoT With JS and Node - Js Env
Industrial IoT With JS and Node - Js Env
Ljubljana, 2018
Content
List of Pictures v
1. Introduction 1
2.2 Nodejs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2.1 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2.4 Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.4 TwinCAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
A.5 Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
iii
iv Content
References 55
List of Pictures
1.1 Traditional supply chain system . . . . . . . . . . . . . . . . . . . . . 1
Cong mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
v
vi List of Pictures
List of Tables
3.1 Staudinger Warehouse Model 226001 Technical Data . . . . . . . . . 15
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Program Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
vii
viii List of Tables
1. Introduction
The main goal of this summer school is to give you an insight into the eld of
by building a small decentralized supply chain system (online store) that will be able
Traditional supply chain systems that are used by big retailers like Amazon
transports and other activities. In contrast, the decentralized supply chain systems
do not have a central system, they have a distributed system that consists of
multitude of smart 'mini' systems or nodes that communicate with each other to
achieve their goals. In Figure 1.1 we can see a simplied example of a traditional
In presented supply chain system the retailer plays the central role in the whole
process. It communicates with the customers via online store, it manages the
packages that are stored in warehouses and organizes the transportation services.
1
2 Introduction
This type of systems rely on the central control systems to eciently control theirs
supply lines.
Decentralized supply line systems don't have a central control system; they have
a distributed system of nodes that communicate with each other directly to achieve
their goals. In this type of systems each node is a free agent that maximizes its
own eciency which consequently maximizes whole supply chain system eciency.
In Figure 1.2 an example of a decentralized supply chain system (that has been
build from the example of a centralized supply chain system seen in Figure 1.1) is
presented.
with each other to arrange storage, transportation and other tasks. Our distributed
supply chain system denes four types of nodes: smart package, retailer, warehouse
and transportation.
to be delivered to the user home address. Smart package node can also be
accelerations and others that can be used to monitor storing and transportation
conditions.
• Retailer node
3
Retailer node represents an online shop provider. Retailer does not own, store
or transport packages it just sells them via its online shop platform. Its main
• Warehouse node
Warehouse node represents a storage facility for storing smart packages. Its
main functions are storing packages and loading and unloading of packages to
• Transportation node
Transportation node represents a transportation service that is able to deliver
packages to desired location. This node can utilize any type of transportation
from a ship, an air plane, drones, autonomous vehicles and any other as long
utilize web technologies. This means that each node has to provide a web server
through which it can communicate with other nodes via web protocols. In order to
host a web server on each node the node must have a proper hardware and software
equipment.
The technology has made a huge progress towards cheap mini computers like
Raspberry Pi that are more than capable of serving the needs of our application
which enables us to use them for our project. We will be using Raspberry Pi 3
nodes that change locations like packages and transportation and an Ethernet port
for wired connectivity for nodes that don't change locations like warehouses and
retailers.
5
6 Raspberry Pi platform and Node.js environment
instructions for the selected operating system can be found online www.reeve.com/
Documents/Articles\%20Papers/Reeve_RPi_BasicSetup.pdf. After installation
of the operating system we can proceed with the installation of a Node.js web server.
2.2 Nodejs
R is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js
Node.js
uses an event-driven, non-blocking I/O model that makes it lightweight and ecient.
Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries
in the world. In this chapter we present how to install Node.js and write three
example applications that will help us build our application of decentralized supply
chain system.
2.2.1 Installation
Installation of the Node.js server on Linux operating system is done using the
1. First we update our local package index with the following command. When
2. Then we type a curl command that utilize a cURL tool to download the latest
package from the server where Node.js package is hosted.
4. To compile and install native add-ons from npm you may also need to install
build tools.
type:
node -version
2.2 Nodejs 7
We open console and type two commands. These commands are universal for
whatever OS you'll be running. The rst command will create a new directory
inside the directory you are currently in. The second will change into this
mkdir helloworld
cd helloworld
After creating your directory, you will need to initialize a project and link it
to the Node.js package manager npm. npm is a public repository where all
Node.js packages are stored. Packages can be viewed as bundles of code, like
npm init
This creates a package.json le in your application folder. The le contains
references for all npm packages you have downloaded to your project. The
command will prompt you to enter a number of things. You can enter your
The install command will go ahead and nd the package you wish to install
and install it to your project. You will now be able to see a node_modules
folder gets created in the root of your project. This is a crucial step, as you
will be able to require any of the recently installed les in your own application
les. The addition of --save will save the package to your dependencies list
fast, unopinionated, minimalist web framework for Node.js. It gives you a set
of robust and easy to use tools to get your web application up and running.
Express has become so popular it now is the de facto standard in the vast
4. Start WebStorm IDE, open the helloworld directory as a project and create a
Here is where you will need to use the package which was recently installed.
The rst line declares a variable which will contain the module called express,
Assigning the function call to another variable gives you access to a predened
set of tools which will in a great deal make your life much easier. You could
view the variable app as an object, whose methods you are utilizing to build
the actual program. The listen method starts a server and listens on port 3000
for connections. It responds with Hello World! for get requests to the root
URL (/). For every other path it will respond with a >404 Not Found.
node index.js
the output (Figure 2.2). You should also see that 'Example app listening on
port 3000!' message gets logged to the command line.
client-side.
expressed in JSON or XML, which is exposed via the web most commonly by
Endpoints are important aspects of interacting with server-side web APIs, as they
specify where resources lie that can be accessed by third party software. Usually the
access is via a URI to which HTTP requests are posted and from which the response
changes (and with it the endpoint) then previously written software will break, as
the required resource can no longer be found at the same place. Endpoint example
to our project that will accept two numbers and return their product. In our code
2.2.4 Services
Our web server will not be used only for serving http requests. We will be using it
also for other purposes that are required for a certain type of node. For an example
a package node has to communicate with a GPS module to obtain its location. In
order to run this type of services we have to add a periodically repeating function
that can communicate with other systems. After the program starts the server (with
let i=0;
setInterval( function() {
i++;
console.log(`Counter:'+i);
},1000);
browser call. We have described a way of calling this endpoints by typing strings into
2.2 Nodejs 11
browser URL led. Here we will describe a way in which nodes can call each other
automatically, which is crucial function for our decentralized supply chain system.
We used a module called request which is available via npm package manager.
IP address with port on which server of the node we wish to call listens and a
desired endpoint in our case clcSurface with parameters a and b. When the code
is executed and if the other node is running it will return a response to our request
and store it in the variable body which can be then printed to the console of caller
node.
12 Raspberry Pi platform and Node.js environment
3. Beckho PLC platform and TwinCAT
environment
3.1 Programmable logic controllers (PLC)
Programmable logic controllers (PLCs) [1] are industrial digital computers adapted
for the control of manufacturing processes, such as assembly lines, robotic devices, or
any activity that requires high reliability control, ease of programming and process
fault diagnosis. They were built to replace hard-wire relays, timers, sequencers
etc. and provide industry with a exible and reliable controllers suitable for harsh
• Central Processing Unit (CPU): This unit contains the brains of the PLC
and is often referred to as a microprocessor. The basic instruction set is a
a PLC for a process control system. Aside from this operating system, it also
Some parts of a memory are used for storing input and output status. Other
parts of the memory are used to store variable contents for variables used in
user programs. For example, timer value or counter value would be stored in
where the programmers develop their logic, store it on a disk, and then
13
14 Beckho PLC platform and TwinCAT environment
download the program to the CPU. It also allows more than one programmer
to develop dierent modules of the program. When connected to the CPU the
programmer can test the system, and watch the logic operate when the system
is running.
controller is to read signals from dierent types of sensors and input devices.
The basic sensors include keys, keyboards, and functional switches, but there
are also more specic automatic devices such as proximity sensors, marginal
analog.
most frequently used devices are motors, solenoids, relays, indicators, sound
or control a simple system such as a system for sorting products all the way
robotic machine.
and medium series already contain the supply module. Separate supplies
need to be supplied to start PLC controller inputs and outputs. The internal
Separate control transformers are often used to isolate inputs and CPU from
output devices. The purpose is to isolate this sensitive circuitry from transient
Figure 3.1) will be used to enable storage functionalities of the distributed supply
chain. The model simulates an automatically working high level storage system as
Table 3.1 presents warehouse model basic technical data. Modes of operation are
Sensors
Read switches 1
Mechanical switches 16
Actuators
Motors with two directions 3
LED 2
16 spare -
17 spare -
18 X- power supply 0 V
19 X- power supply 0 V
Actuators
20 5S1 X axis to X+
21 5S2 X axis to X-
22 7A1 Y axis to Y+
23 7A1 Y axis to Y-
24 7A2 Z axis to Z+
25 7A2 Z axis to Z-
28 spare -
29 spare -
30 spare -
31 spare -
32 spare -
33 spare -
34 spare -
35 spare -
As presented in Table 3.1 15 sensors and 8 actuators are used to control the
warehouse model. Two additional mechanical switches are installed at the both ends
of the X axis to prevent the warehouse system from the fatal mistakes when using
the conveyor or programming the control unit. These two switches (labelled 5S1 and
5S2) are working automatically and can not be directly accessed and manipulated.
Table 3.2 describes all sensors and actuators used in the warehouse model. This
is also a list of connecting pins. Wiring from all the sensors and actuators in the
model is merged into a 37 pin D-sub female connector. 37 D-sub male connector is
Figure 3.2 shows the warehouse layout with locations of all sensors and actuators
Control technology. Their product range covers industrial PCs, I/O and eldbus
cover all elds of industries with products that can be used as a separate components
technology available in miniature format for DIN rail mounting. Several types
of PCs are available: embedded PCs, control cabinet PCs, panel PCs and
control panels.
Beckho oers its own solution, a complete EtherCAT I/O range for the high-
speed Ethernet eldbus based on EtherCAT terminals, the EtherCAT Box and
TwinCAT 2: automation software suite that forms the core of the control
system. TwinCAT can turn almost any PC-based system into a real-time
control with multiple PLC, NC, CNC and robotics runtime systems. More
Embedded PC CX 8090 (Figure 3.3) will be used for the Module 3 project assignment
to manage and remotely control the warehouse. CX 8090 is a control system with
a switched Ethernet port and supports protocols such as real-time Ethernet, ADS
The CX 8090 is able to automatically recognise the type of I/O systems connected.
The control system is programmed with TwinCAT via the eldbus interface or the
functioning of the embedded PC and the connected I/O devices. More technical
Two types of I/O devices will be used to enable the management and control of the
warehouse. Potential distribution units will supply the warehouse with power, while
power. Besides powering the PC and connected I/O devices, it also provides means
EL9186
The potential distribution terminal EL9186 provides eight terminal points with
EL9187
The potential distribution terminal EL9187 provides eight terminal points with a
potential of 0 V and enable the voltage to be picked up without further bus terminal
blocks or wiring.
20 Beckho PLC platform and TwinCAT environment
Protocol)
Current supply 2A
Protection class IP 20
device sensors or to send the control signals to the actuators. All I/O terminals
have two power contacts (for 24 V and 0 V) that supply power to the terminal,
while power supply is also looped through to the next terminal. E-bus or K-bus
to 65535 units with robust housing and safe contacts can be assembled together.
EL1809
The 16-channel 24 V DC digital input terminal EL1809 acquires the binary
control signals from the process level and transmits them, in an electrically isolated
EL2809
The 16-channel 24 V DC digital output terminal EL2809 connects the binary
control signals from the automation device on to the actuators at the process level
3.4 TwinCAT 21
with electrical isolation. The EL2809 is protected against polarity reversal and
processes load currents with outputs protected against overload and short-circuit.
The EtherCAT Terminal contains 16 channels, whose signal states are displayed by
LEDs.
3.4 TwinCAT
Beckho developed a specialized software to manage their control and automation
that turns any compatible PC into a real-time controller with a multi-PLC system,
PLC controllers and operating devices with an open and compatible PC hardware
and embedded software PLC based on IEC 61131-3 standard for PLC programming.
TwinCAT includes programming and run-time systems and oers connection to all
interfaces and other programs by means of open Microsoft standards (OPC, OCX,
DLL, etc.).
PTP and NC Interpolation functionalities, and drivers for Windows I/O and control
panels. The two most important components, used in any PLC application, are:
relationships are dened among the number of PLC systems, PLC system
manufacturer; PLC Control supports all languages of the IEC standard and
enables online connections with PLC runtime systems around the world with
• Both Embedded PC (CX 8090) and remote working station must be running
including the input and output variables mapped to the physical connectors
on the I/O devices and programming logic that enables all modes of operation.
• After the control program is developed, checked and compiled, TwinCAT PLC
• Using TwinCAT PLC Control the control program is remotely run, stopped,
via Ethernet, read state of the sensors and set the actuators.
A short tutorial on how to use TwinCAT System Manager and TwinCAT PLC
enables connection from remote working station to the PC running the PLC
• Add I/O devices and generate mappings from PLC program variables and
• Check conguration.
• Activate conguration.
open new .tsm (TwinCAT System Manager) le. Save this le for future reference
connected to the control PC (CX 8090) with the Ethernet cable. From the program
window on the left select SYSTEM - Conguration. From the program window on
A Choose Target System pop-up window appears. Select the Search (Ethernet)
button.
AnAdd Route Dialog pop-up window appears. Select IP Address option in the
letters.
Select the detected embedded PC and press the Add Route button. A pop-up
window appears to enter user credentials to log on the target system. Enter:
• user: Administrator
• password: 1
and press OK. This will create a route from the working station to the control
PC and connect both devices. A letter X appears next to the host name of the
Press close to return to the previous window. The connected CX 8090 device
appears on the list of possible target systems. Select it and press OK. Remote
appears. Find and select a TwinCAT Project Info le (.tpy ). It must be previously
created to add it in this step. TwinCAT Project Info le is created in TwinCAT
PLC Control program after creating and building a PLC project. Refer to Chapter
ARM architecture) and what are the run-time number and port.
The selected PLC project appears in the left program windows in the PLC -
Input and output variables that you dene in the control program to work with
the controlled device will appear in this sections and need to be linked to the physical
connections of the appropriate I/O devices. From the list of variables select it, one
by one, right click on it and select Change Link. A list of available addresses on the
toolbar select the Set/Reset to Cong mode icon or press Shift + F4 (Figure 3.7) .
3.4 TwinCAT 25
Figure 3.6: TwinCAT System Manager - PLC Conguration - Append PLC Project
Figure 3.7: TwinCAT System Manager - I/O Conguration - Set TwinCAT to Cong
mode
From the program window on the left select I/O - Conguration. Select I/O
Devices and right click on it. Select the Scan Devices option and then click OK
when a pop-up window appears informing you that some devices may not be found
automatically. This process rst nds all primary devices (which include EtherCAT
devices and internal PC RAM) and then goes on to nd I/O boxes (connected
terminals).
A list of discovered devices appear in the program window on the left under
I/O devices. Expand the Device 1 (EtherCAT) to see the terminals found on this
26 Beckho PLC platform and TwinCAT environment
device. It rst nds Term 1 (EK1200) terminal which is an EtherCAT bus and if
you expand it even further you will discover three more terminals attached to the
input and output signals from the connected terminals. After all the devices and
boxes have been discovered System Manager asks you if you want to enable Free
Run option (still in Cong mode). With this option enabled go to Term 4 (output
terminal), expand it and select Channel 8 (led light). Expand it again and select
Output. In program window on the right select the Online tab. Current value of
the output signal is shown in the Value eld and to change it use the Write button.
One of the LED lights on the warehouse model will turn on.
After all the I/O terminals have been discovered and congured (this is done
automatically), you need to generate mappings between PLC project variables and
I/O devices channels. Select Generate Mappings from the I/O - Conguration
option in the left program window of the System Manager.
icons on the toolbar (Figures 3.8 and 3.9). If everything is OK, the System Manager
asks you if you wish to set the TwinCAT in run mode. A successful conguration
check and setting the TwinCAT in run mode is conrmed with a green colour of the
status bar in the right bottom corner of the System Manager program window.
the IEC standard, we will focus on a high-level textual language Structured Text
TwinCAT PLC Control program window is divided into several parts and sub-
windows (Figure 3.10). Menu bar allows access to all the functionalities of the
software while most important functions of the program are also accessible from a
program organization units (POUs) are listed here. The main POU is MAIN
(Program) and other POUs such as functions and function blocks can also be
dened here. This window is also used to access additional resources, such as
• In the top centre position: start of the POU (program, function or function
block) with a type of the POU, name of the POU and declaration and
28 Beckho PLC platform and TwinCAT environment
initialization of the local variables. Functions' input variables are also dened
• In the middle centre position: main programming window, all the program
• In the bottom centre position: log window with compiler logger information,
such as loading a library, compiling, warnings and errors in the code etc.
• Program
• Function
• Function block
For simple PLC applications all the code can go into a program (like MAIN),
while functions and function block are useful when the code gets larger and some
functionalities of the application repeat. Functions and function blocks are used to
Basic dierence between a function and a function block is that given the same
parameter values a function always returns the same value, while a function block
output depends on the state of internal variables from previous invocation of the
function block. FB output is not necessarily the same every time it is invoked.
Function blocks must rst be created as a "class" objects with input, internal and
output variables, then instantiated and receive an instance identier. Function block
have output variables, that can be accessed at any time. Functions, on the other
hand, do not have outputs, but rather a return value as the function identier is
actually a "container" for the result value, so it must be assigned a data type.
Many predened functions and function blocks are available from libraries and
custom functions and function block can also be dened, as applicable in a specic
PLC application. Here are some examples of the standard functions and function
blocks:
• functions: ADD, SQRT, SIN, COS, GT, MIN, MAX, AND, OR etc.
Variables
Variables are an essential part of any programming language. PLC programming
• global : accessible from any part of the PLC application (see Figure 3.11)
• local: accessible inside a POU (program, function, function block) (see Figure
3.12)
controlled device
• static and temporary: static variables' values are stored to the internal
DB and can be used later when a function block is invoked again; temporary
variables' values are not stored and are lost after a function or function block
is exited
• external: external variables are global variables that are imported into a
function block
• AT: set variable at specic location (for example, input or output location,
• RETAIN: retain variables retain their value after an uncontrolled stop (after
again); when the program restarts, the system continues to operate with the
stored values
30 Beckho PLC platform and TwinCAT environment
• CONSTANT: variables that are constants, i.e. their values can not be
changed
Libraries
Many useful functions and function blocks have already been written and it is a good
a PLC application via a Library Manager (see Figure 3.13). Standard.lib library
automatically included and other libraries such as TcSystem.lib and TcBase.lib can
be added (right click in the area where libraries are listed and select Additional
Library ).
are used. More details about writing PLC application in a Structured Text (ST)
programming language are presented in Chapter 3.5, here we present just an excerpt
from a real-life application for you to get an insight how a PLC application looks
program is written in such a way that it is started and run only once and when all the
program steps (code statements) are executed the program stops and exists. A PLC
3.4 TwinCAT 31
never stops. It constantly checks the state of the device sensors and, depending
TwinCAT does that by going through a code and executing program statements
over and over again until the application is stopped manually. Program repetition
frequency is dened by PLC Cycle (Base) time in TwinCAT System Manager, inside
Log window
Logging window at the bottom of the PLC Control application displays notications
libraries it includes in the project and if, when checking the code, it encounters some
problems. The compiler displays warnings and errors that need to be corrected.
building the code and creating a translated version of the code specic to a control
PC, logging on a control PC and running the code. We will go through some of the
• Create new PLC project: Select File → New. A Choose Target System Type
dialog window appears. Beckho CX 8090 embedded PC which is based on
PLC Project le (.pro ) is created. Save it with an appropriate le name. Next,
run-time system must be selected that will run the PLC application. Select
window appears where you select the control PC and an available run-time
system within the PC. Note: the remote working station must be physically
connected to the control PC at this point and a proper connection must also
appropriate program windows - use the top centre window for declaration of
variables and the middle window for the main part of the code. Use appropriate
coding syntax, data types, programming constructs etc. (for more information
Chapter 3.5).
• Building the code: Select Project → Build. This will check the code
for possible mistakes and show where they are. If (or when) the code is
syntactically OK, the building process results in a translated code that suits the
recognizes I/O variables in the program and lists them in the conguration.
These need to be linked to a physical inputs and outputs of the PLC hardware
assembly. Once a program is built and added to the System Conguration for
the rst time, all further changes in the program are immediately visible in
3.5 Structured Text (ST) 33
• Connecting to the control PC: Select Online → Login. This connects the
remote working station to the control PC. If there is no PLC application on the
control PC, the pop-up window appears asking you if you want to download
on the control PC, the pop-up window appears informing you that Online
Changes will be made to the PLC application. After a successful login the
TwinCAT PLC Control window in the middle of the screen changes and shows
program variables' current values. If you want to log out the control PC select
Online → Logout. Note: always stop the application and reset the variables'
Stop or press Shift + F8. If you want to analyse the application step by step
then run the application. The application will stop at breakpoint(s) and allow
you to go through the code step by step. To step over a line of code select
languages for PLC programming and is a part of IEC 61131-3 standard [7].
IEC 61131-3 is the third part (of 10) of the open international standard IEC
61131 for programmable logic controllers, and was rst published in December 1993
by the IEC. The current (third) edition was published in February 2013.
Part 3 of IEC 61131 deals with basic software architecture and programming
languages of the control program within PLC. It denes two graphical and two
For more information on PLC programming with Structured Text language check
[8].
(supervisory) computer and a slave remote terminal unit. A good advantage of this
protocol is also that it moves raw bits or words without placing many restrictions
on vendors.
Many variants of the Modbus protocol exist. The most common implementation
communication and uses a compact binary representation of the data for protocol
communication. Modbus TCP [10] (or Modbus TCP/IP) variant is used for
communication over TCP/IP network and connects over port 502. Modbus TCP
will be used in the project assignment to connect to and control the warehouse.
Modbus denes 4 basic object types that are provided by a Modbus slave device
to a Modbus master device (see Table 3.4). A coil is a single-bit physical output
which can be both read and written to. A discrete input is a single-bit physical
input and it can only be read. An input register is an object type that allows to
These operations include data access, diagnostics and some other operations. Here,
we focus on data access operations. Table 3.5 presents the most basic and commonly
3.6 Modbus TCP 35
used data access operations - reading and writing single or multiple physical inputs
and outputs.
specialized Embedded PC for Ethernet - CX 8090 (see Chapter 3.3.1) will be used
8090. CX 8090 Modbus TCP protocol support provides both server and client
PLC areas (variables) and a supplied PLC library allows to communicate with other
The default mapping between Modbus and TwinCAT PLC areas is described
in Table 3.6. Let's look at some examples to easier grasp the principle of Modbus
addressing.
Let's assume we have a CX 8090 terminal assembly with one digital input
terminal EL 1809 and one digital output terminal EL 2809 3.3.2. Both terminals have
16 physical connections. When conguring both I/O devices in the TwinCAT System
Manager the TwinCAT assigns specic addresses to all the physical connections. As
both terminals are digital, assigned addresses are in a binary form. For example,
digital input terminal physical connections are assigned addresses from 40.0 to 40.7
and from 41.0 41.7. Digital output terminal physical connections are assigned
to
addresses from 26.0 to 26.7 and from 27.0 to 27.7. The decimal number in the rst
part of the address represents number of bytes, while the last digit in the address
To read values of sensors (digital input terminal) from Modbus TCP client
we use the function Read Discrete Inputs with addresses from 320 to 335 (if used
in a decimal form) or from 140 to 14E (if used in a hexadecimal form). Where do
36 Beckho PLC platform and TwinCAT environment
this numbers come from? First, as presented in table 3.6 discrete inputs start at
but we need it in bit form. So: 40 · 8 + 0 = 320 bits. Similarly 41.7 in bytes form is
41 · 8 + 7 = 335 bits. 140 and 14E are hexadecimal representations of 320 and 335.
To control the actuators (start or stop them) we use the function Write Single
is in bytes form, but we need it in bit form. So 26 · 8 + 0 = 208 bits. Similarly 27.7
To read or write to a variable in the PLC memory area (if the M attribute
is used in the variable declaration, for example: var AT \%MX0.0 : BOOL := TRUE)
we use the functions Read Holding Register or Write Holding Registers. Addresses
from 12288 (decimal form) or from 3000 (hexadecimal form) on must be used.
To read or write to a variable in PLC data area (if not extra attribute is
used in the variable declaration) we use the functions Read Holding Register or Write
Holding Registers. Addresses from 24576 (decimal form) or from 6000 (hexadecimal
form) on must be used. Two registers (each is 16 bits long) are reserved for each
variable.
A 2018 Summer School Project Assignments
Summer school is set in a way that participants have the main role in designing
the nodes and communications between them. One of the most representational
Chart (MSC). MSC in Figure A.1 shows all four types of nodes and a customer that
MSC shows an example process of adding a new package into the system. In the
rst step, the package node requests to be registered on retailer node by sending a
request message registerToShop. This means that package node sends a message
to the retailer node Web API endpoint that enables a package to be registered and
can appear in the retailers online shop. In the second step, a package node requests
a warehouse node to store the package by sending a message load. When the
warehouse node loads the package it returns, for example, a message of the package
37
38 2018 Summer School Project Assignments
package.
• content
• type: Type denes special treatment of the package based on its content
• size: Size of the package in the form: Length x Width x Height (in mm).
• state: State denes the current state of the package. States of the
package are:
initial: the package server has started but the package is not yet created
created: the package properties has been set
registered: the package has been registered with the retailer node
purchased: the package has been purchased by some user
transport_ordered: transport of the package has been ordered
transport_pickup: transport is on its way to pick up the package
warehouse_unloading: transport has arrived at the warehouse
location and is waiting for the package to be unloaded
• createPackage
Create package with all the package parameters. Example:
http://192.168.1.100:3000/createPackage?content=banane&
owner=Janez&type=glass
• registerToMarketPlace
Call retailer node and register to its online shop. Example:
http://192.168.1.100:3000/registerToMarketPlaces?reatailer_
ip=192.168.1.101
Because the package node will communicate with the retailer node we
• load
initial load of package to the warehouse. Example http://192.168.1.
100:3000/load?warehouse_ip=192.168.1.102
Because the package node has to communicate with the warehouse node
• packages: Array of packages that can be lled with packages data that
• initialize
Initialize the retailer shop on startup of the system. Example: http:
//192.168.1.101:3000/initialize
• getPackages
Return all packages that are registered in the shop.
• removePackage
Remove package from the retailers memory of items that are oered in the
memory.
• buy
Buy a package with a certain id. When customer visits online store and
4. Include also economic aspect into the system. The retailer charge the package
• occupation information
• state:
(a) loadPackage
Load a package into the warehouse. Smart Package node calls this API
when it receives an order from the smart package node to deliver the
3. Include also economic aspects into the system. The warehouse node charges
the package a fee for storage and for loading/unloading services. (optional)
In this module you will rst create a PLC application for warehouse control and
then a warehouse node with logic for communication with the warehouse and APIs
For more information on how to use TwinCAT System Manager, see Chapter 3.4.2.
connected to the warehouse and will run the PLC project code.
First, create and save a System Manager le (.tsm le). This le will contain
• System Conguration:
◦ Choose Target
◦ Search (Ethernet)
◦ Broadcast Search (select IP Address option in the Address Info section)
• PLC Conguration:
◦ In TwinCAT PLC Control create and save a new PLC Project (.pro le) -
• I/O Conguration
successful and turn on one of the two LED lights on the warehouse
◦ Check conguration
◦ Activate conguration
◦ Run mode
◦ Status bar in the bottom right corner of the TwinCAT System Manager
write a program for warehouse control. Warehouse control program will be running
in 2 modes of operation:
• loading a package to the warehouse (the program needs to know the state of
• unloading a package from the warehouse (the program needs to know which
package to unload)
I/O Variables
First, input and output variables for control of warehouse sensors and actuators will
sensors on the warehouse, i.e. digital input terminal EL1809 physical connections.
Also declare 8 output variables and set them to link to the addresses of matching
actuators on the warehouse, i.e. digital output terminal EL809 physical connections.
I/O variables are declared as boolean variables because the controlled sensors
aa.a are addresses on the input and output terminals and are automatically
assigned during the I/O conguration. Check the addresses in the I/O Conguration
Use tables A.1 and A.2 to help you link and map programming variables to pins
on input and output terminals, i.e. functions of the warehouse model. Table A.3
will help you to easier understand where are the various positions of the warehouse
After all I/O variables are correctly declared, rebuild the PLC project and refresh
menu. Inputs and Outputs options contain a list of all declared input and output
variables in the PLC Project. These variables need to be linked and mapped to
Table A.1: Warehouse Model Inputs - Mapping from Connected Pins to Program
Variables
Table A.2: Warehouse Model Outputs - Mapping from Connected Pins to Program
Variables
Position
X axis position 1 (X+) warehouse tower end position on X axis
X axis position 2 warehouse tower middle position on X axis
X axis position 3 (X-) warehouse tower starting position on X axis
Y axis position 1 (Y-) feeder outer position (loading / unloading the package)
Y axis position 2 feeder middle position
Y axis position 3 (Y+) feeder inner position (inside the warehouse)
Z axis position 1 (Z+) warehouse tower top position on Z axis (3rd oor)
Z axis position 2 warehouse tower middle position on Z axis (2nd oor)
Z axis position 3 (Z-) warehouse tower bottom position on Z axis (1st oor)
• Check conguration
• Activate conguration
• Run mode
• Status bar in the bottom right corner of the TwinCAT System Manager window
turns green.
Write a program
build it, download the program to the control PC and run it.
that will turn both LED lights on the warehouse model on.
Before downloading the program to the control PC, set the Run-time system:
• Login
• Download program
• Logout
46 2018 Summer School Project Assignments
warehouse and unloading the package from the warehouse. Mode of operation is
First, declare a set of variables to dene the current state of the warehouse -
which warehouse locations are full and which are empty. Warehouse has a 3 by 3
sized eld of locations so declare 9 boolean variables, one for each of the locations.
Declare this variables as global. These variables will be used during the loading of
Next, declare two variables that will be used during the unloading of the package
from the warehouse and will dene the package location (one variable for X axis and
one variable for Z axis). Declare these two variables as global. These two variables
Warehouse Control Project must be written as a nite state machine and must
1. When started the program waits for a command that enables one of two modes
used for this purpose. These could either be physically pressed on the model
2. Until one of the two modes of operation is enabled, the red LED light is on.
Once one of the two modes is enabled, the red light is turned o and the green
LED is turned on. When the current mode of operation is nished, the green
b) Move the feeder out by Y axis to the outside position and wait for 2 seconds
check if the package was actually loaded. If it was not, repeat the previous
step (3.b)).
d) Move the warehouse tower by X axis to the correct position where you want
e) Move the warehouse tower by Z axis to the correct position. NOTE: move
the warehouse tower ABOVE the position where you want to put the
package.
g) Move the warehouse tower DOWN by Z axis to the position BELOW the
position where you want to put the package.
k) Switch to step 1.
axis).
b) Move the warehouse tower by X axis to the correct position where you want
c) Move the warehouse tower by Z axis to the correct position. NOTE: move
the warehouse tower BELOW the position where you want to take the
package from.
i) Move the feeder out by Y axis to the outside position and wait for 2 seconds
to check if the package was actually unloaded. If it was not, repeat the
k) Switch to step 1.
• Write the program as a nite state machine. Dene the states and conditions
• Consider the fact that the PLC Project will run continuously with a pre-
dened run cycle (see Chapter 3.4.3). This demands a thoughtful approach to
program design.
• At any bigger change to the PLC program, save the PLC project le, re-build
the project and refresh the PLC Conguration in TwinCAT System Manager.
48 2018 Summer School Project Assignments
• Use the correct states of input variables (sensors) when they are enabled or
disabled. NOTE: some sensors are TRUE when they are disabled, while most
of the sensors are TRUE when they are enabled (see Table A.1).
• Use the correct values for enabling actuators (motors, LED lights...). Actuators
are enabled by setting the appropriate variable to TRUE (see Table A.2).
The application should dene two types of entities, a warehouse and a package and
the package, unloading the package, extending the feeder to accept / release a
package etc. (this could be a textual representation of the nite state machine
• load: main function for loading a new package into the warehouse; this
activates the loading mode of the warehouse; the function call include the
getNewPackageLocation function.
• unload: the main function for unloading a package from the warehouse; this
activates the unloading mode of the warehouse; the function call must include
For communication with the warehouse over a local network use Modbus
protocol. Include node-modbus library in Node.js and dene a Modbus client. Use
Parameter Value
host 192.168.1.6
port 502
unitId 1
timeout 2000
autoReconnect true
reconnectTimeout 15000
logLabel 'ModbusClientTCP'
logLevel 'debug'
logEnabled false
Modbus TCP client of the node-modbus module denes all read and write
functions standardized in the Modbus protocol (see Table 3.5 in Chapter 3.6). Use:
As described in Chapter 3.6, a local variable can be dened in a general PLC data
starting from address 0x3000 (hexadecimal) (see Table 3.6). If you use the PLC
memory area, you have to declare the variable with the AT attribute, for example:
area, the compiler independently selects the variable's address (thus no AT attribute
is used in the declaration) and the address depends on the number, the order and
Table A.5 gives some examples on to how to correctly use Modbus TCP functions
to address variables in the PLC application. There are a couple of details that require
a comment:
• To load or unload a package from the warehouse declare two extra local
variables in the PLC application. Do not try to change the PLC I/O variables
• The actual address used in the Modbus TCP client function call needs to be
when reading holding register(s) and the function results need to be properly
parsed.
First example in Table A.5 shows how to check the current state a warehouse is
in. A state variable in the PLC application is declared in the general PLC data area
and is the rst variable to be declared, therefore it's address is 0x6000. We only
The second example shows how to read the current state of slots in the warehouse.
In this case, the 9 variables that correspond to 9 warehouse slots are dened in the
PLC memory address, therefore the address starts with 0x3000. These variables are
boolean (TRUE / FALSE) and are 1 bit long, so similarly to the rst example only
one register needs to be read. The return values are 1 if the slot is occupied or 0 if
Loading a package into the warehouse works similarly to checking the warehouse
state. A dedicated local variable is set to 1 (or TRUE) which enables the loading
mode of the warehouse. The warehouse automatically nd the free slot.
Unloading a package from the warehouse requires a bit more work. First, the
location of the package that is to be unloaded must be set. In this example two
coordinates of the warehouse 3 by 3 storage eld are set to 2. Next, a dedicated local
variable is set to 1 (or TRUE) which enables the unloading mode of the warehouse.
Build a simple web GUI that will enable visual representation of the warehouse
state and control functions. The GUI should enable all of the functions developed
coordinates
• occupation information
• state:
order
(a) orderTransport
Order a new transport for the package and provide all
http://192.168.1.104:3000/orderTransport?buyerlocation=
'Ljubljana'&id=gfwqgebgwtegvwe&warehouse=192.168.1.103
(b) packageLoaded
Warehouse calls this endpoint to conrm that the package has been loaded
Example: http://192.168.1.104:3000/packageDelivered
3. Include also economic aspects into the system. The transportation node
A.5 Integration
Integrate all 4 modules into a fully functional distributed supply chain. Figure A.3
shows an example of the process of ordering a product from our Decentralized supply
chain system.
First customer browses the retailer shop for a product. When he decides for
one product he buys it by calling web API endpoint buy() of the retailer node.
Retailer node then responds with the purchased response. Customer then calls
the process of moving to the warehouse to load the package when it arrives it
of unloading the package which nishes with the response unloaded back to the
delivery.
References
[1] P. Zhang. Industrial Control Technology, A Handbook for Engineers and
Researchers. William Andrew Inc., 2008.
May 2018.
php?content=../content/1033/tcoverview/html/default.htm&id=
7054720205085915955. Last accessed in May 2018.
https://infosys.beckhoff.
[5] Twincat 2 system manager overview website.
com/english.php?content=../content/1033/tcsystemmanager/basics/
tcsysmgr_common_intro.htm&id=1211772295410824515. Last accessed in
May 2018.
https://infosys.beckhoff.com/
[8] Structured text in beckho system website.
english.php?content=../content/1033/tcplccontrol/html/TcPlcCtrl_
Languages\%20ST.htm&id=. Last accessed in May 2018.
2018.
http://www.
[10] Modbus messaging on tcp/ip implementation guide v1.0b.
modbus.org/docs/Modbus_Messaging_Implementation_Guide_V1_0b.pdf.
Last accessed in May 2018.
55