CoDeSys series-4, build Profinet master-slave environment based on Ubuntu’s codesys runtime extension package

CoDeSys series-4, building Profinet master-slave environment based on Ubuntu’s codesys runtime extension package

Article directory

  • CoDeSys series-4, build Profinet master-slave environment based on Ubuntu’s codesys runtime extension package
    • I. Introduction
    • 2. Data collection
    • 3. Ubuntu 18.04 from installation to replacement of real-time kernel
      • 1. Download and install Ubuntu18.04
      • 2. Download and install the real-time kernel to solve compilation problems and problems when replacing the kernel.
        • 2.1. First use this command to check your Ubuntu kernel. The kernel version of my Ubuntu18.04 is 5.4.0-84-generic.
        • 2.2. Install dependencies
        • 2.3. Create the kernel directory
        • 2.4. Download the linux kernel and RT-PREEMPT patch
        • 2.5. Unzip and patch the kernel
        • 2.6. Configure the kernel
        • 2.7. Compile the kernel
        • 2.8. Install the kernel
        • 2.9. Modify grub files
        • 2.10. Update grub
        • 2.11. Restart
      • 3. Real-time testing
    • 4. CoDeSys installation and Linux runtime package download and installation
    • 5. Create the application and test it
      • 1. Create a project
        • 1.1. Create standard project
        • 1.2. Add device and activate
        • 1.3. Echo test
        • 1.4. Install p-net type Profinet equipment
        • 1.5. Configure and add Profinet master network
      • 3. PLC programming
      • 4. Burn the program and test it
    • 6. Finally

1. Foreword

We have previously run a soft PLC based on codesys on a windows machine, and it has performed simple IO operations as the master station and the p-net slave station running on the virtual machine. However, the runtime PLC running on Windows will affect Network card, but not running the runtime will cause some timeout problems due to insufficient real-time performance, so here we replace the real-time Linux kernel on Ubuntu18.04, and then use the for Linux expansion package of codesys to transform the real-time Ubuntu18.04 running software The PLC serves as the master station to interact with the Raspberry Pi slave station via Profinet, and also familiarize yourself with the process of replacing the Linux real-time kernel.

2. Data collection

● Ubuntu18.04 virtual machine (using desktop version) to build codesys: http://bbs.hicodesys.com/thread-28335-1-1.html
● Compile and replace real-time kernel: https://www.jianshu.com/p/b74b05d26cf9
● Kernel compilation error handling: https://blog.csdn.net/qq_36393978/article/details/118157426
● An error occurs when modifying grup: https://askubuntu.com/questions/1024593/failed-to-execute-child-process-net-when-entering-nautilus
● Raspberry Pi installs codesys runtime, and Ubuntu installs codesys runtime and can learn from each other: https://www.cnblogs.com/ChenMichael/p/16352297.html

3. Ubuntu18.04 from installation to replacement of real-time kernel

1. Download and install Ubuntu18.04

It is recommended to do it under a virtual machine. There are already many tutorials on this, so I won’t go into details here. You can also refer to here:
Ubuntu18.04 download, install and configure the network:
Download address: https://releases.ubuntu.com/18.04/
Virtual machine installation reference:
Ubuntu replaces domestic sources: https://www.qkeke.com/archives/1762.html
To solve the problem that vmtools still cannot be copied and pasted to Windows after installing it (or the network card is configured and related tools are automatically installed during the installation of the virtual machine):

sudo apt-get autoremove open-vm-tools
sudo apt-get update
sudo apt-get install open-vm-tools
sudo apt-get install open-vm-tools-desktop
reboot

1. Back up the original source

sudo cp /etc/apt/sources.list /etc/apt/sources_init.list

2. Use vim to edit the source

sudo apt-get install vim
sudo vim /etc/apt/sources.list

3. Change to Alibaba’s source (Ubuntu18.04)

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

4. Update source

sudo apt-get update

5. Restore the damaged software package, try to uninstall the erroneous package, and reinstall the correct version.

sudo apt-get -f install

6. Update software

sudo apt-get upgrade

2. Download and install the real-time kernel to solve compilation problems and problems when replacing the kernel

Just follow the steps based on the information online.

2.1. First use this command to check your Ubuntu kernel. The kernel version of my Ubuntu18.04 is 5.4.0-84-generic
uname -a

Linux ubuntu 5.4.0-84-generic #94~18.04.1-Ubuntu SMP Thu Aug 26 23:17:46 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
2.2, Install dependencies
sudo apt-get install libncurses-dev
sudo apt-get install libssl-dev
2.3. Create the kernel directory
mkdir ~/kernel & amp; & amp; cd ~/kernel
2.4, download linux kernel and RT-PREEMPT patch

