Codesys Ros2 [upd] ✧

CODESYS and ROS2: Bridging the Gap Between Industrial Automation and Advanced Robotics

The divide between traditional industrial automation and high-level robotic intelligence is narrowing. For decades, CODESYS has been the gold standard for IEC 61131-3 PLC programming, powering the world’s factories with deterministic, stable control. On the other side, the Robot Operating System 2 (ROS2) has emerged as the powerhouse for autonomous navigation, computer vision, and complex path planning.

Integrating these two ecosystems allows developers to combine the "hard" real-time reliability of a PLC with the cutting-edge libraries of the robotics world. Here is an in-depth look at why this integration matters and how to achieve it. Why Integrate CODESYS with ROS2?

Historically, PLCs handled simple I/O and motion control, while a separate PC handled "smart" tasks like SLAM (Simultaneous Localization and Mapping). Integrating them directly offers several advantages:

Safety and Determinism: Use CODESYS for safety-critical logic and motor torque loops while ROS2 handles high-level mission planning.

Edge Intelligence: Bring AI-driven vision or machine learning (via ROS2 nodes) to standard industrial hardware.

Standardization: Use the DDS (Data Distribution Service) backbone of ROS2 to create a unified communication layer across a factory floor.

Hardware Flexibility: CODESYS runs on everything from Raspberry Pis to high-end industrial IPCs, making it an ideal gateway to ROS2. Architectures for Communication codesys ros2

Getting CODESYS (Structured Text/Ladder Logic) to talk to ROS2 (C++/Python) requires a middleware bridge. There are three primary ways to do this: 1. The Micro-ROS Approach

Micro-ROS allows ROS2 nodes to run on microcontrollers and RTOS-based systems. For CODESYS users, this often involves using a Micro-ROS Agent.

How it works: The CODESYS controller acts as a client that sends data to an agent running on a Linux-based gateway (or the same IPC).

Best for: Resource-constrained hardware where you want a native-ish ROS2 feel. 2. MQTT or OPC UA Bridges

Since CODESYS has excellent native support for OPC UA and MQTT, you can use these as a "handshake" protocol.

How it works: CODESYS publishes data to an MQTT broker; a simple ROS2 Python node subscribes to that broker and republishes the data as a ROS2 Topic.

Best for: Rapid prototyping and systems where millisecond latency isn't the primary concern. 3. Shared Memory (For Single-Platform Systems) CODESYS and ROS2: Bridging the Gap Between Industrial

If you are running CODESYS Control for Linux on the same industrial PC as your ROS2 Humble or Iron distribution, shared memory is the fastest route.

How it works: Using a C-Extension in CODESYS to write to a shared memory segment that a ROS2 node reads.

Best for: High-performance applications like low-latency robotic arm control. Use Cases: Where CODESYS Meets ROS2 Autonomous Mobile Robots (AMRs)

In a warehouse AMR, CODESYS manages the battery management system (BMS), emergency stops, and low-level motor encoders. Meanwhile, ROS2 runs the navigation stack (Nav2), processing LiDAR data to find the best path around a pallet. Vision-Guided Pick and Place

A CODESYS-controlled Delta robot receives high-level coordinates from a ROS2 node running OpenCV or a neural network. ROS2 identifies the object's orientation, and CODESYS executes the precise high-speed motion profile. Digital Twins and Simulation

Using the CODESYS Communication Lib, you can link a PLC project to a robot simulated in Gazebo or NVIDIA Isaac Sim. This allows for "Software-in-the-Loop" (SiL) testing before the physical hardware is even built. Challenges to Consider

Jitter and Latency: ROS2 (unless tuned specifically with a Real-Time Kernel) is not inherently deterministic. Developers must ensure that a delay in a ROS2 node doesn't cause a timeout in the CODESYS task. Open your CODESYS project

Data Mapping: Converting PLC data types (like REAL or INT) into ROS2 messages (sensor_msgs/LaserScan, etc.) requires careful serialization.

Complexity: Managing two distinct build environments (CODESYS IDE and the Linux terminal/Colcon) increases the learning curve for traditional PLC engineers. Conclusion

The synergy between CODESYS and ROS2 represents the future of Industry 4.0. By offloading complex "thinking" to ROS2 and keeping the "acting" within CODESYS, engineers can build robots that are both incredibly smart and industrially robust.

As more vendors release pre-built ROS2 drivers for CODESYS-compatible hardware, the barrier to entry is falling. Whether you are building an autonomous forklift or a collaborative assembly cell, mastering this bridge is a vital skill for the modern automation engineer.

This is a comprehensive guide to bridging the gap between industrial PLCs (CODESYS) and the robotics world (ROS 2).

Step 1: Library Setup

  1. Open your CODESYS project.
  2. Open the Library Repository.
  3. Install the SysSock, SysSockets, and SL Iot MQTT Client libraries if not already present.

The Top 3 Benefits for Robotics Engineers

Example Code

Here's an example code snippet in C++ that demonstrates how to integrate a CoDeSys controller with ROS 2:

#include <ros2/ros2.hpp>
#include <industrial_ros2/industrial_ros2.hpp>
int main(int argc, char* argv[]) 
  // Initialize ROS 2
  rclcpp::init(argc, argv);
// Create a ROS 2 node
  auto node = rclcpp::Node::create_node("co_de_sys_node");
// Create a CoDeSys controller object
  industrial_ros2::CoDeSysController controller;
// Configure the CoDeSys controller
  controller.configure("co_de_sys_controller");
// Start the CoDeSys controller
  controller.start();
// Create a ROS 2 publisher
  auto publisher = node->create_publisher<std_msgs::msg::String>("co_de_sys_topic", 10);
// Publish data from the CoDeSys controller
  while (rclcpp::ok()) 
    std_msgs::msg::String msg;
    msg.data = controller.read_data();
    publisher->publish(msg);
// Shutdown ROS 2
  rclcpp::shutdown();
return 0;

2.1 CODESYS Architecture

CODESYS Control Runtime follows a cyclic executive pattern: