Notifications
Clear all

Raspberry Pi Hybrid EtherCAT & Modbus Master

1 Posts
1 Users
0 Reactions
203 Views
Yassin
(@yassin)
Member
Joined: 5 months ago
Posts: 47
Topic starter  
Introduction
 
In the field of modern industrial control, industrial equipment of various types and levels usually adopts differentiated communication protocols for data interaction, resulting in widespread problems of poor protocol compatibility and heterogeneous communication. Modbus RTU features a simple structure and excellent universality, which is widely applied to low-speed sensors, serial lower computers and other industrial devices. In contrast, EtherCAT possesses nanosecond-level synchronization accuracy, high real-time performance and high bandwidth, and is mostly used in real-time control scenarios such as servo drives and high-speed I/O devices. To solve the difficulties in data intercommunication and high protocol adaptation cost between devices with the two protocols, this paper develops a multi-protocol hybrid gateway with both real-time motion control and low-speed serial acquisition capabilities based on the Raspberry Pi 5 development board. Built on open-source protocol stacks, the gateway realizes data transparent transmission and protocol conversion between the high-speed EtherCAT bus and the Modbus RTU serial bus. Meanwhile, it is equipped with a visual human-computer interaction interface to complete real-time monitoring and manual control of EtherCAT slave devices and Modbus acquisition equipment. With a concise structure and strong scalability, this design can provide a reference for multi-protocol integration schemes of small and medium-sized industrial control systems.

 

Hardware Design

For the Modbus RTU part, a self-developed Modbus RFID reader is applied, as shown in the figure below. It is designed based on the STM32F0 chip and supports Modbus RTU communication and RS485 interface.

1
The schematic diagram is shown below. The FM17550 is adopted as the RFID radio frequency chip.
 
2
For the EtherCAT slave device, AX58100 is used as the ESC chip and STM32F411 serves as the main control MCU. The LEDs on the core board can be controlled by the main station.
 
3

 

The schematic of the AX58100 circuit is shown below. Two RJ45 network interfaces are led out, serving as the IN and OUT ports for EtherCAT communication respectively.
 
4
A 1280×800 capacitive touch screen is adopted as the display device. All wiring of the Modbus RFID reader, EtherCAT slave device, Raspberry Pi and the touch screen are completed as shown in the figure below.
 
5

Software Design

 

Open-Source Protocol Stack Software

 
According to the project requirements, it is necessary to select the communication protocol stack software. The software scheme is designed as follows:
 
For EtherCAT communication, SOEM is adopted as the EtherCAT master protocol stack software.

 

For Modbus communication, libmodbus is adopted as the Modbus master protocol stack software.

 

For the UI interaction part, Dear ImGui is used as the GUI library.

 
 
Next, the above protocol stack software needs to be integrated. This project uses CMake as the build system, and the CMake scripts are developed as follows. The content of the ImGui CMake file is shown below.
project(imgui)

message("imgui cmake..")

add_library(imgui
    lib/imgui/imgui.cpp
    lib/imgui/imgui_demo.cpp
    lib/imgui/imgui_draw.cpp
    lib/imgui/imgui_tables.cpp
    lib/imgui/imgui_widgets.cpp
    lib/imgui/backends/imgui_impl_sdl2.cpp
    lib/imgui/backends/imgui_impl_opengl3.cpp
)

find_package(SDL2 REQUIRED)
find_package(OpenGL REQUIRED)

target_link_libraries(imgui PUBLIC SDL2::SDL2)
target_link_libraries(imgui PUBLIC OpenGL::GL)

include_directories(lib/imgui)
include_directories(lib/imgui/backends)

The project’s CMakeLists.txt is shown below. Core dependencies: SOEM, SDL2, OpenGL, ImGui, libmodbus, and pthread.

# project info
cmake_minimum_required(VERSION 3.28)
project(master VERSION 0.1.0 LANGUAGES C CXX)

# set some option
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(SOEM_BUILD_SAMPLES ON)

# libs
add_subdirectory(lib/SOEM)
include(lib/imgui.cmake)

add_executable(master master.cpp imgui_main.cpp fieldbus.c)

