# CODESYS PLCopen XML Analysis

## Overview

This document contains detailed information extracted from the PLCopen XML export (`Home_Automation.xml`), which provides a more structured view of the project compared to the CODESYS export format.

## Project Information

- **Format**: PLCopen XML (TC6 0200)
- **CODESYS Version**: V3.5 SP21
- **Export Date**: 2026-01-27T14:48:52
- **Project Name**: Home_Automation.project

## Task Configuration

### MainTask
- **Type**: Cyclic
- **Interval**: t#4ms (4 milliseconds)
- **Priority**: 1
- **Watchdog**: Disabled
- **Program**: PLC_PRG

### EtherCAT_Task
- **Type**: Cyclic
- **Interval**: t#4ms (4 milliseconds)
- **Priority**: 1
- **Watchdog**: Disabled
- **Purpose**: EtherCAT communication and network variable updates

## Data Structures

### struct_switches

```iec
TYPE struct_switches :
STRUCT
    all_off: BOOL;  // Command to turn all lights off
    all_on: BOOL;   // Command to turn all lights on
    sw_1: BOOL;     // Switch 1 state
    sw_2: BOOL;     // Switch 2 state
    sw_3: BOOL;     // Switch 3 state
    sw_4: BOOL;     // Switch 4 state
    sw_5: BOOL;     // Switch 5 state
    sw_6: BOOL;     // Switch 6 state
END_STRUCT
END_TYPE
```

### struct_lights

```iec
TYPE struct_lights:
STRUCT
    l_1: BOOL;  // Light 1 control output
    l_2: BOOL;  // Light 2 control output
    l_3: BOOL;  // Light 3 control output
    l_4: BOOL;  // Light 4 control output
    l_5: BOOL;  // Light 5 control output
    l_6: BOOL;  // Light 6 control output
END_STRUCT
END_TYPE
```

## Function Block: fb_toogleButton

### Interface

**Inputs**:
- `Input: BOOL` - Input signal (switch state from network)

**Outputs**:
- `Output: BOOL` - Output signal (light control)

**Internal Variables**:
- `Switch: BOOL` - Internal switch state
- `Light: BOOL` - Internal light state
- `Flag1: BOOL` - Internal flag 1
- `Flag2: BOOL` - Internal flag 2
- `RT: R_TRIG` - Rising edge trigger
- `Pulse: TP` - Pulse timer
- `CTU_0: CTU` - Counter up 0
- `CTD_0: CTD` - Counter down 0
- `CTU_1: CTU` - Counter up 1
- `CTU_2: CTU` - Counter up 2
- `TOF_0: TOF` - Timer off delay 0

### Implementation (Ladder Diagram)

**Network 1: Input Debouncing**
- Input signal → TP (Pulse Timer) with PT = t#100ms
- TP output → TOF (Timer Off Delay) with PT = t#100ms
- TOF output → Switch (internal state)
- **Purpose**: Debounces input signal with 100ms pulse and 100ms off delay

**Network 2: Flag1 Logic**
- Switch AND (NOT Light) AND (NOT Flag2) → Flag1
- **Purpose**: Sets Flag1 when switch is on, light is off, and Flag2 is off

**Network 3: Flag2 Logic**
- Switch AND Light AND (NOT Flag1) → Flag2
- **Purpose**: Sets Flag2 when switch is on, light is on, and Flag1 is off

**Network 4: Light Toggle Logic**
- Flag1 AND (NOT Flag2) → Light (toggle)
- **Purpose**: Toggles light state based on flag conditions

**Network 5: Output Assignment**
- Light → Output
- **Purpose**: Direct output assignment

### Timing Characteristics

- **Input Debounce**: 100ms pulse timer (TP)
- **Output Delay**: 100ms off delay (TOF)
- **Total Response Time**: ~200ms (debounce + delay)

## Function Block: fb_switch

### Interface

**Inputs**:
- `switches: struct_switches` - Switch states from Node-RED

**Outputs**:
- `lights: struct_lights` - Light control outputs to Node-RED

