# Strolch as a software PLC [![Contributors](https://img.shields.io/github/contributors/strolch-li/strolch-plc)](https://github.com/strolch-li/strolch-plc/graphs/contributors) [![License](https://img.shields.io/github/license/strolch-li/strolch-plc)](https://github.com/strolch-li/strolch-plc/blob/master/LICENSE) A soft real time PLC written in Strolch running on Strolch Checkout the documentation at https://strolch.li/plc/ ## Features Strolch PLC supports the following features: * Notification model * Raspberry Pi GPIO Input and Output addresses * I2C Input and Output addresses over PCF8574 chips * DataLogic Scanner connection * Virtual addresses * WebUI to observer and manipulate the addresses * WebSocket connection to Strolch Agent for notifying of changes * Simple two key addressing of hardware addresses to store semantics, e.g. `Convey01 - MotorOn`, instead of `i2cInput.dev01.0.0` ## PlcService PlcServices are sub classes of `PlcService`. You can register for notifications, send keys and delay actions: import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import li.strolch.plc.core.PlcHandler; import li.strolch.plc.core.PlcService; import li.strolch.plc.model.PlcAddress; public class ConveyorPlcService extends PlcService { public static final int BOX_TRANSFER_DURATION = 30; private static final String R_CONVEYOR_01 = "Conveyor01"; private static final String A_START_BUTTON = "StartButton"; private static final String T_MOTOR_ON = "MotorOn"; private static final String T_MOTOR_OFF = "MotorOff"; private static final String A_BOX_DETECTED = "BoxDetected"; private boolean motorOn; private ScheduledFuture motorStopTask; public ConveyorPlcService(PlcHandler plcHandler) { super(plcHandler); } @Override public void handleNotification(PlcAddress address, Object value) { String action = address.action; boolean active = (boolean) value; if (action.equals(A_START_BUTTON)) { if (active) { logger.info("Start button pressed. Starting motors..."); send(R_CONVEYOR_01, T_MOTOR_ON); this.motorOn = true; scheduleStopTask(); } } else if (action.equals(A_BOX_DETECTED)) { if (active && this.motorOn) { logger.info("Container detected, refreshing stop task..."); scheduleStopTask(); } } else { logger.info("Unhandled notification " + address.toKeyAddress()); } } private void scheduleStopTask() { if (this.motorStopTask != null) this.motorStopTask.cancel(false); this.motorStopTask = schedule(this::stopMotor, BOX_TRANSFER_DURATION, TimeUnit.SECONDS); } private void stopMotor() { send(R_CONVEYOR_01, T_MOTOR_OFF); } @Override public void register() { this.plcHandler.register(R_CONVEYOR_01, A_START_BUTTON, this); this.plcHandler.register(R_CONVEYOR_01, A_BOX_DETECTED, this); super.register(); } @Override public void unregister() { this.plcHandler.unregister(R_CONVEYOR_01, A_START_BUTTON, this); this.plcHandler.unregister(R_CONVEYOR_01, A_BOX_DETECTED, this); super.unregister(); } This example registered for changes to the keys `Conveyor01 - StartButton` and `Conveyor01 - BoxDetected`. When the start button is pressed, then it sends the keys `Conveyor01 - MotorOn` and schedules the motor off action with a delay of 30s. This class should then be registered in the `PlcServiceInitializer`: public class CustomPlcServiceInitializer extends PlcServiceInitializer { public CustomPlcServiceInitializer(ComponentContainer container, String componentName) { super(container, componentName); } @Override protected List getPlcServices(PlcHandler plcHandler) { ArrayList plcServices = new ArrayList<>(); StartupPlcService startupPlcService = new StartupPlcService(plcHandler); ConveyorPlcService conveyorPlcService = new ConveyorPlcService(plcHandler); plcServices.add(conveyorPlcService); plcServices.add(startupPlcService); return plcServices; } As one can see, there is little fuss for writing business logic, state is persisted automatically so it is visible in the UI, and the PlcServices can handle their state as needed. ## PlcAddress PlcAddresses store the value of a hardware address: The two parameters `resource` and `action` are the local keys for the address, while `address` is used to find the actual connection and the address on that connection. ## PlcTelegram PlcTelegrams are used to store keys with a default value. For example it is easier to understand `Conveyor01 - MotorOn` instead of `Conveyor01 - Motor` with the value true or false. ## PlcLogicalDevice Multiple PlcAddresses can be grouped together into a PlcLogicalDevice for visualization on the UI. In logistics a single conveyor might have multiple sensors and actors, e.g. the conveyor's motor, a light barrier and a scanner, grouping them together makes it easier for debugging. ## PlcConnections PlcConnections are used to model the actual hardware connections and define with which class the connection is to be established. The following shows some of the implementations: ## Virtual Addresses In some cases, especially in conjunction with a Strolch Agent as the main server, it is necessary to also have virtual addresses, with which to perform notifications. The following shows examples: ## More Information Find more to Strolch PLC at our website: https://strolch.li/plc.html