# link libraries
target_link_libraries(master PRIVATE soem)
target_link_libraries(master PRIVATE imgui)
target_link_libraries(master PRIVATE modbus)
target_link_libraries(master PRIVATE pthread)

Code Development

 
For the UI section, two separate windows have been implemented.

 

One window handles the EtherCAT Master UI interaction, and the other window manages the Modbus Master UI interaction.

 
Since the full source code is quite lengthy, it will not be displayed in the text. The complete code can be downloaded at the end of the article for reference.
 
The core part of the main function code is shown below.

 

Three threads are created:

 
  1. One thread is responsible for EtherCAT communication with a synchronization cycle of 5ms;
  2. Another thread handles Modbus RTU communication with a cycle of 10ms;
  3. The third thread is the UI thread, which runs the interface developed with ImGui.
void ImGui::mywindow(void)
{
    main_window();
    DrawModbusSerialPanel();
}

// 线程函数
void * thread_func_ecat(void *arg)
{
    for (;;)
    {
        op_loop();
        usleep(5000);
    }
    return NULL;
}

// 线程函数
void * thread_func_modbus(void *arg)
{
    for (;;)
    {
        if (g_connected && g_modbusCtx)
        {
            uint16_t regs[2];

            /* 固定站号 2 */
            modbus_set_slave(g_modbusCtx, 2);

            /* 写保持寄存器 1 = 0 */
            modbus_write_register(g_modbusCtx, 0, 0);

            /* 读保持寄存器 2、3 */
            if (modbus_read_registers(g_modbusCtx, 1, 2, regs) == 2)
            {
                g_reg2 = regs[0];
                g_reg3 = regs[1];
            }
        }
        usleep(10000);
    }
    return NULL;
}

// 线程函数
void * thread_func_ui(void *arg)
{
    imgui_init();
    return NULL;
}

int main(int argc, char *argv[])
{
    printf("EtherCAT Debug Tool\n");
    printf("A GUI tool for debugging EtherCAT networks\n");
    pthread_t tid1, tid2, tid3;
    int id1 = 1, id2 = 2, id3 = 3;

    // create thread
    if (pthread_create(&tid1, NULL, thread_func_ui, &id1) != 0) {
        perror("thread_func_ui is created.");
        return 1;
    }

    if (pthread_create(&tid2, NULL, thread_func_ecat, &id2) != 0) {
        perror("thread_func_ecat is created.");
        return 1;
    }
    if (pthread_create(&tid3, NULL, thread_func_modbus, &id3) != 0) {
        perror("thread_func_modbus is created.");
        return 1;
    }
    // wait for threads to finish
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    pthread_join(tid3, NULL);

    return 0;
}
 
The software running screenshot is shown below. The left window is the EtherCAT master UI, which adopts PDO communication mode and realizes the control of onboard LEDs on the EtherCAT slave board. The right window is the Modbus master UI, which implements reading card ID data from the Modbus RFID reader.
 
6
 
In this project, a hybrid EtherCAT and Modbus RTU master gateway is built based on Raspberry Pi 5. With open-source libraries including SOEM, libmodbus and Dear ImGui, multi-protocol communication, data acquisition and visual human-computer interface are completed. The system successfully realizes LED control of EtherCAT slaves and card ID reading of Modbus RFID devices. During debugging, I also found several limitations and imperfections in this solution. The Raspberry Pi 5 generates considerable heat under high load, which may cause thermal throttling and reduce communication stability during long-term operation. In addition, the current multi-thread structure lacks optimized mutex control and scheduling strategy, occasionally resulting in minor data jitter. Furthermore, only basic communication functions are implemented, without disconnection reconnection, error tolerance and data filtering algorithms.
Future optimization will be carried out in both hardware and software aspects. Active heat dissipation will be added to solve thermal throttling, and electromagnetic interference suppression will be optimized. In terms of software, thread scheduling logic will be improved, and fault detection and data filtering will be supplemented. Moreover, a real-time kernel will be transplanted to further reduce communication latency. I am still in the learning stage, and this prototype has many immature details. Any suggestions, optimization ideas and discussions about Raspberry Pi industrial gateway are highly appreciated.
 

Yassin


Yassin | Building Compact, High-Current Connections for Drones & Robots


   
Quote