Yayın3 dk okuma
ROS2 and PLC Integration: Connecting Robots with the Factory
PLCs (Programmable Logic Controllers) have been the backbone of industrial automation for decades, known for their robustness and reliability. On the other hand, ROS2 (Robot Operating System 2) powers modern robotics with flexibility and advanced capabilities.
My first encounter with combining the two was in a factory, where a robot simply needed to open a door. I solved it by making the robot communicate with the PLC over Modbus. It was a simple task, but it showed me something bigger: when PLCs and ROS2 work together, the possibilities in smart factories become endless.
In this article, I’ll walk you through why ROS2–PLC integration matters, what can be achieved, and share a basic Modbus example to get you started.
Why Integrate ROS2 with PLCs?
- Real-Time Control
- PLCs are built for reliability and deterministic control. They ensure safe and precise communication between robots and the production line.
- Data Sharing
- PLCs collect valuable factory data (temperature, pressure, counters, sensors) that robots can use in ROS2.
- Flexibility and Intelligence
- By combining PLC logic with ROS2, you unlock advanced features like AI, navigation, computer vision, and cloud integration.
How to Do It
One of the most common methods is using Modbus (TCP/RTU). On the ROS2 side, Python or C++ libraries can connect to the PLC, read/write registers, and publish them as ROS2 topics.
Basic Python Modbus Example
Here’s how to read and write a simple coil from the PLC:
from pyModbusTCP.client import ModbusClient
import time
# PLC Connection
client = ModbusClient(host="192.168.0.10", port=502, auto_open=True)
# Read 1 number coil ? (Is the door open? ?)
coil = client.read_coils(1, 1)
print("Kapı durumu:", "Açık" if coil[0] else "Kapalı")
# Write to coil number 1 (open the door)
client.write_single_coil(1, True)
time.sleep(2)
client.write_single_coil(1, False)
ROS2 Integration Example
To bring this into ROS2, simply embed the Modbus logic into a ROS2 node.
import rclpy
from rclpy.node import Node
from std_msgs.msg import Bool
from pyModbusTCP.client import ModbusClient
class PLCNode(Node):
def __init__(self):
super().__init__('plc_node')
self.publisher_ = self.create_publisher(Bool, 'door_status', 10)
self.timer = self.create_timer(1.0, self.read_coil)
self.client = ModbusClient(host="192.168.0.10", port=502, auto_open=True)
def read_coil(self):
coil = self.client.read_coils(1, 1)
if coil:
msg = Bool()
msg.data = coil[0]
self.publisher_.publish(msg)
self.get_logger().info(f"Door status: {msg.data}")
def main(args=None):
rclpy.init(args=args)
node = PLCNode()
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()
What Else Can Be Done?
Besides simple door control, ROS2–PLC integration opens many opportunities:
1.Robot → PLC
- Stop a conveyor belt when an AGV arrives at the production line
- Notify the PLC when a robot arm finishes its task, triggering the next machine
- Manage charging stations for mobile robots through PLC signals
2.PLC → Robot
- Adjust robot behavior based on factory sensor data (e.g., reduce speed if the temperature is too high)
- Send emergency stop signals directly into ROS2 for robot safety
- Combine PLC quality sensors with robot vision systems for smarter inspection
3.Advanced Use Cases
- Stream PLC data into ROS2 and forward it to the cloud for IoT applications
- Use PLC sensor data for predictive maintenance models inside ROS2
- Synchronize multiple robots via PLC coordination
Conclusion
ROS2–PLC integration is not just about simple I/O control. It’s a critical step toward Industry 4.0, where robots and industrial automation systems collaborate seamlessly.
My first experiment was just opening a door with Modbus, but that small step revealed a much larger vision: robots and PLCs working together can make factories smarter, safer, and more efficient. 🚀