**Internal Variables**:
- `all_off: fb_toogleButton` - Toggle button for "all off"
- `all_on: fb_toogleButton` - Toggle button for "all on"
- `sw_1: fb_toogleButton` - Toggle button for switch 1
- `sw_2: fb_toogleButton` - Toggle button for switch 2
- `sw_3: fb_toogleButton` - Toggle button for switch 3
- `sw_4: fb_toogleButton` - Toggle button for switch 4
- `sw_5: fb_toogleButton` - Toggle button for switch 5
- `sw_6: fb_toogleButton` - Toggle button for switch 6

### Implementation (Continuous Function Chart - CFC)

**Logic Flow for Each Light (l_1 through l_6)**:

1. **All Off Logic**:
   ```
   all_off.Output AND lights.l_X → AND gate → (forces light off)
   ```

2. **All On Logic**:
   ```
   all_on.Output AND (NOT lights.l_X) → AND gate → (forces light on)
   ```

3. **Individual Switch Logic**:
   ```
   switches.sw_X → sw_X.Input → sw_X.Output → lights.l_X
   ```

4. **Combined Logic (OR)**:
   ```
   (all_on logic) OR (sw_X.Output) OR (other sources) → lights.l_X
   ```

**Execution Order** (example for l_1):
1. Execution Order 1: AND gate for all_on logic
2. Execution Order 2: sw_2 toggle button
3. Execution Order 3: OR gate combining sources
4. Execution Order 4: AND gate for all_off logic
5. Execution Order 5: Output assignment to lights.l_1
6. Execution Order 30: sw_1 toggle button (individual control)

**Key Logic Patterns**:

- **All Off**: Uses AND gate with `all_off.Output` and current light state
- **All On**: Uses AND gate with `all_on.Output` and inverted light state
- **Individual Control**: Each switch has its own toggle button instance
- **Priority**: All_off can override individual controls, all_on combines with individual

## Program: Lights

The "Lights" program contains instances of `fb_switch` for all 15 rooms:

1. `masterBedroom: fb_switch`
2. `masterBathroom: fb_switch`
3. `bedroom_1: fb_switch`
4. `bedroom_2: fb_switch`
5. `bathroom: fb_switch`
6. `guest_wc: fb_switch`
7. `kitchen: fb_switch`
8. `pantry: fb_switch`
9. `livingRoom: fb_switch`
10. `diningRoom: fb_switch`
11. `veranda: fb_switch`
12. `entrance: fb_switch`
13. `front: fb_switch`
14. `back: fb_switch`
15. `side: fb_switch`
16. `hallway: fb_switch`

## Network Variables

### NVL_Sender (Sent to Node-RED)

All 15 room light structures:
- `l_masterBedroom` through `l_outBack` (struct_lights)

### NVL_Receiver (Received from Node-RED)

All 15 room switch structures:
- `masterBedroom` through `outBack` (struct_switches)

## Control Logic Summary

### Individual Light Control

1. **Switch Input** → `switches.sw_X` (from Node-RED)
2. **Toggle Processing** → `fb_toogleButton` instance
   - 100ms debounce (TP)
   - 100ms delay (TOF)
   - Toggle logic with flags
3. **Output** → `lights.l_X` (to Node-RED)

### Global Commands

**All Off**:
- `switches.all_off` → `all_off` toggle button
- Output ANDed with current light state
- Forces all lights off

**All On**:
- `switches.all_on` → `all_on` toggle button
- Output ANDed with inverted light state
- Forces all lights on
- Combined with individual switches via OR logic

### Timing Characteristics

- **Task Cycle**: 4ms (MainTask and EtherCAT_Task)
- **Input Debounce**: 100ms
- **Output Delay**: 100ms
- **Network Variable Update**: 50ms (from .export analysis)
- **Total Response**: ~200ms (debounce + delay)

## Implementation Language

- **fb_toogleButton**: Ladder Diagram (LD)
- **fb_switch**: Continuous Function Chart (CFC)
- **Lights Program**: Continuous Function Chart (CFC)

## Key Insights

1. **Debouncing**: All switch inputs are debounced with 100ms pulse timer
2. **Toggle Logic**: Uses flag-based state machine for reliable toggling
3. **Priority System**: All_off has priority over individual controls
4. **Combined Control**: All_on works in conjunction with individual switches
5. **Fast Response**: 4ms task cycle ensures responsive control
6. **Structured Design**: Each room is independent with its own fb_switch instance

---

**Analysis Date**: January 27, 2026  
**Source File**: `Home_Automation.xml`  
**Format**: PLCopen XML (TC6 0200)