My kernel version is 5.4.0-84-generic, so the download is closest to my current version, linux-5.4.10.tar.gz. We can choose the domestic Tsinghua mirror website. The download speed of foreign sources is particularly slow. Linux kernel URL: https://mirrors.tuna.tsinghua.edu.cn/kernel/v5.x/, you can find the kernel version you want here. I downloaded linux-5.10.180.tar.gzRT -PREEMPT patch:https://mirrors.tuna.tsinghua.edu.cn/kernel/projects/rt/
I downloaded patch-5.10.180-rt89-rc1.patch.gz, more versions can be found in the older folder. It should be noted that the patch must be consistent with the downloaded kernel version.

2.5. Decompress and patch the kernel

After downloading the kernel and patches, move the files to the kernel directory you just created.

tar zxvf linux-5.10.180.tar.gz
gunzip patch-5.10.180-rt89-rc1.patch.gz

Put the patch-file into the linux-5.4.10 folder, (drag the patch file directly into the linux-5.4.10 folder) to execute the patching

cp patch-5.10.180-rt89-rc1.patch linux-5.10.180
cd linux-5.10.180/
patch -p1 < patch-5.10.180-rt89-rc1.patch
2.6. Configuring the kernel
sudo apt install make
sudo apt install make-guile
sudo apt install gcc
sudo apt install flex
sudo apt install bison
make menuconfig

General setup -> [Enter]

Preemption Model (Voluntary Kernel Preemption (Desktop)) ->[Enter]

Fully Preemptible Kernel (Real-Time)-> [Enter] #Select

Then keep pressing the esc key to return to the main page
Kernel hacking –> [Enter]

Memory Debugging [Enter]

Deselect Check for stack overflows. I don’t have this option in mine, so just ignore it.
Return to the main page, save the configuration and exit

2.7, compile kernel
vim.config
#Modify CONFIG_SYSTEM_TRUSTED_KEYS and assign it a null value
#If the value of CONFIG_SYSTEM_REVOCATION_KEYS is not empty, assign it a null value.
CONFIG_SYSTEM_TRUSTED_KEYS=""
CONFIG_SYSTEM_REVOCATION_KEYS=""
make -j8

The number of cores in my cpu is 4, so it is j8, 2 times the number of cores.

2.8, install kernel
sudo make modules_install -j8
sudo make install -j8
2.9. Modify grub files

Open the file /etc/default/grub. This file of mine is a read-only file and cannot be modified directly. Enter the command in the terminal

sudo nautilus

Select the grub file to modify, comment out GRUB_TIMEOUT_STYLE=hidden, and change GRUB_TIMEOUT=0 to GRUB_TIMEOUT=5

2.10, update grub
sudo update-grub
2.11, Restart
sudo reboot

After restarting, on the startup interface, select Ubuntu advanced options and select the installed version with rt (the first one is fine by default).
After booting, check the current kernel version

uname -a

Linux ubuntu 5.10.180-rt89-rc1 #1 SMP PREEMPT_RT Wed Sep 27 01:35:01 PDT 2023 x86_64 x86_64 x86_64 GNU/Linux

The installation is now successful.

3. Real-time testing

Please refer to the following web pages:
https://blog.csdn.net/kl1125290220/article/details/78560220
https://blog.csdn.net/longerzone/article/details/16897655

sudo apt-get install rt-tests

Run the cyclictest program for real-time testing:

shellsudo cyclictest -t 5 -p 80 -n #Run five threads, thread priority is 80, infinite loop

Detailed explanation of cyclictest running results

P: 0 Thread priority is 0
C: 9397 counter. Each time the thread's time interval is reached, the counter is incremented by 1
I: 1000 The time interval is 1000 microseconds (us)
Min: minimum delay (us)
Act: the latest delay (us)
Avg: average latency (us)
Max: Maximum delay (us)

My test results:

# /dev/cpu_dma_latency set to 0us
policy: fifo: loadavg: 2.62 1.81 0.77 1/787 2295

T: 0 (2290) P:80 I:1000 C: 20854 Min: 5 Act: 127 Avg: 251 Max: 1166
T: 1 (2291) P:80 I:1500 C: 13903 Min: 6 Act: 113 Avg: 252 Max: 681
T: 2 (2292) P:80 I:2000 C: 10427 Min: 7 Act: 115 Avg: 255 Max: 1000
T: 3 (2293) P:80 I:2500 C: 8341 Min: 6 Act: 79 Avg: 253 Max: 2721
T: 4 (2294) P:80 I:3000 C: 6951 Min: 7 Act: 114 Avg: 254 Max: 2931

4. CoDeSys installation and Linux runtime package download and installation

Configure the Linux runtime:

Log in to the device through ssh to configure and install. Here you can view, install and operate (then click Install and view System Info to determine whether the installation is successful, and finally Start to start the runtime):

When creating the codesys project, select the Linux device as the device:
Then the communication configuration can be activated:

5. Create application and test

1. Create a project

1.1. Create standard project

Standard project, CODESYS Control for Linux SL (runtime environment), control device (master controller):

1.2. Add device and activate

Add a device (only if the corresponding Linux expansion package is installed can you find and add the following corresponding types of devices):

1.3. Echo test

Send echo service:

1.4. Install p-net type Profinet equipment

Download the corresponding GSDML file: github.com/rtlabs-com/p-net/blob/master/samples/pn_dev/GSDML-V2.4-RT-Labs-P-Net-Sample-App-20220324.xml
Install the GSDML file:


Successful installation:

1.5. Configure and add Profinet master network

● On the left panel, on Device (CODESYS Ccontrol RTE V3 x64), right-click and select Add Device. Use Ethernet adapter, Ethernet.

● On Ethernet, right-click and select Add Device. Using Profinet IO master, PN-controller.

● On the PN_Controller, right-click and select Add Device. Use the P-Net sample application.

● On P_Net_Sample_App, right-click and select Add Device. Use DIO 8xLogicLevel.

● Double-click the Ethernet node in the left menu. Choose your own network interface. The IP address will be updated accordingly.

● Double-click the PN_controller node in the left menu. Adjust the IP range using “First IP” and “Last IP” so that they both have existing IP addresses of the IO device (such as a Linux laptop or embedded Linux board running example_app). In this tutorial, we use First IP 172.16.1.174 and Last IP 172.16.1.174.

● Double-click the P_Net_Sample_App node in the left menu. Set the IP address to the existing address of the IO device. In this tutorial we use 172.16.1.174.

3. PLC programming

● Click PLC Logic ? Applicaiton ? PLC_PRG in the left panel and enter the program.
In the controller application, according to the tutorial, using ST language programming, the code for the variable part and the program part are given. Here we can add it:

PROGRAM PLC_PRG
VAR
    in_pin_button_LED: BOOL;
    out_pin_LED: BOOL;
    in_pin_button_LED_previous: BOOL;
    flashing: BOOL := TRUE;
    oscillator_state: BOOL := FALSE;
    oscillator_cycles: UINT := 0;
END_VAR
oscillator_cycles := oscillator_cycles + 1;
IF oscillator_cycles > 200 THEN oscillator_cycles := 0;
oscillator_state := NOT oscillator_state;
END_IF IF in_pin_button_LED = TRUE THEN IF in_pin_button_LED_previous = FALSE THEN flashing := NOT flashing;
END_IF out_pin_LED := TRUE;
ELSIF flashing = TRUE THEN out_pin_LED := oscillator_state;
ELSE out_pin_LED := FALSE;
END_IF in_pin_button_LED_previous := in_pin_button_LED;

● On the DIO_8xLogicLevel node in the left menu, right-click and select Edit IO Mapping.

● Click on the small + sign to open the “Input 8 bits” line.

● Double-click the icon on the row you want to edit.
● Map “Input Bit 7” to “in_pin_button_LED” (found through Application/PLC_PRG) and map “Output Bit 7” to “out_pin_LED”.
Or you can double-click here to edit:

● Select Cyclic with 4 ms in Application ? MainTask.

● Select a cycle of 10 ms in Application ? Profinet_CommunicationTask. Use priority 14.

4. Burn the program and test

● In the top menu, use Build ? to generate code.

● Use the top menu Online ? Login to transfer the application to Linux. Press “Yes” on the pop-up window.
● In the top menu, use Debug ? Start You can use the top menu Tools ? “Update Linux” to track the controller logs. Click the System Information button and view the Runtime Information text box. If the IO device is not found on the network, it will display an error message. Use Wireshark to verify that the controller is sending LLDP packets every 5 seconds. Every 15 seconds, it sends an ARP packet asking for the (first?) IO device IP address, and a PN-DCP packet asking for the IO device named “rt-labs-dev”.
Actually I need to log in -> run:

Wireshark captures packets (refer here: https://rt-labs.com/docs/p-net/how-to-guides/capturing-and-analyzing-ethernet-packets/, there will be a separate section on how to capture packets later) Analyzing the summary of profinet, the real-time kernel here finally captured the PNIO protocol data):

● After configuring the Codesys softplc running on the Raspberry Pi, you can shut down the PC you used to configure it (running the Codesys desktop application). Keep in mind that you will need to restart your Raspberry Pi running Softplc every two hours if using the trial version.
Codesys Troubleshooting —— — If you receive an error claiming that a library is missing, click PLC Logic ? Applications ? Library Management in the left panel. Codesys should automatically detect if there are any missing libraries. Click Download Missing Libraries under the Library Manager tab to download any missing libraries. (This is also mentioned in the previous section that the library will be missing during the first run and needs to be solved by downloading tz)

6. Finally

Next, you can theoretically install the Raspberry Pi expansion pack and use two Raspberry Pi virtual machines to simulate the IO device interaction of building a PN; in addition, you should also use p-net to do more interactive development, but the current project is due to Some reasons have stopped, so research has been forced to be suspended. Unfortunately, due to life pressure, there is not always an option. The domestic scientific research situation is not clear, but there is indeed not much research space within the company, and it is just anxious to cash out. Let’s leave the problem to future generations, or maybe to ourselves in the future who are not forced by life.